[FIX] mail: show same unpin message everywhere

Before this commit, the unpin message was different depending on whether
the message was unpinned from the “pinned messages panel“ or from the
message action.

This commit unifies both flows by introducing a common method to pin and
unpin messages.

closes odoo/odoo#126072

X-original-commit: 9cf7ec443dce3a3d40a3e67533de60491bc9e7b6
Signed-off-by: Didier Debondt (did) <did@odoo.com>
Signed-off-by: Louis Wicket (wil) <wil@odoo.com>
This commit is contained in:
Louis Wicket (wil)
2023-06-22 19:37:15 +02:00
parent 0785c6982d
commit e9f6c94aa6
3 changed files with 56 additions and 31 deletions
@@ -1,12 +1,10 @@
/* @odoo-module */
import { Message } from "@mail/core/common/message";
import { MessageConfirmDialog } from "@mail/core/common/message_confirm_dialog";
import { useMessagePinService } from "@mail/discuss/message_pin/common/message_pin_service";
import { _t } from "@web/core/l10n/translation";
import { patch } from "@web/core/utils/patch";
import { sprintf } from "@web/core/utils/strings";
patch(Message, "discuss/message_pin/common", {
components: { ...Message.components },
@@ -18,27 +16,12 @@ patch(Message.prototype, "discuss/message_pin/common", {
this.messagePinService = useMessagePinService();
},
onClickPin() {
const pinnedAt = this.messagePinService.getPinnedAt(this.message.id);
const thread = this.message.originThread;
this.env.services.dialog.add(MessageConfirmDialog, {
confirmColor: pinnedAt ? "btn-danger" : undefined,
confirmText: pinnedAt ? _t("Yes, remove it please") : _t("Yeah, pin it!"),
message: this.message,
messageComponent: Message,
prompt: pinnedAt
? _t("Well nothing lasts forever, but are you sure you want to unpin this message?")
: sprintf(
_t("You sure want this message pinned to %(conversation)s forever and ever?"),
{ conversation: thread.prefix + thread.displayName }
),
size: "md",
title: pinnedAt ? _t("Unpin Message") : _t("Pin It"),
onConfirm: () =>
this.messagePinService.setPin(
this.message,
!this.messagePinService.getPinnedAt(this.message.id)
),
});
const isPinned = Boolean(this.messagePinService.getPinnedAt(this.message.id));
if (isPinned) {
this.messagePinService.unpin(this.message);
} else {
this.messagePinService.pin(this.message);
}
},
get attClass() {
const res = this._super();
@@ -1,9 +1,14 @@
/* @odoo-module */
import { Message } from "@mail/core/common/message";
import { MessageConfirmDialog } from "@mail/core/common/message_confirm_dialog";
import { markup, reactive, useState } from "@odoo/owl";
import { _t } from "@web/core/l10n/translation";
import { registry } from "@web/core/registry";
import { useService } from "@web/core/utils/hooks";
import { sprintf } from "@web/core/utils/strings";
export const OTHER_LONG_TYPING = 60000;
@@ -26,6 +31,7 @@ export class MessagePin {
env,
{
bus_service: busService,
dialog: dialogService,
"mail.message": messageService,
"mail.store": storeService,
orm: ormService,
@@ -34,6 +40,7 @@ export class MessagePin {
) {
Object.assign(this, {
busService,
dialogService,
env,
messageService,
ormService,
@@ -149,6 +156,27 @@ export class MessagePin {
}
}
/**
* Prompts the user for confirmation, then sets pinned to true.
*
* @param {import("@mail/core/common/message_model").Message}
*/
pin(message) {
const thread = message.originThread;
this.dialogService.add(MessageConfirmDialog, {
confirmText: _t("Yeah, pin it!"),
message: message,
messageComponent: Message,
prompt: sprintf(
_t("You sure want this message pinned to %(conversation)s forever and ever?"),
{ conversation: thread.prefix + thread.displayName }
),
size: "md",
title: _t("Pin It"),
onConfirm: () => this.setPin(message, true),
});
}
/**
* @param {number} messageId
*/
@@ -178,10 +206,30 @@ export class MessagePin {
pinned,
});
}
/**
* Prompts the user for confirmation, then sets pinned to false.
*
* @param {import("@mail/core/common/message_model").Message}
*/
unpin(message) {
this.dialogService.add(MessageConfirmDialog, {
confirmColor: "btn-danger",
confirmText: _t("Yes, remove it please"),
message: message,
messageComponent: Message,
prompt: _t(
"Well, nothing lasts forever, but are you sure you want to unpin this message?"
),
size: "md",
title: _t("Unpin Message"),
onConfirm: () => this.setPin(message, false),
});
}
}
export const messagePinService = {
dependencies: ["bus_service", "mail.message", "mail.store", "orm", "rpc"],
dependencies: ["bus_service", "dialog", "mail.message", "mail.store", "orm", "rpc"],
start(env, services) {
const messagePin = reactive(new MessagePin(env, services));
messagePin.setup();
@@ -1,7 +1,6 @@
/* @odoo-module */
import { Message } from "@mail/core/common/message";
import { MessageConfirmDialog } from "@mail/core/common/message_confirm_dialog";
import { useMessagePinService } from "@mail/discuss/message_pin/common/message_pin_service";
import { Component, onWillStart, onWillUpdateProps, useState, useSubEnv } from "@odoo/owl";
@@ -59,12 +58,7 @@ export class PinnedMessagesPanel extends Component {
* @param {Message} message
*/
onClickUnpin(message) {
this.env.services.dialog.add(MessageConfirmDialog, {
message,
messageComponent: Message,
prompt: _t("Are you sure you want to remove this pinned message?"),
onConfirm: () => this.messagePinService.setPin(message, false),
});
this.messagePinService.unpin(message);
}
/**