summaryrefslogtreecommitdiff
path: root/addons/mail/static/src
diff options
context:
space:
mode:
authorGéry Debongnie <ged@odoo.com>2017-03-20 15:23:19 +0100
committerAaron Bohy <aab@odoo.com>2017-04-11 19:44:38 +0200
commit2106e3dd2ecd61096d7c7531c2f18b00fb30aeea (patch)
tree1e41a2e698ba993d9d74b6fddd699adb15642001 /addons/mail/static/src
parent749d10e8b55dce94828290d7e4d6b2b6f741e9bd (diff)
[REF] web, *: simplify our RPC system (in JS)
The RPC system was not completely satisfactory, we decided to prepare the future and do it properly. This commit introduces the new rpc system, which replace the previous new one. We now simply have a method, this._rpc, which takes a dictionary of parameters. The idea is that depending on the parameters, it is able to add correct default value when necessary. For situations where we don't have the this._rpc method, we can use the rpc.query method, which takes the same arguments, but directly calls ajax.rpc instead of triggering up some events.
Diffstat (limited to 'addons/mail/static/src')
-rw-r--r--addons/mail/static/src/js/activity.js24
-rw-r--r--addons/mail/static/src/js/chat_manager.js230
-rw-r--r--addons/mail/static/src/js/chatter.js4
-rw-r--r--addons/mail/static/src/js/chatter_composer.js18
-rw-r--r--addons/mail/static/src/js/client_action.js18
-rw-r--r--addons/mail/static/src/js/composer.js4
-rw-r--r--addons/mail/static/src/js/followers.js42
7 files changed, 208 insertions, 132 deletions
diff --git a/addons/mail/static/src/js/activity.js b/addons/mail/static/src/js/activity.js
index a61aea9a982..2ee334551aa 100644
--- a/addons/mail/static/src/js/activity.js
+++ b/addons/mail/static/src/js/activity.js
@@ -52,18 +52,22 @@ var AbstractActivityField = AbstractField.extend({
// private
_markActivityDone: function (id) {
- return this._rpc('mail.activity', 'action_done')
- .args([[id]])
- .exec();
+ return this._rpc({
+ model: 'mail.activity',
+ method: 'action_done',
+ args: [[id]],
+ });
},
_readActivities: function () {
var self = this;
var missing_ids = _.difference(this.value.res_ids, _.pluck(this.activities, 'id'));
var fetch_def;
if (missing_ids.length) {
- fetch_def = this._rpc('mail.activity', 'read')
- .args([missing_ids])
- .exec();
+ fetch_def = this._rpc({
+ model: 'mail.activity',
+ method: 'read',
+ args: [missing_ids],
+ });
}
return $.when(fetch_def).then(function (results) {
// filter out activities that are no longer linked to this record
@@ -168,9 +172,11 @@ var Activity = AbstractActivityField.extend({
_onUnlinkActivity: function (event) {
event.preventDefault();
var activity_id = $(event.currentTarget).data('activity-id');
- return this._rpc('mail.activity', 'unlink')
- .args([[activity_id]])
- .exec()
+ return this._rpc({
+ model: 'mail.activity',
+ method: 'unlink',
+ args: [[activity_id]],
+ })
.then(this._reload.bind(this, {activity: true}));
},
_onMarkActivityDone: function (event) {
diff --git a/addons/mail/static/src/js/chat_manager.js b/addons/mail/static/src/js/chat_manager.js
index 9c60851cc22..3181f940e6e 100644
--- a/addons/mail/static/src/js/chat_manager.js
+++ b/addons/mail/static/src/js/chat_manager.js
@@ -611,9 +611,13 @@ var ChatManager = Class.extend(Mixins.EventDispatcherMixin, ServicesMixin, {
bus.on('notification', null, on_notification);
this.channel_seen = _.throttle(function (channel) {
- return self._rpc('mail.channel', 'channel_seen')
- .args([[channel.id]])
- .exec({options: {shadow: true}});
+ return self._rpc({
+ model: 'mail.channel',
+ method: 'channel_seen',
+ args: [[channel.id]],
+ }, {
+ shadow: true
+ });
}, 3000);
},
@@ -676,10 +680,12 @@ var ChatManager = Class.extend(Mixins.EventDispatcherMixin, ServicesMixin, {
domain = [['id', '<', min_message_id]].concat(domain);
}
- return this._rpc('mail.message', 'message_fetch')
- .args([domain])
- .kwargs({limit: LIMIT, context: session.user_context})
- .exec()
+ return this._rpc({
+ model: 'mail.message',
+ method: 'message_fetch',
+ args: [domain],
+ kwargs: {limit: LIMIT, context: session.user_context},
+ })
.then(function (msgs) {
if (!cache.all_history_loaded) {
cache.all_history_loaded = msgs.length < LIMIT;
@@ -704,10 +710,12 @@ var ChatManager = Class.extend(Mixins.EventDispatcherMixin, ServicesMixin, {
if (options.force_fetch || _.difference(ids.slice(0, LIMIT), loaded_msg_ids).length) {
var ids_to_load = _.difference(ids, loaded_msg_ids).slice(0, LIMIT);
- return this._rpc('mail.message', 'message_format')
- .args([ids_to_load])
- .kwargs({context: session.user_context})
- .exec()
+ return this._rpc({
+ model: 'mail.message',
+ method: 'message_format',
+ args: [ids_to_load],
+ context: session.user_context,
+ })
.then(function (msgs) {
var processed_msgs = [];
_.each(msgs, function (msg) {
@@ -743,15 +751,17 @@ var ChatManager = Class.extend(Mixins.EventDispatcherMixin, ServicesMixin, {
}
if ('channel_id' in options) {
// post a message in a channel or execute a command
- return this._rpc('mail.channel', data.command ? 'execute_command' : 'message_post')
- .args([options.channel_id])
- .kwargs(_.extend(msg, {
- message_type: 'comment',
- content_subtype: 'html',
- subtype: 'mail.mt_comment',
- command: data.command,
- }))
- .exec();
+ return this._rpc({
+ model: 'mail.channel',
+ method: data.command ? 'execute_command' : 'message_post',
+ args: [options.channel_id],
+ kwargs: _.extend(msg, {
+ message_type: 'comment',
+ content_subtype: 'html',
+ subtype: 'mail.mt_comment',
+ command: data.command,
+ }),
+ });
}
if ('model' in options && 'res_id' in options) {
// post a message in a chatter
@@ -763,14 +773,18 @@ var ChatManager = Class.extend(Mixins.EventDispatcherMixin, ServicesMixin, {
subtype_id: data.subtype_id,
});
- return this._rpc(options.model, 'message_post')
- .args([options.res_id])
- .kwargs(msg)
- .exec()
+ return this._rpc({
+ model: options.model,
+ method: 'message_post',
+ args: [options.res_id],
+ kwargs: msg,
+ })
.then(function (msg_id) {
- return self._rpc('mail.message', 'message_format')
- .args([msg_id])
- .exec()
+ return self._rpc({
+ model: 'mail.message',
+ method: 'message_format',
+ args: [msg_id],
+ })
.then(function (msgs) {
msgs[0].model = options.model;
msgs[0].res_id = options.res_id;
@@ -812,24 +826,30 @@ var ChatManager = Class.extend(Mixins.EventDispatcherMixin, ServicesMixin, {
// get messages for a chatter, when it doesn't know the ids (use
// case is when using the full composer)
var domain = [['model', '=', options.model], ['res_id', '=', options.res_id]];
- this._rpc('mail.message', 'message_fetch')
- .args([domain])
- .kwargs({limit: 30})
- .exec()
+ this._rpc({
+ model: 'mail.message',
+ method: 'message_fetch',
+ args: [domain],
+ kwargs: {limit: 30},
+ })
.then(function (msgs) {
return _.map(msgs, add_message);
});
}
},
toggle_star_status: function (message_id) {
- return this._rpc('mail.message', 'toggle_message_starred')
- .args([[message_id]])
- .exec();
+ return this._rpc({
+ model: 'mail.message',
+ method: 'toggle_message_starred',
+ args: [[message_id]],
+ });
},
unstar_all: function () {
- return this._rpc('mail.message', 'unstar_all')
- .args([[]])
- .exec();
+ return this._rpc({
+ model: 'mail.message',
+ method: 'unstar_all',
+ args: [[]]
+ });
},
mark_as_read: function (message_ids) {
var ids = _.filter(message_ids, function (id) {
@@ -838,25 +858,31 @@ var ChatManager = Class.extend(Mixins.EventDispatcherMixin, ServicesMixin, {
return !message || message.is_needaction;
});
if (ids.length) {
- return this._rpc('mail.message', 'set_message_done')
- .args([ids])
- .exec();
+ return this._rpc({
+ model: 'mail.message',
+ method: 'set_message_done',
+ args: [ids],
+ });
} else {
return $.when();
}
},
mark_all_as_read: function (channel, domain) {
if ((channel.id === "channel_inbox" && needaction_counter) || (channel && channel.needaction_counter)) {
- return this._rpc('mail.message', 'mark_all_as_read')
- .kwargs({channel_ids: channel.id !== "channel_inbox" ? [channel.id] : [], domain: domain})
- .exec();
+ return this._rpc({
+ model: 'mail.message',
+ method: 'mark_all_as_read',
+ kwargs: {channel_ids: channel.id !== "channel_inbox" ? [channel.id] : [], domain: domain},
+ });
}
return $.when();
},
undo_mark_as_read: function (message_ids, channel) {
- return this._rpc('mail.message', 'mark_as_unread')
- .args([message_ids, [channel.id]])
- .exec();
+ return this._rpc({
+ model: 'mail.message',
+ method: 'mark_as_unread',
+ args: [message_ids, [channel.id]],
+ });
},
mark_channel_as_seen: function (channel) {
if (channel.unread_counter > 0 && channel.type !== 'static') {
@@ -886,9 +912,13 @@ var ChatManager = Class.extend(Mixins.EventDispatcherMixin, ServicesMixin, {
return mention_partner_suggestions;
}
if (!channel.members_deferred) {
- channel.members_deferred = this._rpc('mail.channel', "channel_fetch_listeners")
- .args([channel.uuid])
- .exec({options: {shadow: true}})
+ channel.members_deferred = this._rpc({
+ model: 'mail.channel',
+ method: 'channel_fetch_listeners',
+ args: [channel.uuid],
+ }, {
+ shadow: true
+ })
.then(function (members) {
var suggestions = [];
_.each(mention_partner_suggestions, function (partners) {
@@ -949,9 +979,13 @@ var ChatManager = Class.extend(Mixins.EventDispatcherMixin, ServicesMixin, {
},
detach_channel: function (channel) {
- return this._rpc('mail.channel', "channel_minimize")
- .args([channel.uuid, true])
- .exec({options: {shadow: true}});
+ return this._rpc({
+ model: 'mail.channel',
+ method: 'channel_minimize',
+ args: [channel.uuid, true],
+ }, {
+ shadow: true,
+ });
},
remove_chatter_messages: function (model) {
messages = _.reject(messages, function (message) {
@@ -963,9 +997,11 @@ var ChatManager = Class.extend(Mixins.EventDispatcherMixin, ServicesMixin, {
var method = type === "dm" ? "channel_get" : "channel_create";
var args = type === "dm" ? [[name]] : [name, type];
- return this._rpc('mail.channel', method)
- .args(args)
- .exec()
+ return this._rpc({
+ model: 'mail.channel',
+ method: method,
+ args: args,
+ })
.then(add_channel);
},
join_channel: function (channel_id, options) {
@@ -978,9 +1014,11 @@ var ChatManager = Class.extend(Mixins.EventDispatcherMixin, ServicesMixin, {
// channel already joined
channel_defs[channel_id] = $.when(channel);
} else {
- channel_defs[channel_id] = this._rpc('mail.channel', 'channel_join_and_get_info')
- .args([[channel_id]])
- .exec()
+ channel_defs[channel_id] = this._rpc({
+ model: 'mail.channel',
+ method: 'channel_join_and_get_info',
+ args: [[channel_id]],
+ })
.then(function (result) {
return add_channel(result, options);
});
@@ -988,9 +1026,11 @@ var ChatManager = Class.extend(Mixins.EventDispatcherMixin, ServicesMixin, {
return channel_defs[channel_id];
},
open_and_detach_dm: function (partner_id) {
- return this._rpc('mail.channel', 'channel_get_and_minimize')
- .args([[partner_id]])
- .exec()
+ return this._rpc({
+ model: 'mail.channel',
+ method: 'channel_get_and_minimize',
+ args: [[partner_id]],
+ })
.then(add_channel);
},
open_channel: function (channel) {
@@ -999,20 +1039,26 @@ var ChatManager = Class.extend(Mixins.EventDispatcherMixin, ServicesMixin, {
unsubscribe: function (channel) {
if (_.contains(['public', 'private'], channel.type)) {
- return this._rpc('mail.channel', 'action_unfollow')
- .args([[channel.id]])
- .exec();
+ return this._rpc({
+ model: 'mail.channel',
+ method: 'action_unfollow',
+ args: [[channel.id]],
+ });
} else {
- return this._rpc('mail.channel', 'channel_pin')
- .args([channel.uuid, false])
- .exec();
+ return this._rpc({
+ model: 'mail.channel',
+ method: 'channel_pin',
+ args: [channel.uuid, false],
+ });
}
},
close_chat_session: function (channel_id) {
var channel = this.get_channel(channel_id);
- this._rpc('mail.channel', "channel_fold")
- .kwargs({uuid : channel.uuid, state : "closed"})
- .exec({options: {shadow: true}});
+ this._rpc({
+ model: 'mail.channel',
+ method: 'channel_fold',
+ kwargs: {uuid : channel.uuid, state : 'closed'},
+ }, {shadow: true});
},
fold_channel: function (channel_id, folded) {
var args = {
@@ -1021,9 +1067,11 @@ var ChatManager = Class.extend(Mixins.EventDispatcherMixin, ServicesMixin, {
if (_.isBoolean(folded)) {
args.state = folded ? 'folded' : 'open';
}
- return this._rpc('mail.channel', "channel_fold")
- .kwargs(args)
- .exec({options: {shadow: true}});
+ return this._rpc({
+ model: 'mail.channel',
+ method: 'channel_fold',
+ kwargs: args,
+ }, {shadow: true});
},
/**
* Special redirection handling for given model and id
@@ -1046,9 +1094,11 @@ var ChatManager = Class.extend(Mixins.EventDispatcherMixin, ServicesMixin, {
};
if (res_model === "res.partner") {
var domain = [["partner_id", "=", res_id]];
- this._rpc('res.users', "search")
- .args([domain])
- .exec()
+ this._rpc({
+ model: 'res.users',
+ method: 'search',
+ args: [domain],
+ })
.then(function (user_ids) {
if (user_ids.length && user_ids[0] !== session.uid && dm_redirection_callback) {
self.create_channel(res_id, 'dm').then(dm_redirection_callback);
@@ -1057,9 +1107,11 @@ var ChatManager = Class.extend(Mixins.EventDispatcherMixin, ServicesMixin, {
}
});
} else {
- this._rpc(res_model, 'get_formview_id')
- .args([[res_id], session.user_context])
- .exec()
+ this._rpc({
+ model: res_model,
+ method: 'get_formview_id',
+ args: [[res_id], session.user_context],
+ })
.then(function (view_id) {
redirect_to_document(res_model, res_id, view_id);
});
@@ -1083,9 +1135,13 @@ var ChatManager = Class.extend(Mixins.EventDispatcherMixin, ServicesMixin, {
if (!channels_preview_def) {
if (missing_channels.length) {
var missing_channel_ids = _.pluck(missing_channels, 'id');
- channels_preview_def = this._rpc('mail.channel', 'channel_fetch_preview')
- .args([missing_channel_ids])
- .exec({options: {shadow: true}});
+ channels_preview_def = this._rpc({
+ model: 'mail.channel',
+ method: 'channel_fetch_preview',
+ args: [missing_channel_ids],
+ }, {
+ shadow: true,
+ });
} else {
channels_preview_def = $.when();
}
@@ -1120,9 +1176,13 @@ var ChatManager = Class.extend(Mixins.EventDispatcherMixin, ServicesMixin, {
});
if (!values.length) {
// extend the research to all users
- def = this._rpc('res.partner', 'im_search')
- .args([search_val, limit || 20])
- .exec({options: {shadow: true}});
+ def = this._rpc({
+ model: 'res.partner',
+ method: 'im_search',
+ args: [search_val, limit || 20],
+ }, {
+ shadow: true,
+ });
} else {
def = $.when(values);
}
diff --git a/addons/mail/static/src/js/chatter.js b/addons/mail/static/src/js/chatter.js
index 41733fbbe89..bff296bf8fa 100644
--- a/addons/mail/static/src/js/chatter.js
+++ b/addons/mail/static/src/js/chatter.js
@@ -243,9 +243,7 @@ var Chatter = Widget.extend(chat_mixin, {
this.suggested_partners_def = $.Deferred();
var method = 'message_get_suggested_recipients';
var args = [[this.context.default_res_id], this.context];
- this._rpc(this.record.model, method)
- .args(args)
- .exec()
+ this._rpc({model: this.record.model, method: method, args: args})
.then(function (result) {
if (!self.suggested_partners_def) {
return; // widget has been reset (e.g. we just switched to another record)
diff --git a/addons/mail/static/src/js/chatter_composer.js b/addons/mail/static/src/js/chatter_composer.js
index 8f64622e209..340c82c349f 100644
--- a/addons/mail/static/src/js/chatter_composer.js
+++ b/addons/mail/static/src/js/chatter_composer.js
@@ -124,10 +124,11 @@ var ChatterComposer = composer.BasicComposer.extend({
// have unknown names -> call message_get_partner_info_from_emails to try to find partner_id
var def;
if (names_to_find.length > 0) {
- var args = [[this.context.default_res_id], names_to_find];
- def = this._rpc(this.model, 'message_partner_info_from_emails')
- .args(args)
- .exec();
+ def = this._rpc({
+ model: this.model,
+ method: 'message_partner_info_from_emails',
+ args: [[this.context.default_res_id], names_to_find],
+ });
}
// for unknown names + incomplete partners -> open popup - cancel = remove from recipients
@@ -172,10 +173,11 @@ var ChatterComposer = composer.BasicComposer.extend({
var new_names_to_find = _.difference(names_to_find, names_to_remove);
var def;
if (new_names_to_find.length > 0) {
- var args = [[self.context.default_res_id], new_names_to_find, true];
- self._rpc(self.model, 'message_partner_info_from_emails')
- .args(args)
- .exec();
+ self._rpc({
+ model: self.model,
+ method: 'message_partner_info_from_emails',
+ args: [[self.context.default_res_id], new_names_to_find, true],
+ });
}
$.when(def).pipe(function (result) {
result = result || [];
diff --git a/addons/mail/static/src/js/client_action.js b/addons/mail/static/src/js/client_action.js
index 28acb02828d..b64f7e3490b 100644
--- a/addons/mail/static/src/js/client_action.js
+++ b/addons/mail/static/src/js/client_action.js
@@ -69,10 +69,12 @@ var PartnerInviteDialog = Dialog.extend({
var self = this;
var data = this.$input.select2('data');
if(data.length >= 1){
- return this._rpc('mail.channel', 'channel_invite')
- .args([this.channel_id])
- .kwargs({partner_ids: _.pluck(data, 'id')})
- .exec()
+ return this._rpc({
+ model: 'mail.channel',
+ method: 'channel_invite',
+ args: [this.channel_id],
+ kwargs: {partner_ids: _.pluck(data, 'id')},
+ })
.then(function(){
var names = _.escape(_.pluck(data, 'text').join(', '));
var notification = _.str.sprintf(_t('You added <b>%s</b> to the conversation.'), names);
@@ -381,9 +383,11 @@ var ChatAction = Widget.extend(ControlPanelMixin, {
},
do_search_channel: function(search_val){
- return this._rpc('mail.channel', 'channel_search_to_join')
- .args([search_val])
- .exec()
+ return this._rpc({
+ model: 'mail.channel',
+ method: 'channel_search_to_join',
+ args: [search_val]
+ })
.then(function(result){
var values = [];
_.each(result, function(channel){
diff --git a/addons/mail/static/src/js/composer.js b/addons/mail/static/src/js/composer.js
index 0471f6e8a5b..88053c22447 100644
--- a/addons/mail/static/src/js/composer.js
+++ b/addons/mail/static/src/js/composer.js
@@ -666,9 +666,7 @@ var BasicComposer = Widget.extend(chat_mixin, {
var def = $.Deferred();
clearTimeout(this.mention_fetch_timer);
this.mention_fetch_timer = setTimeout(function () {
- return self._rpc(model, method)
- .kwargs(kwargs)
- .exec()
+ return self._rpc({model: model, method: method, kwargs: kwargs})
.then(function (results) {
def.resolve(results);
});
diff --git a/addons/mail/static/src/js/followers.js b/addons/mail/static/src/js/followers.js
index 7c4529b203e..f0fc21dab26 100644
--- a/addons/mail/static/src/js/followers.js
+++ b/addons/mail/static/src/js/followers.js
@@ -195,9 +195,10 @@ var Followers = AbstractField.extend({
var missing_ids = _.difference(this.value.res_ids, _.pluck(this.followers, 'id'));
var def;
if (missing_ids.length) {
- def = this._rpc('/mail/read_followers')
- .params({ follower_ids: missing_ids, res_model: this.model })
- .exec();
+ def = this._rpc({
+ route: '/mail/read_followers',
+ params: { follower_ids: missing_ids, res_model: this.model }
+ });
}
return $.when(def).then(function (results) {
if (results) {
@@ -220,10 +221,12 @@ var Followers = AbstractField.extend({
partner_ids: [this.partnerID],
context: {}, // FIXME
};
- this._rpc(this.model, 'message_subscribe')
- .args([[this.res_id]])
- .kwargs(kwargs)
- .exec()
+ this._rpc({
+ model: this.model,
+ method: 'message_subscribe',
+ args: [[this.res_id]],
+ kwargs: kwargs,
+ })
.then(this._reload.bind(this));
},
/**
@@ -243,9 +246,11 @@ var Followers = AbstractField.extend({
ids.channel_ids,
{}, // FIXME
];
- self._rpc(self.model, 'message_unsubscribe')
- .args(args)
- .exec()
+ self._rpc({
+ model: self.model,
+ method: 'message_unsubscribe',
+ args: args
+ })
.then(self._reload.bind(self));
def.resolve();
},
@@ -287,10 +292,12 @@ var Followers = AbstractField.extend({
var kwargs = _.extend({}, ids);
kwargs.subtype_ids = checklist;
kwargs.context = {}; // FIXME
- this._rpc(this.model, 'message_subscribe')
- .args([[this.res_id]])
- .kwargs(kwargs)
- .exec()
+ this._rpc({
+ model: this.model,
+ method: 'message_subscribe',
+ args: [[this.res_id]],
+ kwargs: kwargs,
+ })
.then(this._reload.bind(this));
}
},
@@ -308,9 +315,10 @@ var Followers = AbstractField.extend({
var self = this;
var $currentTarget = $(event.currentTarget);
var follower_id = $currentTarget.data('follower-id'); // id of model mail_follower
- return this._rpc('/mail/read_subscription_data')
- .params({res_model: this.model, follower_id: follower_id})
- .exec()
+ return this._rpc({
+ route: '/mail/read_subscription_data',
+ params: {res_model: this.model, follower_id: follower_id},
+ })
.then(function (data) {
var res_id = $currentTarget.data('oe-id'); // id of model res_partner or mail_channel
var is_channel = $currentTarget.data('oe-model') === 'mail.channel';