From 1e0bc5920f3990d481a3f65a6289b80c5bfbdb56 Mon Sep 17 00:00:00 2001 From: Aaron Bohy Date: Thu, 11 Feb 2016 16:07:41 +0100 Subject: [PATCH] [IMP] mail: follower: remove an RPC when user is follower When loading a form_view with the follower widgets, an RPC was performed to get the followers info (name, email...), and if the user is one of the followers, a second RPC was done to retrieve the subtypes. This rev. batches those two RPCs. Also simplifies read_subscription_data() as it is now always called with a follower_id. A few code refactoring as well. --- addons/mail/controllers/main.py | 30 +++++----- addons/mail/static/src/js/chatter.js | 77 ++++++++++++-------------- addons/mail/static/src/xml/chatter.xml | 12 ++-- 3 files changed, 54 insertions(+), 65 deletions(-) diff --git a/addons/mail/controllers/main.py b/addons/mail/controllers/main.py index 57468f57160..cb83be5bc8e 100644 --- a/addons/mail/controllers/main.py +++ b/addons/mail/controllers/main.py @@ -37,36 +37,34 @@ class MailController(http.Controller): return True @http.route('/mail/read_followers', type='json', auth='user') - def read_followers(self, follower_ids): - result = [] + def read_followers(self, follower_ids, res_model, res_id): + followers = [] is_editable = request.env.user.has_group('base.group_no_one') + partner_id = request.env.user.partner_id + follower_id = None for follower in request.env['mail.followers'].browse(follower_ids): - result.append({ + is_uid = partner_id == follower.partner_id + follower_id = follower.id if is_uid else follower_id + followers.append({ 'id': follower.id, 'name': follower.partner_id.name or follower.channel_id.name, 'email': follower.partner_id.email if follower.partner_id else None, 'res_model': 'res.partner' if follower.partner_id else 'mail.channel', 'res_id': follower.partner_id.id or follower.channel_id.id, 'is_editable': is_editable, - 'is_uid': request.env.user.partner_id == follower.partner_id, + 'is_uid': is_uid, }) - return result + return { + 'followers': followers, + 'subtypes': self.read_subscription_data(res_model, follower_id) if follower_id else None + } @http.route('/mail/read_subscription_data', type='json', auth='user') - def read_subscription_data(self, res_model, res_id, follower_id=None): + def read_subscription_data(self, res_model, follower_id): """ Computes: - message_subtype_data: data about document subtypes: which are available, which are followed if any """ - # find the document followers, update the data - followers = request.env['mail.followers'] - if not follower_id: - followers = followers.search([ - ('partner_id', '=', request.env.user.partner_id.id), - ('res_id', '=', res_id), - ('res_model', '=', res_model), - ]) - else: - followers = followers.browse(follower_id) + followers = request.env['mail.followers'].browse(follower_id) # find current model subtypes, add them to a dictionary subtypes = request.env['mail.message.subtype'].search(['&', ('hidden', '=', False), '|', ('res_model', '=', res_model), ('res_model', '=', False)]) diff --git a/addons/mail/static/src/js/chatter.js b/addons/mail/static/src/js/chatter.js index f55644ad8bc..c9dbe66863c 100644 --- a/addons/mail/static/src/js/chatter.js +++ b/addons/mail/static/src/js/chatter.js @@ -110,7 +110,8 @@ var Followers = form_common.AbstractField.extend({ on_edit_subtype: function (event) { var self = this; var $currentTarget = $(event.currentTarget); - var follower_id = $currentTarget.data('oe-id'); + var follower_id = $currentTarget.data('follower-id'); // id of model mail_follower + var res_id = $currentTarget.data('oe-id'); // id of model res_partner or mail_channel var is_channel = $currentTarget.data('oe-model') === 'mail.channel'; self.dialog = new Dialog(this, { size: 'medium', @@ -120,7 +121,7 @@ var Followers = form_common.AbstractField.extend({ text: _t("Apply"), classes: 'btn-primary', click: function () { - self.do_update_subscription(event, follower_id, is_channel); + self.do_update_subscription(event, res_id, is_channel); }, close: true }, @@ -130,7 +131,9 @@ var Followers = form_common.AbstractField.extend({ }, ], }).open(); - return self.fetch_subtypes($currentTarget.data('id'), $currentTarget.data('oe-model')); + return self.fetch_subtypes(follower_id).then(function (data) { + self.display_subtypes(data, true, is_channel); + }); }, on_invite_follower: function (channel_only) { @@ -183,16 +186,35 @@ var Followers = form_common.AbstractField.extend({ }, render_value: function () { + var self = this; this.reinit(); - return this.fetch_followers(this.value); + this.value = this.value || []; + return this.fetch_followers().then(function (results) { + self.display_followers(results.followers); + if (results.subtypes) { // current user is follower + self.display_subtypes(results.subtypes); + } + self.display_buttons(); + }).fail(this.display_generic.bind(this)); }, - fetch_followers: function (value_) { - this.value = value_ || []; - return ajax.jsonRpc('/mail/read_followers', 'call', {'follower_ids': this.value}) - .then(this.proxy('display_followers'), this.proxy('display_generic')) - .then(this.proxy('display_buttons')) - .then(this.proxy('fetch_subtypes')); + fetch_followers: function () { + return ajax.jsonRpc('/mail/read_followers', 'call', { + follower_ids: this.value, + res_model: this.view.model, + res_id: this.view.datarecord.id, + }); + }, + + /** Fetch subtypes of the given follower + * @param {int} [follower_id] the id of the follower + * @param {string} [follower_model] 'res.partner' or 'mail.channel' + */ + fetch_subtypes: function (follower_id) { + return ajax.jsonRpc('/mail/read_subscription_data', 'call', { + res_model: this.view.model, + follower_id: follower_id, + }); }, _format_followers: function (count){ @@ -250,41 +272,10 @@ var Followers = form_common.AbstractField.extend({ display_buttons: function () { if (this.message_is_follower) { this.$('button.o_followers_follow_button').removeClass('o_followers_notfollow').addClass('o_followers_following'); + this.$('.o_subtypes_list > .dropdown-toggle').attr('disabled', false); } else { this.$('button.o_followers_follow_button').removeClass('o_followers_following').addClass('o_followers_notfollow'); - } - }, - - /** Fetch subtypes, only if current user is follower or if follower_id is given, i.e. if - * the current user is editing the subtypes of another follower - * @param {int} [follower_id] the id of the follower - * @param {string} [follower_model] 'res.partner' or 'mail.channel' - */ - fetch_subtypes: function (follower_id, follower_model) { - var self = this; - var dialog = false; - - if (follower_id) { - dialog = true; - } else { - this.$('.o_subtypes_list ul').empty(); - if (!this.message_is_follower) { - this.$('.o_subtypes_list > .dropdown-toggle').attr('disabled', true); - return; - } else { - this.$('.o_subtypes_list > .dropdown-toggle').attr('disabled', false); - } - } - if (this.follower_id || follower_id) { - return ajax.jsonRpc('/mail/read_subscription_data', 'call', { - res_model: this.view.model, - res_id: this.view.datarecord.id, - follower_id: follower_id, - }).then(function (data) { - self.display_subtypes(data, dialog, (follower_model === 'mail.channel')); - }); - } else { - return $.Deferred().resolve(); + this.$('.o_subtypes_list > .dropdown-toggle').attr('disabled', true); } }, diff --git a/addons/mail/static/src/xml/chatter.xml b/addons/mail/static/src/xml/chatter.xml index 408e425381d..6a3bf533f12 100644 --- a/addons/mail/static/src/xml/chatter.xml +++ b/addons/mail/static/src/xml/chatter.xml @@ -80,8 +80,8 @@ -->
-
-
-
+