1 |
efrain |
1 |
// This file is part of Moodle - http://moodle.org/
|
|
|
2 |
//
|
|
|
3 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
4 |
// it under the terms of the GNU General Public License as published by
|
|
|
5 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
6 |
// (at your option) any later version.
|
|
|
7 |
//
|
|
|
8 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
9 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
10 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
11 |
// GNU General Public License for more details.
|
|
|
12 |
//
|
|
|
13 |
// You should have received a copy of the GNU General Public License
|
|
|
14 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Controls the conversation page in the message drawer.
|
|
|
18 |
*
|
|
|
19 |
* This function handles all of the user actions that the user can take
|
|
|
20 |
* when interacting with the conversation page.
|
|
|
21 |
*
|
|
|
22 |
* It maintains a view state which is a data representation of the view
|
|
|
23 |
* and only operates on that data.
|
|
|
24 |
*
|
|
|
25 |
* The view state is immutable and should never be modified directly. Instead
|
|
|
26 |
* all changes to the view state should be done using the StateManager which
|
|
|
27 |
* will generate a new version of the view state with the requested changes.
|
|
|
28 |
*
|
|
|
29 |
* After any changes to the view state the module will call the render function
|
|
|
30 |
* to ask the renderer to update the UI.
|
|
|
31 |
*
|
|
|
32 |
* General rules for this module:
|
|
|
33 |
* 1.) Never modify viewState directly. All changes should be via the StateManager.
|
|
|
34 |
* 2.) Call render() with the new state when you want to update the UI
|
|
|
35 |
* 3.) Never modify the UI directly in this module. This module is only concerned
|
|
|
36 |
* with the data in the view state.
|
|
|
37 |
*
|
|
|
38 |
* The general flow for a user interaction will be something like:
|
|
|
39 |
* User interaction: User clicks "confirm block" button to block the other user
|
|
|
40 |
* 1.) This module is hears the click
|
|
|
41 |
* 2.) This module sends a request to the server to block the user
|
|
|
42 |
* 3.) The server responds with the new user profile
|
|
|
43 |
* 4.) This module generates a new state using the StateManager with the updated
|
|
|
44 |
* user profile.
|
|
|
45 |
* 5.) This module asks the Patcher to generate a patch from the current state and
|
|
|
46 |
* the newly generated state. This patch tells the renderer what has changed
|
|
|
47 |
* between the states.
|
|
|
48 |
* 6.) This module gives the Renderer the generated patch. The renderer updates
|
|
|
49 |
* the UI with changes according to the patch.
|
|
|
50 |
*
|
|
|
51 |
* @module core_message/message_drawer_view_conversation
|
|
|
52 |
* @copyright 2018 Ryan Wyllie <ryan@moodle.com>
|
|
|
53 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
54 |
*/
|
|
|
55 |
define(
|
|
|
56 |
[
|
|
|
57 |
'jquery',
|
|
|
58 |
'core/auto_rows',
|
|
|
59 |
'core/backoff_timer',
|
|
|
60 |
'core/custom_interaction_events',
|
|
|
61 |
'core/notification',
|
|
|
62 |
'core/pending',
|
|
|
63 |
'core/pubsub',
|
|
|
64 |
'core/str',
|
|
|
65 |
'core_message/message_repository',
|
|
|
66 |
'core_message/message_drawer_events',
|
|
|
67 |
'core_message/message_drawer_view_conversation_constants',
|
|
|
68 |
'core_message/message_drawer_view_conversation_patcher',
|
|
|
69 |
'core_message/message_drawer_view_conversation_renderer',
|
|
|
70 |
'core_message/message_drawer_view_conversation_state_manager',
|
|
|
71 |
'core_message/message_drawer_router',
|
|
|
72 |
'core_message/message_drawer_routes',
|
|
|
73 |
'core/emoji/auto_complete',
|
|
|
74 |
'core/emoji/picker'
|
|
|
75 |
],
|
|
|
76 |
function(
|
|
|
77 |
$,
|
|
|
78 |
AutoRows,
|
|
|
79 |
BackOffTimer,
|
|
|
80 |
CustomEvents,
|
|
|
81 |
Notification,
|
|
|
82 |
Pending,
|
|
|
83 |
PubSub,
|
|
|
84 |
Str,
|
|
|
85 |
Repository,
|
|
|
86 |
MessageDrawerEvents,
|
|
|
87 |
Constants,
|
|
|
88 |
Patcher,
|
|
|
89 |
Renderer,
|
|
|
90 |
StateManager,
|
|
|
91 |
MessageDrawerRouter,
|
|
|
92 |
MessageDrawerRoutes,
|
|
|
93 |
initialiseEmojiAutoComplete,
|
|
|
94 |
initialiseEmojiPicker
|
|
|
95 |
) {
|
|
|
96 |
|
|
|
97 |
// Contains a cache of all view states that have been loaded so far
|
|
|
98 |
// which saves us having to reload stuff with network requests when
|
|
|
99 |
// switching between conversations.
|
|
|
100 |
var stateCache = {};
|
|
|
101 |
// The current data representation of the view.
|
|
|
102 |
var viewState = null;
|
|
|
103 |
var loadedAllMessages = false;
|
|
|
104 |
var messagesOffset = 0;
|
|
|
105 |
var newMessagesPollTimer = null;
|
|
|
106 |
var isRendering = false;
|
|
|
107 |
var renderBuffer = [];
|
|
|
108 |
// If the UI is currently resetting.
|
|
|
109 |
var isResetting = true;
|
|
|
110 |
// If the UI is currently sending a message.
|
|
|
111 |
var isSendingMessage = false;
|
|
|
112 |
// If the UI is currently deleting a conversation.
|
|
|
113 |
var isDeletingConversationContent = false;
|
|
|
114 |
// A buffer of messages to send.
|
|
|
115 |
var sendMessageBuffer = [];
|
|
|
116 |
// These functions which will be generated when this module is
|
|
|
117 |
// first called. See generateRenderFunction for details.
|
|
|
118 |
var render = null;
|
|
|
119 |
// The list of renderers that have been registered to render
|
|
|
120 |
// this conversation. See generateRenderFunction for details.
|
|
|
121 |
var renderers = [];
|
|
|
122 |
|
|
|
123 |
var NEWEST_FIRST = Constants.NEWEST_MESSAGES_FIRST;
|
|
|
124 |
var LOAD_MESSAGE_LIMIT = Constants.LOAD_MESSAGE_LIMIT;
|
|
|
125 |
var MILLISECONDS_IN_SEC = Constants.MILLISECONDS_IN_SEC;
|
|
|
126 |
var SELECTORS = Constants.SELECTORS;
|
|
|
127 |
var CONVERSATION_TYPES = Constants.CONVERSATION_TYPES;
|
|
|
128 |
|
|
|
129 |
/**
|
|
|
130 |
* Get the other user userid.
|
|
|
131 |
*
|
|
|
132 |
* @return {Number} Userid.
|
|
|
133 |
*/
|
|
|
134 |
var getOtherUserId = function() {
|
|
|
135 |
if (!viewState || viewState.type == CONVERSATION_TYPES.PUBLIC) {
|
|
|
136 |
return null;
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
var loggedInUserId = viewState.loggedInUserId;
|
|
|
140 |
if (viewState.type == CONVERSATION_TYPES.SELF) {
|
|
|
141 |
// It's a self-conversation, so the other user is the one logged in.
|
|
|
142 |
return loggedInUserId;
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
var otherUserIds = Object.keys(viewState.members).filter(function(userId) {
|
|
|
146 |
return loggedInUserId != userId;
|
|
|
147 |
});
|
|
|
148 |
|
|
|
149 |
return otherUserIds.length ? otherUserIds[0] : null;
|
|
|
150 |
};
|
|
|
151 |
|
|
|
152 |
/**
|
|
|
153 |
* Search the cache to see if we've already loaded a private conversation
|
|
|
154 |
* with the given user id.
|
|
|
155 |
*
|
|
|
156 |
* @param {Number} userId The id of the other user.
|
|
|
157 |
* @return {Number|null} Conversation id.
|
|
|
158 |
*/
|
|
|
159 |
var getCachedPrivateConversationIdFromUserId = function(userId) {
|
|
|
160 |
return Object.keys(stateCache).reduce(function(carry, id) {
|
|
|
161 |
if (!carry) {
|
|
|
162 |
var state = stateCache[id].state;
|
|
|
163 |
|
|
|
164 |
if (state.type != CONVERSATION_TYPES.PUBLIC) {
|
|
|
165 |
if (userId in state.members) {
|
|
|
166 |
// We've found a cached conversation for this user!
|
|
|
167 |
carry = state.id;
|
|
|
168 |
}
|
|
|
169 |
}
|
|
|
170 |
}
|
|
|
171 |
|
|
|
172 |
return carry;
|
|
|
173 |
}, null);
|
|
|
174 |
};
|
|
|
175 |
|
|
|
176 |
/**
|
|
|
177 |
* Get profile info for logged in user.
|
|
|
178 |
*
|
|
|
179 |
* @param {Object} body Conversation body container element.
|
|
|
180 |
* @return {Object}
|
|
|
181 |
*/
|
|
|
182 |
var getLoggedInUserProfile = function(body) {
|
|
|
183 |
return {
|
|
|
184 |
id: parseInt(body.attr('data-user-id'), 10),
|
|
|
185 |
fullname: null,
|
|
|
186 |
profileimageurl: null,
|
|
|
187 |
profileimageurlsmall: null,
|
|
|
188 |
isonline: null,
|
|
|
189 |
showonlinestatus: null,
|
|
|
190 |
isblocked: null,
|
|
|
191 |
iscontact: null,
|
|
|
192 |
isdeleted: null,
|
|
|
193 |
canmessage: null,
|
|
|
194 |
canmessageevenifblocked: null,
|
|
|
195 |
requirescontact: null,
|
|
|
196 |
contactrequests: []
|
|
|
197 |
};
|
|
|
198 |
};
|
|
|
199 |
|
|
|
200 |
/**
|
|
|
201 |
* Get the messages offset value to load more messages.
|
|
|
202 |
*
|
|
|
203 |
* @return {Number}
|
|
|
204 |
*/
|
|
|
205 |
var getMessagesOffset = function() {
|
|
|
206 |
return messagesOffset;
|
|
|
207 |
};
|
|
|
208 |
|
|
|
209 |
/**
|
|
|
210 |
* Set the messages offset value for loading more messages.
|
|
|
211 |
*
|
|
|
212 |
* @param {Number} value The offset value
|
|
|
213 |
*/
|
|
|
214 |
var setMessagesOffset = function(value) {
|
|
|
215 |
messagesOffset = value;
|
|
|
216 |
stateCache[viewState.id].messagesOffset = value;
|
|
|
217 |
};
|
|
|
218 |
|
|
|
219 |
/**
|
|
|
220 |
* Check if all messages have been loaded.
|
|
|
221 |
*
|
|
|
222 |
* @return {Bool}
|
|
|
223 |
*/
|
|
|
224 |
var hasLoadedAllMessages = function() {
|
|
|
225 |
return loadedAllMessages;
|
|
|
226 |
};
|
|
|
227 |
|
|
|
228 |
/**
|
|
|
229 |
* Set whether all messages have been loaded or not.
|
|
|
230 |
*
|
|
|
231 |
* @param {Bool} value If all messages have been loaded.
|
|
|
232 |
*/
|
|
|
233 |
var setLoadedAllMessages = function(value) {
|
|
|
234 |
loadedAllMessages = value;
|
|
|
235 |
stateCache[viewState.id].loadedAllMessages = value;
|
|
|
236 |
};
|
|
|
237 |
|
|
|
238 |
/**
|
|
|
239 |
* Get the messages container element.
|
|
|
240 |
*
|
|
|
241 |
* @param {Object} body Conversation body container element.
|
|
|
242 |
* @return {Object} The messages container element.
|
|
|
243 |
*/
|
|
|
244 |
var getMessagesContainer = function(body) {
|
|
|
245 |
return body.find(SELECTORS.MESSAGES_CONTAINER);
|
|
|
246 |
};
|
|
|
247 |
|
|
|
248 |
/**
|
|
|
249 |
* Reformat the conversation for an event payload.
|
|
|
250 |
*
|
|
|
251 |
* @param {Object} state The view state.
|
|
|
252 |
* @return {Object} New formatted conversation.
|
|
|
253 |
*/
|
|
|
254 |
var formatConversationForEvent = function(state) {
|
|
|
255 |
return {
|
|
|
256 |
id: state.id,
|
|
|
257 |
name: state.name,
|
|
|
258 |
subname: state.subname,
|
|
|
259 |
imageUrl: state.imageUrl,
|
|
|
260 |
isFavourite: state.isFavourite,
|
|
|
261 |
isMuted: state.isMuted,
|
|
|
262 |
type: state.type,
|
|
|
263 |
totalMemberCount: state.totalMemberCount,
|
|
|
264 |
loggedInUserId: state.loggedInUserId,
|
|
|
265 |
messages: state.messages.map(function(message) {
|
|
|
266 |
return $.extend({}, message);
|
|
|
267 |
}),
|
|
|
268 |
members: Object.keys(state.members).map(function(id) {
|
|
|
269 |
var formattedMember = $.extend({}, state.members[id]);
|
|
|
270 |
formattedMember.contactrequests = state.members[id].contactrequests.map(function(request) {
|
|
|
271 |
return $.extend({}, request);
|
|
|
272 |
});
|
|
|
273 |
return formattedMember;
|
|
|
274 |
})
|
|
|
275 |
};
|
|
|
276 |
};
|
|
|
277 |
|
|
|
278 |
/**
|
|
|
279 |
* Load up an empty private conversation between the logged in user and the
|
|
|
280 |
* other user. Sets all of the conversation details based on the other user.
|
|
|
281 |
*
|
|
|
282 |
* A conversation isn't created until the user sends the first message.
|
|
|
283 |
*
|
|
|
284 |
* @param {Object} loggedInUserProfile The logged in user profile.
|
|
|
285 |
* @param {Number} otherUserId The other user id.
|
|
|
286 |
* @return {Object} Profile returned from repository.
|
|
|
287 |
*/
|
|
|
288 |
var loadEmptyPrivateConversation = function(loggedInUserProfile, otherUserId) {
|
|
|
289 |
var loggedInUserId = loggedInUserProfile.id;
|
|
|
290 |
// If the other user id is the same as the logged in user then this is a self
|
|
|
291 |
// conversation.
|
|
|
292 |
var conversationType = loggedInUserId == otherUserId ? CONVERSATION_TYPES.SELF : CONVERSATION_TYPES.PRIVATE;
|
|
|
293 |
var newState = StateManager.setLoadingMembers(viewState, true);
|
|
|
294 |
newState = StateManager.setLoadingMessages(newState, true);
|
|
|
295 |
render(newState);
|
|
|
296 |
|
|
|
297 |
return Repository.getMemberInfo(loggedInUserId, [otherUserId], true, true)
|
|
|
298 |
.then(function(profiles) {
|
|
|
299 |
if (profiles.length) {
|
|
|
300 |
return profiles[0];
|
|
|
301 |
} else {
|
|
|
302 |
throw new Error('Unable to load other user profile');
|
|
|
303 |
}
|
|
|
304 |
})
|
|
|
305 |
.then(function(profile) {
|
|
|
306 |
// If the conversation is a self conversation then the profile loaded is the
|
|
|
307 |
// logged in user so only add that to the members array.
|
|
|
308 |
var members = conversationType == CONVERSATION_TYPES.SELF ? [profile] : [profile, loggedInUserProfile];
|
|
|
309 |
var newState = StateManager.addMembers(viewState, members);
|
|
|
310 |
newState = StateManager.setLoadingMembers(newState, false);
|
|
|
311 |
newState = StateManager.setLoadingMessages(newState, false);
|
|
|
312 |
newState = StateManager.setName(newState, profile.fullname);
|
|
|
313 |
newState = StateManager.setType(newState, conversationType);
|
|
|
314 |
newState = StateManager.setImageUrl(newState, profile.profileimageurl);
|
|
|
315 |
newState = StateManager.setTotalMemberCount(newState, members.length);
|
|
|
316 |
render(newState);
|
|
|
317 |
return profile;
|
|
|
318 |
})
|
|
|
319 |
.catch(function(error) {
|
|
|
320 |
var newState = StateManager.setLoadingMembers(viewState, false);
|
|
|
321 |
render(newState);
|
|
|
322 |
Notification.exception(error);
|
|
|
323 |
});
|
|
|
324 |
};
|
|
|
325 |
|
|
|
326 |
/**
|
|
|
327 |
* Create a new state from a conversation object.
|
|
|
328 |
*
|
|
|
329 |
* @param {Object} conversation The conversation object.
|
|
|
330 |
* @param {Number} loggedInUserId The logged in user id.
|
|
|
331 |
* @return {Object} new state.
|
|
|
332 |
*/
|
|
|
333 |
var updateStateFromConversation = function(conversation, loggedInUserId) {
|
|
|
334 |
var otherUser = null;
|
|
|
335 |
if (conversation.type == CONVERSATION_TYPES.PRIVATE) {
|
|
|
336 |
// For private conversations, remove current logged in user from the members list to get the other user.
|
|
|
337 |
var otherUsers = conversation.members.filter(function(member) {
|
|
|
338 |
return member.id != loggedInUserId;
|
|
|
339 |
});
|
|
|
340 |
otherUser = otherUsers.length ? otherUsers[0] : null;
|
|
|
341 |
} else if (conversation.type == CONVERSATION_TYPES.SELF) {
|
|
|
342 |
// Self-conversations have only one member.
|
|
|
343 |
otherUser = conversation.members[0];
|
|
|
344 |
}
|
|
|
345 |
|
|
|
346 |
var name = conversation.name;
|
|
|
347 |
var imageUrl = conversation.imageurl;
|
|
|
348 |
|
|
|
349 |
if (conversation.type != CONVERSATION_TYPES.PUBLIC) {
|
|
|
350 |
name = name || otherUser ? otherUser.fullname : '';
|
|
|
351 |
imageUrl = imageUrl || otherUser ? otherUser.profileimageurl : '';
|
|
|
352 |
}
|
|
|
353 |
|
|
|
354 |
var newState = StateManager.addMembers(viewState, conversation.members);
|
|
|
355 |
newState = StateManager.setName(newState, name);
|
|
|
356 |
newState = StateManager.setSubname(newState, conversation.subname);
|
|
|
357 |
newState = StateManager.setType(newState, conversation.type);
|
|
|
358 |
newState = StateManager.setImageUrl(newState, imageUrl);
|
|
|
359 |
newState = StateManager.setTotalMemberCount(newState, conversation.membercount);
|
|
|
360 |
newState = StateManager.setIsFavourite(newState, conversation.isfavourite);
|
|
|
361 |
newState = StateManager.setIsMuted(newState, conversation.ismuted);
|
|
|
362 |
newState = StateManager.addMessages(newState, conversation.messages);
|
|
|
363 |
newState = StateManager.setCanDeleteMessagesForAllUsers(newState, conversation.candeletemessagesforallusers);
|
|
|
364 |
return newState;
|
|
|
365 |
};
|
|
|
366 |
|
|
|
367 |
/**
|
|
|
368 |
* Get the details for a conversation from the conversation id.
|
|
|
369 |
*
|
|
|
370 |
* @param {Number} conversationId The conversation id.
|
|
|
371 |
* @param {Object} loggedInUserProfile The logged in user profile.
|
|
|
372 |
* @param {Number} messageLimit The number of messages to include.
|
|
|
373 |
* @param {Number} messageOffset The number of messages to skip.
|
|
|
374 |
* @param {Bool} newestFirst Order messages newest first.
|
|
|
375 |
* @return {Object} Promise resolved when loaded.
|
|
|
376 |
*/
|
|
|
377 |
var loadNewConversation = function(
|
|
|
378 |
conversationId,
|
|
|
379 |
loggedInUserProfile,
|
|
|
380 |
messageLimit,
|
|
|
381 |
messageOffset,
|
|
|
382 |
newestFirst
|
|
|
383 |
) {
|
|
|
384 |
var loggedInUserId = loggedInUserProfile.id;
|
|
|
385 |
var newState = StateManager.setLoadingMembers(viewState, true);
|
|
|
386 |
newState = StateManager.setLoadingMessages(newState, true);
|
|
|
387 |
render(newState);
|
|
|
388 |
|
|
|
389 |
return Repository.getConversation(
|
|
|
390 |
loggedInUserId,
|
|
|
391 |
conversationId,
|
|
|
392 |
true,
|
|
|
393 |
true,
|
|
|
394 |
0,
|
|
|
395 |
0,
|
|
|
396 |
messageLimit + 1,
|
|
|
397 |
messageOffset,
|
|
|
398 |
newestFirst
|
|
|
399 |
)
|
|
|
400 |
.then(function(conversation) {
|
|
|
401 |
if (conversation.messages.length > messageLimit) {
|
|
|
402 |
conversation.messages = conversation.messages.slice(1);
|
|
|
403 |
} else {
|
|
|
404 |
setLoadedAllMessages(true);
|
|
|
405 |
}
|
|
|
406 |
|
|
|
407 |
setMessagesOffset(messageOffset + messageLimit);
|
|
|
408 |
|
|
|
409 |
return conversation;
|
|
|
410 |
})
|
|
|
411 |
.then(function(conversation) {
|
|
|
412 |
var hasLoggedInUser = conversation.members.filter(function(member) {
|
|
|
413 |
return member.id == loggedInUserProfile.id;
|
|
|
414 |
});
|
|
|
415 |
|
|
|
416 |
if (hasLoggedInUser.length < 1) {
|
|
|
417 |
conversation.members = conversation.members.concat([loggedInUserProfile]);
|
|
|
418 |
}
|
|
|
419 |
|
|
|
420 |
var newState = updateStateFromConversation(conversation, loggedInUserProfile.id);
|
|
|
421 |
newState = StateManager.setLoadingMembers(newState, false);
|
|
|
422 |
newState = StateManager.setLoadingMessages(newState, false);
|
|
|
423 |
return render(newState)
|
|
|
424 |
.then(function() {
|
|
|
425 |
return conversation;
|
|
|
426 |
});
|
|
|
427 |
})
|
|
|
428 |
.then(function() {
|
|
|
429 |
return markConversationAsRead(conversationId);
|
|
|
430 |
})
|
|
|
431 |
.catch(function(error) {
|
|
|
432 |
var newState = StateManager.setLoadingMembers(viewState, false);
|
|
|
433 |
newState = StateManager.setLoadingMessages(newState, false);
|
|
|
434 |
render(newState);
|
|
|
435 |
Notification.exception(error);
|
|
|
436 |
});
|
|
|
437 |
};
|
|
|
438 |
|
|
|
439 |
/**
|
|
|
440 |
* Get the details for a conversation from and existing conversation object.
|
|
|
441 |
*
|
|
|
442 |
* @param {Object} conversation The conversation object.
|
|
|
443 |
* @param {Object} loggedInUserProfile The logged in user profile.
|
|
|
444 |
* @param {Number} messageLimit The number of messages to include.
|
|
|
445 |
* @param {Bool} newestFirst Order messages newest first.
|
|
|
446 |
* @return {Object} Promise resolved when loaded.
|
|
|
447 |
*/
|
|
|
448 |
var loadExistingConversation = function(
|
|
|
449 |
conversation,
|
|
|
450 |
loggedInUserProfile,
|
|
|
451 |
messageLimit,
|
|
|
452 |
newestFirst
|
|
|
453 |
) {
|
|
|
454 |
var hasLoggedInUser = conversation.members.filter(function(member) {
|
|
|
455 |
return member.id == loggedInUserProfile.id;
|
|
|
456 |
});
|
|
|
457 |
|
|
|
458 |
if (hasLoggedInUser.length < 1) {
|
|
|
459 |
conversation.members = conversation.members.concat([loggedInUserProfile]);
|
|
|
460 |
}
|
|
|
461 |
|
|
|
462 |
var messageCount = conversation.messages.length;
|
|
|
463 |
var hasLoadedEnoughMessages = messageCount >= messageLimit;
|
|
|
464 |
var newState = updateStateFromConversation(conversation, loggedInUserProfile.id);
|
|
|
465 |
newState = StateManager.setLoadingMembers(newState, false);
|
|
|
466 |
newState = StateManager.setLoadingMessages(newState, !hasLoadedEnoughMessages);
|
|
|
467 |
var renderPromise = render(newState);
|
|
|
468 |
|
|
|
469 |
return renderPromise.then(function() {
|
|
|
470 |
if (!hasLoadedEnoughMessages) {
|
|
|
471 |
// We haven't got enough messages so let's load some more.
|
|
|
472 |
return loadMessages(conversation.id, messageLimit, messageCount, newestFirst, []);
|
|
|
473 |
} else {
|
|
|
474 |
// We've got enough messages. No need to load any more for now.
|
|
|
475 |
return {messages: conversation.messages};
|
|
|
476 |
}
|
|
|
477 |
})
|
|
|
478 |
.then(function() {
|
|
|
479 |
var messages = viewState.messages;
|
|
|
480 |
// Update the offset to reflect the number of messages we've loaded.
|
|
|
481 |
setMessagesOffset(messages.length);
|
|
|
482 |
markConversationAsRead(viewState.id);
|
|
|
483 |
|
|
|
484 |
return messages;
|
|
|
485 |
})
|
|
|
486 |
.catch(Notification.exception);
|
|
|
487 |
};
|
|
|
488 |
|
|
|
489 |
/**
|
|
|
490 |
* Load messages for this conversation and pass them to the renderer.
|
|
|
491 |
*
|
|
|
492 |
* @param {Number} conversationId Conversation id.
|
|
|
493 |
* @param {Number} limit Number of messages to load.
|
|
|
494 |
* @param {Number} offset Get messages from offset.
|
|
|
495 |
* @param {Bool} newestFirst Get newest messages first.
|
|
|
496 |
* @param {Array} ignoreList Ignore any messages with ids in this list.
|
|
|
497 |
* @param {Number|null} timeFrom Only get messages from this time onwards.
|
|
|
498 |
* @return {Promise} renderer promise.
|
|
|
499 |
*/
|
|
|
500 |
var loadMessages = function(conversationId, limit, offset, newestFirst, ignoreList, timeFrom) {
|
|
|
501 |
return Repository.getMessages(
|
|
|
502 |
viewState.loggedInUserId,
|
|
|
503 |
conversationId,
|
|
|
504 |
limit ? limit + 1 : limit,
|
|
|
505 |
offset,
|
|
|
506 |
newestFirst,
|
|
|
507 |
timeFrom
|
|
|
508 |
)
|
|
|
509 |
.then(function(result) {
|
|
|
510 |
// Prevent older requests from contaminating the current view.
|
|
|
511 |
if (result.id != viewState.id) {
|
|
|
512 |
result.messages = [];
|
|
|
513 |
// Purge old conversation cache to prevent messages lose.
|
|
|
514 |
if (result.id in stateCache) {
|
|
|
515 |
delete stateCache[result.id];
|
|
|
516 |
}
|
|
|
517 |
}
|
|
|
518 |
|
|
|
519 |
return result;
|
|
|
520 |
})
|
|
|
521 |
.then(function(result) {
|
|
|
522 |
if (result.messages.length && ignoreList.length) {
|
|
|
523 |
result.messages = result.messages.filter(function(message) {
|
|
|
524 |
// Skip any messages in our ignore list.
|
|
|
525 |
return ignoreList.indexOf(parseInt(message.id, 10)) < 0;
|
|
|
526 |
});
|
|
|
527 |
}
|
|
|
528 |
|
|
|
529 |
return result;
|
|
|
530 |
})
|
|
|
531 |
.then(function(result) {
|
|
|
532 |
if (!limit) {
|
|
|
533 |
return result;
|
|
|
534 |
} else if (result.messages.length > limit) {
|
|
|
535 |
// Ignore the last result which was just to test if there are more
|
|
|
536 |
// to load.
|
|
|
537 |
result.messages = result.messages.slice(0, -1);
|
|
|
538 |
} else {
|
|
|
539 |
setLoadedAllMessages(true);
|
|
|
540 |
}
|
|
|
541 |
|
|
|
542 |
return result;
|
|
|
543 |
})
|
|
|
544 |
.then(function(result) {
|
|
|
545 |
var membersToAdd = result.members.filter(function(member) {
|
|
|
546 |
return !(member.id in viewState.members);
|
|
|
547 |
});
|
|
|
548 |
var newState = StateManager.addMembers(viewState, membersToAdd);
|
|
|
549 |
newState = StateManager.addMessages(newState, result.messages);
|
|
|
550 |
newState = StateManager.setLoadingMessages(newState, false);
|
|
|
551 |
return render(newState)
|
|
|
552 |
.then(function() {
|
|
|
553 |
return result;
|
|
|
554 |
});
|
|
|
555 |
})
|
|
|
556 |
.catch(function(error) {
|
|
|
557 |
var newState = StateManager.setLoadingMessages(viewState, false);
|
|
|
558 |
render(newState);
|
|
|
559 |
// Re-throw the error for other error handlers.
|
|
|
560 |
throw error;
|
|
|
561 |
});
|
|
|
562 |
};
|
|
|
563 |
|
|
|
564 |
/**
|
|
|
565 |
* Create a callback function for getting new messages for this conversation.
|
|
|
566 |
*
|
|
|
567 |
* @param {Number} conversationId Conversation id.
|
|
|
568 |
* @param {Bool} newestFirst Show newest messages first
|
|
|
569 |
* @return {Function} Callback function that returns a renderer promise.
|
|
|
570 |
*/
|
|
|
571 |
var getLoadNewMessagesCallback = function(conversationId, newestFirst) {
|
|
|
572 |
return function() {
|
|
|
573 |
var messages = viewState.messages;
|
|
|
574 |
var mostRecentMessage = messages.length ? messages[messages.length - 1] : null;
|
|
|
575 |
var lastTimeCreated = mostRecentMessage ? mostRecentMessage.timeCreated : null;
|
|
|
576 |
|
|
|
577 |
if (lastTimeCreated && !isResetting && !isSendingMessage && !isDeletingConversationContent) {
|
|
|
578 |
// There may be multiple messages with the same time created value since
|
|
|
579 |
// the accuracy is only down to the second. The server will include these
|
|
|
580 |
// messages in the result (since it does a >= comparison on time from) so
|
|
|
581 |
// we need to filter them back out of the result so that we're left only
|
|
|
582 |
// with the new messages.
|
|
|
583 |
var ignoreMessageIds = [];
|
|
|
584 |
for (var i = messages.length - 1; i >= 0; i--) {
|
|
|
585 |
var message = messages[i];
|
|
|
586 |
if (message.timeCreated === lastTimeCreated) {
|
|
|
587 |
ignoreMessageIds.push(message.id);
|
|
|
588 |
} else {
|
|
|
589 |
// Since the messages are ordered in ascending order of time created
|
|
|
590 |
// we can break as soon as we hit a message with a different time created
|
|
|
591 |
// because we know all other messages will have lower values.
|
|
|
592 |
break;
|
|
|
593 |
}
|
|
|
594 |
}
|
|
|
595 |
|
|
|
596 |
return loadMessages(
|
|
|
597 |
conversationId,
|
|
|
598 |
0,
|
|
|
599 |
0,
|
|
|
600 |
newestFirst,
|
|
|
601 |
ignoreMessageIds,
|
|
|
602 |
lastTimeCreated
|
|
|
603 |
)
|
|
|
604 |
.then(function(result) {
|
|
|
605 |
if (result.messages.length) {
|
|
|
606 |
// If we found some results then restart the polling timer
|
|
|
607 |
// because the other user might be sending messages.
|
|
|
608 |
newMessagesPollTimer.restart();
|
|
|
609 |
// We've also got a new last message so publish that for other
|
|
|
610 |
// components to update.
|
|
|
611 |
var conversation = formatConversationForEvent(viewState);
|
|
|
612 |
PubSub.publish(MessageDrawerEvents.CONVERSATION_NEW_LAST_MESSAGE, conversation);
|
|
|
613 |
return markConversationAsRead(conversationId);
|
|
|
614 |
} else {
|
|
|
615 |
return result;
|
|
|
616 |
}
|
|
|
617 |
});
|
|
|
618 |
}
|
|
|
619 |
|
|
|
620 |
return $.Deferred().resolve().promise();
|
|
|
621 |
};
|
|
|
622 |
};
|
|
|
623 |
|
|
|
624 |
/**
|
|
|
625 |
* Mark a conversation as read.
|
|
|
626 |
*
|
|
|
627 |
* @param {Number} conversationId The conversation id.
|
|
|
628 |
* @return {Promise} The renderer promise.
|
|
|
629 |
*/
|
|
|
630 |
var markConversationAsRead = function(conversationId) {
|
|
|
631 |
var loggedInUserId = viewState.loggedInUserId;
|
|
|
632 |
var pendingPromise = new Pending('core_message/message_drawer_view_conversation:markConversationAsRead');
|
|
|
633 |
|
|
|
634 |
return Repository.markAllConversationMessagesAsRead(loggedInUserId, conversationId)
|
|
|
635 |
.then(function() {
|
|
|
636 |
var newState = StateManager.markMessagesAsRead(viewState, viewState.messages);
|
|
|
637 |
PubSub.publish(MessageDrawerEvents.CONVERSATION_READ, conversationId);
|
|
|
638 |
return render(newState);
|
|
|
639 |
})
|
|
|
640 |
.then(function(result) {
|
|
|
641 |
pendingPromise.resolve();
|
|
|
642 |
|
|
|
643 |
return result;
|
|
|
644 |
});
|
|
|
645 |
};
|
|
|
646 |
|
|
|
647 |
/**
|
|
|
648 |
* Tell the statemanager there is request to block a user and run the renderer
|
|
|
649 |
* to show the block user dialogue.
|
|
|
650 |
*
|
|
|
651 |
* @param {Number} userId User id.
|
|
|
652 |
*/
|
|
|
653 |
var requestBlockUser = function(userId) {
|
|
|
654 |
cancelRequest(userId);
|
|
|
655 |
var newState = StateManager.addPendingBlockUsersById(viewState, [userId]);
|
|
|
656 |
render(newState);
|
|
|
657 |
};
|
|
|
658 |
|
|
|
659 |
/**
|
|
|
660 |
* Send the repository a request to block a user, update the statemanager and publish
|
|
|
661 |
* a contact has been blocked.
|
|
|
662 |
*
|
|
|
663 |
* @param {Number} userId User id of user to block.
|
|
|
664 |
* @return {Promise} Renderer promise.
|
|
|
665 |
*/
|
|
|
666 |
var blockUser = function(userId) {
|
|
|
667 |
var newState = StateManager.setLoadingConfirmAction(viewState, true);
|
|
|
668 |
var pendingPromise = new Pending('core_message/message_drawer_view_conversation:blockUser');
|
|
|
669 |
|
|
|
670 |
render(newState);
|
|
|
671 |
|
|
|
672 |
return Repository.blockUser(viewState.loggedInUserId, userId)
|
|
|
673 |
.then(function(profile) {
|
|
|
674 |
var newState = StateManager.addMembers(viewState, [profile]);
|
|
|
675 |
newState = StateManager.removePendingBlockUsersById(newState, [userId]);
|
|
|
676 |
newState = StateManager.setLoadingConfirmAction(newState, false);
|
|
|
677 |
PubSub.publish(MessageDrawerEvents.CONTACT_BLOCKED, userId);
|
|
|
678 |
return render(newState);
|
|
|
679 |
})
|
|
|
680 |
.then(function(result) {
|
|
|
681 |
pendingPromise.resolve();
|
|
|
682 |
|
|
|
683 |
return result;
|
|
|
684 |
});
|
|
|
685 |
};
|
|
|
686 |
|
|
|
687 |
/**
|
|
|
688 |
* Tell the statemanager there is a request to unblock a user and run the renderer
|
|
|
689 |
* to show the unblock user dialogue.
|
|
|
690 |
*
|
|
|
691 |
* @param {Number} userId User id of user to unblock.
|
|
|
692 |
*/
|
|
|
693 |
var requestUnblockUser = function(userId) {
|
|
|
694 |
cancelRequest(userId);
|
|
|
695 |
var newState = StateManager.addPendingUnblockUsersById(viewState, [userId]);
|
|
|
696 |
render(newState);
|
|
|
697 |
};
|
|
|
698 |
|
|
|
699 |
/**
|
|
|
700 |
* Send the repository a request to unblock a user, update the statemanager and publish
|
|
|
701 |
* a contact has been unblocked.
|
|
|
702 |
*
|
|
|
703 |
* @param {Number} userId User id of user to unblock.
|
|
|
704 |
* @return {Promise} Renderer promise.
|
|
|
705 |
*/
|
|
|
706 |
var unblockUser = function(userId) {
|
|
|
707 |
var newState = StateManager.setLoadingConfirmAction(viewState, true);
|
|
|
708 |
var pendingPromise = new Pending('core_message/message_drawer_view_conversation:unblockUser');
|
|
|
709 |
render(newState);
|
|
|
710 |
|
|
|
711 |
return Repository.unblockUser(viewState.loggedInUserId, userId)
|
|
|
712 |
.then(function(profile) {
|
|
|
713 |
var newState = StateManager.addMembers(viewState, [profile]);
|
|
|
714 |
newState = StateManager.removePendingUnblockUsersById(newState, [userId]);
|
|
|
715 |
newState = StateManager.setLoadingConfirmAction(newState, false);
|
|
|
716 |
PubSub.publish(MessageDrawerEvents.CONTACT_UNBLOCKED, userId);
|
|
|
717 |
return render(newState);
|
|
|
718 |
})
|
|
|
719 |
.then(function(result) {
|
|
|
720 |
pendingPromise.resolve();
|
|
|
721 |
|
|
|
722 |
return result;
|
|
|
723 |
});
|
|
|
724 |
};
|
|
|
725 |
|
|
|
726 |
/**
|
|
|
727 |
* Tell the statemanager there is a request to remove a user from the contact list
|
|
|
728 |
* and run the renderer to show the remove user from contacts dialogue.
|
|
|
729 |
*
|
|
|
730 |
* @param {Number} userId User id of user to remove from contacts.
|
|
|
731 |
*/
|
|
|
732 |
var requestRemoveContact = function(userId) {
|
|
|
733 |
cancelRequest(userId);
|
|
|
734 |
var newState = StateManager.addPendingRemoveContactsById(viewState, [userId]);
|
|
|
735 |
render(newState);
|
|
|
736 |
};
|
|
|
737 |
|
|
|
738 |
/**
|
|
|
739 |
* Send the repository a request to remove a user from the contacts list. update the statemanager
|
|
|
740 |
* and publish a contact has been removed.
|
|
|
741 |
*
|
|
|
742 |
* @param {Number} userId User id of user to remove from contacts.
|
|
|
743 |
* @return {Promise} Renderer promise.
|
|
|
744 |
*/
|
|
|
745 |
var removeContact = function(userId) {
|
|
|
746 |
var newState = StateManager.setLoadingConfirmAction(viewState, true);
|
|
|
747 |
var pendingPromise = new Pending('core_message/message_drawer_view_conversation:removeContact');
|
|
|
748 |
render(newState);
|
|
|
749 |
|
|
|
750 |
return Repository.deleteContacts(viewState.loggedInUserId, [userId])
|
|
|
751 |
.then(function(profiles) {
|
|
|
752 |
var newState = StateManager.addMembers(viewState, profiles);
|
|
|
753 |
newState = StateManager.removePendingRemoveContactsById(newState, [userId]);
|
|
|
754 |
newState = StateManager.setLoadingConfirmAction(newState, false);
|
|
|
755 |
PubSub.publish(MessageDrawerEvents.CONTACT_REMOVED, userId);
|
|
|
756 |
return render(newState);
|
|
|
757 |
})
|
|
|
758 |
.then(function(result) {
|
|
|
759 |
pendingPromise.resolve();
|
|
|
760 |
|
|
|
761 |
return result;
|
|
|
762 |
});
|
|
|
763 |
};
|
|
|
764 |
|
|
|
765 |
/**
|
|
|
766 |
* Tell the statemanager there is a request to add a user to the contact list
|
|
|
767 |
* and run the renderer to show the add user to contacts dialogue.
|
|
|
768 |
*
|
|
|
769 |
* @param {Number} userId User id of user to add to contacts.
|
|
|
770 |
*/
|
|
|
771 |
var requestAddContact = function(userId) {
|
|
|
772 |
cancelRequest(userId);
|
|
|
773 |
var newState = StateManager.addPendingAddContactsById(viewState, [userId]);
|
|
|
774 |
render(newState);
|
|
|
775 |
};
|
|
|
776 |
|
|
|
777 |
/**
|
|
|
778 |
* Send the repository a request to add a user to the contacts list. update the statemanager
|
|
|
779 |
* and publish a contact has been added.
|
|
|
780 |
*
|
|
|
781 |
* @param {Number} userId User id of user to add to contacts.
|
|
|
782 |
* @return {Promise} Renderer promise.
|
|
|
783 |
*/
|
|
|
784 |
var addContact = function(userId) {
|
|
|
785 |
var newState = StateManager.setLoadingConfirmAction(viewState, true);
|
|
|
786 |
var pendingPromise = new Pending('core_message/message_drawer_view_conversation:addContactRequests');
|
|
|
787 |
render(newState);
|
|
|
788 |
|
|
|
789 |
return Repository.createContactRequest(viewState.loggedInUserId, userId)
|
|
|
790 |
.then(function(response) {
|
|
|
791 |
if (!response.request) {
|
|
|
792 |
throw new Error(response.warnings[0].message);
|
|
|
793 |
}
|
|
|
794 |
|
|
|
795 |
return response.request;
|
|
|
796 |
})
|
|
|
797 |
.then(function(request) {
|
|
|
798 |
var newState = StateManager.removePendingAddContactsById(viewState, [userId]);
|
|
|
799 |
newState = StateManager.addContactRequests(newState, [request]);
|
|
|
800 |
newState = StateManager.setLoadingConfirmAction(newState, false);
|
|
|
801 |
return render(newState);
|
|
|
802 |
})
|
|
|
803 |
.then(function(result) {
|
|
|
804 |
pendingPromise.resolve();
|
|
|
805 |
|
|
|
806 |
return result;
|
|
|
807 |
});
|
|
|
808 |
};
|
|
|
809 |
|
|
|
810 |
/**
|
|
|
811 |
* Set the current conversation as a favourite conversation.
|
|
|
812 |
*
|
|
|
813 |
* @return {Promise} Renderer promise.
|
|
|
814 |
*/
|
|
|
815 |
var setFavourite = function() {
|
|
|
816 |
var userId = viewState.loggedInUserId;
|
|
|
817 |
var conversationId = viewState.id;
|
|
|
818 |
var pendingPromise = new Pending('core_message/message_drawer_view_conversation:setFavourite');
|
|
|
819 |
|
|
|
820 |
return Repository.setFavouriteConversations(userId, [conversationId])
|
|
|
821 |
.then(function() {
|
|
|
822 |
var newState = StateManager.setIsFavourite(viewState, true);
|
|
|
823 |
return render(newState);
|
|
|
824 |
})
|
|
|
825 |
.then(function() {
|
|
|
826 |
return PubSub.publish(
|
|
|
827 |
MessageDrawerEvents.CONVERSATION_SET_FAVOURITE,
|
|
|
828 |
formatConversationForEvent(viewState)
|
|
|
829 |
);
|
|
|
830 |
})
|
|
|
831 |
.then(function(result) {
|
|
|
832 |
pendingPromise.resolve();
|
|
|
833 |
|
|
|
834 |
return result;
|
|
|
835 |
});
|
|
|
836 |
};
|
|
|
837 |
|
|
|
838 |
/**
|
|
|
839 |
* Unset the current conversation as a favourite conversation.
|
|
|
840 |
*
|
|
|
841 |
* @return {Promise} Renderer promise.
|
|
|
842 |
*/
|
|
|
843 |
var unsetFavourite = function() {
|
|
|
844 |
var userId = viewState.loggedInUserId;
|
|
|
845 |
var conversationId = viewState.id;
|
|
|
846 |
var pendingPromise = new Pending('core_message/message_drawer_view_conversation:unsetFavourite');
|
|
|
847 |
|
|
|
848 |
return Repository.unsetFavouriteConversations(userId, [conversationId])
|
|
|
849 |
.then(function() {
|
|
|
850 |
var newState = StateManager.setIsFavourite(viewState, false);
|
|
|
851 |
return render(newState);
|
|
|
852 |
})
|
|
|
853 |
.then(function() {
|
|
|
854 |
return PubSub.publish(
|
|
|
855 |
MessageDrawerEvents.CONVERSATION_UNSET_FAVOURITE,
|
|
|
856 |
formatConversationForEvent(viewState)
|
|
|
857 |
);
|
|
|
858 |
})
|
|
|
859 |
.then(function(result) {
|
|
|
860 |
pendingPromise.resolve();
|
|
|
861 |
|
|
|
862 |
return result;
|
|
|
863 |
});
|
|
|
864 |
};
|
|
|
865 |
|
|
|
866 |
/**
|
|
|
867 |
* Set the current conversation as a muted conversation.
|
|
|
868 |
*
|
|
|
869 |
* @return {Promise} Renderer promise.
|
|
|
870 |
*/
|
|
|
871 |
var setMuted = function() {
|
|
|
872 |
var userId = viewState.loggedInUserId;
|
|
|
873 |
var conversationId = viewState.id;
|
|
|
874 |
var pendingPromise = new Pending('core_message/message_drawer_view_conversation:markConversationAsRead');
|
|
|
875 |
|
|
|
876 |
return Repository.setMutedConversations(userId, [conversationId])
|
|
|
877 |
.then(function() {
|
|
|
878 |
var newState = StateManager.setIsMuted(viewState, true);
|
|
|
879 |
return render(newState);
|
|
|
880 |
})
|
|
|
881 |
.then(function() {
|
|
|
882 |
return PubSub.publish(
|
|
|
883 |
MessageDrawerEvents.CONVERSATION_SET_MUTED,
|
|
|
884 |
formatConversationForEvent(viewState)
|
|
|
885 |
);
|
|
|
886 |
})
|
|
|
887 |
.then(function(result) {
|
|
|
888 |
pendingPromise.resolve();
|
|
|
889 |
|
|
|
890 |
return result;
|
|
|
891 |
});
|
|
|
892 |
};
|
|
|
893 |
|
|
|
894 |
/**
|
|
|
895 |
* Unset the current conversation as a muted conversation.
|
|
|
896 |
*
|
|
|
897 |
* @return {Promise} Renderer promise.
|
|
|
898 |
*/
|
|
|
899 |
var unsetMuted = function() {
|
|
|
900 |
var userId = viewState.loggedInUserId;
|
|
|
901 |
var conversationId = viewState.id;
|
|
|
902 |
|
|
|
903 |
return Repository.unsetMutedConversations(userId, [conversationId])
|
|
|
904 |
.then(function() {
|
|
|
905 |
var newState = StateManager.setIsMuted(viewState, false);
|
|
|
906 |
return render(newState);
|
|
|
907 |
})
|
|
|
908 |
.then(function() {
|
|
|
909 |
return PubSub.publish(
|
|
|
910 |
MessageDrawerEvents.CONVERSATION_UNSET_MUTED,
|
|
|
911 |
formatConversationForEvent(viewState)
|
|
|
912 |
);
|
|
|
913 |
});
|
|
|
914 |
};
|
|
|
915 |
|
|
|
916 |
/**
|
|
|
917 |
* Tell the statemanager there is a request to delete the selected messages
|
|
|
918 |
* and run the renderer to show confirm delete messages dialogue.
|
|
|
919 |
*
|
|
|
920 |
* @param {Number} userId User id.
|
|
|
921 |
*/
|
|
|
922 |
var requestDeleteSelectedMessages = function(userId) {
|
|
|
923 |
var selectedMessageIds = viewState.selectedMessageIds;
|
|
|
924 |
cancelRequest(userId);
|
|
|
925 |
var newState = StateManager.addPendingDeleteMessagesById(viewState, selectedMessageIds);
|
|
|
926 |
render(newState);
|
|
|
927 |
};
|
|
|
928 |
|
|
|
929 |
/**
|
|
|
930 |
* Send the repository a request to delete the messages pending deletion. Update the statemanager
|
|
|
931 |
* and publish a message deletion event.
|
|
|
932 |
*
|
|
|
933 |
* @return {Promise} Renderer promise.
|
|
|
934 |
*/
|
|
|
935 |
var deleteSelectedMessages = function() {
|
|
|
936 |
var pendingPromise = new Pending('core_message/message_drawer_view_conversation:deleteSelectedMessages');
|
|
|
937 |
var messageIds = viewState.pendingDeleteMessageIds;
|
|
|
938 |
var sentMessages = viewState.messages.filter(function(message) {
|
|
|
939 |
// If a message sendState is null then it means it was loaded from the server or if it's
|
|
|
940 |
// set to sent then it means the user has successfully sent it in this page load.
|
|
|
941 |
return messageIds.indexOf(message.id) >= 0 && (message.sendState == 'sent' || message.sendState === null);
|
|
|
942 |
});
|
|
|
943 |
var newState = StateManager.setLoadingConfirmAction(viewState, true);
|
|
|
944 |
|
|
|
945 |
render(newState);
|
|
|
946 |
|
|
|
947 |
var deleteMessagesPromise = $.Deferred().resolve().promise();
|
|
|
948 |
|
|
|
949 |
|
|
|
950 |
if (sentMessages.length) {
|
|
|
951 |
// We only need to send a request to the server if we're trying to delete messages that
|
|
|
952 |
// have successfully been sent.
|
|
|
953 |
var sentMessageIds = sentMessages.map(function(message) {
|
|
|
954 |
return message.id;
|
|
|
955 |
});
|
|
|
956 |
if (newState.deleteMessagesForAllUsers) {
|
|
|
957 |
deleteMessagesPromise = Repository.deleteMessagesForAllUsers(viewState.loggedInUserId, sentMessageIds);
|
|
|
958 |
} else {
|
|
|
959 |
deleteMessagesPromise = Repository.deleteMessages(viewState.loggedInUserId, sentMessageIds);
|
|
|
960 |
}
|
|
|
961 |
}
|
|
|
962 |
|
|
|
963 |
// Mark that we are deleting content from the conversation to prevent updates of it.
|
|
|
964 |
isDeletingConversationContent = true;
|
|
|
965 |
|
|
|
966 |
// Stop polling for new messages to the open conversation.
|
|
|
967 |
if (newMessagesPollTimer) {
|
|
|
968 |
newMessagesPollTimer.stop();
|
|
|
969 |
}
|
|
|
970 |
|
|
|
971 |
return deleteMessagesPromise.then(function() {
|
|
|
972 |
var newState = StateManager.removeMessagesById(viewState, messageIds);
|
|
|
973 |
newState = StateManager.removePendingDeleteMessagesById(newState, messageIds);
|
|
|
974 |
newState = StateManager.removeSelectedMessagesById(newState, messageIds);
|
|
|
975 |
newState = StateManager.setLoadingConfirmAction(newState, false);
|
|
|
976 |
newState = StateManager.setDeleteMessagesForAllUsers(newState, false);
|
|
|
977 |
|
|
|
978 |
var prevLastMessage = viewState.messages[viewState.messages.length - 1];
|
|
|
979 |
var newLastMessage = newState.messages.length ? newState.messages[newState.messages.length - 1] : null;
|
|
|
980 |
|
|
|
981 |
if (newLastMessage && newLastMessage.id != prevLastMessage.id) {
|
|
|
982 |
var conversation = formatConversationForEvent(newState);
|
|
|
983 |
PubSub.publish(MessageDrawerEvents.CONVERSATION_NEW_LAST_MESSAGE, conversation);
|
|
|
984 |
} else if (!newState.messages.length) {
|
|
|
985 |
PubSub.publish(MessageDrawerEvents.CONVERSATION_DELETED, newState.id);
|
|
|
986 |
}
|
|
|
987 |
|
|
|
988 |
isDeletingConversationContent = false;
|
|
|
989 |
return render(newState);
|
|
|
990 |
})
|
|
|
991 |
.then(function(result) {
|
|
|
992 |
pendingPromise.resolve();
|
|
|
993 |
|
|
|
994 |
return result;
|
|
|
995 |
})
|
|
|
996 |
.catch(Notification.exception);
|
|
|
997 |
};
|
|
|
998 |
|
|
|
999 |
/**
|
|
|
1000 |
* Tell the statemanager there is a request to delete a conversation
|
|
|
1001 |
* and run the renderer to show confirm delete conversation dialogue.
|
|
|
1002 |
*
|
|
|
1003 |
* @param {Number} userId User id of other user.
|
|
|
1004 |
*/
|
|
|
1005 |
var requestDeleteConversation = function(userId) {
|
|
|
1006 |
cancelRequest(userId);
|
|
|
1007 |
var newState = StateManager.setPendingDeleteConversation(viewState, true);
|
|
|
1008 |
render(newState);
|
|
|
1009 |
};
|
|
|
1010 |
|
|
|
1011 |
/**
|
|
|
1012 |
* Send the repository a request to delete a conversation. Update the statemanager
|
|
|
1013 |
* and publish a conversation deleted event.
|
|
|
1014 |
*
|
|
|
1015 |
* @return {Promise} Renderer promise.
|
|
|
1016 |
*/
|
|
|
1017 |
var deleteConversation = function() {
|
|
|
1018 |
var pendingPromise = new Pending('core_message/message_drawer_view_conversation:markConversationAsRead');
|
|
|
1019 |
var newState = StateManager.setLoadingConfirmAction(viewState, true);
|
|
|
1020 |
render(newState);
|
|
|
1021 |
|
|
|
1022 |
// Mark that we are deleting the conversation to prevent updates of it.
|
|
|
1023 |
isDeletingConversationContent = true;
|
|
|
1024 |
|
|
|
1025 |
// Stop polling for new messages to the open conversation.
|
|
|
1026 |
if (newMessagesPollTimer) {
|
|
|
1027 |
newMessagesPollTimer.stop();
|
|
|
1028 |
}
|
|
|
1029 |
|
|
|
1030 |
return Repository.deleteConversation(viewState.loggedInUserId, viewState.id)
|
|
|
1031 |
.then(function() {
|
|
|
1032 |
var newState = StateManager.removeMessages(viewState, viewState.messages);
|
|
|
1033 |
newState = StateManager.removeSelectedMessagesById(newState, viewState.selectedMessageIds);
|
|
|
1034 |
newState = StateManager.setPendingDeleteConversation(newState, false);
|
|
|
1035 |
newState = StateManager.setLoadingConfirmAction(newState, false);
|
|
|
1036 |
PubSub.publish(MessageDrawerEvents.CONVERSATION_DELETED, newState.id);
|
|
|
1037 |
|
|
|
1038 |
isDeletingConversationContent = false;
|
|
|
1039 |
|
|
|
1040 |
return render(newState);
|
|
|
1041 |
})
|
|
|
1042 |
.then(function(result) {
|
|
|
1043 |
pendingPromise.resolve();
|
|
|
1044 |
|
|
|
1045 |
return result;
|
|
|
1046 |
});
|
|
|
1047 |
};
|
|
|
1048 |
|
|
|
1049 |
/**
|
|
|
1050 |
* Tell the statemanager to cancel all pending actions.
|
|
|
1051 |
*
|
|
|
1052 |
* @param {Number} userId User id.
|
|
|
1053 |
*/
|
|
|
1054 |
var cancelRequest = function(userId) {
|
|
|
1055 |
var pendingDeleteMessageIds = viewState.pendingDeleteMessageIds;
|
|
|
1056 |
var newState = StateManager.removePendingAddContactsById(viewState, [userId]);
|
|
|
1057 |
newState = StateManager.removePendingRemoveContactsById(newState, [userId]);
|
|
|
1058 |
newState = StateManager.removePendingUnblockUsersById(newState, [userId]);
|
|
|
1059 |
newState = StateManager.removePendingBlockUsersById(newState, [userId]);
|
|
|
1060 |
newState = StateManager.removePendingDeleteMessagesById(newState, pendingDeleteMessageIds);
|
|
|
1061 |
newState = StateManager.setPendingDeleteConversation(newState, false);
|
|
|
1062 |
newState = StateManager.setDeleteMessagesForAllUsers(newState, false);
|
|
|
1063 |
render(newState);
|
|
|
1064 |
};
|
|
|
1065 |
|
|
|
1066 |
/**
|
|
|
1067 |
* Accept the contact request from the given user.
|
|
|
1068 |
*
|
|
|
1069 |
* @param {Number} userId User id of other user.
|
|
|
1070 |
* @return {Promise} Renderer promise.
|
|
|
1071 |
*/
|
|
|
1072 |
var acceptContactRequest = function(userId) {
|
|
|
1073 |
var pendingPromise = new Pending('core_message/message_drawer_view_conversation:acceptContactRequest');
|
|
|
1074 |
|
|
|
1075 |
// Search the list of the logged in user's contact requests to find the
|
|
|
1076 |
// one from this user.
|
|
|
1077 |
var loggedInUserId = viewState.loggedInUserId;
|
|
|
1078 |
var requests = viewState.members[userId].contactrequests.filter(function(request) {
|
|
|
1079 |
return request.requesteduserid == loggedInUserId;
|
|
|
1080 |
});
|
|
|
1081 |
var request = requests[0];
|
|
|
1082 |
var newState = StateManager.setLoadingConfirmAction(viewState, true);
|
|
|
1083 |
render(newState);
|
|
|
1084 |
|
|
|
1085 |
return Repository.acceptContactRequest(userId, loggedInUserId)
|
|
|
1086 |
.then(function(profile) {
|
|
|
1087 |
var newState = StateManager.removeContactRequests(viewState, [request]);
|
|
|
1088 |
newState = StateManager.addMembers(viewState, [profile]);
|
|
|
1089 |
newState = StateManager.setLoadingConfirmAction(newState, false);
|
|
|
1090 |
return render(newState);
|
|
|
1091 |
})
|
|
|
1092 |
.then(function() {
|
|
|
1093 |
PubSub.publish(MessageDrawerEvents.CONTACT_ADDED, viewState.members[userId]);
|
|
|
1094 |
PubSub.publish(MessageDrawerEvents.CONTACT_REQUEST_ACCEPTED, request);
|
|
|
1095 |
return;
|
|
|
1096 |
})
|
|
|
1097 |
.then(function(result) {
|
|
|
1098 |
pendingPromise.resolve();
|
|
|
1099 |
|
|
|
1100 |
return result;
|
|
|
1101 |
});
|
|
|
1102 |
};
|
|
|
1103 |
|
|
|
1104 |
/**
|
|
|
1105 |
* Decline the contact request from the given user.
|
|
|
1106 |
*
|
|
|
1107 |
* @param {Number} userId User id of other user.
|
|
|
1108 |
* @return {Promise} Renderer promise.
|
|
|
1109 |
*/
|
|
|
1110 |
var declineContactRequest = function(userId) {
|
|
|
1111 |
var pendingPromise = new Pending('core_message/message_drawer_view_conversation:declineContactRequest');
|
|
|
1112 |
|
|
|
1113 |
// Search the list of the logged in user's contact requests to find the
|
|
|
1114 |
// one from this user.
|
|
|
1115 |
var loggedInUserId = viewState.loggedInUserId;
|
|
|
1116 |
var requests = viewState.members[userId].contactrequests.filter(function(request) {
|
|
|
1117 |
return request.requesteduserid == loggedInUserId;
|
|
|
1118 |
});
|
|
|
1119 |
var request = requests[0];
|
|
|
1120 |
var newState = StateManager.setLoadingConfirmAction(viewState, true);
|
|
|
1121 |
render(newState);
|
|
|
1122 |
|
|
|
1123 |
return Repository.declineContactRequest(userId, loggedInUserId)
|
|
|
1124 |
.then(function(profile) {
|
|
|
1125 |
var newState = StateManager.removeContactRequests(viewState, [request]);
|
|
|
1126 |
newState = StateManager.addMembers(viewState, [profile]);
|
|
|
1127 |
newState = StateManager.setLoadingConfirmAction(newState, false);
|
|
|
1128 |
return render(newState);
|
|
|
1129 |
})
|
|
|
1130 |
.then(function() {
|
|
|
1131 |
PubSub.publish(MessageDrawerEvents.CONTACT_REQUEST_DECLINED, request);
|
|
|
1132 |
return;
|
|
|
1133 |
})
|
|
|
1134 |
.then(function(result) {
|
|
|
1135 |
pendingPromise.resolve();
|
|
|
1136 |
|
|
|
1137 |
return result;
|
|
|
1138 |
});
|
|
|
1139 |
};
|
|
|
1140 |
|
|
|
1141 |
/**
|
|
|
1142 |
* Send all of the messages in the buffer to the server to be created. Update the
|
|
|
1143 |
* UI with the newly created message information.
|
|
|
1144 |
*
|
|
|
1145 |
* This function will recursively call itself in order to make sure the buffer is
|
|
|
1146 |
* always being processed.
|
|
|
1147 |
*/
|
|
|
1148 |
var processSendMessageBuffer = function() {
|
|
|
1149 |
if (isSendingMessage) {
|
|
|
1150 |
// We're already sending messages so nothing to do.
|
|
|
1151 |
return;
|
|
|
1152 |
}
|
|
|
1153 |
if (!sendMessageBuffer.length) {
|
|
|
1154 |
// No messages waiting to send. Nothing to do.
|
|
|
1155 |
return;
|
|
|
1156 |
}
|
|
|
1157 |
|
|
|
1158 |
var pendingPromise = new Pending('core_message/message_drawer_view_conversation:processSendMessageBuffer');
|
|
|
1159 |
|
|
|
1160 |
// Flag that we're processing the queue.
|
|
|
1161 |
isSendingMessage = true;
|
|
|
1162 |
// Grab all of the messages in the buffer.
|
|
|
1163 |
var messagesToSend = sendMessageBuffer.slice();
|
|
|
1164 |
// Empty the buffer since we're processing it.
|
|
|
1165 |
sendMessageBuffer = [];
|
|
|
1166 |
var conversationId = viewState.id;
|
|
|
1167 |
var newConversationId = null;
|
|
|
1168 |
var messagesText = messagesToSend.map(function(message) {
|
|
|
1169 |
return message.text;
|
|
|
1170 |
});
|
|
|
1171 |
var messageIds = messagesToSend.map(function(message) {
|
|
|
1172 |
return message.id;
|
|
|
1173 |
});
|
|
|
1174 |
var sendMessagePromise = null;
|
|
|
1175 |
var newCanDeleteMessagesForAllUsers = null;
|
|
|
1176 |
if (!conversationId && (viewState.type != CONVERSATION_TYPES.PUBLIC)) {
|
|
|
1177 |
// If it's a new private conversation then we need to use the old
|
|
|
1178 |
// web service function to create the conversation.
|
|
|
1179 |
var otherUserId = getOtherUserId();
|
|
|
1180 |
sendMessagePromise = Repository.sendMessagesToUser(otherUserId, messagesText)
|
|
|
1181 |
.then(function(messages) {
|
|
|
1182 |
if (messages.length) {
|
|
|
1183 |
newConversationId = parseInt(messages[0].conversationid, 10);
|
|
|
1184 |
newCanDeleteMessagesForAllUsers = messages[0].candeletemessagesforallusers;
|
|
|
1185 |
}
|
|
|
1186 |
return messages;
|
|
|
1187 |
});
|
|
|
1188 |
} else {
|
|
|
1189 |
sendMessagePromise = Repository.sendMessagesToConversation(conversationId, messagesText);
|
|
|
1190 |
}
|
|
|
1191 |
|
|
|
1192 |
sendMessagePromise
|
|
|
1193 |
.then(function(messages) {
|
|
|
1194 |
var newMessageIds = messages.map(function(message) {
|
|
|
1195 |
return message.id;
|
|
|
1196 |
});
|
|
|
1197 |
var data = [];
|
|
|
1198 |
var selectedToRemove = [];
|
|
|
1199 |
var selectedToAdd = [];
|
|
|
1200 |
|
|
|
1201 |
messagesToSend.forEach(function(oldMessage, index) {
|
|
|
1202 |
var newMessage = messages[index];
|
|
|
1203 |
// Update messages expects and array of arrays where the first value
|
|
|
1204 |
// is the old message to update and the second value is the new values
|
|
|
1205 |
// to set.
|
|
|
1206 |
data.push([oldMessage, newMessage]);
|
|
|
1207 |
|
|
|
1208 |
if (viewState.selectedMessageIds.indexOf(oldMessage.id) >= 0) {
|
|
|
1209 |
// If the message was added to the "selected messages" list while it was still
|
|
|
1210 |
// being sent then we should update it's id in that list now to make sure future
|
|
|
1211 |
// actions work.
|
|
|
1212 |
selectedToRemove.push(oldMessage.id);
|
|
|
1213 |
selectedToAdd.push(newMessage.id);
|
|
|
1214 |
}
|
|
|
1215 |
});
|
|
|
1216 |
var newState = StateManager.updateMessages(viewState, data);
|
|
|
1217 |
newState = StateManager.setMessagesSendSuccessById(newState, newMessageIds);
|
|
|
1218 |
|
|
|
1219 |
if (selectedToRemove.length) {
|
|
|
1220 |
newState = StateManager.removeSelectedMessagesById(newState, selectedToRemove);
|
|
|
1221 |
}
|
|
|
1222 |
|
|
|
1223 |
if (selectedToAdd.length) {
|
|
|
1224 |
newState = StateManager.addSelectedMessagesById(newState, selectedToAdd);
|
|
|
1225 |
}
|
|
|
1226 |
|
|
|
1227 |
var conversation = formatConversationForEvent(newState);
|
|
|
1228 |
|
|
|
1229 |
if (!newState.id) {
|
|
|
1230 |
// If this message created the conversation then save the conversation
|
|
|
1231 |
// id.
|
|
|
1232 |
newState = StateManager.setId(newState, newConversationId);
|
|
|
1233 |
conversation.id = newConversationId;
|
|
|
1234 |
resetMessagePollTimer(newConversationId);
|
|
|
1235 |
PubSub.publish(MessageDrawerEvents.CONVERSATION_CREATED, conversation);
|
|
|
1236 |
newState = StateManager.setCanDeleteMessagesForAllUsers(newState, newCanDeleteMessagesForAllUsers);
|
|
|
1237 |
}
|
|
|
1238 |
|
|
|
1239 |
// Update the UI with the new message values from the server.
|
|
|
1240 |
render(newState);
|
|
|
1241 |
// Recurse just in case there has been more messages added to the buffer.
|
|
|
1242 |
isSendingMessage = false;
|
|
|
1243 |
processSendMessageBuffer();
|
|
|
1244 |
PubSub.publish(MessageDrawerEvents.CONVERSATION_NEW_LAST_MESSAGE, conversation);
|
|
|
1245 |
return;
|
|
|
1246 |
})
|
|
|
1247 |
.then(function(result) {
|
|
|
1248 |
pendingPromise.resolve();
|
|
|
1249 |
|
|
|
1250 |
return result;
|
|
|
1251 |
})
|
|
|
1252 |
.catch(function(e) {
|
|
|
1253 |
var errorMessage;
|
|
|
1254 |
if (e.message) {
|
|
|
1255 |
errorMessage = $.Deferred().resolve(e.message).promise();
|
|
|
1256 |
} else {
|
|
|
1257 |
errorMessage = Str.get_string('unknownerror', 'core');
|
|
|
1258 |
}
|
|
|
1259 |
|
|
|
1260 |
var handleFailedMessages = function(errorMessage) {
|
|
|
1261 |
// We failed to create messages so remove the old messages from the pending queue
|
|
|
1262 |
// and update the UI to indicate that the message failed.
|
|
|
1263 |
var newState = StateManager.setMessagesSendFailById(viewState, messageIds, errorMessage);
|
|
|
1264 |
render(newState);
|
|
|
1265 |
isSendingMessage = false;
|
|
|
1266 |
processSendMessageBuffer();
|
|
|
1267 |
};
|
|
|
1268 |
|
|
|
1269 |
errorMessage.then(handleFailedMessages)
|
|
|
1270 |
.then(function(result) {
|
|
|
1271 |
pendingPromise.resolve();
|
|
|
1272 |
|
|
|
1273 |
return result;
|
|
|
1274 |
})
|
|
|
1275 |
.catch(function(e) {
|
|
|
1276 |
// Hrmm, we can't even load the error messages string! We'll have to
|
|
|
1277 |
// hard code something in English here if we still haven't got a message
|
|
|
1278 |
// to show.
|
|
|
1279 |
var finalError = e.message || 'Something went wrong!';
|
|
|
1280 |
handleFailedMessages(finalError);
|
|
|
1281 |
});
|
|
|
1282 |
});
|
|
|
1283 |
};
|
|
|
1284 |
|
|
|
1285 |
/**
|
|
|
1286 |
* Create a plain version of an HTML text.
|
|
|
1287 |
*
|
|
|
1288 |
* This texts is used as a message preview while is sent to the server. This way
|
|
|
1289 |
* it is possible to prevent self-xss.
|
|
|
1290 |
*
|
|
|
1291 |
* @param {String} text Text to send.
|
|
|
1292 |
* @return {String} The plain text version of the text.
|
|
|
1293 |
*/
|
|
|
1294 |
const previewText = function(text) {
|
|
|
1295 |
// Remove all script and styles from text (we don't want it there).
|
|
|
1296 |
let plaintext = text.replace(/<style([\s\S]*?)<\/style>/gi, '');
|
|
|
1297 |
plaintext = plaintext.replace(/<script([\s\S]*?)<\/script>/gi, '');
|
|
|
1298 |
// Beautify a bit the output adding some line breaks.
|
|
|
1299 |
plaintext = plaintext.replace(/<\/div>/ig, '\n');
|
|
|
1300 |
plaintext = plaintext.replace(/<\/li>/ig, '\n');
|
|
|
1301 |
plaintext = plaintext.replace(/<li>/ig, ' * ');
|
|
|
1302 |
plaintext = plaintext.replace(/<\/ul>/ig, '\n');
|
|
|
1303 |
plaintext = plaintext.replace(/<\/p>/ig, '\n');
|
|
|
1304 |
plaintext = plaintext.replace(/<br[^>]*>/gi, '\n');
|
|
|
1305 |
// Remove all remaining tags and convert line breaks into html.
|
|
|
1306 |
plaintext = plaintext.replace(/<[^>]+>/ig, '');
|
|
|
1307 |
plaintext = plaintext.replace(/\n+/ig, '\n');
|
|
|
1308 |
return plaintext.replace(/\n/ig, '<br>');
|
|
|
1309 |
};
|
|
|
1310 |
|
|
|
1311 |
/**
|
|
|
1312 |
* Buffers messages to be sent to the server. We use a buffer here to allow the
|
|
|
1313 |
* user to freely input messages without blocking the interface for them.
|
|
|
1314 |
*
|
|
|
1315 |
* Instead we just queue all of their messages up and send them as fast as we can.
|
|
|
1316 |
*
|
|
|
1317 |
* @param {String} text Text to send.
|
|
|
1318 |
*/
|
|
|
1319 |
var sendMessage = function(text) {
|
|
|
1320 |
var id = 'temp' + Date.now();
|
|
|
1321 |
// Render a preview version of the message while sending.
|
|
|
1322 |
let loadingmessage = {
|
|
|
1323 |
id: id,
|
|
|
1324 |
useridfrom: viewState.loggedInUserId,
|
|
|
1325 |
text: previewText(text),
|
|
|
1326 |
timecreated: null
|
|
|
1327 |
};
|
|
|
1328 |
var newState = StateManager.addMessages(viewState, [loadingmessage]);
|
|
|
1329 |
render(newState);
|
|
|
1330 |
// Send the real message.
|
|
|
1331 |
var message = {
|
|
|
1332 |
id: id,
|
|
|
1333 |
useridfrom: viewState.loggedInUserId,
|
|
|
1334 |
text: text,
|
|
|
1335 |
timecreated: null
|
|
|
1336 |
};
|
|
|
1337 |
sendMessageBuffer.push(message);
|
|
|
1338 |
processSendMessageBuffer();
|
|
|
1339 |
};
|
|
|
1340 |
|
|
|
1341 |
/**
|
|
|
1342 |
* Retry sending a message that failed.
|
|
|
1343 |
*
|
|
|
1344 |
* @param {Object} message The message to send.
|
|
|
1345 |
*/
|
|
|
1346 |
var retrySendMessage = function(message) {
|
|
|
1347 |
var newState = StateManager.setMessagesSendPendingById(viewState, [message.id]);
|
|
|
1348 |
render(newState);
|
|
|
1349 |
sendMessageBuffer.push(message);
|
|
|
1350 |
processSendMessageBuffer();
|
|
|
1351 |
};
|
|
|
1352 |
|
|
|
1353 |
/**
|
|
|
1354 |
* Toggle the selected messages update the statemanager and render the result.
|
|
|
1355 |
*
|
|
|
1356 |
* @param {Number} messageId The id of the message to be toggled
|
|
|
1357 |
*/
|
|
|
1358 |
var toggleSelectMessage = function(messageId) {
|
|
|
1359 |
var newState = viewState;
|
|
|
1360 |
|
|
|
1361 |
if (viewState.selectedMessageIds.indexOf(messageId) > -1) {
|
|
|
1362 |
newState = StateManager.removeSelectedMessagesById(viewState, [messageId]);
|
|
|
1363 |
} else {
|
|
|
1364 |
newState = StateManager.addSelectedMessagesById(viewState, [messageId]);
|
|
|
1365 |
}
|
|
|
1366 |
|
|
|
1367 |
render(newState);
|
|
|
1368 |
};
|
|
|
1369 |
|
|
|
1370 |
/**
|
|
|
1371 |
* Cancel edit mode (selecting the messages).
|
|
|
1372 |
*/
|
|
|
1373 |
var cancelEditMode = function() {
|
|
|
1374 |
cancelRequest(getOtherUserId());
|
|
|
1375 |
var newState = StateManager.removeSelectedMessagesById(viewState, viewState.selectedMessageIds);
|
|
|
1376 |
render(newState);
|
|
|
1377 |
};
|
|
|
1378 |
|
|
|
1379 |
/**
|
|
|
1380 |
* Process the patches in the render buffer one at a time in order until the
|
|
|
1381 |
* buffer is empty.
|
|
|
1382 |
*
|
|
|
1383 |
* @param {Object} header The conversation header container element.
|
|
|
1384 |
* @param {Object} body The conversation body container element.
|
|
|
1385 |
* @param {Object} footer The conversation footer container element.
|
|
|
1386 |
*/
|
|
|
1387 |
var processRenderBuffer = function(header, body, footer) {
|
|
|
1388 |
if (isRendering) {
|
|
|
1389 |
return;
|
|
|
1390 |
}
|
|
|
1391 |
|
|
|
1392 |
if (!renderBuffer.length) {
|
|
|
1393 |
return;
|
|
|
1394 |
}
|
|
|
1395 |
|
|
|
1396 |
isRendering = true;
|
|
|
1397 |
var renderable = renderBuffer.shift();
|
|
|
1398 |
var renderPromises = renderers.map(function(renderFunc) {
|
|
|
1399 |
return renderFunc(renderable.patch);
|
|
|
1400 |
});
|
|
|
1401 |
|
|
|
1402 |
$.when.apply(null, renderPromises)
|
|
|
1403 |
.then(function() {
|
|
|
1404 |
isRendering = false;
|
|
|
1405 |
renderable.deferred.resolve(true);
|
|
|
1406 |
// Keep processing the buffer until it's empty.
|
|
|
1407 |
processRenderBuffer(header, body, footer);
|
|
|
1408 |
|
|
|
1409 |
return;
|
|
|
1410 |
})
|
|
|
1411 |
.catch(function(error) {
|
|
|
1412 |
isRendering = false;
|
|
|
1413 |
renderable.deferred.reject(error);
|
|
|
1414 |
Notification.exception(error);
|
|
|
1415 |
});
|
|
|
1416 |
};
|
|
|
1417 |
|
|
|
1418 |
/**
|
|
|
1419 |
* Create a function to render the Conversation.
|
|
|
1420 |
*
|
|
|
1421 |
* @param {Object} header The conversation header container element.
|
|
|
1422 |
* @param {Object} body The conversation body container element.
|
|
|
1423 |
* @param {Object} footer The conversation footer container element.
|
|
|
1424 |
* @param {Bool} isNewConversation Has someone else already initialised a conversation?
|
|
|
1425 |
* @return {Promise} Renderer promise.
|
|
|
1426 |
*/
|
|
|
1427 |
var generateRenderFunction = function(header, body, footer, isNewConversation) {
|
|
|
1428 |
var rendererFunc = function(patch) {
|
|
|
1429 |
return Renderer.render(header, body, footer, patch);
|
|
|
1430 |
};
|
|
|
1431 |
|
|
|
1432 |
if (!isNewConversation) {
|
|
|
1433 |
// Looks like someone got here before us! We'd better update our
|
|
|
1434 |
// UI to make sure it matches.
|
|
|
1435 |
var initialState = StateManager.buildInitialState(viewState.midnight, viewState.loggedInUserId, viewState.id);
|
|
|
1436 |
var syncPatch = Patcher.buildPatch(initialState, viewState);
|
|
|
1437 |
rendererFunc(syncPatch);
|
|
|
1438 |
}
|
|
|
1439 |
|
|
|
1440 |
renderers.push(rendererFunc);
|
|
|
1441 |
|
|
|
1442 |
return function(newState) {
|
|
|
1443 |
var patch = Patcher.buildPatch(viewState, newState);
|
|
|
1444 |
var deferred = $.Deferred();
|
|
|
1445 |
|
|
|
1446 |
// Check if the patch has any data. Ignore empty patches.
|
|
|
1447 |
if (Object.keys(patch).length) {
|
|
|
1448 |
// Add the patch to the render buffer which gets processed in order.
|
|
|
1449 |
renderBuffer.push({
|
|
|
1450 |
patch: patch,
|
|
|
1451 |
deferred: deferred
|
|
|
1452 |
});
|
|
|
1453 |
} else {
|
|
|
1454 |
deferred.resolve(true);
|
|
|
1455 |
}
|
|
|
1456 |
// This is a great place to add in some console logging if you need
|
|
|
1457 |
// to debug something. You can log the current state, the next state,
|
|
|
1458 |
// and the generated patch and see exactly what will be updated.
|
|
|
1459 |
|
|
|
1460 |
// Optimistically update the state. We're going to assume that the rendering
|
|
|
1461 |
// will always succeed. The rendering is asynchronous (annoyingly) so it's buffered
|
|
|
1462 |
// but it'll reach eventual consistency with the current state.
|
|
|
1463 |
viewState = newState;
|
|
|
1464 |
if (newState.id) {
|
|
|
1465 |
// Only cache created conversations.
|
|
|
1466 |
stateCache[newState.id] = {
|
|
|
1467 |
state: newState,
|
|
|
1468 |
messagesOffset: getMessagesOffset(),
|
|
|
1469 |
loadedAllMessages: hasLoadedAllMessages()
|
|
|
1470 |
};
|
|
|
1471 |
}
|
|
|
1472 |
|
|
|
1473 |
// Start processing the buffer.
|
|
|
1474 |
processRenderBuffer(header, body, footer);
|
|
|
1475 |
|
|
|
1476 |
return deferred.promise();
|
|
|
1477 |
};
|
|
|
1478 |
};
|
|
|
1479 |
|
|
|
1480 |
/**
|
|
|
1481 |
* Create a confirm action function.
|
|
|
1482 |
*
|
|
|
1483 |
* @param {Function} actionCallback The callback function.
|
|
|
1484 |
* @return {Function} Confirm action handler.
|
|
|
1485 |
*/
|
|
|
1486 |
var generateConfirmActionHandler = function(actionCallback) {
|
|
|
1487 |
return function(e, data) {
|
|
|
1488 |
if (!viewState.loadingConfirmAction) {
|
|
|
1489 |
actionCallback(getOtherUserId());
|
|
|
1490 |
var newState = StateManager.setLoadingConfirmAction(viewState, false);
|
|
|
1491 |
render(newState);
|
|
|
1492 |
}
|
|
|
1493 |
data.originalEvent.preventDefault();
|
|
|
1494 |
};
|
|
|
1495 |
};
|
|
|
1496 |
|
|
|
1497 |
/**
|
|
|
1498 |
* Send message event handler.
|
|
|
1499 |
*
|
|
|
1500 |
* @param {Object} e Element this event handler is called on.
|
|
|
1501 |
* @param {Object} data Data for this event.
|
|
|
1502 |
*/
|
|
|
1503 |
var handleSendMessage = function(e, data) {
|
|
|
1504 |
var target = $(e.target);
|
|
|
1505 |
var footerContainer = target.closest(SELECTORS.FOOTER_CONTAINER);
|
|
|
1506 |
var textArea = footerContainer.find(SELECTORS.MESSAGE_TEXT_AREA);
|
|
|
1507 |
var text = textArea.val().trim();
|
|
|
1508 |
|
|
|
1509 |
if (text !== '') {
|
|
|
1510 |
sendMessage(text);
|
|
|
1511 |
textArea.val('');
|
|
|
1512 |
textArea.focus();
|
|
|
1513 |
}
|
|
|
1514 |
|
|
|
1515 |
data.originalEvent.preventDefault();
|
|
|
1516 |
};
|
|
|
1517 |
|
|
|
1518 |
/**
|
|
|
1519 |
* Select message event handler.
|
|
|
1520 |
*
|
|
|
1521 |
* @param {Object} e Element this event handler is called on.
|
|
|
1522 |
* @param {Object} data Data for this event.
|
|
|
1523 |
*/
|
|
|
1524 |
var handleSelectMessage = function(e, data) {
|
|
|
1525 |
var selection = window.getSelection();
|
|
|
1526 |
var target = $(e.target);
|
|
|
1527 |
|
|
|
1528 |
if (selection.toString() != '') {
|
|
|
1529 |
// Bail if we're selecting.
|
|
|
1530 |
return;
|
|
|
1531 |
}
|
|
|
1532 |
|
|
|
1533 |
if (target.is('a')) {
|
|
|
1534 |
// Clicking on a link in the message so ignore it.
|
|
|
1535 |
return;
|
|
|
1536 |
}
|
|
|
1537 |
|
|
|
1538 |
var element = target.closest(SELECTORS.MESSAGE);
|
|
|
1539 |
var messageId = element.attr('data-message-id');
|
|
|
1540 |
|
|
|
1541 |
toggleSelectMessage(messageId);
|
|
|
1542 |
|
|
|
1543 |
data.originalEvent.preventDefault();
|
|
|
1544 |
};
|
|
|
1545 |
|
|
|
1546 |
/**
|
|
|
1547 |
* Handle retry sending of message.
|
|
|
1548 |
*
|
|
|
1549 |
* @param {Object} e Element this event handler is called on.
|
|
|
1550 |
* @param {Object} data Data for this event.
|
|
|
1551 |
*/
|
|
|
1552 |
var handleRetrySendMessage = function(e, data) {
|
|
|
1553 |
var target = $(e.target);
|
|
|
1554 |
var element = target.closest(SELECTORS.MESSAGE);
|
|
|
1555 |
var messageId = element.attr('data-message-id');
|
|
|
1556 |
var messages = viewState.messages.filter(function(message) {
|
|
|
1557 |
return message.id == messageId;
|
|
|
1558 |
});
|
|
|
1559 |
var message = messages.length ? messages[0] : null;
|
|
|
1560 |
|
|
|
1561 |
if (message) {
|
|
|
1562 |
retrySendMessage(message);
|
|
|
1563 |
}
|
|
|
1564 |
|
|
|
1565 |
data.originalEvent.preventDefault();
|
|
|
1566 |
data.originalEvent.stopPropagation();
|
|
|
1567 |
e.stopPropagation();
|
|
|
1568 |
};
|
|
|
1569 |
|
|
|
1570 |
/**
|
|
|
1571 |
* Cancel edit mode event handler.
|
|
|
1572 |
*
|
|
|
1573 |
* @param {Object} e Element this event handler is called on.
|
|
|
1574 |
* @param {Object} data Data for this event.
|
|
|
1575 |
*/
|
|
|
1576 |
var handleCancelEditMode = function(e, data) {
|
|
|
1577 |
cancelEditMode();
|
|
|
1578 |
data.originalEvent.preventDefault();
|
|
|
1579 |
};
|
|
|
1580 |
|
|
|
1581 |
/**
|
|
|
1582 |
* Show the view contact page.
|
|
|
1583 |
*
|
|
|
1584 |
* @param {String} namespace Unique identifier for the Routes
|
|
|
1585 |
* @return {Function} View contact handler.
|
|
|
1586 |
*/
|
|
|
1587 |
var generateHandleViewContact = function(namespace) {
|
|
|
1588 |
return function(e, data) {
|
|
|
1589 |
var otherUserId = getOtherUserId();
|
|
|
1590 |
var otherUser = viewState.members[otherUserId];
|
|
|
1591 |
MessageDrawerRouter.go(namespace, MessageDrawerRoutes.VIEW_CONTACT, otherUser);
|
|
|
1592 |
data.originalEvent.preventDefault();
|
|
|
1593 |
};
|
|
|
1594 |
};
|
|
|
1595 |
|
|
|
1596 |
/**
|
|
|
1597 |
* Set this conversation as a favourite.
|
|
|
1598 |
*
|
|
|
1599 |
* @param {Object} e Element this event handler is called on.
|
|
|
1600 |
* @param {Object} data Data for this event.
|
|
|
1601 |
*/
|
|
|
1602 |
var handleSetFavourite = function(e, data) {
|
|
|
1603 |
setFavourite().catch(Notification.exception);
|
|
|
1604 |
data.originalEvent.preventDefault();
|
|
|
1605 |
};
|
|
|
1606 |
|
|
|
1607 |
/**
|
|
|
1608 |
* Unset this conversation as a favourite.
|
|
|
1609 |
*
|
|
|
1610 |
* @param {Object} e Element this event handler is called on.
|
|
|
1611 |
* @param {Object} data Data for this event.
|
|
|
1612 |
*/
|
|
|
1613 |
var handleUnsetFavourite = function(e, data) {
|
|
|
1614 |
unsetFavourite().catch(Notification.exception);
|
|
|
1615 |
data.originalEvent.preventDefault();
|
|
|
1616 |
};
|
|
|
1617 |
|
|
|
1618 |
/**
|
|
|
1619 |
* Show the view group info page.
|
|
|
1620 |
* Set this conversation as muted.
|
|
|
1621 |
*
|
|
|
1622 |
* @param {Object} e Element this event handler is called on.
|
|
|
1623 |
* @param {Object} data Data for this event.
|
|
|
1624 |
*/
|
|
|
1625 |
var handleSetMuted = function(e, data) {
|
|
|
1626 |
setMuted().catch(Notification.exception);
|
|
|
1627 |
data.originalEvent.preventDefault();
|
|
|
1628 |
};
|
|
|
1629 |
|
|
|
1630 |
/**
|
|
|
1631 |
* Unset this conversation as muted.
|
|
|
1632 |
*
|
|
|
1633 |
* @param {Object} e Element this event handler is called on.
|
|
|
1634 |
* @param {Object} data Data for this event.
|
|
|
1635 |
*/
|
|
|
1636 |
var handleUnsetMuted = function(e, data) {
|
|
|
1637 |
unsetMuted().catch(Notification.exception);
|
|
|
1638 |
data.originalEvent.preventDefault();
|
|
|
1639 |
};
|
|
|
1640 |
|
|
|
1641 |
/**
|
|
|
1642 |
* Handle clicking on the checkbox that toggles deleting messages for
|
|
|
1643 |
* all users.
|
|
|
1644 |
*
|
|
|
1645 |
* @param {Object} e Element this event handler is called on.
|
|
|
1646 |
*/
|
|
|
1647 |
var handleDeleteMessagesForAllUsersToggle = function(e) {
|
|
|
1648 |
var newValue = $(e.target).prop('checked');
|
|
|
1649 |
var newState = StateManager.setDeleteMessagesForAllUsers(viewState, newValue);
|
|
|
1650 |
render(newState);
|
|
|
1651 |
};
|
|
|
1652 |
|
|
|
1653 |
/**
|
|
|
1654 |
* Show the view contact page.
|
|
|
1655 |
*
|
|
|
1656 |
* @param {String} namespace Unique identifier for the Routes
|
|
|
1657 |
* @return {Function} View group info handler.
|
|
|
1658 |
*/
|
|
|
1659 |
var generateHandleViewGroupInfo = function(namespace) {
|
|
|
1660 |
return function(e, data) {
|
|
|
1661 |
MessageDrawerRouter.go(
|
|
|
1662 |
namespace,
|
|
|
1663 |
MessageDrawerRoutes.VIEW_GROUP_INFO,
|
|
|
1664 |
{
|
|
|
1665 |
id: viewState.id,
|
|
|
1666 |
name: viewState.name,
|
|
|
1667 |
subname: viewState.subname,
|
|
|
1668 |
imageUrl: viewState.imageUrl,
|
|
|
1669 |
totalMemberCount: viewState.totalMemberCount
|
|
|
1670 |
},
|
|
|
1671 |
viewState.loggedInUserId
|
|
|
1672 |
);
|
|
|
1673 |
data.originalEvent.preventDefault();
|
|
|
1674 |
};
|
|
|
1675 |
};
|
|
|
1676 |
|
|
|
1677 |
/**
|
|
|
1678 |
* Handle clicking on the emoji toggle button.
|
|
|
1679 |
*
|
|
|
1680 |
* @param {Object} e The event
|
|
|
1681 |
* @param {Object} data The custom interaction event data
|
|
|
1682 |
*/
|
|
|
1683 |
var handleToggleEmojiPicker = function(e, data) {
|
|
|
1684 |
var newState = StateManager.setShowEmojiPicker(viewState, !viewState.showEmojiPicker);
|
|
|
1685 |
render(newState);
|
|
|
1686 |
data.originalEvent.preventDefault();
|
|
|
1687 |
};
|
|
|
1688 |
|
|
|
1689 |
/**
|
|
|
1690 |
* Handle clicking outside the emoji picker to close it.
|
|
|
1691 |
*
|
|
|
1692 |
* @param {Object} e The event
|
|
|
1693 |
*/
|
|
|
1694 |
var handleCloseEmojiPicker = function(e) {
|
|
|
1695 |
var target = $(e.target);
|
|
|
1696 |
|
|
|
1697 |
if (
|
|
|
1698 |
viewState.showEmojiPicker &&
|
|
|
1699 |
!target.closest(SELECTORS.EMOJI_PICKER_CONTAINER).length &&
|
|
|
1700 |
!target.closest(SELECTORS.TOGGLE_EMOJI_PICKER_BUTTON).length
|
|
|
1701 |
) {
|
|
|
1702 |
var newState = StateManager.setShowEmojiPicker(viewState, false);
|
|
|
1703 |
render(newState);
|
|
|
1704 |
}
|
|
|
1705 |
};
|
|
|
1706 |
|
|
|
1707 |
/**
|
|
|
1708 |
* Listen to, and handle events for conversations.
|
|
|
1709 |
*
|
|
|
1710 |
* @param {string} namespace The route namespace.
|
|
|
1711 |
* @param {Object} header Conversation header container element.
|
|
|
1712 |
* @param {Object} body Conversation body container element.
|
|
|
1713 |
* @param {Object} footer Conversation footer container element.
|
|
|
1714 |
*/
|
|
|
1715 |
var registerEventListeners = function(namespace, header, body, footer) {
|
|
|
1716 |
var isLoadingMoreMessages = false;
|
|
|
1717 |
var messagesContainer = getMessagesContainer(body);
|
|
|
1718 |
var emojiPickerElement = footer.find(SELECTORS.EMOJI_PICKER);
|
|
|
1719 |
var emojiAutoCompleteContainer = footer.find(SELECTORS.EMOJI_AUTO_COMPLETE_CONTAINER);
|
|
|
1720 |
var messageTextArea = footer.find(SELECTORS.MESSAGE_TEXT_AREA);
|
|
|
1721 |
var headerActivateHandlers = [
|
|
|
1722 |
[SELECTORS.ACTION_REQUEST_BLOCK, generateConfirmActionHandler(requestBlockUser)],
|
|
|
1723 |
[SELECTORS.ACTION_REQUEST_UNBLOCK, generateConfirmActionHandler(requestUnblockUser)],
|
|
|
1724 |
[SELECTORS.ACTION_REQUEST_ADD_CONTACT, generateConfirmActionHandler(requestAddContact)],
|
|
|
1725 |
[SELECTORS.ACTION_REQUEST_REMOVE_CONTACT, generateConfirmActionHandler(requestRemoveContact)],
|
|
|
1726 |
[SELECTORS.ACTION_REQUEST_DELETE_CONVERSATION, generateConfirmActionHandler(requestDeleteConversation)],
|
|
|
1727 |
[SELECTORS.ACTION_CANCEL_EDIT_MODE, handleCancelEditMode],
|
|
|
1728 |
[SELECTORS.ACTION_VIEW_CONTACT, generateHandleViewContact(namespace)],
|
|
|
1729 |
[SELECTORS.ACTION_VIEW_GROUP_INFO, generateHandleViewGroupInfo(namespace)],
|
|
|
1730 |
[SELECTORS.ACTION_CONFIRM_FAVOURITE, handleSetFavourite],
|
|
|
1731 |
[SELECTORS.ACTION_CONFIRM_MUTE, handleSetMuted],
|
|
|
1732 |
[SELECTORS.ACTION_CONFIRM_UNFAVOURITE, handleUnsetFavourite],
|
|
|
1733 |
[SELECTORS.ACTION_CONFIRM_UNMUTE, handleUnsetMuted]
|
|
|
1734 |
];
|
|
|
1735 |
var bodyActivateHandlers = [
|
|
|
1736 |
[SELECTORS.ACTION_CANCEL_CONFIRM, generateConfirmActionHandler(cancelRequest)],
|
|
|
1737 |
[SELECTORS.ACTION_CONFIRM_BLOCK, generateConfirmActionHandler(blockUser)],
|
|
|
1738 |
[SELECTORS.ACTION_CONFIRM_UNBLOCK, generateConfirmActionHandler(unblockUser)],
|
|
|
1739 |
[SELECTORS.ACTION_CONFIRM_ADD_CONTACT, generateConfirmActionHandler(addContact)],
|
|
|
1740 |
[SELECTORS.ACTION_CONFIRM_REMOVE_CONTACT, generateConfirmActionHandler(removeContact)],
|
|
|
1741 |
[SELECTORS.ACTION_CONFIRM_DELETE_SELECTED_MESSAGES, generateConfirmActionHandler(deleteSelectedMessages)],
|
|
|
1742 |
[SELECTORS.ACTION_CONFIRM_DELETE_CONVERSATION, generateConfirmActionHandler(deleteConversation)],
|
|
|
1743 |
[SELECTORS.ACTION_OKAY_CONFIRM, generateConfirmActionHandler(cancelRequest)],
|
|
|
1744 |
[SELECTORS.ACTION_REQUEST_ADD_CONTACT, generateConfirmActionHandler(requestAddContact)],
|
|
|
1745 |
[SELECTORS.ACTION_ACCEPT_CONTACT_REQUEST, generateConfirmActionHandler(acceptContactRequest)],
|
|
|
1746 |
[SELECTORS.ACTION_DECLINE_CONTACT_REQUEST, generateConfirmActionHandler(declineContactRequest)],
|
|
|
1747 |
[SELECTORS.MESSAGE, handleSelectMessage],
|
|
|
1748 |
[SELECTORS.DELETE_MESSAGES_FOR_ALL_USERS_TOGGLE, handleDeleteMessagesForAllUsersToggle],
|
|
|
1749 |
[SELECTORS.RETRY_SEND, handleRetrySendMessage]
|
|
|
1750 |
];
|
|
|
1751 |
var footerActivateHandlers = [
|
|
|
1752 |
[SELECTORS.SEND_MESSAGE_BUTTON, handleSendMessage],
|
|
|
1753 |
[SELECTORS.TOGGLE_EMOJI_PICKER_BUTTON, handleToggleEmojiPicker],
|
|
|
1754 |
[SELECTORS.ACTION_REQUEST_DELETE_SELECTED_MESSAGES, generateConfirmActionHandler(requestDeleteSelectedMessages)],
|
|
|
1755 |
[SELECTORS.ACTION_REQUEST_ADD_CONTACT, generateConfirmActionHandler(requestAddContact)],
|
|
|
1756 |
[SELECTORS.ACTION_REQUEST_UNBLOCK, generateConfirmActionHandler(requestUnblockUser)],
|
|
|
1757 |
];
|
|
|
1758 |
|
|
|
1759 |
AutoRows.init(footer);
|
|
|
1760 |
|
|
|
1761 |
if (emojiAutoCompleteContainer.length) {
|
|
|
1762 |
initialiseEmojiAutoComplete(
|
|
|
1763 |
emojiAutoCompleteContainer[0],
|
|
|
1764 |
messageTextArea[0],
|
|
|
1765 |
function(hasSuggestions) {
|
|
|
1766 |
var newState = StateManager.setShowEmojiAutoComplete(viewState, hasSuggestions);
|
|
|
1767 |
render(newState);
|
|
|
1768 |
},
|
|
|
1769 |
function(emoji) {
|
|
|
1770 |
var newState = StateManager.setShowEmojiAutoComplete(viewState, false);
|
|
|
1771 |
render(newState);
|
|
|
1772 |
|
|
|
1773 |
messageTextArea.focus();
|
|
|
1774 |
var cursorPos = messageTextArea.prop('selectionStart');
|
|
|
1775 |
var currentText = messageTextArea.val();
|
|
|
1776 |
var textBefore = currentText.substring(0, cursorPos).replace(/\S*$/, '');
|
|
|
1777 |
var textAfter = currentText.substring(cursorPos).replace(/^\S*/, '');
|
|
|
1778 |
|
|
|
1779 |
messageTextArea.val(textBefore + emoji + textAfter);
|
|
|
1780 |
// Set the cursor position to after the inserted emoji.
|
|
|
1781 |
messageTextArea.prop('selectionStart', textBefore.length + emoji.length);
|
|
|
1782 |
messageTextArea.prop('selectionEnd', textBefore.length + emoji.length);
|
|
|
1783 |
}
|
|
|
1784 |
);
|
|
|
1785 |
}
|
|
|
1786 |
|
|
|
1787 |
if (emojiPickerElement.length) {
|
|
|
1788 |
initialiseEmojiPicker(emojiPickerElement[0], function(emoji) {
|
|
|
1789 |
var newState = StateManager.setShowEmojiPicker(viewState, !viewState.showEmojiPicker);
|
|
|
1790 |
render(newState);
|
|
|
1791 |
|
|
|
1792 |
messageTextArea.focus();
|
|
|
1793 |
var cursorPos = messageTextArea.prop('selectionStart');
|
|
|
1794 |
var currentText = messageTextArea.val();
|
|
|
1795 |
var textBefore = currentText.substring(0, cursorPos);
|
|
|
1796 |
var textAfter = currentText.substring(cursorPos, currentText.length);
|
|
|
1797 |
|
|
|
1798 |
messageTextArea.val(textBefore + emoji + textAfter);
|
|
|
1799 |
// Set the cursor position to after the inserted emoji.
|
|
|
1800 |
messageTextArea.prop('selectionStart', cursorPos + emoji.length);
|
|
|
1801 |
messageTextArea.prop('selectionEnd', cursorPos + emoji.length);
|
|
|
1802 |
});
|
|
|
1803 |
}
|
|
|
1804 |
|
|
|
1805 |
CustomEvents.define(header, [
|
|
|
1806 |
CustomEvents.events.activate
|
|
|
1807 |
]);
|
|
|
1808 |
CustomEvents.define(body, [
|
|
|
1809 |
CustomEvents.events.activate
|
|
|
1810 |
]);
|
|
|
1811 |
CustomEvents.define(footer, [
|
|
|
1812 |
CustomEvents.events.activate,
|
|
|
1813 |
CustomEvents.events.enter,
|
|
|
1814 |
CustomEvents.events.escape
|
|
|
1815 |
]);
|
|
|
1816 |
CustomEvents.define(messagesContainer, [
|
|
|
1817 |
CustomEvents.events.scrollTop,
|
|
|
1818 |
CustomEvents.events.scrollLock
|
|
|
1819 |
]);
|
|
|
1820 |
|
|
|
1821 |
messagesContainer.on(CustomEvents.events.scrollTop, function(e, data) {
|
|
|
1822 |
var hasMembers = Object.keys(viewState.members).length > 1;
|
|
|
1823 |
|
|
|
1824 |
if (!isResetting && !isLoadingMoreMessages && !hasLoadedAllMessages() && hasMembers) {
|
|
|
1825 |
isLoadingMoreMessages = true;
|
|
|
1826 |
var newState = StateManager.setLoadingMessages(viewState, true);
|
|
|
1827 |
render(newState);
|
|
|
1828 |
|
|
|
1829 |
loadMessages(viewState.id, LOAD_MESSAGE_LIMIT, getMessagesOffset(), NEWEST_FIRST, [])
|
|
|
1830 |
.then(function() {
|
|
|
1831 |
isLoadingMoreMessages = false;
|
|
|
1832 |
setMessagesOffset(getMessagesOffset() + LOAD_MESSAGE_LIMIT);
|
|
|
1833 |
return;
|
|
|
1834 |
})
|
|
|
1835 |
.catch(function(error) {
|
|
|
1836 |
isLoadingMoreMessages = false;
|
|
|
1837 |
Notification.exception(error);
|
|
|
1838 |
});
|
|
|
1839 |
}
|
|
|
1840 |
|
|
|
1841 |
data.originalEvent.preventDefault();
|
|
|
1842 |
});
|
|
|
1843 |
|
|
|
1844 |
headerActivateHandlers.forEach(function(handler) {
|
|
|
1845 |
var selector = handler[0];
|
|
|
1846 |
var handlerFunction = handler[1];
|
|
|
1847 |
header.on(CustomEvents.events.activate, selector, handlerFunction);
|
|
|
1848 |
});
|
|
|
1849 |
|
|
|
1850 |
bodyActivateHandlers.forEach(function(handler) {
|
|
|
1851 |
var selector = handler[0];
|
|
|
1852 |
var handlerFunction = handler[1];
|
|
|
1853 |
body.on(CustomEvents.events.activate, selector, handlerFunction);
|
|
|
1854 |
});
|
|
|
1855 |
|
|
|
1856 |
footerActivateHandlers.forEach(function(handler) {
|
|
|
1857 |
var selector = handler[0];
|
|
|
1858 |
var handlerFunction = handler[1];
|
|
|
1859 |
footer.on(CustomEvents.events.activate, selector, handlerFunction);
|
|
|
1860 |
});
|
|
|
1861 |
|
|
|
1862 |
footer.on(CustomEvents.events.enter, SELECTORS.MESSAGE_TEXT_AREA, function(e, data) {
|
|
|
1863 |
var enterToSend = footer.attr('data-enter-to-send');
|
|
|
1864 |
if (enterToSend && enterToSend != 'false' && enterToSend != '0') {
|
|
|
1865 |
handleSendMessage(e, data);
|
|
|
1866 |
}
|
|
|
1867 |
});
|
|
|
1868 |
|
|
|
1869 |
footer.on(CustomEvents.events.escape, SELECTORS.EMOJI_PICKER_CONTAINER, handleToggleEmojiPicker);
|
|
|
1870 |
$(document.body).on('click', handleCloseEmojiPicker);
|
|
|
1871 |
|
|
|
1872 |
PubSub.subscribe(MessageDrawerEvents.ROUTE_CHANGED, function(newRouteData) {
|
|
|
1873 |
if (newMessagesPollTimer) {
|
|
|
1874 |
if (newRouteData.route != MessageDrawerRoutes.VIEW_CONVERSATION) {
|
|
|
1875 |
newMessagesPollTimer.stop();
|
|
|
1876 |
}
|
|
|
1877 |
}
|
|
|
1878 |
});
|
|
|
1879 |
};
|
|
|
1880 |
|
|
|
1881 |
/**
|
|
|
1882 |
* Reset the timer that polls for new messages.
|
|
|
1883 |
*
|
|
|
1884 |
* @param {Number} conversationId The conversation id
|
|
|
1885 |
*/
|
|
|
1886 |
var resetMessagePollTimer = function(conversationId) {
|
|
|
1887 |
if (newMessagesPollTimer) {
|
|
|
1888 |
newMessagesPollTimer.stop();
|
|
|
1889 |
}
|
|
|
1890 |
|
|
|
1891 |
newMessagesPollTimer = new BackOffTimer(
|
|
|
1892 |
getLoadNewMessagesCallback(conversationId, NEWEST_FIRST),
|
|
|
1893 |
BackOffTimer.getIncrementalCallback(
|
|
|
1894 |
viewState.messagePollMin * MILLISECONDS_IN_SEC,
|
|
|
1895 |
MILLISECONDS_IN_SEC,
|
|
|
1896 |
viewState.messagePollMax * MILLISECONDS_IN_SEC,
|
|
|
1897 |
viewState.messagePollAfterMax * MILLISECONDS_IN_SEC
|
|
|
1898 |
)
|
|
|
1899 |
);
|
|
|
1900 |
|
|
|
1901 |
newMessagesPollTimer.start();
|
|
|
1902 |
};
|
|
|
1903 |
|
|
|
1904 |
/**
|
|
|
1905 |
* Reset the state to the initial state and render the UI.
|
|
|
1906 |
*
|
|
|
1907 |
* @param {Object} body Conversation body container element.
|
|
|
1908 |
* @param {Number|null} conversationId The conversation id.
|
|
|
1909 |
* @param {Object} loggedInUserProfile The logged in user's profile.
|
|
|
1910 |
*/
|
|
|
1911 |
var resetState = function(body, conversationId, loggedInUserProfile) {
|
|
|
1912 |
// Reset all of the states back to the beginning if we're loading a new
|
|
|
1913 |
// conversation.
|
|
|
1914 |
if (newMessagesPollTimer) {
|
|
|
1915 |
newMessagesPollTimer.stop();
|
|
|
1916 |
}
|
|
|
1917 |
loadedAllMessages = false;
|
|
|
1918 |
messagesOffset = 0;
|
|
|
1919 |
newMessagesPollTimer = null;
|
|
|
1920 |
isRendering = false;
|
|
|
1921 |
renderBuffer = [];
|
|
|
1922 |
isResetting = true;
|
|
|
1923 |
isSendingMessage = false;
|
|
|
1924 |
isDeletingConversationContent = false;
|
|
|
1925 |
sendMessageBuffer = [];
|
|
|
1926 |
|
|
|
1927 |
var loggedInUserId = loggedInUserProfile.id;
|
|
|
1928 |
var midnight = parseInt(body.attr('data-midnight'), 10);
|
|
|
1929 |
var messagePollMin = parseInt(body.attr('data-message-poll-min'), 10);
|
|
|
1930 |
var messagePollMax = parseInt(body.attr('data-message-poll-max'), 10);
|
|
|
1931 |
var messagePollAfterMax = parseInt(body.attr('data-message-poll-after-max'), 10);
|
|
|
1932 |
var initialState = StateManager.buildInitialState(
|
|
|
1933 |
midnight,
|
|
|
1934 |
loggedInUserId,
|
|
|
1935 |
conversationId,
|
|
|
1936 |
messagePollMin,
|
|
|
1937 |
messagePollMax,
|
|
|
1938 |
messagePollAfterMax
|
|
|
1939 |
);
|
|
|
1940 |
|
|
|
1941 |
if (!viewState) {
|
|
|
1942 |
viewState = initialState;
|
|
|
1943 |
}
|
|
|
1944 |
|
|
|
1945 |
render(initialState);
|
|
|
1946 |
};
|
|
|
1947 |
|
|
|
1948 |
/**
|
|
|
1949 |
* Load a new empty private conversation between two users or self-conversation.
|
|
|
1950 |
*
|
|
|
1951 |
* @param {Object} body Conversation body container element.
|
|
|
1952 |
* @param {Object} loggedInUserProfile The logged in user's profile.
|
|
|
1953 |
* @param {Int} otherUserId The other user's id.
|
|
|
1954 |
* @return {Promise} Renderer promise.
|
|
|
1955 |
*/
|
|
|
1956 |
var resetNoConversation = function(body, loggedInUserProfile, otherUserId) {
|
|
|
1957 |
// Always reset the state back to the initial state so that the
|
|
|
1958 |
// state manager and patcher can work correctly.
|
|
|
1959 |
resetState(body, null, loggedInUserProfile);
|
|
|
1960 |
|
|
|
1961 |
var resetNoConversationPromise = null;
|
|
|
1962 |
|
|
|
1963 |
if (loggedInUserProfile.id != otherUserId) {
|
|
|
1964 |
// Private conversation between two different users.
|
|
|
1965 |
resetNoConversationPromise = Repository.getConversationBetweenUsers(
|
|
|
1966 |
loggedInUserProfile.id,
|
|
|
1967 |
otherUserId,
|
|
|
1968 |
true,
|
|
|
1969 |
true,
|
|
|
1970 |
0,
|
|
|
1971 |
0,
|
|
|
1972 |
LOAD_MESSAGE_LIMIT,
|
|
|
1973 |
0,
|
|
|
1974 |
NEWEST_FIRST
|
|
|
1975 |
);
|
|
|
1976 |
} else {
|
|
|
1977 |
// Self conversation.
|
|
|
1978 |
resetNoConversationPromise = Repository.getSelfConversation(
|
|
|
1979 |
loggedInUserProfile.id,
|
|
|
1980 |
LOAD_MESSAGE_LIMIT,
|
|
|
1981 |
0,
|
|
|
1982 |
NEWEST_FIRST
|
|
|
1983 |
);
|
|
|
1984 |
}
|
|
|
1985 |
|
|
|
1986 |
return resetNoConversationPromise.then(function(conversation) {
|
|
|
1987 |
// Looks like we have a conversation after all! Let's use that.
|
|
|
1988 |
return resetByConversation(body, conversation, loggedInUserProfile);
|
|
|
1989 |
})
|
|
|
1990 |
.catch(function() {
|
|
|
1991 |
// Can't find a conversation. Oh well. Just load up a blank one.
|
|
|
1992 |
return loadEmptyPrivateConversation(loggedInUserProfile, otherUserId);
|
|
|
1993 |
});
|
|
|
1994 |
};
|
|
|
1995 |
|
|
|
1996 |
/**
|
|
|
1997 |
* Load new messages into the conversation based on a time interval.
|
|
|
1998 |
*
|
|
|
1999 |
* @param {Object} body Conversation body container element.
|
|
|
2000 |
* @param {Number} conversationId The conversation id.
|
|
|
2001 |
* @param {Object} loggedInUserProfile The logged in user's profile.
|
|
|
2002 |
* @return {Promise} Renderer promise.
|
|
|
2003 |
*/
|
|
|
2004 |
var resetById = function(body, conversationId, loggedInUserProfile) {
|
|
|
2005 |
var cache = null;
|
|
|
2006 |
if (conversationId in stateCache) {
|
|
|
2007 |
cache = stateCache[conversationId];
|
|
|
2008 |
}
|
|
|
2009 |
|
|
|
2010 |
// Always reset the state back to the initial state so that the
|
|
|
2011 |
// state manager and patcher can work correctly.
|
|
|
2012 |
resetState(body, conversationId, loggedInUserProfile);
|
|
|
2013 |
|
|
|
2014 |
var promise = $.Deferred().resolve({}).promise();
|
|
|
2015 |
if (cache) {
|
|
|
2016 |
// We've seen this conversation before so there is no need to
|
|
|
2017 |
// send any network requests.
|
|
|
2018 |
var newState = cache.state;
|
|
|
2019 |
// Reset some loading states just in case they were left weirdly.
|
|
|
2020 |
newState = StateManager.setLoadingMessages(newState, false);
|
|
|
2021 |
newState = StateManager.setLoadingMembers(newState, false);
|
|
|
2022 |
setMessagesOffset(cache.messagesOffset);
|
|
|
2023 |
setLoadedAllMessages(cache.loadedAllMessages);
|
|
|
2024 |
render(newState);
|
|
|
2025 |
} else {
|
|
|
2026 |
promise = loadNewConversation(
|
|
|
2027 |
conversationId,
|
|
|
2028 |
loggedInUserProfile,
|
|
|
2029 |
LOAD_MESSAGE_LIMIT,
|
|
|
2030 |
0,
|
|
|
2031 |
NEWEST_FIRST
|
|
|
2032 |
);
|
|
|
2033 |
}
|
|
|
2034 |
|
|
|
2035 |
return promise.then(function() {
|
|
|
2036 |
return resetMessagePollTimer(conversationId);
|
|
|
2037 |
});
|
|
|
2038 |
};
|
|
|
2039 |
|
|
|
2040 |
/**
|
|
|
2041 |
* Load new messages into the conversation based on a time interval.
|
|
|
2042 |
*
|
|
|
2043 |
* @param {Object} body Conversation body container element.
|
|
|
2044 |
* @param {Object} conversation The conversation.
|
|
|
2045 |
* @param {Object} loggedInUserProfile The logged in user's profile.
|
|
|
2046 |
* @return {Promise} Renderer promise.
|
|
|
2047 |
*/
|
|
|
2048 |
var resetByConversation = function(body, conversation, loggedInUserProfile) {
|
|
|
2049 |
var cache = null;
|
|
|
2050 |
if (conversation.id in stateCache) {
|
|
|
2051 |
cache = stateCache[conversation.id];
|
|
|
2052 |
}
|
|
|
2053 |
|
|
|
2054 |
// Always reset the state back to the initial state so that the
|
|
|
2055 |
// state manager and patcher can work correctly.
|
|
|
2056 |
resetState(body, conversation.id, loggedInUserProfile);
|
|
|
2057 |
|
|
|
2058 |
var promise = $.Deferred().resolve({}).promise();
|
|
|
2059 |
if (cache) {
|
|
|
2060 |
// We've seen this conversation before so there is no need to
|
|
|
2061 |
// send any network requests.
|
|
|
2062 |
var newState = cache.state;
|
|
|
2063 |
// Reset some loading states just in case they were left weirdly.
|
|
|
2064 |
newState = StateManager.setLoadingMessages(newState, false);
|
|
|
2065 |
newState = StateManager.setLoadingMembers(newState, false);
|
|
|
2066 |
setMessagesOffset(cache.messagesOffset);
|
|
|
2067 |
setLoadedAllMessages(cache.loadedAllMessages);
|
|
|
2068 |
render(newState);
|
|
|
2069 |
} else {
|
|
|
2070 |
promise = loadExistingConversation(
|
|
|
2071 |
conversation,
|
|
|
2072 |
loggedInUserProfile,
|
|
|
2073 |
LOAD_MESSAGE_LIMIT,
|
|
|
2074 |
NEWEST_FIRST
|
|
|
2075 |
);
|
|
|
2076 |
}
|
|
|
2077 |
|
|
|
2078 |
return promise.then(function() {
|
|
|
2079 |
return resetMessagePollTimer(conversation.id);
|
|
|
2080 |
});
|
|
|
2081 |
};
|
|
|
2082 |
|
|
|
2083 |
/**
|
|
|
2084 |
* Setup the conversation page. This is a rather complex function because there are a
|
|
|
2085 |
* few combinations of arguments that can be provided to this function to show the
|
|
|
2086 |
* conversation.
|
|
|
2087 |
*
|
|
|
2088 |
* There are:
|
|
|
2089 |
* 1.) A conversation object with no action or other user id (e.g. from the overview page)
|
|
|
2090 |
* 2.) A conversation id with no action or other user id (e.g. from the contacts page)
|
|
|
2091 |
* 3.) No conversation/id with an action and other other user id. (e.g. from contact page)
|
|
|
2092 |
*
|
|
|
2093 |
* @param {string} namespace The route namespace.
|
|
|
2094 |
* @param {Object} header Conversation header container element.
|
|
|
2095 |
* @param {Object} body Conversation body container element.
|
|
|
2096 |
* @param {Object} footer Conversation footer container element.
|
|
|
2097 |
* @param {Object|Number|null} conversationOrId Conversation or id or null
|
|
|
2098 |
* @param {String} action An action to take on the conversation
|
|
|
2099 |
* @param {Number} otherUserId The other user id for a private conversation
|
|
|
2100 |
* @return {Object} jQuery promise
|
|
|
2101 |
*/
|
|
|
2102 |
var show = function(namespace, header, body, footer, conversationOrId, action, otherUserId) {
|
|
|
2103 |
var conversation = null;
|
|
|
2104 |
var conversationId = null;
|
|
|
2105 |
|
|
|
2106 |
// Check what we were given to identify the conversation.
|
|
|
2107 |
if (conversationOrId && conversationOrId !== null && typeof conversationOrId == 'object') {
|
|
|
2108 |
conversation = conversationOrId;
|
|
|
2109 |
conversationId = parseInt(conversation.id, 10);
|
|
|
2110 |
} else {
|
|
|
2111 |
conversation = null;
|
|
|
2112 |
conversationId = parseInt(conversationOrId, 10);
|
|
|
2113 |
conversationId = isNaN(conversationId) ? null : conversationId;
|
|
|
2114 |
}
|
|
|
2115 |
|
|
|
2116 |
if (!conversationId && action && otherUserId) {
|
|
|
2117 |
// If we didn't get a conversation id got a user id then let's see if we've
|
|
|
2118 |
// previously loaded a private conversation with this user.
|
|
|
2119 |
conversationId = getCachedPrivateConversationIdFromUserId(otherUserId);
|
|
|
2120 |
}
|
|
|
2121 |
|
|
|
2122 |
// This is a new conversation if:
|
|
|
2123 |
// 1. We don't already have a state
|
|
|
2124 |
// 2. The given conversation doesn't match the one currently loaded
|
|
|
2125 |
// 3. We have a view state without a conversation id and we weren't given one
|
|
|
2126 |
// but we were given a different other user id. This happens when the user
|
|
|
2127 |
// goes from viewing a user that they haven't yet initialised a conversation
|
|
|
2128 |
// with to viewing a different user that they also haven't initialised a
|
|
|
2129 |
// conversation with.
|
|
|
2130 |
var isNewConversation = !viewState || (viewState.id != conversationId) || (otherUserId && otherUserId != getOtherUserId());
|
|
|
2131 |
|
|
|
2132 |
if (!body.attr('data-init')) {
|
|
|
2133 |
// Generate the render function to bind the header, body, and footer
|
|
|
2134 |
// elements to it so that we don't need to pass them around this module.
|
|
|
2135 |
render = generateRenderFunction(header, body, footer, isNewConversation);
|
|
|
2136 |
registerEventListeners(namespace, header, body, footer);
|
|
|
2137 |
body.attr('data-init', true);
|
|
|
2138 |
}
|
|
|
2139 |
|
|
|
2140 |
if (isNewConversation) {
|
|
|
2141 |
var renderPromise = null;
|
|
|
2142 |
var loggedInUserProfile = getLoggedInUserProfile(body);
|
|
|
2143 |
|
|
|
2144 |
if (conversation) {
|
|
|
2145 |
renderPromise = resetByConversation(body, conversation, loggedInUserProfile, otherUserId);
|
|
|
2146 |
} else if (conversationId) {
|
|
|
2147 |
renderPromise = resetById(body, conversationId, loggedInUserProfile, otherUserId);
|
|
|
2148 |
} else {
|
|
|
2149 |
renderPromise = resetNoConversation(body, loggedInUserProfile, otherUserId);
|
|
|
2150 |
}
|
|
|
2151 |
|
|
|
2152 |
return renderPromise
|
|
|
2153 |
.then(function() {
|
|
|
2154 |
isResetting = false;
|
|
|
2155 |
// Focus the first element that can receieve it in the header.
|
|
|
2156 |
header.find(Constants.SELECTORS.CAN_RECEIVE_FOCUS).first().focus();
|
|
|
2157 |
return;
|
|
|
2158 |
})
|
|
|
2159 |
.catch(function(error) {
|
|
|
2160 |
isResetting = false;
|
|
|
2161 |
Notification.exception(error);
|
|
|
2162 |
});
|
|
|
2163 |
}
|
|
|
2164 |
|
|
|
2165 |
// We're not loading a new conversation so we should reset the poll timer to try to load
|
|
|
2166 |
// new messages.
|
|
|
2167 |
resetMessagePollTimer(conversationId);
|
|
|
2168 |
|
|
|
2169 |
if (viewState.type == CONVERSATION_TYPES.PRIVATE && action) {
|
|
|
2170 |
// There are special actions that the user can perform in a private (aka 1-to-1)
|
|
|
2171 |
// conversation.
|
|
|
2172 |
var currentOtherUserId = getOtherUserId();
|
|
|
2173 |
|
|
|
2174 |
switch (action) {
|
|
|
2175 |
case 'block':
|
|
|
2176 |
return requestBlockUser(currentOtherUserId);
|
|
|
2177 |
case 'unblock':
|
|
|
2178 |
return requestUnblockUser(currentOtherUserId);
|
|
|
2179 |
case 'add-contact':
|
|
|
2180 |
return requestAddContact(currentOtherUserId);
|
|
|
2181 |
case 'remove-contact':
|
|
|
2182 |
return requestRemoveContact(currentOtherUserId);
|
|
|
2183 |
}
|
|
|
2184 |
}
|
|
|
2185 |
|
|
|
2186 |
// Final fallback to return a promise if we didn't need to do anything.
|
|
|
2187 |
return $.Deferred().resolve().promise();
|
|
|
2188 |
};
|
|
|
2189 |
|
|
|
2190 |
/**
|
|
|
2191 |
* String describing this page used for aria-labels.
|
|
|
2192 |
*
|
|
|
2193 |
* @return {Object} jQuery promise
|
|
|
2194 |
*/
|
|
|
2195 |
var description = function() {
|
|
|
2196 |
return Str.get_string('messagedrawerviewconversation', 'core_message', viewState.name);
|
|
|
2197 |
};
|
|
|
2198 |
|
|
|
2199 |
return {
|
|
|
2200 |
show: show,
|
|
|
2201 |
description: description
|
|
|
2202 |
};
|
|
|
2203 |
});
|