Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fname to subscriptions in memory #6597

Merged
merged 11 commits into from
Apr 6, 2017
22 changes: 22 additions & 0 deletions client/notifications/UsersNameChanged.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Meteor.startup(function() {
RocketChat.Notifications.onLogged('Users:NameChanged', function({_id, name, username}) {
RocketChat.models.Messages.update({
'u._id': _id
}, {
$set: {
'u.name': name
}
}, {
multi: true
});

RocketChat.models.Subscriptions.update({
name: username,
t: 'd'
}, {
$set: {
fname: name
}
});
});
});
6 changes: 5 additions & 1 deletion packages/rocketchat-lib/client/methods/sendMessage.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ Meteor.methods
else
message.ts = new Date(Date.now() + TimeSync.serverOffset())

user = Meteor.user()
message.u =
_id: Meteor.userId()
username: Meteor.user().username
username: user.username

if RocketChat.settings.get('UI_Use_Real_Name')
message.u.name = user.name

message.temp = true

Expand Down
3 changes: 1 addition & 2 deletions packages/rocketchat-lib/server/functions/addUserToRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ RocketChat.addUserToRoom = function(rid, user, inviter, silenced) {
ts: now,
u: {
_id: inviter._id,
username: inviter.username,
name: inviter.name
username: inviter.username
}
});
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ RocketChat.sendMessage = (user, message, room, upsert = false) ->
unless message.ts?
message.ts = new Date()

message.u = _.pick user, ['_id','username', 'name']
message.u = _.pick user, ['_id','username']

if not Match.test(message.msg, String)
message.msg = ''
Expand Down
16 changes: 9 additions & 7 deletions packages/rocketchat-lib/server/functions/setRealName.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ RocketChat._setRealName = function(userId, name) {
return user;
}

const previousName = user.name;

if (previousName) {
RocketChat.models.Messages.updateAllNamesByUserId(user._id, name);
RocketChat.models.Subscriptions.setRealNameForDirectRoomsWithUsername(user.username, name);
}

// Set new name
RocketChat.models.Users.setName(user._id, name);
user.name = name;

if (RocketChat.settings.get('UI_Use_Real_Name') === true) {
RocketChat.Notifications.notifyLogged('Users:NameChanged', {
_id: user._id,
name: user.name,
username: user.username
});
}

return user;
};

Expand Down
6 changes: 6 additions & 0 deletions packages/rocketchat-lib/server/methods/getChannelHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,14 @@ Meteor.methods({
records = RocketChat.models.Messages.findVisibleByRoomIdBetweenTimestamps(rid, oldest, latest, options).fetch();
}

const UI_Use_Real_Name = RocketChat.settings.get('UI_Use_Real_Name') === true;

const messages = _.map(records, (message) => {
message.starred = _.findWhere(message.starred, { _id: fromUserId });
if (message.u && message.u._id && UI_Use_Real_Name) {
const user = RocketChat.models.Users.findOneById(message.u._id);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as my other comment, would it be better to fetch all users in result at once, then map the name on to message.u.name?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same answer 😄

message.u.name = user && user.name;
}
return message;
});

Expand Down
10 changes: 0 additions & 10 deletions packages/rocketchat-lib/server/methods/setRealName.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,6 @@ Meteor.methods({
throw new Meteor.Error('error-invalid-user', 'Invalid user', { method: 'setRealName' });
}

const user = Meteor.user();

if (user.name === name) {
return name;
}

if (_.trim(name)) {
name = _.trim(name);
}

if (!RocketChat.setRealName(Meteor.userId(), name)) {
throw new Meteor.Error('error-could-not-change-name', 'Could not change name', { method: 'setRealName' });
}
Expand Down
11 changes: 0 additions & 11 deletions packages/rocketchat-lib/server/models/Messages.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -320,16 +320,6 @@ RocketChat.models.Messages = new class extends RocketChat.models._Base

return @update query, update

updateAllNamesByUserId: (userId, name) ->
query =
'u._id': userId

update =
$set:
"u.name": name

return @update query, update, { multi: true }

updateUserStarById: (_id, userId, starred) ->
query =
_id: _id
Expand Down Expand Up @@ -390,7 +380,6 @@ RocketChat.models.Messages = new class extends RocketChat.models._Base
u:
_id: user._id
username: user.username
name: user.name
groupable: false

_.extend record, extraData
Expand Down
12 changes: 1 addition & 11 deletions packages/rocketchat-lib/server/models/Subscriptions.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ModelSubscriptions extends RocketChat.models._Base

this.cache.ensureIndex('rid', 'array')
this.cache.ensureIndex('u._id', 'array')
this.cache.ensureIndex('name', 'array')
this.cache.ensureIndex(['rid', 'u._id'], 'unique')
this.cache.ensureIndex(['name', 'u._id'], 'unique')

Expand Down Expand Up @@ -242,17 +243,6 @@ class ModelSubscriptions extends RocketChat.models._Base

return @update query, update, { multi: true }

setRealNameForDirectRoomsWithUsername: (username, name) ->
query =
name: username
t: "d"

update =
$set:
fname: name

return @update query, update, { multi: true }

setNameForDirectRoomsWithOldName: (oldName, name) ->
query =
name: oldName
Expand Down
2 changes: 2 additions & 0 deletions packages/rocketchat-lib/server/models/Users.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class ModelUsers extends RocketChat.models._Base
@tryEnsureIndex { 'statusConnection': 1 }, { sparse: 1 }
@tryEnsureIndex { 'type': 1 }

this.cache.ensureIndex('username', 'unique')

findOneByImportId: (_id, options) ->
return @findOne { importIds: _id }, options

Expand Down
20 changes: 15 additions & 5 deletions packages/rocketchat-lib/server/models/_BaseCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,18 +230,24 @@ class ModelsBaseCache extends EventEmitter {
localRecord[field] = [];
}

if (typeof link.where === 'function' && link.where(localRecord, record) === false) {
continue;
}

let mutableRecord = record;

if (typeof link.transform === 'function') {
record = link.transform(localRecord, record);
mutableRecord = link.transform(localRecord, mutableRecord);
}

if (multi === true) {
localRecord[field].push(record);
localRecord[field].push(mutableRecord);
} else {
localRecord[field] = record;
localRecord[field] = mutableRecord;
}

this.emit(`join:${ field }:inserted`, localRecord, record);
this.emit(`join:${ field }:changed`, 'inserted', localRecord, record);
this.emit(`join:${ field }:inserted`, localRecord, mutableRecord);
this.emit(`join:${ field }:changed`, 'inserted', localRecord, mutableRecord);
}
}

Expand All @@ -255,6 +261,10 @@ class ModelsBaseCache extends EventEmitter {
for (let i = 0; i < records.length; i++) {
let record = records[i];

if (typeof link.where === 'function' && link.where(localRecord, record) === false) {
continue;
}

if (typeof link.transform === 'function') {
record = link.transform(localRecord, record);
}
Expand Down
22 changes: 22 additions & 0 deletions packages/rocketchat-lib/server/startup/cache/CacheLoad.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,28 @@ RocketChat.models.Subscriptions.cache.hasOne('Users', {
}
});

RocketChat.models.Subscriptions.cache.hasOne('Users', {
field: 'fname',
link: {
local: 'name',
remote: 'username',
where(subscription/*, user*/) {
return subscription.t === 'd';
},
transform(subscription, user) {
if (user == null || subscription == null) {
return undefined;
}
// Prevent client cache for old subscriptions with new names
// Cuz when a user change his name, the subscription's _updateAt
// will not change
if (subscription._updatedAt < user._updatedAt) {
subscription._updatedAt = user._updatedAt;
}
return user.name;
}
}
});

RocketChat.models.Users.cache.load();
RocketChat.models.Rooms.cache.load();
Expand Down
3 changes: 1 addition & 2 deletions server/methods/addRoomModerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ Meteor.methods({
RocketChat.models.Messages.createSubscriptionRoleAddedWithRoomIdAndUser(rid, user, {
u: {
_id: fromUser._id,
username: fromUser.username,
name: fromUser.name
username: fromUser.username
},
role: 'moderator'
});
Expand Down
6 changes: 3 additions & 3 deletions server/methods/addRoomOwner.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ Meteor.methods({
RocketChat.models.Messages.createSubscriptionRoleAddedWithRoomIdAndUser(rid, user, {
u: {
_id: fromUser._id,
username: fromUser.username,
name: fromUser.name
username: fromUser.username
},
role: 'owner'
});
Expand All @@ -56,7 +55,8 @@ Meteor.methods({
_id: 'owner',
u: {
_id: user._id,
username: user.username
username: user.username,
name: user.name
},
scope: rid
});
Expand Down
2 changes: 0 additions & 2 deletions server/methods/createDirectMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ Meteor.methods({
},
$setOnInsert: {
name: to.username,
fname: to.name,
t: 'd',
alert: false,
unread: 0,
Expand All @@ -89,7 +88,6 @@ Meteor.methods({
}, {
$setOnInsert: {
name: me.username,
fname: me.name,
t: 'd',
open: false,
alert: false,
Expand Down
6 changes: 6 additions & 0 deletions server/methods/loadHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,16 @@ Meteor.methods({
records = RocketChat.models.Messages.findVisibleByRoomIdNotContainingTypes(rid, hideMessagesOfType, options).fetch();
}

const UI_Use_Real_Name = RocketChat.settings.get('UI_Use_Real_Name') === true;

const messages = records.map((message) => {
message.starred = _.findWhere(message.starred, {
_id: fromId
});
if (message.u && message.u._id && UI_Use_Real_Name) {
const user = RocketChat.models.Users.findOneById(message.u._id);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this going to make quite a lot of request to the db to get the users name?
Would it be better to collect an array of user ids then fetch the full names all at once outside the loop?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not to the DB, to the memory cache, it's really fast cuz is indexed using a hash map

message.u.name = user && user.name;
}
return message;
});

Expand Down
3 changes: 1 addition & 2 deletions server/methods/muteUserInRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@ Meteor.methods({
RocketChat.models.Messages.createUserMutedWithRoomIdAndUser(data.rid, mutedUser, {
u: {
_id: fromUser._id,
username: fromUser.username,
name: fromUser.name
username: fromUser.username
}
});

Expand Down
6 changes: 3 additions & 3 deletions server/methods/removeRoomModerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ Meteor.methods({
RocketChat.models.Messages.createSubscriptionRoleRemovedWithRoomIdAndUser(rid, user, {
u: {
_id: fromUser._id,
username: fromUser.username,
name: fromUser.name
username: fromUser.username
},
role: 'moderator'
});
Expand All @@ -56,7 +55,8 @@ Meteor.methods({
_id: 'moderator',
u: {
_id: user._id,
username: user.username
username: user.username,
name: user.name
},
scope: rid
});
Expand Down
6 changes: 3 additions & 3 deletions server/methods/removeRoomOwner.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ Meteor.methods({
RocketChat.models.Messages.createSubscriptionRoleRemovedWithRoomIdAndUser(rid, user, {
u: {
_id: fromUser._id,
username: fromUser.username,
name: fromUser.name
username: fromUser.username
},
role: 'owner'
});
Expand All @@ -63,7 +62,8 @@ Meteor.methods({
_id: 'owner',
u: {
_id: user._id,
username: user.username
username: user.username,
name: user.name
},
scope: rid
});
Expand Down
3 changes: 1 addition & 2 deletions server/methods/removeUserFromRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ Meteor.methods({
RocketChat.models.Messages.createUserRemovedWithRoomIdAndUser(data.rid, removedUser, {
u: {
_id: fromUser._id,
username: fromUser.username,
name: fromUser.name
username: fromUser.username
}
});

Expand Down
2 changes: 1 addition & 1 deletion server/methods/saveUserProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Meteor.methods({
}

if (settings.realname) {
Meteor.call('setRealName', settings.realname);
RocketChat.setRealName(Meteor.userId(), settings.realname);
}

if (settings.username) {
Expand Down
3 changes: 1 addition & 2 deletions server/methods/unmuteUserInRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ Meteor.methods({
RocketChat.models.Messages.createUserUnmutedWithRoomIdAndUser(data.rid, unmutedUser, {
u: {
_id: fromUser._id,
username: fromUser.username,
name: fromUser.name
username: fromUser.username
}
});

Expand Down
Loading