diff --git a/addons/website_mail_channel/controllers/main.py b/addons/website_mail_channel/controllers/main.py index dd7de5fce26..37dd899b3fc 100644 --- a/addons/website_mail_channel/controllers/main.py +++ b/addons/website_mail_channel/controllers/main.py @@ -94,6 +94,9 @@ class MailGroup(http.Controller): """ unsubscribe = subscription == 'on' channel = request.env['mail.channel'].browse(int(channel_id)) + if not channel.exists(): + return False + partner_ids = [] # search partner_id diff --git a/addons/website_mail_channel/static/src/snippets/s_channel/options.js b/addons/website_mail_channel/static/src/snippets/s_channel/options.js index b6d89d8928f..858ba8715cd 100644 --- a/addons/website_mail_channel/static/src/snippets/s_channel/options.js +++ b/addons/website_mail_channel/static/src/snippets/s_channel/options.js @@ -8,12 +8,33 @@ var wUtils = require('website.utils'); var _t = core._t; options.registry.Channel = options.Class.extend({ + /** + * @override + */ + async start() { + await this._super(...arguments); + this.publicChannels = await this._getPublicChannels(); + }, /** * @override */ cleanForSave: function () { this.$target.addClass('d-none'); }, + /** + * If we have already created channels => select the first one + * else => modal prompt (create a new channel) + * + * @override + */ + onBuilt() { + if (this.publicChannels.length) { + this.$target[0].dataset.id = this.publicChannels[0][0]; + } else { + const widget = this._requestUserValueWidgets('create_mail_channel_opt')[0]; + widget.$el.click(); + } + }, //-------------------------------------------------------------------------- // Options @@ -56,19 +77,25 @@ options.registry.Channel = options.Class.extend({ /** * @override */ - _renderCustomXML: function (uiFragment) { + async _renderCustomXML(uiFragment) { + const channels = await this._getPublicChannels(); + const menuEl = uiFragment.querySelector('.select_discussion_list'); + for (const channel of channels) { + const el = document.createElement('we-button'); + el.dataset.selectDataAttribute = channel[0]; + el.textContent = channel[1]; + menuEl.appendChild(el); + } + }, + /** + * @private + * @return {Promise} + */ + _getPublicChannels() { return this._rpc({ model: 'mail.channel', method: 'name_search', args: ['', [['public', '=', 'public']]], - }).then(channels => { - const menuEl = uiFragment.querySelector('.select_discussion_list'); - for (const channel of channels) { - const el = document.createElement('we-button'); - el.dataset.selectDataAttribute = channel[0]; - el.textContent = channel[1]; - menuEl.appendChild(el); - } }); }, }); diff --git a/addons/website_mail_channel/views/snippets/s_channel.xml b/addons/website_mail_channel/views/snippets/s_channel.xml index bd38a8d1aad..78217040d6c 100644 --- a/addons/website_mail_channel/views/snippets/s_channel.xml +++ b/addons/website_mail_channel/views/snippets/s_channel.xml @@ -32,7 +32,7 @@ + data-create-channel="" data-no-preview="true" data-name="create_mail_channel_opt"/>