1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
|
odoo.define('mail/static/src/models/thread_view/thread_view.js', function (require) {
'use strict';
const { registerNewModel } = require('mail/static/src/model/model_core.js');
const { RecordDeletedError } = require('mail/static/src/model/model_errors.js');
const { attr, many2many, many2one, one2one } = require('mail/static/src/model/model_field.js');
const { clear } = require('mail/static/src/model/model_field_command.js');
function factory(dependencies) {
class ThreadView extends dependencies['mail.model'] {
/**
* @override
*/
_willDelete() {
this.env.browser.clearTimeout(this._loaderTimeout);
return super._willDelete(...arguments);
}
//----------------------------------------------------------------------
// Public
//----------------------------------------------------------------------
/**
* This function register a hint for the component related to this
* record. Hints are information on changes around this viewer that
* make require adjustment on the component. For instance, if this
* ThreadView initiated a thread cache load and it now has become
* loaded, then it may need to auto-scroll to last message.
*
* @param {string} hintType name of the hint. Used to determine what's
* the broad type of adjustement the component has to do.
* @param {any} [hintData] data of the hint. Used to fine-tune
* adjustments on the component.
*/
addComponentHint(hintType, hintData) {
const hint = { data: hintData, type: hintType };
this.update({
componentHintList: this.componentHintList.concat([hint]),
});
}
/**
* @param {Object} hint
*/
markComponentHintProcessed(hint) {
this.update({
componentHintList: this.componentHintList.filter(h => h !== hint),
});
this.env.messagingBus.trigger('o-thread-view-hint-processed', {
hint,
threadViewer: this.threadViewer,
});
}
/**
* @param {mail.message} message
*/
handleVisibleMessage(message) {
if (!this.lastVisibleMessage || this.lastVisibleMessage.id < message.id) {
this.update({ lastVisibleMessage: [['link', message]] });
}
}
//----------------------------------------------------------------------
// Private
//----------------------------------------------------------------------
/**
* @private
* @returns {mail.messaging}
*/
_computeMessaging() {
return [['link', this.env.messaging]];
}
/**
* @private
* @returns {string[]}
*/
_computeTextInputSendShortcuts() {
if (!this.thread) {
return;
}
const isMailingList = this.thread.model === 'mail.channel' && this.thread.mass_mailing;
// Actually in mobile there is a send button, so we need there 'enter' to allow new line.
// Hence, we want to use a different shortcut 'ctrl/meta enter' to send for small screen
// size with a non-mailing channel.
// here send will be done on clicking the button or using the 'ctrl/meta enter' shortcut.
if (this.env.messaging.device.isMobile || isMailingList) {
return ['ctrl-enter', 'meta-enter'];
}
return ['enter'];
}
/**
* @private
* @returns {integer|undefined}
*/
_computeThreadCacheInitialScrollHeight() {
if (!this.threadCache) {
return clear();
}
const threadCacheInitialScrollHeight = this.threadCacheInitialScrollHeights[this.threadCache.localId];
if (threadCacheInitialScrollHeight !== undefined) {
return threadCacheInitialScrollHeight;
}
return clear();
}
/**
* @private
* @returns {integer|undefined}
*/
_computeThreadCacheInitialScrollPosition() {
if (!this.threadCache) {
return clear();
}
const threadCacheInitialScrollPosition = this.threadCacheInitialScrollPositions[this.threadCache.localId];
if (threadCacheInitialScrollPosition !== undefined) {
return threadCacheInitialScrollPosition;
}
return clear();
}
/**
* Not a real field, used to trigger `thread.markAsSeen` when one of
* the dependencies changes.
*
* @private
* @returns {boolean}
*/
_computeThreadShouldBeSetAsSeen() {
if (!this.thread) {
return;
}
if (!this.thread.lastNonTransientMessage) {
return;
}
if (!this.lastVisibleMessage) {
return;
}
if (this.lastVisibleMessage !== this.lastMessage) {
return;
}
if (!this.hasComposerFocus) {
// FIXME condition should not be on "composer is focused" but "threadView is active"
// See task-2277543
return;
}
this.thread.markAsSeen(this.thread.lastNonTransientMessage).catch(e => {
// prevent crash when executing compute during destroy
if (!(e instanceof RecordDeletedError)) {
throw e;
}
});
}
/**
* @private
*/
_onThreadCacheChanged() {
// clear obsolete hints
this.update({ componentHintList: clear() });
this.addComponentHint('change-of-thread-cache');
if (this.threadCache) {
this.threadCache.update({
isCacheRefreshRequested: true,
isMarkAllAsReadRequested: true,
});
}
this.update({ lastVisibleMessage: [['unlink']] });
}
/**
* @private
*/
_onThreadCacheIsLoadingChanged() {
if (this.threadCache && this.threadCache.isLoading) {
if (!this.isLoading && !this.isPreparingLoading) {
this.update({ isPreparingLoading: true });
this.async(() =>
new Promise(resolve => {
this._loaderTimeout = this.env.browser.setTimeout(resolve, 400);
}
)).then(() => {
const isLoading = this.threadCache
? this.threadCache.isLoading
: false;
this.update({ isLoading, isPreparingLoading: false });
});
}
return;
}
this.env.browser.clearTimeout(this._loaderTimeout);
this.update({ isLoading: false, isPreparingLoading: false });
}
}
ThreadView.fields = {
checkedMessages: many2many('mail.message', {
related: 'threadCache.checkedMessages',
}),
/**
* List of component hints. Hints contain information that help
* components make UI/UX decisions based on their UI state.
* For instance, on receiving new messages and the last message
* is visible, it should auto-scroll to this new last message.
*
* Format of a component hint:
*
* {
* type: {string} the name of the component hint. Useful
* for components to dispatch behaviour
* based on its type.
* data: {Object} data related to the component hint.
* For instance, if hint suggests to scroll
* to a certain message, data may contain
* message id.
* }
*/
componentHintList: attr({
default: [],
}),
composer: many2one('mail.composer', {
related: 'thread.composer',
}),
/**
* Serves as compute dependency.
*/
device: one2one('mail.device', {
related: 'messaging.device',
}),
/**
* Serves as compute dependency.
*/
deviceIsMobile: attr({
related: 'device.isMobile',
}),
hasComposerFocus: attr({
related: 'composer.hasFocus',
}),
/**
* States whether `this.threadCache` is currently loading messages.
*
* This field is related to `this.threadCache.isLoading` but with a
* delay on its update to avoid flickering on the UI.
*
* It is computed through `_onThreadCacheIsLoadingChanged` and it should
* otherwise be considered read-only.
*/
isLoading: attr({
default: false,
}),
/**
* States whether `this` is aware of `this.threadCache` currently
* loading messages, but `this` is not yet ready to display that loading
* on the UI.
*
* This field is computed through `_onThreadCacheIsLoadingChanged` and
* it should otherwise be considered read-only.
*
* @see `this.isLoading`
*/
isPreparingLoading: attr({
default: false,
}),
/**
* Determines whether `this` should automatically scroll on receiving
* a new message. Detection of new message is done through the component
* hint `message-received`.
*/
hasAutoScrollOnMessageReceived: attr({
default: true,
}),
/**
* Last message in the context of the currently displayed thread cache.
*/
lastMessage: many2one('mail.message', {
related: 'thread.lastMessage',
}),
/**
* Serves as compute dependency.
*/
lastNonTransientMessage: many2one('mail.message', {
related: 'thread.lastNonTransientMessage',
}),
/**
* Most recent message in this ThreadView that has been shown to the
* current partner in the currently displayed thread cache.
*/
lastVisibleMessage: many2one('mail.message'),
messages: many2many('mail.message', {
related: 'threadCache.messages',
}),
/**
* Serves as compute dependency.
*/
messaging: many2one('mail.messaging', {
compute: '_computeMessaging',
}),
nonEmptyMessages: many2many('mail.message', {
related: 'threadCache.nonEmptyMessages',
}),
/**
* Not a real field, used to trigger `_onThreadCacheChanged` when one of
* the dependencies changes.
*/
onThreadCacheChanged: attr({
compute: '_onThreadCacheChanged',
dependencies: [
'threadCache'
],
}),
/**
* Not a real field, used to trigger `_onThreadCacheIsLoadingChanged`
* when one of the dependencies changes.
*
* @see `this.isLoading`
*/
onThreadCacheIsLoadingChanged: attr({
compute: '_onThreadCacheIsLoadingChanged',
dependencies: [
'threadCache',
'threadCacheIsLoading',
],
}),
/**
* Determines the domain to apply when fetching messages for `this.thread`.
*/
stringifiedDomain: attr({
related: 'threadViewer.stringifiedDomain',
}),
/**
* Determines the keyboard shortcuts that are available to send a message
* from the composer of this thread viewer.
*/
textInputSendShortcuts: attr({
compute: '_computeTextInputSendShortcuts',
dependencies: [
'device',
'deviceIsMobile',
'thread',
'threadMassMailing',
'threadModel',
],
}),
/**
* Determines the `mail.thread` currently displayed by `this`.
*/
thread: many2one('mail.thread', {
inverse: 'threadViews',
related: 'threadViewer.thread',
}),
/**
* States the `mail.thread_cache` currently displayed by `this`.
*/
threadCache: many2one('mail.thread_cache', {
inverse: 'threadViews',
related: 'threadViewer.threadCache',
}),
threadCacheInitialScrollHeight: attr({
compute: '_computeThreadCacheInitialScrollHeight',
dependencies: [
'threadCache',
'threadCacheInitialScrollHeights',
],
}),
threadCacheInitialScrollPosition: attr({
compute: '_computeThreadCacheInitialScrollPosition',
dependencies: [
'threadCache',
'threadCacheInitialScrollPositions',
],
}),
/**
* Serves as compute dependency.
*/
threadCacheIsLoading: attr({
related: 'threadCache.isLoading',
}),
/**
* List of saved initial scroll heights of thread caches.
*/
threadCacheInitialScrollHeights: attr({
default: {},
related: 'threadViewer.threadCacheInitialScrollHeights',
}),
/**
* List of saved initial scroll positions of thread caches.
*/
threadCacheInitialScrollPositions: attr({
default: {},
related: 'threadViewer.threadCacheInitialScrollPositions',
}),
/**
* Serves as compute dependency.
*/
threadMassMailing: attr({
related: 'thread.mass_mailing',
}),
/**
* Serves as compute dependency.
*/
threadModel: attr({
related: 'thread.model',
}),
/**
* Not a real field, used to trigger `thread.markAsSeen` when one of
* the dependencies changes.
*/
threadShouldBeSetAsSeen: attr({
compute: '_computeThreadShouldBeSetAsSeen',
dependencies: [
'hasComposerFocus',
'lastMessage',
'lastNonTransientMessage',
'lastVisibleMessage',
'threadCache',
],
}),
/**
* Determines the `mail.thread_viewer` currently managing `this`.
*/
threadViewer: one2one('mail.thread_viewer', {
inverse: 'threadView',
}),
uncheckedMessages: many2many('mail.message', {
related: 'threadCache.uncheckedMessages',
}),
};
ThreadView.modelName = 'mail.thread_view';
return ThreadView;
}
registerNewModel('mail.thread_view', factory);
});
|