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

wip message unread counter from server #162081

Closed
Closed
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
8 changes: 7 additions & 1 deletion addons/mail/models/discuss/discuss_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1065,7 +1065,13 @@ def _set_last_seen_message(self, last_message, allow_older=False, notify=True):
})
if notify:
data = {
'channel_id': self.id,
'channel': {
'id': self.id,
'model': 'discuss.channel',
'message_unread_counter': member.message_unread_counter,
# sudo - bus.bus: reading non sensitive last id
'message_unread_counter_bus_id': self.env["bus.bus"].sudo()._bus_last_id()
},
'id': member.id,
'last_message_id': last_message.id,
}
Expand Down
19 changes: 5 additions & 14 deletions addons/mail/static/src/core/common/thread_model.js
Original file line number Diff line number Diff line change
Expand Up @@ -853,15 +853,11 @@ export class Thread extends Record {
rpc("/discuss/channel/set_last_seen_message", {
channel_id: this.id,
last_message_id: newestPersistentMessage.id,
})
.then(() => {
this.updateSeen(newestPersistentMessage);
})
.catch((e) => {
if (e.code !== 404) {
throw e;
}
});
}).catch((e) => {
if (e.code !== 404) {
throw e;
}
});
} else if (newestPersistentMessage) {
this.updateSeen();
}
Expand Down Expand Up @@ -1112,21 +1108,16 @@ export class Thread extends Record {
updateSeen(lastSeen = this.newestPersistentOfAllMessage) {
const lastReadIndex = this.messages.findIndex((message) => message.eq(lastSeen));
let newNeedactionCounter = 0;
let newUnreadCounter = 0;
for (const message of this.messages.slice(lastReadIndex + 1)) {
if (message.isNeedaction) {
newNeedactionCounter++;
}
if (Number.isInteger(message.id)) {
newUnreadCounter++;
}
}
if (this.selfMember) {
this.selfMember.seen_message_id = lastSeen;
}
Object.assign(this, {
message_needaction_counter: newNeedactionCounter,
message_unread_counter: newUnreadCounter,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ export class DiscussCoreCommon {
});
});
this.busService.subscribe("discuss.channel.member/seen", (payload) => {
const { channel_id, guest_id, id, last_message_id, partner_id } = payload;
const { channel, guest_id, id, last_message_id, partner_id } = payload;
const member = this.store.ChannelMember.insert({
id,
seen_message_id: last_message_id ? { id: last_message_id } : null,
persona: { type: partner_id ? "partner" : "guest", id: partner_id ?? guest_id },
thread: { id: channel_id, model: "discuss.channel" },
thread: this.store.Thread.insert(channel),
});
if (member?.persona.eq(this.store.self)) {
member.thread.updateSeen(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,12 @@ patch(MockServer.prototype, {
target = channel;
}
this.pyEnv["bus.bus"]._sendone(target, "discuss.channel.member/seen", {
channel_id: channel.id,
channel: {
id: channel.id,
model: "discuss.channel",
message_unread_counter: memberOfCurrentUser.message_unread_counter,
message_unread_counter_bus_id: this.lastBusNotificationId,
},
id: memberOfCurrentUser?.id,
last_message_id: message_id,
[guest ? "guest_id" : "partner_id"]: guest?.id ?? partner.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ test("'channel_seen' notification received is correctly handled", async () => {
["channel_id", "=", channelId],
["partner_id", "=", partnerId],
])[0],
channel_id: channelId,
channel: { id: channelId, model: "discuss.channel" },
last_message_id: 100,
partner_id: partnerId,
});
Expand Down Expand Up @@ -283,7 +283,7 @@ test("'channel_fetch' notification then 'channel_seen' received are correctly ha
["channel_id", "=", channelId],
["partner_id", "=", partnerId],
])[0],
channel_id: channelId,
channel: { id: channelId, model: "discuss.channel" },
last_message_id: 100,
partner_id: partnerId,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -996,21 +996,34 @@ export class DiscussChannel extends models.ServerModel {
const ResPartner = this.env["res.partner"];

const memberOfCurrentUser = this._find_or_create_member_for_self(ids[0]);
const [channel] = this.search_read([["id", "in", ids]]);
const messages = this.env["mail.message"]._filter([
["model", "=", "discuss.channel"],
["res_id", "=", channel.id],
]);
const message_unread_counter = !message_id
? messages.length
: messages.filter(({ id }) => id > message_id).length;
if (memberOfCurrentUser) {
DiscussChannelMember.write([memberOfCurrentUser.id], {
fetched_message_id: message_id,
message_unread_counter,
seen_message_id: message_id,
});
}
if (notify) {
const [channel] = this.search_read([["id", "in", ids]]);
const [partner, guest] = ResPartner._get_current_persona();
let target = guest ?? partner;
if (this._types_allowing_seen_infos().includes(channel.channel_type)) {
target = channel;
}
BusBus._sendone(target, "discuss.channel.member/seen", {
channel_id: channel.id,
channel: {
id: channel.id,
message_unread_counter,
message_unread_counter_bus_id: this.lastBusNotificationId,
model: "discuss.channel",
},
id: memberOfCurrentUser?.id,
last_message_id: message_id,
[guest ? "guest_id" : "partner_id"]: guest?.id ?? partner?.id,
Expand Down
8 changes: 7 additions & 1 deletion addons/mail/tests/discuss/test_discuss_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ def test_channel_info_seen(self):
@users('employee')
def test_set_last_seen_message_should_send_notification_only_once(self):
chat = self.env['discuss.channel'].with_user(self.user_admin).channel_get((self.partner_employee | self.user_admin.partner_id).ids)
member = self.env['discuss.channel.member'].search([('partner_id', '=', self.user_admin.partner_id.id), ('channel_id', '=', chat.id)])
msg_1 = self._add_messages(chat, 'Body1', author=self.user_employee.partner_id)

self.env['bus.bus'].sudo().search([]).unlink()
Expand All @@ -213,7 +214,12 @@ def test_set_last_seen_message_should_send_notification_only_once(self):
[{
"type": "discuss.channel.member/seen",
"payload": {
'channel_id': chat.id,
'channel': {
'id': chat.id,
'model': 'discuss.channel',
'message_unread_counter': 0,
'message_unread_counter_bus_id': self.env["bus.bus"].sudo()._bus_last_id(),
},
'id': chat.channel_member_ids.filtered(lambda m: m.partner_id == self.user_admin.partner_id).id,
'last_message_id': msg_1.id,
'partner_id': self.user_admin.partner_id.id,
Expand Down