Files
odoo_source/addons/mail/static/src/models/message_reaction_group.js
T
Louis Wicket (wil) e16976ddfc [IMP] mail, *: move up model files
Move model definition files from `models/thing/thing.js` to
`models/thing.js` and remove the now empty intermediate directory.

Task-2759582
 * = calendar, crm_livechat, hr, im_livechat, mail_bot, sms, snailmail,
     website_livechat, website_slides

closes odoo/odoo#86464

Related: odoo/enterprise#25291
Signed-off-by: Alexandre Kühn (aku) <aku@odoo.com>
2022-03-17 11:27:59 +01:00

110 lines
3.7 KiB
JavaScript

/** @odoo-module **/
import { registerModel } from '@mail/model/model_core';
import { attr, many, one } from '@mail/model/model_field';
import { insertAndReplace } from '@mail/model/model_field_command';
import { markEventHandled } from '@mail/utils/utils';
registerModel({
name: 'MessageReactionGroup',
identifyingFields: ['message', 'content'],
recordMethods: {
/**
* Handles click on the reaction group.
*
* @param {MouseEvent} ev
*/
onClick(ev) {
markEventHandled(ev, 'MessageReactionGroup.Click');
if (this.hasUserReacted) {
this.message.removeReaction(this.content);
} else {
this.message.addReaction(this.content);
}
},
/**
* @private
* @returns {boolean}
*/
_computeHasUserReacted() {
return Boolean(
(this.messaging.currentPartner && this.partners.includes(this.messaging.currentPartner)) ||
(this.messaging.currentGuest && this.guests.includes(this.messaging.currentGuest))
);
},
/**
* @private
* @returns {FieldCommand}
*/
_computeMessage() {
return insertAndReplace({ id: this.messageId });
},
/**
* @private
* @returns {string}
*/
_computeSummary() {
const {
length,
0: firstUser,
1: secondUser,
2: thirdUser,
} = [
...this.partners.map(partner => {
if (this.message.originThread) {
return this.message.originThread.getMemberName(partner);
}
return partner.nameOrDisplayName;
}),
...this.guests.map(guest => guest.name),
];
switch (length) {
case 1:
return _.str.sprintf(this.env._t('%s has reacted with %s'), firstUser, this.content);
case 2:
return _.str.sprintf(this.env._t('%s and %s have reacted with %s'), firstUser, secondUser, this.content);
case 3:
return _.str.sprintf(this.env._t('%s, %s, %s have reacted with %s'), firstUser, secondUser, thirdUser, this.content);
case 4:
return _.str.sprintf(this.env._t('%s, %s, %s and 1 other person have reacted with %s'), firstUser, secondUser, thirdUser, this.content);
default:
return _.str.sprintf(this.env._t('%s, %s, %s and %s other persons have reacted with %s'), firstUser, secondUser, thirdUser, length - 3, this.content);
}
},
},
fields: {
content: attr({
readonly: true,
required: true,
}),
count: attr({
required: true,
}),
/**
* States the guests that have used this reaction on this message.
*/
guests: many('Guest'),
hasUserReacted: attr({
compute: '_computeHasUserReacted',
default: false,
}),
message: one('Message', {
compute: '_computeMessage',
inverse: 'messageReactionGroups',
readonly: true,
required: true,
}),
messageId: attr({
readonly: true,
required: true,
}),
/**
* States the partners that have used this reaction on this message.
*/
partners: many('Partner'),
summary: attr({
compute: '_computeSummary',
}),
},
});