[IMP] mail,*: remove server data key in thread insert

*: im_livechat, website_livechat.

Before this commit, the `thread.insert` method received an arbitrary
`serverData` key. This was cumbersome since we needed to create this
key manually at each insertion.Moreover some attributes where coming from
dedicated keys, some from the  serverData key, some from both which led
to a bug-prone, hard to maintain code.

This commit removes the `serverData` key in order to solve those
issues.

closes odoo/odoo#121117

Signed-off-by: Sébastien Theys (seb) <seb@odoo.com>
This commit is contained in:
tsm-odoo
2023-05-12 19:55:05 +02:00
parent 3fb24aa05e
commit b07085ef21
9 changed files with 29 additions and 54 deletions
@@ -10,8 +10,8 @@ patch(ThreadService.prototype, "im_livechat", {
const isUnknown = !(createLocalId(data.model, data.id) in this.store.threads);
const thread = this._super(data);
if (thread.type === "livechat") {
if (data.serverData?.channel) {
assignDefined(thread, data.serverData.channel, ["anonymous_name"]);
if (data?.channel) {
assignDefined(thread, data.channel, ["anonymous_name"]);
}
if (isUnknown) {
this.store.discuss.livechat.threads.push(thread.localId);
@@ -221,9 +221,7 @@ export class Messaging {
...channel,
model: "discuss.channel",
rtcSessions: undefined,
serverData: {
channel: channel.channel,
},
channel: channel.channel,
type: channel.channel.channel_type,
});
const rtcSessions = channel.rtcSessions;
@@ -243,8 +241,8 @@ export class Messaging {
this.threadService.insert({
id: notif.payload.channel.id,
model: "discuss.channel",
serverData: notif.payload,
type: notif.payload.channel.channel_type,
...notif.payload,
});
break;
case "discuss.channel/transient_message": {
@@ -459,7 +457,7 @@ export class Messaging {
const { id, last_interest_dt } = notif.payload;
const channel = this.store.threads[createLocalId("discuss.channel", id)];
if (channel) {
this.threadService.update(channel, { serverData: { last_interest_dt } });
this.threadService.update(channel, { last_interest_dt });
}
if (["chat", "group"].includes(channel?.type)) {
this.threadService.sortChannels();
@@ -472,10 +470,9 @@ export class Messaging {
if (!channel || !channel.type) {
const [channelData] = await this.orm.call("discuss.channel", "channel_info", [id]);
channel = this.threadService.insert({
id: channelData.id,
model: "discuss.channel",
type: channelData.channel.channel_type,
serverData: channelData,
...channelData,
});
}
if (!channel.is_pinned) {
@@ -544,23 +541,14 @@ export class Messaging {
_handleNotificationRecordInsert(notif) {
if (notif.payload.Thread) {
this.threadService.insert({
id: notif.payload.Thread.id,
model: notif.payload.Thread.model,
serverData: notif.payload.Thread,
});
this.threadService.insert(notif.payload.Thread);
}
if (notif.payload.Channel) {
this.threadService.insert({
id: notif.payload.Channel.id,
model: "discuss.channel",
serverData: {
channel: {
avatarCacheKey: notif.payload.Channel.avatarCacheKey,
...notif.payload.Channel,
},
},
channel: notif.payload.Channel,
});
}
if (notif.payload.RtcSession) {
@@ -133,6 +133,7 @@ export class Thread {
_store: store,
});
store.threads[this.localId] = this;
return store.threads[this.localId];
}
get accessRestrictedToGroupText() {
+15 -21
View File
@@ -58,21 +58,13 @@ export class ThreadService {
* @returns {Thread}
*/
createChannelThread(serverData) {
const { id, name, description, channel, uuid, authorizedGroupFullName } = serverData;
const type = channel.channel_type;
const channelType = serverData.channel.channel_type;
const isAdmin =
channelType !== "group" && serverData.create_uid === this.store.user?.user?.id;
const thread = this.insert({
id,
...serverData,
model: "discuss.channel",
name,
type,
description,
serverData: serverData,
isAdmin,
uuid,
authorizedGroupFullName,
type: serverData.channel.channel_type,
isAdmin:
serverData.channel.channel_type !== "group" &&
serverData.create_uid === this.store.user?.user?.id,
});
return thread;
}
@@ -582,7 +574,7 @@ export class ThreadService {
model: "discuss.channel",
name,
type: "channel",
serverData: { channel: { avatarCacheKey: "hello" } },
channel: { avatarCacheKey: "hello" },
});
this.sortChannels();
this.open(thread);
@@ -594,11 +586,9 @@ export class ThreadService {
partners_to: [id],
});
return this.insert({
id: data.id,
...data,
model: "discuss.channel",
name: undefined,
type: "chat",
serverData: data,
});
}
@@ -681,8 +671,8 @@ export class ThreadService {
* @param {Object} data
*/
update(thread, data) {
const { attachments, serverData, ...remainingData } = data;
assignDefined(thread, remainingData);
const { id, name, attachments, description, ...serverData } = data;
assignDefined(thread, { id, name, description });
if (attachments) {
// smart process to avoid triggering reactives when there is no change between the 2 arrays
replaceArrayWithCompare(
@@ -698,10 +688,15 @@ export class ThreadService {
"description",
"hasWriteAccess",
"is_pinned",
"isLoaded",
"isLoadingAttachments",
"mainAttachment",
"message_unread_counter",
"message_needaction_counter",
"name",
"seen_message_id",
"state",
"type",
"group_based_subscription",
"last_interest_dt",
"defaultDisplayMode",
@@ -845,7 +840,7 @@ export class ThreadService {
this.update(thread, data);
return thread;
}
let thread = new Thread(this.store, data);
const thread = new Thread(this.store, data);
onChange(thread, "isLoaded", () => thread.isLoadedDeferred.resolve());
onChange(thread, "channelMembers", () => this.store.updateBusSubscription());
onChange(thread, "is_pinned", () => {
@@ -853,7 +848,6 @@ export class ThreadService {
this.store.discuss.threadLocalId = null;
}
});
thread = this.store.threads[thread.localId] = thread;
this.update(thread, data);
this.insertComposer({ thread });
return thread;
@@ -54,7 +54,7 @@ export class DiscussPublic extends Component {
id: this.props.data.channelData.id,
model: "discuss.channel",
type: this.props.data.channelData.channel.channel_type,
serverData: { uuid: this.props.data.channelData.uuid },
uuid: this.props.data.channelData.uuid,
});
}
}
@@ -107,9 +107,7 @@ export class CallParticipantCard extends Component {
member_ids: [this.channelMember.id],
});
this.threadService.update(this.props.thread, {
serverData: {
invitedMembers: channelData.invitedMembers,
},
invitedMembers: channelData.invitedMembers,
});
}
+1 -6
View File
@@ -738,12 +738,7 @@ export class Rtc {
this.clear();
this.state.logs.clear();
this.state.channel = channel;
this.threadService.update(this.state.channel, {
serverData: {
rtcSessions,
invitedMembers,
},
});
this.threadService.update(this.state.channel, { rtcSessions, invitedMembers });
this.state.selfSession = this.store.rtcSessions[sessionId];
this.state.iceServers = iceServers || DEFAULT_ICE_SERVERS;
this.state.logs.set("channelId", this.state.channel?.id);
@@ -7,9 +7,9 @@ import { patch } from "@web/core/utils/patch";
patch(ThreadService.prototype, "website_livechat", {
update(thread, data) {
this._super(thread, data);
if (data.serverData?.visitor) {
if (data?.visitor) {
thread.visitor = this.personaService.insert({
...data.serverData.visitor,
...data.visitor,
type: "visitor",
});
}
@@ -17,7 +17,6 @@ export const websiteLivechatNotifications = {
...payload,
id: payload.id,
model: "discuss.channel",
serverData: payload,
type: payload.channel.channel_type,
});
const chatWindow = chatWindowService.insert({ thread: channel });