Skip to content

Commit

Permalink
Merge pull request #140 from RocketChat/user-references-as-object
Browse files Browse the repository at this point in the history
User references as object
  • Loading branch information
engelgabriel committed Jun 4, 2015
2 parents 897248b + 49655aa commit 57a3412
Show file tree
Hide file tree
Showing 50 changed files with 353 additions and 275 deletions.
2 changes: 1 addition & 1 deletion client/lib/RoomManager.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
ready: false

if myRoomActivity.ready()
if ChatSubscription.findOne { rid: roomId, uid: Meteor.userId() }, { reactive: false }
if ChatSubscription.findOne { rid: roomId, 'u._id': Meteor.userId() }, { reactive: false }
openedRooms[roomId].active = true
setRoomExpireExcept roomId
computation.invalidate()
Expand Down
14 changes: 7 additions & 7 deletions client/lib/UserManager.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

dep = new Tracker.Dependency

addUser = (userIds) ->
# console.log 'addUser', userIds if window.rocketUserDebug
userIds = [].concat userIds
for userId in userIds
unless users[userId]
users[userId] = 1
addUser = (usernames) ->
# console.log 'addUser', usernames if window.rocketUserDebug
usernames = [].concat usernames
for username in usernames
unless users[username]
users[username] = 1
dep.changed()

subscribeFn = ->
Expand All @@ -21,7 +21,7 @@
dep.depend()
subscribe.run()

init()
# init()

addUser: addUser
users: users
2 changes: 1 addition & 1 deletion client/lib/chatMessages.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
self.typingTimeout = null

startEditingLastMessage = (rid, imput) ->
lastMessage = ChatMessage.findOne { rid: rid, t: {$exists: false}, uid: Meteor.userId() }, { sort: { ts: -1 } }
lastMessage = ChatMessage.findOne { rid: rid, t: {$exists: false}, 'u._id': Meteor.userId() }, { sort: { ts: -1 } }
if not lastMessage?
return

Expand Down
4 changes: 2 additions & 2 deletions client/methods/leaveRoom.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ Meteor.methods

update =
$pull:
uids: Meteor.userId()
usernames: Meteor.user().username

ChatSubscription.remove { rid: roomId, uid: Meteor.userId() }
ChatSubscription.remove { rid: roomId, 'u._id': Meteor.userId() }

ChatRoom.update roomId, update
5 changes: 3 additions & 2 deletions client/methods/sendMessage.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ Meteor.methods
Tracker.nonreactive ->
now = new Date(Date.now() + TimeSync.serverOffset())

ChatMessage.upsert { rid: msg.rid, uid: Meteor.userId(), t: 't' },
ChatMessage.upsert { rid: msg.rid, t: 't' },
$set:
ts: now
msg: msg.message
'u.username': Meteor.user().username
$unset:
t: 1
expireAt: 1
Expand All @@ -15,7 +16,7 @@ Meteor.methods
Tracker.nonreactive ->
now = new Date(Date.now() + TimeSync.serverOffset())

ChatMessage.update { _id: msg.id, uid: Meteor.userId() },
ChatMessage.update { _id: msg.id, 'u._id': Meteor.userId() },
$set:
ets: now
msg: msg.message
4 changes: 3 additions & 1 deletion client/methods/typingStatus.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ Meteor.methods
filter =
t: 't'
rid: typingData.rid
uid: Meteor.userId()
$and: [{'u._id': Meteor.userId()}]

if start
msgData =
'$set':
expireAt: moment().add(30, 'seconds').toDate()
'$setOnInsert':
msg: '...'
'u._id': Meteor.userId()
'u.username': Meteor.user().username
ts: moment().add(1, 'years').toDate()

ChatMessage.upsert(filter, msgData)
Expand Down
43 changes: 17 additions & 26 deletions client/startup/startup.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Meteor.startup ->
UserPresence.awayTime = 300000
UserPresence.start()

Session.setDefault('AvatarRandom', Date.now())
Session.setDefault('AvatarRandom', 0)

window.lastMessageWindow = {}
window.lastMessageWindowHistory = {}
Expand All @@ -26,42 +26,33 @@ Meteor.startup ->
TAPi18n.setLanguage(userLanguage)
moment.locale(userLanguage)

Meteor.users.find({}, { fields: { name: 1, pictures: 1, status: 1, emails: 1, phone: 1, services: 1 } }).observe
Meteor.users.find({}, { fields: { name: 1, username: 1, pictures: 1, status: 1, emails: 1, phone: 1, services: 1 } }).observe
added: (user) ->
Session.set('user_' + user._id + '_name', user.name)
Session.set('user_' + user._id + '_status', user.status)
Session.set('user_' + user._id + '_emails', user.emails)
Session.set('user_' + user._id + '_phone', user.phone)
Session.set('user_' + user.username + '_status', user.status)

UserAndRoom.insert({ type: 'u', uid: user._id, name: user.name})
# UserAndRoom.insert({ type: 'u', uid: user._id, username: user.username, name: user.name})
changed: (user) ->
Session.set('user_' + user._id + '_name', user.name)
Session.set('user_' + user._id + '_status', user.status)
Session.set('user_' + user._id + '_emails', user.emails)
Session.set('user_' + user._id + '_phone', user.phone)
Session.set('user_' + user.username + '_status', user.status)

UserAndRoom.update({ uid: user._id }, { $set: { name: user.name } })
# UserAndRoom.update({ uid: user._id }, { $set: { username: user.username, name: user.name } })
removed: (user) ->
Session.set('user_' + user._id + '_name', null)
Session.set('user_' + user._id + '_status', null)
Session.set('user_' + user._id + '_emails', null)
Session.set('user_' + user._id + '_phone', null)
Session.set('user_' + user.username + '_status', null)

UserAndRoom.remove({ uid: user._id })
# UserAndRoom.remove({ uid: user._id })

ChatRoom.find({ t: { $ne: 'd' } }, { fields: { t: 1, name: 1 } }).observe
added: (room) ->
roomData = { type: 'r', t: room.t, rid: room._id, name: room.name }
# ChatRoom.find({ t: { $ne: 'd' } }, { fields: { t: 1, name: 1 } }).observe
# added: (room) ->
# roomData = { type: 'r', t: room.t, rid: room._id, name: room.name }

UserAndRoom.insert(roomData)
changed: (room) ->
UserAndRoom.update({ rid: room._id }, { $set: { t: room.t, name: room.name } })
removed: (room) ->
UserAndRoom.remove({ rid: room._id })
# UserAndRoom.insert(roomData)
# changed: (room) ->
# UserAndRoom.update({ rid: room._id }, { $set: { t: room.t, name: room.name } })
# removed: (room) ->
# UserAndRoom.remove({ rid: room._id })

Tracker.autorun ->
rooms = []
ChatSubscription.find({ uid: Meteor.userId() }, { fields: { rid: 1 } }).forEach (sub) ->
ChatSubscription.find({ 'u._id': Meteor.userId() }, { fields: { rid: 1 } }).forEach (sub) ->
rooms.push sub.rid

ChatRoom.find({ _id: $in: rooms }).observe
Expand Down
10 changes: 5 additions & 5 deletions client/stylesheets/base.less
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ input.search {
}
}

form.search-form {
.search-form {
position: relative;
}

Expand Down Expand Up @@ -668,8 +668,8 @@ a.github-fork {
.avatar-image {
height: 100%;
width: 100%;
min-height: 40px;
min-width: 40px;
min-height: 20px;
min-width: 20px;
display: block;
background-color: transparent;
background-size: cover;
Expand Down Expand Up @@ -2069,7 +2069,7 @@ a.github-fork {
}
}

@user-image-square: 40px;
@user-image-square: 30px;
.user-image {
margin: 4px;
height: @user-image-square;
Expand Down Expand Up @@ -2146,7 +2146,7 @@ a.github-fork {
a{
.cf_;
padding: 5px 0;
height: auto;
height: 30px;
background-color: transparent;
display: block;
> div{
Expand Down
16 changes: 7 additions & 9 deletions client/views/app/chatMessageDashboard.coffee
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
Template.chatMessageDashboard.helpers
own: ->
return 'own' if this.data.uid is Meteor.userId()
return 'own' if this.data.u?._id is Meteor.userId()

username: ->
if this.uid?
return Session.get('user_' + this.uid + '_name')
return this.u.username

isSystemMessage: ->
return this.t in ['s', 'p', 'f', 'r', 'au', 'ru', 'ul', 'nu', 'wm']
Expand All @@ -30,8 +29,8 @@ Template.chatMessageDashboard.helpers
message: ->
if this.by
UserManager.addUser(this.by)
else if this.uid
UserManager.addUser(this.uid)
else if this.u?.username
UserManager.addUser this.u.username
switch this.t
when 'p' then "<i class='icon-link-ext'></i><a href=\"#{this.url}\" target=\"_blank\">#{this.msg}</a>"
when 'r' then t('chatMessageDashboard.Room_name_changed', { room_name: this.msg, user_by: Session.get('user_' + this.by + '_name') }) + '.'
Expand Down Expand Up @@ -76,10 +75,9 @@ Template.chatMessageDashboard.events
Meteor.defer ->
$('.input-message-editing').select()

# TODO open flextab with user info
# 'click .mention-link': ->
# Session.set('flexOpened', true)
# Session.set('showUserInfo', $(e.currentTarget).data('username'))
'click .mention-link': (e) ->
Session.set('flexOpened', true)
Session.set('showUserInfo', $(e.currentTarget).data('username'))

Template.chatMessageDashboard.onRendered ->
chatMessages = $('.messages-box .wrapper')
Expand Down
6 changes: 3 additions & 3 deletions client/views/app/chatMessageDashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
{{#if isSystemMessage}}
<p class="system">{{{message}}}</p>
{{else}}
<a class="thumb user-card-message" href="#" data-userid="{{uid}}" tabindex="1">
{{> avatar userId=uid}}
<a class="thumb user-card-message" href="#" data-username="{{username}}" tabindex="1">
{{> avatar username=username}}
</a>
<a class="user user-card-message" href="#" data-userid="{{uid}}" tabindex="1">{{username}}</a>
<a class="user user-card-message" href="#" data-username="{{username}}" tabindex="1">{{username}}</a>
<span class="time">
{{time}}
{{#if ets}}
Expand Down
Loading

0 comments on commit 57a3412

Please sign in to comment.