2020-04-10 01:01:46 +08:00
|
|
|
import { Meteor } from 'meteor/meteor';
|
|
|
|
import { check } from 'meteor/check';
|
|
|
|
import Logger from '/imports/startup/server/logger';
|
|
|
|
import ConnectionStatus from '/imports/api/connection-status';
|
2020-09-02 00:31:11 +08:00
|
|
|
import AuthTokenValidation, { ValidationStates } from '/imports/api/auth-token-validation';
|
2023-02-14 00:52:32 +08:00
|
|
|
import Users from '/imports/api/users';
|
|
|
|
import { publicationSafeGuard } from '/imports/api/common/server/helpers';
|
|
|
|
|
|
|
|
const ROLE_MODERATOR = Meteor.settings.public.user.role_moderator;
|
2020-04-10 01:01:46 +08:00
|
|
|
|
2023-04-01 04:40:41 +08:00
|
|
|
async function connectionStatus() {
|
|
|
|
const tokenValidation = await AuthTokenValidation
|
|
|
|
.findOneAsync({ connectionId: this.connection.id });
|
2020-09-02 00:31:11 +08:00
|
|
|
|
|
|
|
if (!tokenValidation || tokenValidation.validationStatus !== ValidationStates.VALIDATED) {
|
|
|
|
Logger.warn(`Publishing ConnectionStatus was requested by unauth connection ${this.connection.id}`);
|
2020-04-10 01:01:46 +08:00
|
|
|
return ConnectionStatus.find({ meetingId: '' });
|
|
|
|
}
|
|
|
|
|
2020-09-02 00:31:11 +08:00
|
|
|
const { meetingId, userId } = tokenValidation;
|
2020-04-10 01:01:46 +08:00
|
|
|
|
|
|
|
check(meetingId, String);
|
2020-09-02 00:31:11 +08:00
|
|
|
check(userId, String);
|
2020-04-10 01:01:46 +08:00
|
|
|
|
2023-02-15 01:51:31 +08:00
|
|
|
const fields = {
|
|
|
|
meetingId: 1,
|
|
|
|
userId: 1,
|
|
|
|
status: 1,
|
|
|
|
statusUpdatedAt: 1,
|
|
|
|
clientNotResponding: 1,
|
2023-04-01 04:40:41 +08:00
|
|
|
};
|
2023-02-15 01:51:31 +08:00
|
|
|
|
2023-04-01 04:40:41 +08:00
|
|
|
const User = await Users.findOneAsync({ userId, meetingId }, { fields: { role: 1 } });
|
2020-09-02 00:31:11 +08:00
|
|
|
Logger.info(`Publishing connection status for ${meetingId} ${userId}`);
|
2020-04-10 01:01:46 +08:00
|
|
|
|
2023-02-14 00:52:32 +08:00
|
|
|
if (!!User && User.role === ROLE_MODERATOR) {
|
|
|
|
// Monitor this publication and stop it when user is not a moderator anymore
|
2023-04-01 04:40:41 +08:00
|
|
|
const comparisonFunc = async () => {
|
|
|
|
const user = await Users
|
|
|
|
.findOneAsync({ userId, meetingId }, { fields: { role: 1, userId: 1 } });
|
2023-02-14 00:52:32 +08:00
|
|
|
const condition = user.role === ROLE_MODERATOR;
|
|
|
|
|
|
|
|
if (!condition) {
|
|
|
|
Logger.info(`conditions aren't filled anymore in publication ${this._name}:
|
|
|
|
user.role === ROLE_MODERATOR :${condition}, user.role: ${user.role} ROLE_MODERATOR: ${ROLE_MODERATOR}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return condition;
|
|
|
|
};
|
|
|
|
publicationSafeGuard(comparisonFunc, this);
|
2023-04-01 04:40:41 +08:00
|
|
|
return ConnectionStatus.find({ meetingId }, { fields });
|
2023-02-14 00:52:32 +08:00
|
|
|
}
|
|
|
|
|
2023-04-01 04:40:41 +08:00
|
|
|
return ConnectionStatus.find({ meetingId, userId }, { fields });
|
2020-04-10 01:01:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function publish(...args) {
|
|
|
|
const boundNote = connectionStatus.bind(this);
|
|
|
|
return boundNote(...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
Meteor.publish('connection-status', publish);
|