2019-02-27 01:08:15 +08:00
|
|
|
import GuestUsers from '/imports/api/guest-users/';
|
2021-07-31 03:31:04 +08:00
|
|
|
import Users from '/imports/api/users';
|
2019-02-27 01:08:15 +08:00
|
|
|
import { Meteor } from 'meteor/meteor';
|
|
|
|
import Logger from '/imports/startup/server/logger';
|
2020-09-02 00:31:11 +08:00
|
|
|
import AuthTokenValidation, { ValidationStates } from '/imports/api/auth-token-validation';
|
2021-09-07 04:51:42 +08:00
|
|
|
import { publicationSafeGuard } from '/imports/api/common/server/helpers';
|
2019-02-27 01:08:15 +08:00
|
|
|
|
2021-07-31 03:31:04 +08:00
|
|
|
const ROLE_MODERATOR = Meteor.settings.public.user.role_moderator;
|
|
|
|
|
2023-04-01 04:40:41 +08:00
|
|
|
async function guestUsers() {
|
|
|
|
const tokenValidation = await AuthTokenValidation
|
|
|
|
.findOneAsync({ connectionId: this.connection.id });
|
2020-09-02 00:31:11 +08:00
|
|
|
|
|
|
|
if (!tokenValidation || tokenValidation.validationStatus !== ValidationStates.VALIDATED) {
|
2021-07-31 03:31:04 +08:00
|
|
|
Logger.warn(`Publishing GuestUser was requested by unauth connection ${this.connection.id}`);
|
2020-02-07 04:47:28 +08:00
|
|
|
return GuestUsers.find({ meetingId: '' });
|
|
|
|
}
|
2019-02-27 01:08:15 +08:00
|
|
|
|
2020-09-02 00:31:11 +08:00
|
|
|
const { meetingId, userId } = tokenValidation;
|
2019-02-27 01:08:15 +08:00
|
|
|
|
2023-04-01 04:40:41 +08:00
|
|
|
const User = await Users.findOneAsync({ userId, meetingId }, { fields: { role: 1 } });
|
2021-07-31 03:31:04 +08:00
|
|
|
if (!User || User.role !== ROLE_MODERATOR) {
|
|
|
|
Logger.warn(
|
2021-08-04 23:34:52 +08:00
|
|
|
'Publishing GuestUser was requested by non-moderator connection',
|
2021-07-31 03:31:04 +08:00
|
|
|
{ meetingId, userId, connectionId: this.connection.id },
|
|
|
|
);
|
2021-09-07 04:51:42 +08:00
|
|
|
|
2021-07-31 03:31:04 +08:00
|
|
|
return GuestUsers.find({ meetingId: '' });
|
|
|
|
}
|
2021-09-07 04:51:42 +08:00
|
|
|
// 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 } });
|
2021-09-07 04:51:42 +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);
|
2020-09-02 00:31:11 +08:00
|
|
|
Logger.debug(`Publishing GuestUsers for ${meetingId} ${userId}`);
|
2019-02-27 01:08:15 +08:00
|
|
|
|
|
|
|
return GuestUsers.find({ meetingId });
|
|
|
|
}
|
|
|
|
|
|
|
|
function publish(...args) {
|
|
|
|
const boundSlides = guestUsers.bind(this);
|
|
|
|
return boundSlides(...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
Meteor.publish('guestUser', publish);
|