Skip to content

Commit

Permalink
update user eintity for status tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
huytran17 committed May 26, 2024
1 parent 5d98dbd commit b68d411
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
16 changes: 12 additions & 4 deletions core/server/src/config/socket.io/nsp/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,17 @@ export default function makeInitialClientNsp({ userDb }: { userDb: IUserDb }) {
io.of(SocketIONsp.PRIVATE_CLIENT);

client_nsp.on(SocketEvents.CONNECT, (socket) => {
socket.on(
ClientEvents.ONLINE,
({ user_id }: IUserPayload) => (online_users[socket.id] = user_id)
);
socket.on(ClientEvents.ONLINE, async ({ user_id }: IUserPayload) => {
const user_ids = Array.from(new Set(Object.values(online_users)));
if (!user_ids.includes(user_id)) {
await userDb.update({
_id: user_id,
is_online: true,
});
}

online_users[socket.id] = user_id;
});

socket.on(SocketEvents.DISCONNECT, async () => {
const offline_user_id = online_users[socket.id];
Expand All @@ -47,6 +54,7 @@ export default function makeInitialClientNsp({ userDb }: { userDb: IUserDb }) {
await userDb.update({
_id: offline_user_id,
last_online_at: new Date(),
is_online: false,
});
});
});
Expand Down
3 changes: 3 additions & 0 deletions core/server/src/database/entities/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default class User implements IUser {
public readonly blocked_comment_at?: Date;
public readonly is_blocked_comment?: boolean;
public readonly is_enabled_2fa?: boolean;
public readonly is_online?: boolean;
public readonly tfa_secret?: string;
public readonly socialite?: {
provider?: string;
Expand Down Expand Up @@ -42,6 +43,7 @@ export default class User implements IUser {
tfa_secret,
login_failed_times,
last_online_at,
is_online,
}: IUser) {
this._id = _id;
this.ip = ip;
Expand All @@ -61,5 +63,6 @@ export default class User implements IUser {
this.tfa_secret = tfa_secret;
this.login_failed_times = login_failed_times;
this.last_online_at = last_online_at;
this.is_online = is_online;
}
}
1 change: 1 addition & 0 deletions core/server/src/database/interfaces/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export default interface IUser {
avatar?: Record<string, unknown>;
blocked_comment_at?: Date;
is_blocked_comment?: boolean;
is_online?: boolean;
is_enabled_2fa?: boolean;
avatar_url?: string;
email: string;
Expand Down
1 change: 1 addition & 0 deletions core/server/src/database/schemas/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const userSchema = new Schema<IUser, Model<IUser>>(
},
is_blocked_comment: { type: Boolean, default: false },
is_enabled_2fa: { type: Boolean, default: false },
is_online: { type: Boolean, default: false },
blocked_comment_at: { type: Date },
avatar: { type: Object },
email: { type: String, trim: true, lowercase: true, required: true },
Expand Down

0 comments on commit b68d411

Please sign in to comment.