Skip to content

Commit

Permalink
Add ADD_MESSAGES notification as an optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
ilkkao committed Jun 4, 2017
1 parent 7240e55 commit 2f1e839
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 10 deletions.
18 changes: 18 additions & 0 deletions client/app/stores/windows.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,24 @@ export default Store.extend({
return true;
},

handleAddMessagesServer(data) {
data.messages.forEach(windowMessages => {
const windowObject = this._getWindow(windowMessages.windowId)

if (windowObject) {
windowMessages.messages.forEach(message => {
message.window = windowObject;
});

windowObject.messages.upsertModels(windowMessages.messages);

this._trimBacklog(windowObject.messages);
}
});

return true;
},

handleAddError(data) {
data.window.messages.upsertModel({
body: data.body,
Expand Down
1 change: 1 addition & 0 deletions client/app/utils/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const serverIdToEventMap = {
CONFIRM_FRIENDS: 'CONFIRM_FRIENDS_SERVER',
FINISH_INIT: 'FINISH_STARTUP_SERVER',
ADD_MESSAGE: 'ADD_MESSAGE_SERVER',
ADD_MESSAGES: 'ADD_MESSAGES_SERVER',
UPDATE_NETWORKS: 'UPDATE_NETWORKS_SERVER',
UPDATE_SETTINGS: 'UPDATE_SETTINGS_SERVER',
UPDATE_WINDOW: 'UPDATE_WINDOW_SERVER',
Expand Down
35 changes: 35 additions & 0 deletions doc/MAS-client-API.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,41 @@ Client can for example use different colors for different categories.

```status``` is an optional property. It can be have value 'original', 'edited', or 'deleted'. A missing ```status``` property means 'original'.

### ADD_MESSAGES

Add several message lines to windows.

```JSON
{
"type": "ADD_MESSAGES",

messages: [{
windowId: 42,
messages: [{
"body": "Hello worlds!",
"cat": "msg",
"ts": 2093243,
"updatedTs": 2093315.
"userId": "m432454",
"status": "edited",
"gid": 823458234
}]
}, {
windowId: 43,
messages: [{
"body": "Hello again",
"cat": "msg",
"ts": 2093243,
"updatedTs": 2093315.
"userId": "m432454",
"status": "edited",
"gid": 923458134
}]
}]
}

See `ADD_MESSAGE` for description

### DELETE_WINDOW

Close window.
Expand Down
2 changes: 1 addition & 1 deletion server/controllers/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ exports.setup = function setup(server) {

socket.emit('ntf', ntf);

if (ntf.id !== 'ADD_MESSAGE') {
if (!(ntf.id === 'ADD_MESSAGE' || ntf.id === 'ADD_MESSAGES')) {
log.info(user, `Emitted ${ntf.type} (sessionId: ${session.id}) ${message}`);
}

Expand Down
23 changes: 14 additions & 9 deletions server/services/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ exports.init = async function init(user, session, maxBacklogLines, cachedUpto) {
await alerts.sendAlerts(user, session.id);
await sendNetworkList(user, session.id);

const ntfs = [];
const messages = [];
const windows = await Window.find({ userId: user.id });

Expand All @@ -65,7 +66,7 @@ exports.init = async function init(user, session, maxBacklogLines, cachedUpto) {
oneOnOneMember = members.find(member => member.get('userGId') !== user.gIdString);
}

messages.push({
ntfs.push({
type: 'ADD_WINDOW',
windowId: window.id,
name: conversation.get('name'),
Expand All @@ -87,7 +88,7 @@ exports.init = async function init(user, session, maxBacklogLines, cachedUpto) {
role
});

messages.push({
ntfs.push({
type: 'UPDATE_MEMBERS',
windowId: window.id,
reset: true,
Expand All @@ -100,24 +101,28 @@ exports.init = async function init(user, session, maxBacklogLines, cachedUpto) {
const lines = await ConversationMessage.find({ conversationId: conversation.id });
const firstBacklogMessage = lines.length - maxBacklogLines;

const windowMessages = [];

lines.forEach((message, index) => {
const newMsg = index >= firstBacklogMessage && message.id > cachedUpto;
const newEdit = message.get('status') !== 'original' &&
message.get('updatedId') >= cachedUpto;

if (newMsg || newEdit) {
const ntf = message.convertToNtf();
ntf.type = 'ADD_MESSAGE';
ntf.windowId = window.id;

messages.push(ntf);
windowMessages.push(message.convertToNtf());
}
});

messages.push({
windowId: window.id,
messages: windowMessages
});
}

messages.push({ type: 'FINISH_INIT' });
ntfs.push({ type: 'ADD_MESSAGES', messages });
ntfs.push({ type: 'FINISH_INIT' });

await notification.send(user, session.id, messages);
await notification.send(user, session.id, ntfs);

// Check if the user was away too long
courier.callNoWait('ircparser', 'reconnectifinactive', { userId: user.id });
Expand Down

0 comments on commit 2f1e839

Please sign in to comment.