Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMP] mail: clean up starredPersonas #159466

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion addons/im_livechat/tests/test_message.py
Expand Up @@ -107,7 +107,7 @@ def test_message_format(self):
'res_id': channel_livechat_1.id,
'scheduledDatetime': False,
'sms_ids': [],
'starredPersonas': [],
'isStarred': False,
'subject': False,
'subtype_description': False,
'trackingValues': [],
Expand Down
6 changes: 3 additions & 3 deletions addons/mail/models/mail_message.py
Expand Up @@ -825,7 +825,7 @@ def unstar_all(self):
partner.starred_message_ids -= starred_messages
self.env['bus.bus']._sendone(partner, 'mail.message/toggle_star', {
'message_ids': starred_messages.ids,
'starred': False,
'isStarred': False,
})

def toggle_message_starred(self):
Expand All @@ -843,7 +843,7 @@ def toggle_message_starred(self):

self.env['bus.bus']._sendone(partner, 'mail.message/toggle_star', {
'message_ids': [self.id],
'starred': starred,
'isStarred': starred,
})

def _message_reaction(self, content, action):
Expand Down Expand Up @@ -1028,7 +1028,7 @@ def _message_format(self, format_reply=True, msg_vals=None, for_current_user=Fal
if record and hasattr(record, '_track_filter_for_display'):
displayed_tracking_ids = record._track_filter_for_display(displayed_tracking_ids)
vals["needaction_partner_ids"] = notifs.filtered(lambda n: not n.is_read).res_partner_id.ids
vals["starredPersonas"] = [{"id": partner_id, "type": "partner"} for partner_id in message_sudo.starred_partner_ids.ids]
vals["isStarred"] = message_sudo.starred
vals["trackingValues"] = displayed_tracking_ids._tracking_value_format()
return vals_list

Expand Down
Expand Up @@ -36,21 +36,19 @@ export class MailCoreCommon {
this.store.Message.insert(payload.elements, { html: true });
});
this.busService.subscribe("mail.message/toggle_star", (payload, { id: notifId }) => {
const { message_ids: messageIds, starred } = payload;
const { message_ids: messageIds, isStarred } = payload;
for (const messageId of messageIds) {
const message = this.store.Message.insert({ id: messageId });
const message = this.store.Message.insert({ id: messageId, isStarred });
const starredBox = this.store.discuss.starred;
if (starred) {
if (isStarred) {
if (notifId > starredBox.counter_bus_id) {
starredBox.counter++;
}
message.starredPersonas.add(this.store.self);
starredBox.messages.add(message);
} else {
if (notifId > starredBox.counter_bus_id) {
starredBox.counter--;
}
message.starredPersonas.delete(this.store.self);
starredBox.messages.delete(message);
}
}
Expand Down
7 changes: 1 addition & 6 deletions addons/mail/static/src/core/common/message_model.js
Expand Up @@ -142,7 +142,6 @@ export class Message extends Record {
threadAsNewest = Record.one("Thread");
/** @type {DateTime} */
scheduledDatetime = Record.attr(undefined, { type: "datetime" });
starredPersonas = Record.many("Persona");
onlyEmojis = Record.attr(false, {
compute() {
const div = document.createElement("div");
Expand Down Expand Up @@ -174,6 +173,7 @@ export class Message extends Record {
create_date = Record.attr(undefined, { type: "datetime" });
/** @type {luxon.DateTime} */
write_date = Record.attr(undefined, { type: "datetime" });
isStarred = false;

/**
* We exclude the milliseconds because datetime string from the server don't
Expand Down Expand Up @@ -238,10 +238,6 @@ export class Message extends Record {
eager: true,
});

get isStarred() {
return this.store.self.in(this.starredPersonas);
}

get isNeedaction() {
return (
this.store.self.type === "partner" &&
Expand Down Expand Up @@ -299,7 +295,6 @@ export class Message extends Record {
/** @this {import("models").Message} */
onUpdate() {
if (this.isEmpty && this.isStarred) {
this.starredPersonas.delete(this.store.self);
const starred = this.store.discuss.starred;
starred.counter--;
starred.messages.delete(this);
Expand Down
2 changes: 1 addition & 1 deletion addons/mail/static/tests/core/message_model.test.js
Expand Up @@ -32,7 +32,7 @@ test("Message model properties", async () => {
date: deserializeDateTime("2019-05-05 10:00:00"),
id: 4000,
needaction_partner_ids: [serverState.partnerId],
starredPersonas: { id: serverState.partnerId, type: "partner" },
isStarred: true,
model: "res.partner",
thread: { id: serverState.partnerId, model: "res.partner" },
res_id: serverState.partnerId,
Expand Down
12 changes: 5 additions & 7 deletions addons/mail/static/tests/crosstab/crosstab.test.js
Expand Up @@ -37,9 +37,10 @@ test("Messages are received cross-tab", async () => {
test("Delete starred message updates counter", async () => {
const pyEnv = await startServer();
const channelId = pyEnv["discuss.channel"].create({ name: "General" });
const messageId = pyEnv["mail.message"].create({
pyEnv["mail.message"].create({
body: "Hello World!",
model: "discuss.channel",
message_type: "comment",
res_id: channelId,
starred_partner_ids: [serverState.partnerId],
});
Expand All @@ -48,12 +49,9 @@ test("Delete starred message updates counter", async () => {
await openDiscuss(channelId, { target: env1 });
await openDiscuss(channelId, { target: env2 });
await contains("button", { target: env2, text: "Starred1" });
rpc = rpcWithEnv(env1);
rpc("/mail/message/update_content", {
message_id: messageId,
body: "",
attachment_ids: [],
});
await click(":nth-child(1 of .o-mail-Message) [title='Expand']", { target: env2 });
await click(".o-mail-Message-moreMenu [title='Delete']", { target: env2 });
await click("button", { text: "Confirm" }, { target: env2 });
await contains("button", { count: 0, target: env2, text: "Starred1" });
});

Expand Down
Expand Up @@ -455,7 +455,7 @@ patch(MockServer.prototype, {
});
this.pyEnv["bus.bus"]._sendone(this.pyEnv.currentPartner, "mail.message/toggle_star", {
message_ids: [message.id],
starred: !wasStared,
isStarred: !wasStared,
});
}
},
Expand All @@ -474,7 +474,7 @@ patch(MockServer.prototype, {
);
this.pyEnv["bus.bus"]._sendone(this.pyEnv.currentPartner, "mail.message/toggle_star", {
message_ids: messages.map((message) => message.id),
starred: false,
isStarred: false,
});
},
});
10 changes: 6 additions & 4 deletions addons/mail/static/tests/mock_server/mock_models/mail_message.js
Expand Up @@ -67,7 +67,7 @@ export class MailMessage extends models.ServerModel {
}

/** @param {number[]} ids */
_message_format(ids, for_current_user=false) {
_message_format(ids, for_current_user = false) {
/** @type {import("mock_models").IrAttachment} */
const IrAttachment = this.env["ir.attachment"];
/** @type {import("mock_models").MailGuest} */
Expand Down Expand Up @@ -208,7 +208,9 @@ export class MailMessage extends models.ServerModel {
.filter((notification) => notification.is_read)
.map((notification) => notification.res_partner_id);
response["history_partner_ids"] = historyPartnerIds;
response["starredPersonas"] = message.starred_partner_ids.map((id) => ({ id, type: "partner" }));
response["isStarred"] = message.starred_partner_ids?.includes(
this.env.user?.partner_id
);
const trackingValues = MailTrackingValue._filter([
["id", "in", message.tracking_value_ids],
]);
Expand Down Expand Up @@ -290,7 +292,7 @@ export class MailMessage extends models.ServerModel {
const [partner] = ResPartner.read(this.env.user.partner_id);
BusBus._sendone(partner, "mail.message/toggle_star", {
message_ids: [message.id],
starred: !wasStarred,
isStarred: !wasStarred,
});
}
}
Expand All @@ -309,7 +311,7 @@ export class MailMessage extends models.ServerModel {
const [partner] = ResPartner.read(this.env.user.partner_id);
BusBus._sendone(partner, "mail.message/toggle_star", {
message_ids: messages.map((message) => message.id),
starred: false,
isStarred: false,
});
}

Expand Down
12 changes: 6 additions & 6 deletions addons/test_discuss_full/tests/test_performance.py
Expand Up @@ -1412,7 +1412,7 @@ def _expected_result_for_message(self, channel):
"res_id": channel.id,
"scheduledDatetime": False,
"sms_ids": [],
"starredPersonas": [{"id": self.users[0].partner_id.id, "type": "partner"}],
"isStarred": True,
"subject": False,
"subtype_description": False,
"trackingValues": [],
Expand Down Expand Up @@ -1455,7 +1455,7 @@ def _expected_result_for_message(self, channel):
"res_id": channel.id,
"scheduledDatetime": False,
"sms_ids": [],
"starredPersonas": [],
"isStarred": False,
"subject": False,
"subtype_description": False,
"trackingValues": [],
Expand Down Expand Up @@ -1498,7 +1498,7 @@ def _expected_result_for_message(self, channel):
"res_id": channel.id,
"scheduledDatetime": False,
"sms_ids": [],
"starredPersonas": [],
"isStarred": False,
"subject": False,
"subtype_description": False,
"trackingValues": [],
Expand Down Expand Up @@ -1541,7 +1541,7 @@ def _expected_result_for_message(self, channel):
"res_id": channel.id,
"scheduledDatetime": False,
"sms_ids": [],
"starredPersonas": [],
"isStarred": False,
"subject": False,
"subtype_description": False,
"trackingValues": [],
Expand Down Expand Up @@ -1583,7 +1583,7 @@ def _expected_result_for_message(self, channel):
"res_id": channel.id,
"scheduledDatetime": False,
"sms_ids": [],
"starredPersonas": [],
"isStarred": False,
"subject": False,
"subtype_description": False,
"trackingValues": [],
Expand Down Expand Up @@ -1618,7 +1618,7 @@ def _expected_result_for_message(self, channel):
"res_id": channel.id,
"scheduledDatetime": False,
"sms_ids": [],
"starredPersonas": [],
"isStarred": False,
"subject": False,
"subtype_description": False,
"trackingValues": [],
Expand Down