[IMP] mail: add group chat feature

task-2360934

closes odoo/odoo#75633

Signed-off-by: Alexandre Kühn (aku) <aku@odoo.com>
This commit is contained in:
Julien Giannone
2021-08-26 17:25:06 +00:00
committed by Sébastien Theys
parent 806a8db35b
commit 845bb6cc93
15 changed files with 520 additions and 154 deletions
@@ -34,14 +34,32 @@ function factory(dependencies) {
* @param {MouseEvent} ev
*/
async onClickInvite(ev) {
await this.env.services.rpc(({
model: 'mail.channel',
method: 'add_members',
args: [this.thread.id],
kwargs: {
partner_ids: this.selectedPartners.map(partner => partner.id),
},
}));
if (this.thread.channel_type === 'chat') {
const channelData = await this.env.services.rpc(({
model: 'mail.channel',
method: 'create_group',
kwargs: {
partners_to: [...new Set([
this.messaging.currentPartner.id,
...this.thread.members.map(member => member.id),
...this.selectedPartners.map(partner => partner.id),
])],
},
}));
const channel = this.messaging.models['mail.thread'].insert(
this.messaging.models['mail.thread'].convertData(channelData)
);
channel.open();
} else {
await this.env.services.rpc(({
model: 'mail.channel',
method: 'add_members',
args: [[this.thread.id]],
kwargs: {
partner_ids: this.selectedPartners.map(partner => partner.id),
},
}));
}
this.update({
searchTerm: "",
selectedPartners: unlinkAll(),
@@ -148,9 +166,15 @@ function factory(dependencies) {
* @returns {string}
*/
_computeInviteButtonText() {
if (!this.thread || this.thread.channel_type !== 'channel') {
if (!this.thread) {
return clear();
}
switch (this.thread.channel_type) {
case 'chat':
return this.env._t("Create group chat");
case 'group':
return this.env._t("Invite to group chat");
}
return this.env._t("Invite to Channel");
}
@@ -38,6 +38,7 @@ function factory(dependencies) {
_computeAvatarUrl() {
switch (this.channelType) {
case 'channel':
case 'group':
return `/web/image/mail.channel/${this.channelId}/image_128`;
case 'chat':
return this.channel.correspondent.avatarUrl;
@@ -64,6 +65,7 @@ function factory(dependencies) {
case 'channel':
return this.channel.message_needaction_counter;
case 'chat':
case 'group':
return this.channel.localMessageUnreadCounter;
}
}
@@ -73,9 +75,11 @@ function factory(dependencies) {
* @returns {boolean}
*/
_computeHasLeaveCommand() {
return this.channelType === 'channel' &&
return (
['channel', 'group'].includes(this.channelType) &&
!this.channel.message_needaction_counter &&
!this.channel.group_based_subscription;
!this.channel.group_based_subscription
);
}
/**
@@ -121,6 +125,7 @@ function factory(dependencies) {
switch (this.channelType) {
case 'channel':
case 'chat':
case 'group':
return true;
}
}
@@ -144,10 +149,13 @@ function factory(dependencies) {
*/
async onClickCommandLeave(ev) {
ev.stopPropagation();
if (this.channel.creator === this.messaging.currentUser) {
if (this.channel.channel_type !== 'group' && this.channel.creator === this.messaging.currentUser) {
await this._askAdminConfirmation();
}
this.channel.unsubscribe();
if (this.channel.channel_type === 'group') {
await this._askLeaveGroupConfirmation();
}
this.channel.leave();
}
/**
@@ -201,6 +209,33 @@ function factory(dependencies) {
);
});
}
/**
* @private
* @returns {Promise}
*/
_askLeaveGroupConfirmation() {
return new Promise(resolve => {
Dialog.confirm(this,
this.env._t("You are about to leave this group conversation and will no longer have access to it unless you are invited again. Are you sure you want to continue?"),
{
buttons: [
{
text: this.env._t("Leave"),
classes: 'btn-primary',
close: true,
click: resolve
},
{
text: this.env._t("Discard"),
close: true
}
]
}
);
});
}
}
DiscussSidebarCategoryItem.fields = {
@@ -241,7 +241,7 @@ function factory(dependencies) {
newItemPlaceholderText: this.env._t("Find or start a conversation..."),
serverStateKey: 'is_discuss_sidebar_category_chat_open',
sortComputeMethod: 'last_action',
supportedChannelTypes: ['chat'],
supportedChannelTypes: ['chat', 'group'],
}),
});
}
@@ -1210,6 +1210,9 @@ function factory(dependencies) {
if (this.channel_type === 'chat' && this.correspondent) {
return this.custom_channel_name || this.correspondent.nameOrDisplayName;
}
if (this.channel_type === 'group') {
return this.name || this.members.map(partner => partner.nameOrDisplayName).join(', ');
}
return this.name;
}
@@ -1226,10 +1229,7 @@ function factory(dependencies) {
* @returns {boolean}
*/
_computeHasInviteFeature() {
if (this.model !== 'mail.channel') {
return false;
}
return this.channel_type === 'channel';
return this.model === 'mail.channel';
}
/**
@@ -1248,7 +1248,7 @@ function factory(dependencies) {
* @returns {boolean}
*/
_computeIsChannelDescriptionChangeable() {
return this.model === 'mail.channel' && this.channel_type === 'channel';
return this.model === 'mail.channel' && ['channel', 'group'].includes(this.channel_type);
}
/**
@@ -1258,7 +1258,7 @@ function factory(dependencies) {
_computeIsChannelRenamable() {
return (
this.model === 'mail.channel' &&
['chat', 'channel'].includes(this.channel_type)
['chat', 'channel', 'group'].includes(this.channel_type)
);
}
@@ -1267,7 +1267,7 @@ function factory(dependencies) {
* @returns {boolean}
*/
_computeHasMemberListFeature() {
return this.model === 'mail.channel' && ['channel'].includes(this.channel_type);
return this.model === 'mail.channel' && ['channel', 'group'].includes(this.channel_type);
}
/**
@@ -294,6 +294,9 @@ function factory(dependencies) {
if (newName && this.thread.channel_type === 'channel' && newName !== this.thread.name) {
this.thread.rename(newName);
}
if (this.thread.channel_type === 'group' && newName !== this.thread.name) {
this.thread.rename(newName);
}
}
/**
@@ -305,7 +308,7 @@ function factory(dependencies) {
isEditingThreadDescription: false,
pendingThreadDescription: clear(),
});
if (this.thread.channel_type === 'channel' && newDescription !== this.thread.description) {
if (newDescription !== this.thread.description) {
this.thread.changeDescription(newDescription);
}
}