2017-02-07 23:29:27 +08:00
|
|
|
import { Meteor } from 'meteor/meteor';
|
2017-02-07 21:08:06 +08:00
|
|
|
import { check } from 'meteor/check';
|
2017-10-12 10:00:28 +08:00
|
|
|
import RedisPubSub from '/imports/startup/server/redis';
|
2017-02-07 21:08:06 +08:00
|
|
|
import Logger from '/imports/startup/server/logger';
|
2020-09-10 19:49:22 +08:00
|
|
|
import AuthTokenValidation from '/imports/api/auth-token-validation';
|
2017-10-12 10:00:28 +08:00
|
|
|
import Users from '/imports/api/users';
|
2021-01-30 04:29:07 +08:00
|
|
|
import ClientConnections from '/imports/startup/server/ClientConnections';
|
2017-02-07 21:08:06 +08:00
|
|
|
|
2023-03-15 21:18:31 +08:00
|
|
|
export default async function userLeaving(meetingId, userId, connectionId) {
|
2021-05-06 00:47:43 +08:00
|
|
|
try {
|
|
|
|
const REDIS_CONFIG = Meteor.settings.private.redis;
|
|
|
|
const CHANNEL = REDIS_CONFIG.channels.toAkkaApps;
|
|
|
|
const EVENT_NAME = 'UserLeaveReqMsg';
|
|
|
|
|
|
|
|
check(userId, String);
|
|
|
|
|
|
|
|
const selector = {
|
|
|
|
meetingId,
|
|
|
|
userId,
|
|
|
|
};
|
|
|
|
|
2023-03-15 21:18:31 +08:00
|
|
|
const user = await Users.findOneAsync(selector);
|
2021-05-06 00:47:43 +08:00
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
Logger.info(`Skipping userLeaving. Could not find ${userId} in ${meetingId}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-03-15 21:18:31 +08:00
|
|
|
const auth = await AuthTokenValidation.findOneAsync({
|
2021-05-06 00:47:43 +08:00
|
|
|
meetingId,
|
|
|
|
userId,
|
|
|
|
}, { sort: { updatedAt: -1 } });
|
|
|
|
|
|
|
|
// If the current user connection is not the same that triggered the leave we skip
|
|
|
|
if (auth?.connectionId !== connectionId) {
|
|
|
|
Logger.info(`Skipping userLeaving. User connectionId=${user.connectionId} is different from requester connectionId=${connectionId}`);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const payload = {
|
|
|
|
userId,
|
|
|
|
sessionId: meetingId,
|
2021-07-06 03:04:26 +08:00
|
|
|
loggedOut: user.loggedOut || false,
|
2021-05-06 00:47:43 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
ClientConnections.removeClientConnection(`${meetingId}--${userId}`, connectionId);
|
|
|
|
|
2022-10-07 21:38:50 +08:00
|
|
|
let reason;
|
|
|
|
|
|
|
|
if (user.loggedOut) {
|
|
|
|
// User explicitly requested logout.
|
|
|
|
reason = 'logout';
|
|
|
|
} else if (user.exitReason) {
|
|
|
|
// User didn't requested logout but exited graciously.
|
|
|
|
reason = user.exitReason;
|
|
|
|
} else {
|
|
|
|
// User didn't exit graciously (disconnection).
|
|
|
|
reason = 'disconnection';
|
|
|
|
}
|
2022-10-07 02:58:41 +08:00
|
|
|
|
|
|
|
Logger.info(`User '${userId}' is leaving meeting '${meetingId}' reason=${reason}`);
|
2021-05-06 00:47:43 +08:00
|
|
|
RedisPubSub.publishUserMessage(CHANNEL, EVENT_NAME, meetingId, userId, payload);
|
|
|
|
} catch (err) {
|
|
|
|
Logger.error(`Exception while invoking method userLeaving ${err.stack}`);
|
2017-03-11 02:33:46 +08:00
|
|
|
}
|
2017-06-03 03:25:02 +08:00
|
|
|
}
|