Set/Unset moderator via streams

pull/1889/head
Marcelo Schmidt 10 years ago
parent aa6d0265e0
commit 6789facdfc
  1. 9
      packages/rocketchat-lib/server/models/Subscriptions.coffee
  2. 2
      packages/rocketchat-ui/lib/collections.coffee
  3. 17
      packages/rocketchat-ui/views/app/room.coffee
  4. 15
      server/methods/getRoomModerators.coffee
  5. 32
      server/publications/roomModerators.coffee

@ -25,6 +25,15 @@ RocketChat.models.Subscriptions = new class extends RocketChat.models._Base
return @find query, options
# FIND
findByRoomIdAndRoles: (roomId, roles, options) ->
roles = [].concat roles
query =
"rid": roomId
"roles": { $in: roles }
return @find query, options
getLastSeen: (options = {}) ->
query = { ls: { $exists: 1 } }
options.sort = { ls: -1 }

@ -1,7 +1,7 @@
@ChatMessage = new Meteor.Collection null
@ChatRoom = new Meteor.Collection 'rocketchat_room'
@ChatSubscription = new Meteor.Collection 'rocketchat_subscription'
@RoomModerators = new Mongo.Collection 'room_moderators'
@RoomModerators = new Mongo.Collection null
@UserAndRoom = new Meteor.Collection null
@CachedChannelList = new Meteor.Collection null

@ -493,7 +493,22 @@ Template.room.onCreated ->
@autorun =>
@subscribe 'fullUserData', Session.get('showUserInfo'), 1
@subscribe 'roomModerators', @data._id
Meteor.call 'getRoomModerators', @data._id, (error, results) ->
if error
return toastr.error error.reason
for record in results
RoomModerators.insert record
RocketChat.callbacks.add('streamMessage', (msg) ->
if msg.t is 'new-moderator'
user = Meteor.users.findOne({ username: msg.msg }, { fields: { username: 1 } })
RoomModerators.upsert({ _id: msg._id }, { $set: { rid: msg.rid, u: user } }) # use message _id to prevent from running it twice (https://github.com/RocketChat/Rocket.Chat/issues/1876)
else if msg.t is 'moderator-removed'
user = Meteor.users.findOne({ username: msg.msg })
RoomModerators.remove({ rid: msg.rid, "u._id": user._id });
return msg
, RocketChat.callbacks.priority.LOW, 'addOrRemoveModerator')
Template.room.onDestroyed ->
window.removeEventListener 'resize', this.onWindowResize

@ -0,0 +1,15 @@
Meteor.methods
getRoomModerators: (rid) ->
unless Meteor.userId()
throw new Meteor.Error 'invalid-user', '[methods] getRoomModerators -> Invalid user'
check rid, String
options =
sort:
"u.username": 1
fields:
rid: 1
u: 1
return RocketChat.models.Subscriptions.findByRoomIdAndRoles(rid, 'moderator', options).fetch()

@ -1,32 +0,0 @@
Meteor.publish 'roomModerators', (rid, limit = 50) ->
unless this.userId
return this.ready()
pub = this
query =
rid: rid
roles: 'moderator'
options =
limit: limit
sort:
"u.username": 1
fields:
rid: 1
u: 1
cursor = RocketChat.models.Subscriptions.find(query, options).observeChanges
added: (_id, record) ->
pub.added('room_moderators', _id, record)
changed: (_id, record) ->
pub.changed('room_moderators', _id, record)
removed: (_id, record) ->
pub.removed('room_moderators', _id, record)
this.ready()
this.onStop ->
cursor.stop()
Loading…
Cancel
Save