summaryrefslogtreecommitdiff
path: root/addons/mail/static/src/models/message_seen_indicator
diff options
context:
space:
mode:
authorXavier Dubuc <xdu@odoo.com>2020-09-18 10:09:04 +0000
committerXavier Dubuc <xdu@odoo.com>2020-09-23 18:53:51 +0000
commit4f12598008eba2bb2e65016e12acf3edd5d83f8c (patch)
tree19377b572c6f506aab32ebfd948f222343f77783 /addons/mail/static/src/models/message_seen_indicator
parent318135d38a743effbe7067ded4f3e6cb5b1d14e0 (diff)
[FIX] mail: use compute fields for identifying relations
(in message seen indicators) task-2335647 closes odoo/odoo#58373 X-original-commit: 3d4ff42f4474a5761c9e0737297d973992a2fcc2 Signed-off-by: Sébastien Theys (seb) <seb@odoo.com>
Diffstat (limited to 'addons/mail/static/src/models/message_seen_indicator')
-rw-r--r--addons/mail/static/src/models/message_seen_indicator/message_seen_indicator.js95
1 files changed, 73 insertions, 22 deletions
diff --git a/addons/mail/static/src/models/message_seen_indicator/message_seen_indicator.js b/addons/mail/static/src/models/message_seen_indicator/message_seen_indicator.js
index bab1ef2046e..dd1848aaf7b 100644
--- a/addons/mail/static/src/models/message_seen_indicator/message_seen_indicator.js
+++ b/addons/mail/static/src/models/message_seen_indicator/message_seen_indicator.js
@@ -13,23 +13,6 @@ function factory(dependencies) {
//----------------------------------------------------------------------
/**
- * FIXME replace by using messageId & channelId as identifying fields (task-2335647)
- *
- * @static
- * @param {mail.message|integer} message
- * @param {mail.thread|integer} thread
- * @returns {string|undefined}
- */
- static computeId(message, thread) {
- if (message && thread) {
- const messageId = typeof message !== 'object' ? message : message.id;
- const threadId = typeof thread !== 'object' ? thread : thread.id;
- return [messageId, threadId].join('-');
- }
- return undefined;
- }
-
- /**
* @static
* @param {mail.thread} [channel] the concerned thread
*/
@@ -73,7 +56,8 @@ function factory(dependencies) {
* @override
*/
static _createRecordLocalId(data) {
- return `${this.modelName}_${data.id}`;
+ const { channelId, messageId } = data;
+ return `${this.modelName}_${channelId}_${messageId}`;
}
/**
@@ -239,11 +223,46 @@ function factory(dependencies) {
}
return [['replace', otherPartnersThatHaveSeen]];
}
+
+ /**
+ * @private
+ * @returns {mail.message}
+ */
+ _computeMessage() {
+ return [['insert', { id: this.messageId }]];
+ }
+
+ /**
+ * @private
+ * @returns {mail.thread}
+ */
+ _computeThread() {
+ return [['insert', {
+ id: this.channelId,
+ model: 'mail.channel',
+ }]];
+ }
}
MessageSeenIndicator.modelName = 'mail.message_seen_indicator';
MessageSeenIndicator.fields = {
+ /**
+ * The id of the channel this seen indicator is related to.
+ *
+ * Should write on this field to set relation between the channel and
+ * this seen indicator, not on `thread`.
+ *
+ * Reason for not setting the relation directly is the necessity to
+ * uniquely identify a seen indicator based on channel and message from data.
+ * Relational data are list of commands, which is problematic to deduce
+ * identifying records.
+ *
+ * TODO: task-2322536 (normalize relational data) & task-2323665
+ * (required fields) should improve and let us just use the relational
+ * fields.
+ */
+ channelId: attr(),
hasEveryoneFetched: attr({
compute: '_computeHasEveryoneFetched',
default: false,
@@ -273,13 +292,36 @@ function factory(dependencies) {
'threadLastCurrentPartnerMessageSeenByEveryone',
],
}),
- message: many2one('mail.message'),
+ /**
+ * The message concerned by this seen indicator.
+ * This is automatically computed based on messageId field.
+ * @see messageId
+ */
+ message: many2one('mail.message', {
+ compute: '_computeMessage',
+ dependencies: [
+ 'messageId',
+ ],
+ }),
messageAuthor: many2one('mail.partner', {
related: 'message.author',
}),
- messageId: attr({
- related: 'message.id',
- }),
+ /**
+ * The id of the message this seen indicator is related to.
+ *
+ * Should write on this field to set relation between the channel and
+ * this seen indicator, not on `message`.
+ *
+ * Reason for not setting the relation directly is the necessity to
+ * uniquely identify a seen indicator based on channel and message from data.
+ * Relational data are list of commands, which is problematic to deduce
+ * identifying records.
+ *
+ * TODO: task-2322536 (normalize relational data) & task-2323665
+ * (required fields) should improve and let us just use the relational
+ * fields.
+ */
+ messageId: attr(),
partnersThatHaveFetched: many2many('mail.partner', {
compute: '_computePartnersThatHaveFetched',
dependencies: ['messageAuthor', 'messageId', 'threadPartnerSeenInfos'],
@@ -288,7 +330,16 @@ function factory(dependencies) {
compute: '_computePartnersThatHaveSeen',
dependencies: ['messageAuthor', 'messageId', 'threadPartnerSeenInfos'],
}),
+ /**
+ * The thread concerned by this seen indicator.
+ * This is automatically computed based on channelId field.
+ * @see channelId
+ */
thread: many2one('mail.thread', {
+ compute: '_computeThread',
+ dependencies: [
+ 'channelId',
+ ],
inverse: 'messageSeenIndicators'
}),
threadPartnerSeenInfos: one2many('mail.thread_partner_seen_info', {