Skip to content
Snippets Groups Projects
Commit 83705397 authored by Rodrigo Nascimento's avatar Rodrigo Nascimento
Browse files

Replace ChatSubscription from packages

parent fddb27ac
No related branches found
No related tags found
No related merge requests found
......@@ -328,7 +328,8 @@ class IrcClient
t: 'd'
msgs: 0
ts: now
ChatSubscription.upsert
RocketChat.models.Subscriptions.upsert
rid: rid
$and: [{'u._id': target._id}]
,
......
......@@ -10,7 +10,7 @@ RocketChat.setUsername = (user, username) ->
if user.username is username
return user
# Check username availability
# Check username availability
unless RocketChat.checkUsernameAvailability username
return false
......@@ -19,7 +19,7 @@ RocketChat.setUsername = (user, username) ->
# Username is available; if coming from old username, update all references
if previousUsername
ChatMessage.update { "u._id": user._id }, { $set: { "u.username": username } }, { multi: true }
ChatMessage.find({ "mentions.username": previousUsername }).forEach (msg) ->
updatedMsg = msg.msg.replace(new RegExp("@#{previousUsername}", "ig"), "@#{username}")
ChatMessage.update { _id: msg._id, "mentions.username": previousUsername }, { $set: { "mentions.$.username": username, "msg": updatedMsg } }
......@@ -27,8 +27,8 @@ RocketChat.setUsername = (user, username) ->
ChatRoom.update { usernames: previousUsername }, { $set: { "usernames.$": username } }, { multi: true }
ChatRoom.update { "u._id": user._id }, { $set: { "u.username": username } }, { multi: true }
ChatSubscription.update { "u._id": user._id }, { $set: { "u.username": username } }, { multi: true }
ChatSubscription.update { name: previousUsername, t: "d" }, { $set: { name: username } }, { multi: true }
RocketChat.models.Subscriptions.setUserUsernameByUserId user._id, username
RocketChat.models.Subscriptions.setNameForDirectRoomsWithOldName previousUsername, username
# Set new username
Meteor.users.update { _id: user._id }, { $set: { username: username } }
......
......@@ -16,21 +16,14 @@ Meteor.methods
$addToSet:
usernames: user.username
if not ChatSubscription.findOne(rid: room._id, 'u._id': user._id)?
if not RocketChat.models.Subscriptions.findOneByRoomIdAndUserId(room._id, user._id)?
# Add a subscription to this user
ChatSubscription.insert
rid: room._id
name: room.name
RocketChat.models.Subscriptions.createWithRoomAndUser room, user,
ts: new Date()
t: room.t
f: false
open: true
alert: true
unread: 1
u:
_id: user._id
username: user.username
# Insert user joined message
ChatMessage.insert
......
RocketChat.models.Subscriptions = new class asd extends RocketChat.models._Base
RocketChat.models.Subscriptions = new class asd extends RocketChat.models._Base
constructor: ->
@model = new Meteor.Collection 'rocketchat_subscription'
......@@ -11,6 +10,14 @@ RocketChat.models.Subscriptions = new class asd extends RocketChat.models._Base
@tryEnsureIndex { 'ts': 1 }
# FIND ONE
findOneByRoomIdAndUserId: (roomId, userId) ->
query =
rid: roomId
"u._id": userId
return @findOne query
# FIND
findByUserId: (userId, options) ->
query =
......@@ -105,6 +112,44 @@ RocketChat.models.Subscriptions = new class asd extends RocketChat.models._Base
return @update query, update, { multi: true }
setUserUsernameByUserId: (userId, username) ->
query =
"u._id": userId
update =
$set:
"u.username": username
return @update query, update, { multi: true }
setNameForDirectRoomsWithOldName: (oldName, name) ->
query =
name: oldName
t: "d"
update =
$set:
name: name
return @update query, update, { multi: true }
incUnreadOfDirectForRoomIdExcludingUserId: (roomId, userId, inc=1) ->
query =
rid: roomId
t: 'd'
'u._id':
$ne: userId
update =
$set:
alert: true
open: true
$inc:
unread: inc
return @update query, update, { multi: true }
# INSERT
createWithRoomAndUser: (room, user, extraData) ->
subscription =
......
......@@ -57,23 +57,7 @@ RocketChat.sendMessage = (user, message, room, options) ->
###
Update the other subscriptions
###
ChatSubscription.update
# only subscriptions to the same room
rid: message.rid
# only direct messages subscriptions
t: 'd'
# not the msg owner
'u._id':
$ne: message.u._id
,
$set:
# alert de user
alert: true
# open the room for the user
open: true
# increment unread couter
$inc:
unread: 1
RocketChat.models.Subscriptions.incUnreadOfDirectForRoomIdExcludingUserId message.rid, message.u._id, 1
userOfMention = Meteor.users.findOne({_id: message.rid.replace(message.u._id, '')}, {fields: {username: 1, statusConnection: 1}})
if userOfMention?
......@@ -121,28 +105,12 @@ RocketChat.sendMessage = (user, message, room, options) ->
Update all other subscriptions of mentioned users to alert their owners and incrementing
the unread counter for mentions and direct messages
###
query =
# only subscriptions to the same room
rid: message.rid
if mentionIds.indexOf('all') > -1
# all users except sender if mention is for all
query['u._id'] = $ne: user._id
RocketChat.models.Subscriptions.incUnreadForRoomIdExcludingUserId message.rid, user._id, 1
else
# the mentioned user if mention isn't for all
query['u._id'] = $in: mentionIds
ChatSubscription.update query,
$set:
# alert de user
alert: true
# open the room for the user
open: true
# increment unread couter
$inc:
unread: 1
,
multi: true
RocketChat.models.Subscriptions.incUnreadForRoomIdAndUserIds message.rid, mentionIds, 1
query =
statusConnection: {$ne: 'online'}
......@@ -191,22 +159,6 @@ RocketChat.sendMessage = (user, message, room, options) ->
Update all other subscriptions to alert their owners but witout incrementing
the unread counter, as it is only for mentions and direct messages
###
ChatSubscription.update
# only subscriptions to the same room
rid: message.rid
# only the ones that have not been alerted yet
alert: { $ne: true }
# not the msg owner
'u._id':
$ne: message.u._id
,
$set:
# alert de user
alert: true
# open the room for the user
open: true
,
# make sure we alert all matching subscription
multi: true
RocketChat.models.Subscriptions.setAlertForRoomIdExcludingUserId message.rid, message.u._id, true
return message
......@@ -61,7 +61,7 @@ Meteor.methods
v:
token: message.token
ChatSubscription.insert
RocketChat.models.Subscriptions.insert
rid: message.rid
name: guest.username
alert: true
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment