[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.
This commit is contained in:
@@ -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)])
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
@@ -80,8 +80,8 @@
|
||||
-->
|
||||
<t t-name="mail.Followers">
|
||||
<div class="o_followers">
|
||||
<div t-attf-class="o_followers_title_box">
|
||||
<button t-attf-class="btn btn-sm btn-default dropdown-toggle"
|
||||
<div class="o_followers_title_box">
|
||||
<button class="btn btn-sm btn-default dropdown-toggle"
|
||||
data-toggle="dropdown" aria-expanded="false">
|
||||
<span class="o_followers_count"/>
|
||||
<span class="caret"/>
|
||||
@@ -89,14 +89,14 @@
|
||||
<ul class="dropdown-menu o_followers_list" role="menu"></ul>
|
||||
</div>
|
||||
<div class="o_followers_actions">
|
||||
<div t-attf-class="btn-group btn-group-sm o_subtypes_list">
|
||||
<div class="btn-group btn-group-sm o_subtypes_list">
|
||||
<button class="btn btn-sm btn-primary o_followers_follow_button o_followers_notfollow">
|
||||
<span class="o_follow">Follow</span>
|
||||
<span class="fa fa-times o_followers_unfollow"/>
|
||||
<span class="o_followers_unfollow"> Unfollow</span>
|
||||
<span class="fa fa-check o_followers_following"/>
|
||||
<span class="o_followers_following"> Following</span>
|
||||
</button><button t-attf-class="btn btn-sm btn-primary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
|
||||
</button><button class="btn btn-sm btn-primary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
|
||||
<span class="caret"/>
|
||||
</button>
|
||||
<ul class="dropdown-menu" role="menu"/>
|
||||
@@ -113,13 +113,13 @@
|
||||
<li class='o_partner'>
|
||||
<img t-att-src="record.avatar_url"/>
|
||||
<a class="o_mail_redirect"
|
||||
t-attf-href="#"
|
||||
href="#"
|
||||
t-att-title="record.name"
|
||||
t-att-data-oe-model="record.res_model"
|
||||
t-att-data-oe-id="record.res_id"><t t-esc="record.name"/></a>
|
||||
<i t-if="record.is_editable and (widget.records_length > 1)"
|
||||
class="fa fa-pencil o_edit_subtype hide" title="Edit subscription"
|
||||
t-att-data-id="record.id"
|
||||
t-att-data-follower-id="record.id"
|
||||
t-att-data-oe-id="record.res_id"
|
||||
t-att-data-oe-model="record.res_model"/>
|
||||
<i t-if="widget.view_is_editable" class="fa fa-remove o_remove_follower"
|
||||
|
||||
Reference in New Issue
Block a user