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
|
odoo.define('mail/static/src/components/notification_list/notification_list.js', function (require) {
'use strict';
const components = {
NotificationGroup: require('mail/static/src/components/notification_group/notification_group.js'),
NotificationRequest: require('mail/static/src/components/notification_request/notification_request.js'),
ThreadNeedactionPreview: require('mail/static/src/components/thread_needaction_preview/thread_needaction_preview.js'),
ThreadPreview: require('mail/static/src/components/thread_preview/thread_preview.js'),
};
const useShouldUpdateBasedOnProps = require('mail/static/src/component_hooks/use_should_update_based_on_props/use_should_update_based_on_props.js');
const useStore = require('mail/static/src/component_hooks/use_store/use_store.js');
const { Component } = owl;
class NotificationList extends Component {
/**
* @override
*/
constructor(...args) {
super(...args);
useShouldUpdateBasedOnProps();
this.storeProps = useStore((...args) => this._useStoreSelector(...args), {
compareDepth: {
// list + notification object created in useStore
notifications: 2,
},
});
}
mounted() {
this._loadPreviews();
}
//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
/**
* @returns {Object[]}
*/
get notifications() {
const { notifications } = this.storeProps;
return notifications;
}
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* Load previews of given thread. Basically consists of fetching all missing
* last messages of each thread.
*
* @private
*/
async _loadPreviews() {
const threads = this.notifications
.filter(notification => notification.thread && notification.thread.exists())
.map(notification => notification.thread);
this.env.models['mail.thread'].loadPreviews(threads);
}
/**
* @private
* @param {Object} props
*/
_useStoreSelector(props) {
const threads = this._useStoreSelectorThreads(props);
let threadNeedactionNotifications = [];
if (props.filter === 'all') {
// threads with needactions
threadNeedactionNotifications = this.env.models['mail.thread']
.all(t => t.model !== 'mail.box' && t.needactionMessagesAsOriginThread.length > 0)
.sort((t1, t2) => {
if (t1.needactionMessagesAsOriginThread.length > 0 && t2.needactionMessagesAsOriginThread.length === 0) {
return -1;
}
if (t1.needactionMessagesAsOriginThread.length === 0 && t2.needactionMessagesAsOriginThread.length > 0) {
return 1;
}
if (t1.lastNeedactionMessageAsOriginThread && t2.lastNeedactionMessageAsOriginThread) {
return t1.lastNeedactionMessageAsOriginThread.date.isBefore(t2.lastNeedactionMessageAsOriginThread.date) ? 1 : -1;
}
if (t1.lastNeedactionMessageAsOriginThread) {
return -1;
}
if (t2.lastNeedactionMessageAsOriginThread) {
return 1;
}
return t1.id < t2.id ? -1 : 1;
})
.map(thread => {
return {
thread,
type: 'thread_needaction',
uniqueId: thread.localId + '_needaction',
};
});
}
// thread notifications
const threadNotifications = threads
.sort((t1, t2) => {
if (t1.localMessageUnreadCounter > 0 && t2.localMessageUnreadCounter === 0) {
return -1;
}
if (t1.localMessageUnreadCounter === 0 && t2.localMessageUnreadCounter > 0) {
return 1;
}
if (t1.lastMessage && t2.lastMessage) {
return t1.lastMessage.date.isBefore(t2.lastMessage.date) ? 1 : -1;
}
if (t1.lastMessage) {
return -1;
}
if (t2.lastMessage) {
return 1;
}
return t1.id < t2.id ? -1 : 1;
})
.map(thread => {
return {
thread,
type: 'thread',
uniqueId: thread.localId,
};
});
let notifications = threadNeedactionNotifications.concat(threadNotifications);
if (props.filter === 'all') {
const notificationGroups = this.env.messaging.notificationGroupManager.groups;
notifications = Object.values(notificationGroups)
.sort((group1, group2) =>
group1.date.isAfter(group2.date) ? -1 : 1
).map(notificationGroup => {
return {
notificationGroup,
uniqueId: notificationGroup.localId,
};
}).concat(notifications);
}
// native notification request
if (props.filter === 'all' && this.env.messaging.isNotificationPermissionDefault()) {
notifications.unshift({
type: 'odoobotRequest',
uniqueId: 'odoobotRequest',
});
}
return {
isDeviceMobile: this.env.messaging.device.isMobile,
notifications,
};
}
/**
* @private
* @param {Object} props
* @throws {Error} in case `props.filter` is not supported
* @returns {mail.thread[]}
*/
_useStoreSelectorThreads(props) {
if (props.filter === 'mailbox') {
return this.env.models['mail.thread']
.all(thread => thread.isPinned && thread.model === 'mail.box')
.sort((mailbox1, mailbox2) => {
if (mailbox1 === this.env.messaging.inbox) {
return -1;
}
if (mailbox2 === this.env.messaging.inbox) {
return 1;
}
if (mailbox1 === this.env.messaging.starred) {
return -1;
}
if (mailbox2 === this.env.messaging.starred) {
return 1;
}
const mailbox1Name = mailbox1.displayName;
const mailbox2Name = mailbox2.displayName;
mailbox1Name < mailbox2Name ? -1 : 1;
});
} else if (props.filter === 'channel') {
return this.env.models['mail.thread']
.all(thread =>
thread.channel_type === 'channel' &&
thread.isPinned &&
thread.model === 'mail.channel'
)
.sort((c1, c2) => c1.displayName < c2.displayName ? -1 : 1);
} else if (props.filter === 'chat') {
return this.env.models['mail.thread']
.all(thread =>
thread.isChatChannel &&
thread.isPinned &&
thread.model === 'mail.channel'
)
.sort((c1, c2) => c1.displayName < c2.displayName ? -1 : 1);
} else if (props.filter === 'all') {
// "All" filter is for channels and chats
return this.env.models['mail.thread']
.all(thread => thread.isPinned && thread.model === 'mail.channel')
.sort((c1, c2) => c1.displayName < c2.displayName ? -1 : 1);
} else {
throw new Error(`Unsupported filter ${props.filter}`);
}
}
}
Object.assign(NotificationList, {
_allowedFilters: ['all', 'mailbox', 'channel', 'chat'],
components,
defaultProps: {
filter: 'all',
},
props: {
filter: {
type: String,
validate: prop => NotificationList._allowedFilters.includes(prop),
},
},
template: 'mail.NotificationList',
});
return NotificationList;
});
|