Skip to content
This repository has been archived by the owner on Aug 30, 2024. It is now read-only.

Commit

Permalink
DEV-4826 add stream-notify-admin stream with `subscribtions-changed…
Browse files Browse the repository at this point in the history
…` event (#12)
  • Loading branch information
Sergey-Volkov authored Jun 11, 2020
1 parent 8280406 commit dfa9c14
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
19 changes: 14 additions & 5 deletions app/notifications/server/lib/Notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DDPCommon } from 'meteor/ddp-common';
import { WEB_RTC_EVENTS } from '../../../webrtc';
import { Subscriptions, Rooms } from '../../../models/server';
import { settings } from '../../../settings/server';
import { hasRole } from '../../../authorization';

const changedPayload = function(collection, id, fields) {
return DDPCommon.stringifyDDP({
Expand Down Expand Up @@ -65,19 +66,16 @@ class Notifications {
this.notifyUser = this.notifyUser.bind(this);
this.streamAll = new Meteor.Streamer('notify-all');
this.streamLogged = new Meteor.Streamer('notify-logged');
this.streamAdmin = new Meteor.Streamer('notify-admin');
this.streamRoom = new Meteor.Streamer('notify-room');
this.streamRoomUsers = new Meteor.Streamer('notify-room-users');
this.streamUser = new RoomStreamer('notify-user');
this.streamAll.allowWrite('none');
this.streamLogged.allowWrite('none');
this.streamAdmin.allowWrite('none');
this.streamRoom.allowWrite('none');
this.streamRoomUsers.allowWrite(function(eventName, ...args) {
const [roomId, e] = eventName.split('/');
// const user = Meteor.users.findOne(this.userId, {
// fields: {
// username: 1
// }
// });
if (Subscriptions.findOneByRoomIdAndUserId(roomId, this.userId) != null) {
const subscriptions = Subscriptions.findByRoomIdAndNotUserId(roomId, this.userId).fetch();
subscriptions.forEach((subscription) => self.notifyUser(subscription.u._id, e, ...args));
Expand All @@ -87,6 +85,9 @@ class Notifications {
this.streamUser.allowWrite('logged');
this.streamAll.allowRead('all');
this.streamLogged.allowRead('logged');
this.streamAdmin.allowRead(function() {
return this.userId && hasRole(this.userId, 'admin');
});
this.streamRoom.allowRead(function(eventName, extraData) {
const [roomId] = eventName.split('/');
const room = Rooms.findOneById(roomId);
Expand Down Expand Up @@ -142,6 +143,14 @@ class Notifications {
return this.streamUser.emit.apply(this.streamUser, args);
}

notifyAdmin(userId, eventName, ...args) {
if (this.debug === true) {
console.log('notifyAdmin', [eventName, ...args]);
}
args.unshift(eventName);
return this.streamAdmin.emit.apply(this.streamAdmin, args);
}

notifyAllInThisInstance(eventName, ...args) {
if (this.debug === true) {
console.log('notifyAll', [eventName, ...args]);
Expand Down
13 changes: 12 additions & 1 deletion server/publications/subscription/emitter.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Notifications } from '../../../app/notifications';
import { Subscriptions } from '../../../app/models';
import { Subscriptions, Users } from '../../../app/models';
import { msgStream } from '../../../app/lib/server/lib/msgStream';

import { fields } from '.';
Expand Down Expand Up @@ -27,4 +27,15 @@ Subscriptions.on('change', ({ clientAction, id, data }) => {
clientAction,
data,
);

const { customFields } = Users.findOne(data.u._id, { fields: { customFields: 1 } }) || {};
if (customFields) {
data.u.customFields = customFields;
Notifications.notifyAdmin(
data.u._id,
'subscriptions-changed',
clientAction,
data,
);
}
});

0 comments on commit dfa9c14

Please sign in to comment.