You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1464 lines
60 KiB

[LINT] ************* Module mail_archives mail_archives/views/templates.xml:1: [W7908(missing-newline-extrafiles), ] Missing newline mail_archives/README.rst:1: [W7908(missing-newline-extrafiles), ] Missing newline ************* Module mail_to mail_to/static/src/js/mail_to.js:11: [W7903(javascript-lint), ] Missing semicolon. mail_to/templates.xml:1: [W7908(missing-newline-extrafiles), ] Missing newline mail_to/doc/changelog.rst:1: [W7908(missing-newline-extrafiles), ] Missing newline ************* Module mail_move_message mail_move_message/doc/changelog.rst:1: [E7901(rst-syntax-error), ] Hyperlink target "changelog" is not referenced. mail_move_message/data/mail_move_message_data.xml:1: [W7908(missing-newline-extrafiles), ] Missing newline ************* Module mail_move_message.mail_move_message_models mail_move_message/mail_move_message_models.py:74: [C8108(method-compute), Wizard] Name of compute method should starts with "_compute_" mail_move_message/mail_move_message_models.py:91: [W8104(api-one-deprecated), Wizard.get_can_move] api.one deprecated mail_move_message/mail_move_message_models.py:141: [W8104(api-one-deprecated), Wizard.check_access] api.one deprecated mail_move_message/mail_move_message_models.py:198: [W8104(api-one-deprecated), Wizard.delete] api.one deprecated mail_move_message/mail_move_message_models.py:233: [W8104(api-one-deprecated), Wizard.read_close] api.one deprecated mail_move_message/mail_move_message_models.py:248: [C8108(method-compute), MailMessage] Name of compute method should starts with "_compute_" mail_move_message/mail_move_message_models.py:251: [W8104(api-one-deprecated), MailMessage._get_all_childs] api.one deprecated mail_move_message/mail_move_message_models.py:274: [W8104(api-one-deprecated), MailMessage.move] api.one deprecated ************* Module mail_base mail_base/static/src/js/base.js:245: [W7903(javascript-lint), ] Misleading line break before '||'; readers may interpret this as an expression boundary. mail_base/static/src/js/base.js:1119: [W7903(javascript-lint), ] Missing semicolon. mail_base/static/src/js/base.js:1121: [W7903(javascript-lint), ] Missing semicolon. mail_base/views/templates.xml:1: [W7908(missing-newline-extrafiles), ] Missing newline ************* Module mail_sent mail_sent/doc/changelog.rst:1: [E7901(rst-syntax-error), ] Hyperlink target "changelog" is not referenced. mail_sent/views/templates.xml:1: [W7908(missing-newline-extrafiles), ] Missing newline ************* Module mail_sent.models mail_sent/models.py:8: [C8108(method-compute), MailMessage] Name of compute method should starts with "_compute_" mail_sent/models.py:12: [W8104(api-one-deprecated), MailMessage._get_sent] api.one deprecated ************* Module mail_reply mail_reply/templates.xml:1: [W7908(missing-newline-extrafiles), ] Missing newline ************* Module res_partner_mails_count res_partner_mails_count/README.rst:1: [W7908(missing-newline-extrafiles), ] Missing newline ************* Module res_partner_mails_count.models res_partner_mails_count/models.py:8: [C8108(method-compute), ResPartner] Name of compute method should starts with "_compute_" res_partner_mails_count/models.py:9: [C8108(method-compute), ResPartner] Name of compute method should starts with "_compute_" res_partner_mails_count/models.py:12: [W8104(api-one-deprecated), ResPartner._mails_to] api.one deprecated res_partner_mails_count/models.py:17: [W8104(api-one-deprecated), ResPartner._mails_from] api.one deprecated
8 years ago
[LINT] ************* Module mail_archives mail_archives/views/templates.xml:1: [W7908(missing-newline-extrafiles), ] Missing newline mail_archives/README.rst:1: [W7908(missing-newline-extrafiles), ] Missing newline ************* Module mail_to mail_to/static/src/js/mail_to.js:11: [W7903(javascript-lint), ] Missing semicolon. mail_to/templates.xml:1: [W7908(missing-newline-extrafiles), ] Missing newline mail_to/doc/changelog.rst:1: [W7908(missing-newline-extrafiles), ] Missing newline ************* Module mail_move_message mail_move_message/doc/changelog.rst:1: [E7901(rst-syntax-error), ] Hyperlink target "changelog" is not referenced. mail_move_message/data/mail_move_message_data.xml:1: [W7908(missing-newline-extrafiles), ] Missing newline ************* Module mail_move_message.mail_move_message_models mail_move_message/mail_move_message_models.py:74: [C8108(method-compute), Wizard] Name of compute method should starts with "_compute_" mail_move_message/mail_move_message_models.py:91: [W8104(api-one-deprecated), Wizard.get_can_move] api.one deprecated mail_move_message/mail_move_message_models.py:141: [W8104(api-one-deprecated), Wizard.check_access] api.one deprecated mail_move_message/mail_move_message_models.py:198: [W8104(api-one-deprecated), Wizard.delete] api.one deprecated mail_move_message/mail_move_message_models.py:233: [W8104(api-one-deprecated), Wizard.read_close] api.one deprecated mail_move_message/mail_move_message_models.py:248: [C8108(method-compute), MailMessage] Name of compute method should starts with "_compute_" mail_move_message/mail_move_message_models.py:251: [W8104(api-one-deprecated), MailMessage._get_all_childs] api.one deprecated mail_move_message/mail_move_message_models.py:274: [W8104(api-one-deprecated), MailMessage.move] api.one deprecated ************* Module mail_base mail_base/static/src/js/base.js:245: [W7903(javascript-lint), ] Misleading line break before '||'; readers may interpret this as an expression boundary. mail_base/static/src/js/base.js:1119: [W7903(javascript-lint), ] Missing semicolon. mail_base/static/src/js/base.js:1121: [W7903(javascript-lint), ] Missing semicolon. mail_base/views/templates.xml:1: [W7908(missing-newline-extrafiles), ] Missing newline ************* Module mail_sent mail_sent/doc/changelog.rst:1: [E7901(rst-syntax-error), ] Hyperlink target "changelog" is not referenced. mail_sent/views/templates.xml:1: [W7908(missing-newline-extrafiles), ] Missing newline ************* Module mail_sent.models mail_sent/models.py:8: [C8108(method-compute), MailMessage] Name of compute method should starts with "_compute_" mail_sent/models.py:12: [W8104(api-one-deprecated), MailMessage._get_sent] api.one deprecated ************* Module mail_reply mail_reply/templates.xml:1: [W7908(missing-newline-extrafiles), ] Missing newline ************* Module res_partner_mails_count res_partner_mails_count/README.rst:1: [W7908(missing-newline-extrafiles), ] Missing newline ************* Module res_partner_mails_count.models res_partner_mails_count/models.py:8: [C8108(method-compute), ResPartner] Name of compute method should starts with "_compute_" res_partner_mails_count/models.py:9: [C8108(method-compute), ResPartner] Name of compute method should starts with "_compute_" res_partner_mails_count/models.py:12: [W8104(api-one-deprecated), ResPartner._mails_to] api.one deprecated res_partner_mails_count/models.py:17: [W8104(api-one-deprecated), ResPartner._mails_from] api.one deprecated
8 years ago
[LINT] ************* Module mail_archives mail_archives/views/templates.xml:1: [W7908(missing-newline-extrafiles), ] Missing newline mail_archives/README.rst:1: [W7908(missing-newline-extrafiles), ] Missing newline ************* Module mail_to mail_to/static/src/js/mail_to.js:11: [W7903(javascript-lint), ] Missing semicolon. mail_to/templates.xml:1: [W7908(missing-newline-extrafiles), ] Missing newline mail_to/doc/changelog.rst:1: [W7908(missing-newline-extrafiles), ] Missing newline ************* Module mail_move_message mail_move_message/doc/changelog.rst:1: [E7901(rst-syntax-error), ] Hyperlink target "changelog" is not referenced. mail_move_message/data/mail_move_message_data.xml:1: [W7908(missing-newline-extrafiles), ] Missing newline ************* Module mail_move_message.mail_move_message_models mail_move_message/mail_move_message_models.py:74: [C8108(method-compute), Wizard] Name of compute method should starts with "_compute_" mail_move_message/mail_move_message_models.py:91: [W8104(api-one-deprecated), Wizard.get_can_move] api.one deprecated mail_move_message/mail_move_message_models.py:141: [W8104(api-one-deprecated), Wizard.check_access] api.one deprecated mail_move_message/mail_move_message_models.py:198: [W8104(api-one-deprecated), Wizard.delete] api.one deprecated mail_move_message/mail_move_message_models.py:233: [W8104(api-one-deprecated), Wizard.read_close] api.one deprecated mail_move_message/mail_move_message_models.py:248: [C8108(method-compute), MailMessage] Name of compute method should starts with "_compute_" mail_move_message/mail_move_message_models.py:251: [W8104(api-one-deprecated), MailMessage._get_all_childs] api.one deprecated mail_move_message/mail_move_message_models.py:274: [W8104(api-one-deprecated), MailMessage.move] api.one deprecated ************* Module mail_base mail_base/static/src/js/base.js:245: [W7903(javascript-lint), ] Misleading line break before '||'; readers may interpret this as an expression boundary. mail_base/static/src/js/base.js:1119: [W7903(javascript-lint), ] Missing semicolon. mail_base/static/src/js/base.js:1121: [W7903(javascript-lint), ] Missing semicolon. mail_base/views/templates.xml:1: [W7908(missing-newline-extrafiles), ] Missing newline ************* Module mail_sent mail_sent/doc/changelog.rst:1: [E7901(rst-syntax-error), ] Hyperlink target "changelog" is not referenced. mail_sent/views/templates.xml:1: [W7908(missing-newline-extrafiles), ] Missing newline ************* Module mail_sent.models mail_sent/models.py:8: [C8108(method-compute), MailMessage] Name of compute method should starts with "_compute_" mail_sent/models.py:12: [W8104(api-one-deprecated), MailMessage._get_sent] api.one deprecated ************* Module mail_reply mail_reply/templates.xml:1: [W7908(missing-newline-extrafiles), ] Missing newline ************* Module res_partner_mails_count res_partner_mails_count/README.rst:1: [W7908(missing-newline-extrafiles), ] Missing newline ************* Module res_partner_mails_count.models res_partner_mails_count/models.py:8: [C8108(method-compute), ResPartner] Name of compute method should starts with "_compute_" res_partner_mails_count/models.py:9: [C8108(method-compute), ResPartner] Name of compute method should starts with "_compute_" res_partner_mails_count/models.py:12: [W8104(api-one-deprecated), ResPartner._mails_to] api.one deprecated res_partner_mails_count/models.py:17: [W8104(api-one-deprecated), ResPartner._mails_from] api.one deprecated
8 years ago
  1. odoo.define('mail_base.base', function (require) {
  2. "use strict";
  3. var bus = require('bus.bus').bus;
  4. var utils = require('mail.utils');
  5. var config = require('web.config');
  6. var core = require('web.core');
  7. var data = require('web.data');
  8. var Model = require('web.Model');
  9. var session = require('web.session');
  10. var time = require('web.time');
  11. var web_client = require('web.web_client');
  12. var composer = require('mail.composer');
  13. var config = require('web.config');
  14. var Chatter = require('mail.Chatter');
  15. var form_common = require('web.form_common');
  16. var _t = core._t;
  17. var _lt = core._lt;
  18. var LIMIT = 100;
  19. var preview_msg_max_size = 350; // optimal for native english speakers
  20. var ODOOBOT_ID = "ODOOBOT";
  21. var MessageModel = new Model('mail.message', session.context);
  22. var ChannelModel = new Model('mail.channel', session.context);
  23. var UserModel = new Model('res.users', session.context);
  24. var PartnerModel = new Model('res.partner', session.context);
  25. var chat_manager = require('mail.chat_manager');
  26. // Private model
  27. //----------------------------------------------------------------------------------
  28. var messages = [];
  29. var channels = [];
  30. var channels_preview_def;
  31. var channel_defs = {};
  32. var chat_unread_counter = 0;
  33. var unread_conversation_counter = 0;
  34. var emojis = [];
  35. var emoji_substitutions = {};
  36. var needaction_counter = 0;
  37. var starred_counter = 0;
  38. var mention_partner_suggestions = [];
  39. var canned_responses = [];
  40. var commands = [];
  41. var discuss_menu_id;
  42. var global_unread_counter = 0;
  43. var pinned_dm_partners = []; // partner_ids we have a pinned DM with
  44. var client_action_open = false;
  45. bus.on("window_focus", null, function() {
  46. global_unread_counter = 0;
  47. web_client.set_title_part("_chat");
  48. });
  49. var channel_seen = _.throttle(function (channel) {
  50. return ChannelModel.call('channel_seen', [[channel.id]], {}, {shadow: true});
  51. }, 3000);
  52. var ChatAction = core.action_registry.get('mail.chat.instant_messaging');
  53. ChatAction.include({
  54. init: function(parent, action, options) {
  55. this._super.apply(this, arguments);
  56. this.channels_show_send_button = ['channel_inbox'];
  57. this.channels_display_subject = [];
  58. },
  59. start: function() {
  60. var result = this._super.apply(this, arguments);
  61. var search_defaults = {};
  62. var context = this.action ? this.action.context : [];
  63. _.each(context, function (value, key) {
  64. var match = /^search_default_(.*)$/.exec(key);
  65. if (match) {
  66. search_defaults[match[1]] = value;
  67. }
  68. });
  69. this.searchview.defaults = search_defaults;
  70. var self = this;
  71. return $.when(result).done(function() {
  72. $('.oe_leftbar').toggle(false);
  73. self.searchview.do_search();
  74. });
  75. },
  76. destroy: function() {
  77. var result = this._super.apply(this, arguments);
  78. $('.oe_leftbar .oe_secondary_menu').each(function(){
  79. if ($(this).css('display') == 'block'){
  80. if ($(this).children().length > 0) {
  81. $('.oe_leftbar').toggle(true);
  82. }
  83. return false;
  84. }
  85. });
  86. return result;
  87. },
  88. set_channel: function(channel){
  89. var result = this._super.apply(this, arguments);
  90. var self = this;
  91. return $.when(result).done(function() {
  92. self.$buttons
  93. .find('.o_mail_chat_button_new_message')
  94. .toggle(self.channels_show_send_button.indexOf(channel.id) != -1);
  95. });
  96. },
  97. get_thread_rendering_options: function (messages) {
  98. var options = this._super.apply(this, arguments);
  99. options.display_subject = options.display_subject || this.channels_display_subject.indexOf(this.channel.id) != -1;
  100. return options;
  101. },
  102. update_message_on_current_channel: function(current_channel_id, message){
  103. var starred = current_channel_id === "channel_starred" && !message.is_starred;
  104. var inbox = current_channel_id === "channel_inbox" && !message.is_needaction;
  105. return starred || inbox;
  106. },
  107. on_update_message: function (message) {
  108. var self = this;
  109. var current_channel_id = this.channel.id;
  110. if (this.update_message_on_current_channel(current_channel_id, message)) {
  111. chat_manager.get_messages({channel_id: this.channel.id, domain: this.domain}).then(function (messages) {
  112. var options = self.get_thread_rendering_options(messages);
  113. self.thread.remove_message_and_render(message.id, messages, options).then(function () {
  114. self.update_button_status(messages.length === 0);
  115. });
  116. });
  117. } else if (_.contains(message.channel_ids, current_channel_id)) {
  118. this.fetch_and_render_thread();
  119. }
  120. }
  121. });
  122. var MailComposer = composer.BasicComposer.extend({
  123. template: 'mail.chatter.ChatComposer',
  124. init: function (parent, dataset, options) {
  125. this._super(parent, options);
  126. this.thread_dataset = dataset;
  127. this.suggested_partners = [];
  128. this.options = _.defaults(this.options, {
  129. display_mode: 'textarea',
  130. record_name: false,
  131. is_log: false,
  132. });
  133. if (this.options.is_log) {
  134. this.options.send_text = _t('Log');
  135. }
  136. this.events = _.extend(this.events, {
  137. 'click .o_composer_button_full_composer': 'on_open_full_composer',
  138. });
  139. },
  140. willStart: function () {
  141. if (this.options.is_log) {
  142. return this._super.apply(this, arguments);
  143. }
  144. return $.when(this._super.apply(this, arguments), this.message_get_suggested_recipients());
  145. },
  146. should_send: function () {
  147. return false;
  148. },
  149. preprocess_message: function () {
  150. var self = this;
  151. var def = $.Deferred();
  152. this._super().then(function (message) {
  153. message = _.extend(message, {
  154. subtype: 'mail.mt_comment',
  155. message_type: 'comment',
  156. content_subtype: 'html',
  157. context: self.context,
  158. });
  159. // Subtype
  160. if (self.options.is_log) {
  161. message.subtype = 'mail.mt_note';
  162. }
  163. // Partner_ids
  164. if (!self.options.is_log) {
  165. var checked_suggested_partners = self.get_checked_suggested_partners();
  166. self.check_suggested_partners(checked_suggested_partners).done(function (partner_ids) {
  167. message.partner_ids = (message.partner_ids || []).concat(partner_ids);
  168. // update context
  169. message.context = _.defaults({}, message.context, {
  170. mail_post_autofollow: true,
  171. });
  172. def.resolve(message);
  173. });
  174. } else {
  175. def.resolve(message);
  176. }
  177. });
  178. return def;
  179. },
  180. /**
  181. * Send the message on SHIFT+ENTER, but go to new line on ENTER
  182. */
  183. prevent_send: function (event) {
  184. return !event.shiftKey;
  185. },
  186. message_get_suggested_recipients: function () {
  187. var self = this;
  188. var email_addresses = _.pluck(this.suggested_partners, 'email_address');
  189. return this.thread_dataset
  190. .call('message_get_suggested_recipients', [[this.context.default_res_id], this.context])
  191. .done(function (suggested_recipients) {
  192. var thread_recipients = suggested_recipients[self.context.default_res_id];
  193. _.each(thread_recipients, function (recipient) {
  194. var parsed_email = utils.parse_email(recipient[1]);
  195. if (_.indexOf(email_addresses, parsed_email[1]) === -1) {
  196. self.suggested_partners.push({
  197. checked: true,
  198. partner_id: recipient[0],
  199. full_name: recipient[1],
  200. name: parsed_email[0],
  201. email_address: parsed_email[1],
  202. reason: recipient[2],
  203. });
  204. }
  205. });
  206. });
  207. },
  208. /**
  209. * Get the list of selected suggested partners
  210. * @returns Array() : list of 'recipient' selected partners (may not be created in db)
  211. **/
  212. get_checked_suggested_partners: function () {
  213. var self = this;
  214. var checked_partners = [];
  215. this.$('.o_composer_suggested_partners input:checked').each(function() {
  216. var full_name = $(this).data('fullname');
  217. checked_partners = checked_partners.concat(_.filter(self.suggested_partners, function(item) {
  218. return full_name === item.full_name;
  219. }));
  220. });
  221. return checked_partners;
  222. },
  223. /**
  224. * Check the additional partners (not necessary registered partners), and open a popup form view
  225. * for the ones who informations is missing.
  226. * @param Array : list of 'recipient' partners to complete informations or validate
  227. * @returns Deferred resolved with the list of checked suggested partners (real partner)
  228. **/
  229. check_suggested_partners: function (checked_suggested_partners) {
  230. var self = this;
  231. var check_done = $.Deferred();
  232. var recipients = _.filter(checked_suggested_partners, function (recipient) { return recipient.checked; });
  233. var recipients_to_find = _.filter(recipients, function (recipient) { return (! recipient.partner_id); });
  234. var names_to_find = _.pluck(recipients_to_find, 'full_name');
  235. var recipients_to_check = _.filter(recipients, function (recipient) { return (recipient.partner_id && ! recipient.email_address); });
  236. var recipient_ids = _.pluck(_.filter(recipients, function (recipient) { return recipient.partner_id && recipient.email_address; }), 'partner_id');
  237. var names_to_remove = [];
  238. var recipient_ids_to_remove = [];
  239. // have unknown names -> call message_get_partner_info_from_emails to try to find partner_id
  240. var find_done = $.Deferred();
  241. if (names_to_find.length > 0) {
  242. find_done = self.thread_dataset.call('message_partner_info_from_emails', [[this.context.default_res_id], names_to_find]);
  243. } else {
  244. find_done.resolve([]);
  245. }
  246. // for unknown names + incomplete partners -> open popup - cancel = remove from recipients
  247. $.when(find_done).pipe(function (result) {
  248. var emails_deferred = [];
  249. var recipient_popups = result.concat(recipients_to_check);
  250. _.each(recipient_popups, function (partner_info) {
  251. var deferred = $.Deferred();
  252. emails_deferred.push(deferred);
  253. var partner_name = partner_info.full_name;
  254. var partner_id = partner_info.partner_id;
  255. var parsed_email = utils.parse_email(partner_name);
  256. var dialog = new form_common.FormViewDialog(self, {
  257. res_model: 'res.partner',
  258. res_id: partner_id,
  259. context: {
  260. force_email: true,
  261. ref: "compound_context",
  262. default_name: parsed_email[0],
  263. default_email: parsed_email[1],
  264. },
  265. title: _t("Please complete partner's informations"),
  266. disable_multiple_selection: true,
  267. }).open();
  268. dialog.on('closed', self, function () {
  269. deferred.resolve();
  270. });
  271. dialog.opened().then(function () {
  272. dialog.view_form.on('on_button_cancel', self, function () {
  273. names_to_remove.push(partner_name);
  274. if (partner_id) {
  275. recipient_ids_to_remove.push(partner_id);
  276. }
  277. });
  278. });
  279. });
  280. $.when.apply($, emails_deferred).then(function () {
  281. var new_names_to_find = _.difference(names_to_find, names_to_remove);
  282. find_done = $.Deferred();
  283. if (new_names_to_find.length > 0) {
  284. find_done = self.thread_dataset.call('message_partner_info_from_emails', [[self.context.default_res_id], new_names_to_find, true]);
  285. } else {
  286. find_done.resolve([]);
  287. }
  288. $.when(find_done).pipe(function (result) {
  289. var recipient_popups = result.concat(recipients_to_check);
  290. _.each(recipient_popups, function (partner_info) {
  291. if (partner_info.partner_id && _.indexOf(partner_info.partner_id, recipient_ids_to_remove) === -1) {
  292. recipient_ids.push(partner_info.partner_id);
  293. }
  294. });
  295. }).pipe(function () {
  296. check_done.resolve(recipient_ids);
  297. });
  298. });
  299. });
  300. return check_done;
  301. },
  302. on_open_full_composer: function() {
  303. if (!this.do_check_attachment_upload()){
  304. return false;
  305. }
  306. var self = this;
  307. var recipient_done = $.Deferred();
  308. if (this.options.is_log) {
  309. recipient_done.resolve([]);
  310. } else {
  311. var checked_suggested_partners = this.get_checked_suggested_partners();
  312. recipient_done = this.check_suggested_partners(checked_suggested_partners);
  313. }
  314. recipient_done.then(function (partner_ids) {
  315. var context = {
  316. default_parent_id: self.id,
  317. default_body: utils.get_text2html(self.$input.val()),
  318. default_attachment_ids: _.pluck(self.get('attachment_ids'), 'id'),
  319. default_partner_ids: partner_ids,
  320. default_is_log: self.options.is_log,
  321. mail_post_autofollow: true,
  322. };
  323. if (self.context.default_model && self.context.default_res_id) {
  324. context.default_model = self.context.default_model;
  325. context.default_res_id = self.context.default_res_id;
  326. }
  327. self.do_action({
  328. type: 'ir.actions.act_window',
  329. res_model: 'mail.compose.message',
  330. view_mode: 'form',
  331. view_type: 'form',
  332. views: [[false, 'form']],
  333. target: 'new',
  334. context: context,
  335. }, {
  336. on_close: function() {
  337. self.trigger('need_refresh');
  338. var parent = self.getParent();
  339. chat_manager.get_messages({model: parent.model, res_id: parent.res_id});
  340. },
  341. }).then(self.trigger.bind(self, 'close_composer'));
  342. });
  343. }
  344. });
  345. Chatter.include({
  346. open_composer: function (options) {
  347. var self = this;
  348. var old_composer = this.composer;
  349. // create the new composer
  350. this.composer = new MailComposer(this, this.thread_dataset, {
  351. commands_enabled: false,
  352. context: this.context,
  353. input_min_height: 50,
  354. input_max_height: Number.MAX_VALUE, // no max_height limit for the chatter
  355. input_baseline: 14,
  356. is_log: options && options.is_log,
  357. record_name: this.record_name,
  358. default_body: old_composer && old_composer.$input && old_composer.$input.val(),
  359. default_mention_selections: old_composer && old_composer.mention_get_listener_selections(),
  360. });
  361. this.composer.on('input_focused', this, function () {
  362. this.composer.mention_set_prefetched_partners(this.mention_suggestions || []);
  363. });
  364. this.composer.insertBefore(this.$('.o_mail_thread')).then(function () {
  365. // destroy existing composer
  366. if (old_composer) {
  367. old_composer.destroy();
  368. }
  369. if (!config.device.touch) {
  370. self.composer.focus();
  371. }
  372. self.composer.on('post_message', self, self.on_post_message);
  373. self.composer.on('need_refresh', self, self.refresh_followers);
  374. self.composer.on('close_composer', null, self.close_composer.bind(self, true));
  375. });
  376. this.mute_new_message_button(true);
  377. },
  378. });
  379. var MailTools = core.Class.extend({
  380. notify_incoming_message: function (msg, options) {
  381. if (bus.is_odoo_focused() && options.is_displayed) {
  382. // no need to notify
  383. return;
  384. }
  385. var title = _t('New message');
  386. if (msg.author_id[1]) {
  387. title = _.escape(msg.author_id[1]);
  388. }
  389. var content = utils.parse_and_transform(msg.body, utils.strip_html).substr(0, preview_msg_max_size);
  390. if (!bus.is_odoo_focused()) {
  391. global_unread_counter++;
  392. var tab_title = _.str.sprintf(_t("%d Messages"), global_unread_counter);
  393. web_client.set_title_part("_chat", tab_title);
  394. }
  395. utils.send_notification(title, content);
  396. },
  397. // Message and channel manipulation helpers
  398. //----------------------------------------------------------------------------------
  399. // options: channel_id, silent
  400. add_message: function (data, options) {
  401. options = options || {};
  402. var msg = _.findWhere(messages, { id: data.id });
  403. if (!msg) {
  404. msg = chat_manager.mail_tools.make_message(data);
  405. // Keep the array ordered by id when inserting the new message
  406. messages.splice(_.sortedIndex(messages, msg, 'id'), 0, msg);
  407. _.each(msg.channel_ids, function (channel_id) {
  408. var channel = chat_manager.get_channel(channel_id);
  409. if (channel) {
  410. chat_manager.mail_tools.add_to_cache(msg, []);
  411. if (options.domain && options.domain !== []) {
  412. chat_manager.mail_tools.add_to_cache(msg, options.domain);
  413. }
  414. if (channel.hidden) {
  415. channel.hidden = false;
  416. chat_manager.bus.trigger('new_channel', channel);
  417. }
  418. if (channel.type !== 'static' && !msg.is_author && !msg.is_system_notification) {
  419. if (options.increment_unread) {
  420. chat_manager.mail_tools.update_channel_unread_counter(channel, channel.unread_counter+1);
  421. }
  422. if (channel.is_chat && options.show_notification) {
  423. if (!client_action_open && config.device.size_class !== config.device.SIZES.XS) {
  424. // automatically open chat window
  425. chat_manager.bus.trigger('open_chat', channel, { passively: true });
  426. }
  427. var query = {is_displayed: false};
  428. chat_manager.bus.trigger('anyone_listening', channel, query);
  429. chat_manager.mail_tools.notify_incoming_message(msg, query);
  430. }
  431. }
  432. }
  433. });
  434. if (!options.silent) {
  435. chat_manager.bus.trigger('new_message', msg);
  436. }
  437. } else if (options.domain && options.domain !== []) {
  438. chat_manager.mail_tools.add_to_cache(msg, options.domain);
  439. }
  440. return msg;
  441. },
  442. property_descr: function (channel, msg, self) {
  443. return {
  444. enumerable: true,
  445. get: function () {
  446. return _.contains(msg.channel_ids, channel);
  447. },
  448. set: function (bool) {
  449. if (bool) {
  450. self.add_channel_to_message(msg, channel);
  451. } else {
  452. msg.channel_ids = _.without(msg.channel_ids, channel);
  453. }
  454. }
  455. };
  456. },
  457. get_properties: function(msg){
  458. return {
  459. is_starred: chat_manager.mail_tools.property_descr("channel_starred", msg, chat_manager.mail_tools),
  460. is_needaction: chat_manager.mail_tools.property_descr("channel_inbox", msg, chat_manager.mail_tools)
  461. };
  462. },
  463. set_channel_flags: function(data, msg){
  464. if (_.contains(data.needaction_partner_ids, session.partner_id)) {
  465. msg.is_needaction = true;
  466. }
  467. if (_.contains(data.starred_partner_ids, session.partner_id)) {
  468. msg.is_starred = true;
  469. }
  470. return msg;
  471. },
  472. get_channel_array: function(msg){
  473. return [ msg.channel_ids, 'channel_inbox', 'channel_starred' ];
  474. },
  475. make_message: function (data) {
  476. var msg = {
  477. id: data.id,
  478. author_id: data.author_id,
  479. body: data.body || "",
  480. date: moment(time.str_to_datetime(data.date)),
  481. message_type: data.message_type,
  482. subtype_description: data.subtype_description,
  483. is_author: data.author_id && data.author_id[0] === session.partner_id,
  484. is_note: data.is_note,
  485. is_system_notification: data.message_type === 'notification' && data.model === 'mail.channel' || data.info === 'transient_message',
  486. attachment_ids: data.attachment_ids || [],
  487. subject: data.subject,
  488. email_from: data.email_from,
  489. customer_email_status: data.customer_email_status,
  490. customer_email_data: data.customer_email_data,
  491. record_name: data.record_name,
  492. tracking_value_ids: data.tracking_value_ids,
  493. channel_ids: data.channel_ids,
  494. model: data.model,
  495. res_id: data.res_id,
  496. url: session.url("/mail/view?message_id=" + data.id)
  497. };
  498. _.each(_.keys(emoji_substitutions), function (key) {
  499. var escaped_key = String(key).replace(/([.*+?=^!:${}()|[\]\/\\])/g, '\\$1');
  500. var regexp = new RegExp("(?:^|\\s|<[a-z]*>)(" + escaped_key + ")(?=\\s|$|</[a-z]*>)", "g");
  501. msg.body = msg.body.replace(regexp, ' <span class="o_mail_emoji">'+emoji_substitutions[key]+'</span> ');
  502. });
  503. Object.defineProperties(msg, chat_manager.mail_tools.get_properties(msg));
  504. msg = chat_manager.mail_tools.set_channel_flags(data, msg);
  505. if (msg.model === 'mail.channel') {
  506. var real_channels = _.without(chat_manager.mail_tools.get_channel_array(msg));
  507. var origin = real_channels.length === 1 ? real_channels[0] : undefined;
  508. var channel = origin && chat_manager.get_channel(origin);
  509. if (channel) {
  510. msg.origin_id = origin;
  511. msg.origin_name = channel.name;
  512. }
  513. }
  514. // Compute displayed author name or email
  515. if ((!msg.author_id || !msg.author_id[0]) && msg.email_from) {
  516. msg.mailto = msg.email_from;
  517. } else {
  518. msg.displayed_author = (msg.author_id === ODOOBOT_ID) && "OdooBot" ||
  519. msg.author_id && msg.author_id[1] ||
  520. msg.email_from || _t('Anonymous');
  521. }
  522. // Don't redirect on author clicked of self-posted or OdooBot messages
  523. msg.author_redirect = !msg.is_author && msg.author_id !== ODOOBOT_ID;
  524. // Compute the avatar_url
  525. if (msg.author_id === ODOOBOT_ID) {
  526. msg.avatar_src = "/mail/static/src/img/odoo_o.png";
  527. } else if (msg.author_id && msg.author_id[0]) {
  528. msg.avatar_src = "/web/image/res.partner/" + msg.author_id[0] + "/image_small";
  529. } else if (msg.message_type === 'email') {
  530. msg.avatar_src = "/mail/static/src/img/email_icon.png";
  531. } else {
  532. msg.avatar_src = "/mail/static/src/img/smiley/avatar.jpg";
  533. }
  534. // add anchor tags to urls
  535. msg.body = utils.parse_and_transform(msg.body, utils.add_link);
  536. // Compute url of attachments
  537. _.each(msg.attachment_ids, function(a) {
  538. a.url = '/web/content/' + a.id + '?download=true';
  539. });
  540. return msg;
  541. },
  542. add_channel_to_message: function (message, channel_id) {
  543. message.channel_ids.push(channel_id);
  544. message.channel_ids = _.uniq(message.channel_ids);
  545. },
  546. add_channel: function (data, options) {
  547. options = typeof options === "object" ? options : {};
  548. var channel = chat_manager.get_channel(data.id);
  549. if (channel) {
  550. if (channel.is_folded !== (data.state === "folded")) {
  551. channel.is_folded = (data.state === "folded");
  552. chat_manager.bus.trigger("channel_toggle_fold", channel);
  553. }
  554. } else {
  555. channel = chat_manager.mail_tools.make_channel(data, options);
  556. channels.push(channel);
  557. channels = _.sortBy(channels, function (channel) { return _.isString(channel.name) ? channel.name.toLowerCase() : ''; });
  558. if (!options.silent) {
  559. chat_manager.bus.trigger("new_channel", channel);
  560. }
  561. if (channel.is_detached) {
  562. chat_manager.bus.trigger("open_chat", channel);
  563. }
  564. }
  565. return channel;
  566. },
  567. make_channel: function (data, options) {
  568. var channel = {
  569. id: data.id,
  570. name: data.name,
  571. server_type: data.channel_type,
  572. type: data.type || data.channel_type,
  573. all_history_loaded: false,
  574. uuid: data.uuid,
  575. is_detached: data.is_minimized,
  576. is_folded: data.state === "folded",
  577. autoswitch: 'autoswitch' in options ? options.autoswitch : true,
  578. hidden: options.hidden,
  579. display_needactions: options.display_needactions,
  580. mass_mailing: data.mass_mailing,
  581. needaction_counter: data.message_needaction_counter || 0,
  582. unread_counter: 0,
  583. last_seen_message_id: data.seen_message_id,
  584. cache: {'[]': {
  585. all_history_loaded: false,
  586. loaded: false,
  587. messages: []
  588. }}
  589. };
  590. if (channel.type === "channel" && data.public !== "private") {
  591. channel.type = "public";
  592. } else if (data.public === "private") {
  593. channel.type = "private";
  594. }
  595. if (_.size(data.direct_partner) > 0) {
  596. channel.type = "dm";
  597. channel.name = data.direct_partner[0].name;
  598. channel.direct_partner_id = data.direct_partner[0].id;
  599. channel.status = data.direct_partner[0].im_status;
  600. pinned_dm_partners.push(channel.direct_partner_id);
  601. bus.update_option('bus_presence_partner_ids', pinned_dm_partners);
  602. } else if ('anonymous_name' in data) {
  603. channel.name = data.anonymous_name;
  604. }
  605. if (data.last_message_date) {
  606. channel.last_message_date = moment(time.str_to_datetime(data.last_message_date));
  607. }
  608. channel.is_chat = !channel.type.match(/^(public|private|static)$/);
  609. if (data.message_unread_counter) {
  610. chat_manager.mail_tools.update_channel_unread_counter(channel, data.message_unread_counter);
  611. }
  612. return channel;
  613. },
  614. remove_channel: function (channel) {
  615. if (!channel) { return; }
  616. if (channel.type === 'dm') {
  617. var index = pinned_dm_partners.indexOf(channel.direct_partner_id);
  618. if (index > -1) {
  619. pinned_dm_partners.splice(index, 1);
  620. bus.update_option('bus_presence_partner_ids', pinned_dm_partners);
  621. }
  622. }
  623. channels = _.without(channels, channel);
  624. delete channel_defs[channel.id];
  625. },
  626. get_channel_cache: function (channel, domain) {
  627. var stringified_domain = JSON.stringify(domain || []);
  628. if (!channel.cache[stringified_domain]) {
  629. channel.cache[stringified_domain] = {
  630. all_history_loaded: false,
  631. loaded: false,
  632. messages: []
  633. };
  634. }
  635. return channel.cache[stringified_domain];
  636. },
  637. invalidate_caches: function (channel_ids) {
  638. _.each(channel_ids, function (channel_id) {
  639. var channel = chat_manager.get_channel(channel_id);
  640. if (channel) {
  641. channel.cache = { '[]': channel.cache['[]']};
  642. }
  643. });
  644. },
  645. clear_cache_all_channels: function(){
  646. _.each(channels, function(channel){
  647. channel.cache = {
  648. '[]': {
  649. all_history_loaded: false,
  650. loaded: false,
  651. messages: []
  652. }
  653. }
  654. });
  655. },
  656. add_to_cache: function (message, domain) {
  657. _.each(message.channel_ids, function (channel_id) {
  658. var channel = chat_manager.get_channel(channel_id);
  659. if (channel) {
  660. var channel_cache = chat_manager.mail_tools.get_channel_cache(channel, domain);
  661. var index = _.sortedIndex(channel_cache.messages, message, 'id');
  662. if (channel_cache.messages[index] !== message) {
  663. channel_cache.messages.splice(index, 0, message);
  664. }
  665. }
  666. });
  667. },
  668. remove_from_cache: function(message, domain){
  669. var self = this;
  670. _.each(message.channel_ids, function (channel_id) {
  671. var channel = chat_manager.get_channel(channel_id);
  672. if (channel) {
  673. var channel_cache = self.get_channel_cache(channel, domain);
  674. var index = _.sortedIndex(channel_cache.messages, message, 'id');
  675. if (channel_cache.messages[index] === message) {
  676. channel_cache.messages.splice(index, 1);
  677. }
  678. }
  679. });
  680. },
  681. remove_message_from_channel: function (channel_id, message) {
  682. message.channel_ids = _.without(message.channel_ids, channel_id);
  683. var channel = _.findWhere(channels, { id: channel_id });
  684. _.each(channel.cache, function (cache) {
  685. cache.messages = _.without(cache.messages, message);
  686. });
  687. },
  688. get_domain: function(channel){
  689. return (channel.id === "channel_inbox") ? [['needaction', '=', true]] :
  690. (channel.id === "channel_starred") ? [['starred', '=', true]] : false;
  691. },
  692. // options: domain, load_more
  693. fetch_from_channel: function (channel, options) {
  694. options = options || {};
  695. var domain = chat_manager.mail_tools.get_domain(channel) || [['channel_ids', 'in', channel.id]];
  696. var cache = chat_manager.mail_tools.get_channel_cache(channel, options.domain);
  697. if (options.domain) {
  698. domain = new data.CompoundDomain(domain, options.domain || []);
  699. }
  700. if (options.load_more) {
  701. var min_message_id = cache.messages[0].id;
  702. domain = new data.CompoundDomain([['id', '<', min_message_id]], domain);
  703. }
  704. return MessageModel.call('message_fetch', [domain], {limit: LIMIT}).then(function (msgs) {
  705. if (!cache.all_history_loaded) {
  706. cache.all_history_loaded = msgs.length < LIMIT;
  707. }
  708. cache.loaded = true;
  709. _.each(msgs, function (msg) {
  710. chat_manager.mail_tools.add_message(msg, {channel_id: channel.id, silent: true, domain: options.domain});
  711. });
  712. var channel_cache = chat_manager.mail_tools.get_channel_cache(channel, options.domain || []);
  713. return channel_cache.messages;
  714. });
  715. },
  716. // options: force_fetch
  717. fetch_document_messages: function (ids, options) {
  718. var loaded_msgs = _.filter(messages, function (message) {
  719. return _.contains(ids, message.id);
  720. });
  721. var loaded_msg_ids = _.pluck(loaded_msgs, 'id');
  722. options = options || {};
  723. if (options.force_fetch || _.difference(ids.slice(0, LIMIT), loaded_msg_ids).length) {
  724. var ids_to_load = _.difference(ids, loaded_msg_ids).slice(0, LIMIT);
  725. return MessageModel.call('message_format', [ids_to_load]).then(function (msgs) {
  726. var processed_msgs = [];
  727. _.each(msgs, function (msg) {
  728. processed_msgs.push(chat_manager.mail_tools.add_message(msg, {silent: true}));
  729. });
  730. return _.sortBy(loaded_msgs.concat(processed_msgs), function (msg) {
  731. return msg.date;
  732. });
  733. });
  734. } else {
  735. return $.when(loaded_msgs);
  736. }
  737. },
  738. update_channel_unread_counter: function (channel, counter) {
  739. if (channel.unread_counter > 0 && counter === 0) {
  740. unread_conversation_counter = Math.max(0, unread_conversation_counter-1);
  741. } else if (channel.unread_counter === 0 && counter > 0) {
  742. unread_conversation_counter++;
  743. }
  744. if (channel.is_chat) {
  745. chat_unread_counter = Math.max(0, chat_unread_counter - channel.unread_counter + counter);
  746. }
  747. channel.unread_counter = counter;
  748. chat_manager.bus.trigger("update_channel_unread_counter", channel);
  749. },
  750. // Notification handlers
  751. // ---------------------------------------------------------------------------------
  752. on_notification: function (notifications) {
  753. // sometimes, the web client receives unsubscribe notification and an extra
  754. // notification on that channel. This is then followed by an attempt to
  755. // rejoin the channel that we just left. The next few lines remove the
  756. // extra notification to prevent that situation to occur.
  757. var unsubscribed_notif = _.find(notifications, function (notif) {
  758. return notif[1].info === "unsubscribe";
  759. });
  760. if (unsubscribed_notif) {
  761. notifications = _.reject(notifications, function (notif) {
  762. return notif[0][1] === "mail.channel" && notif[0][2] === unsubscribed_notif[1].id;
  763. });
  764. }
  765. _.each(notifications, function (notification) {
  766. var model = notification[0][1];
  767. if (model === 'ir.needaction') {
  768. // new message in the inbox
  769. chat_manager.mail_tools.on_needaction_notification(notification[1]);
  770. } else if (model === 'mail.channel') {
  771. // new message in a channel
  772. chat_manager.mail_tools.on_channel_notification(notification[1]);
  773. } else if (model === 'res.partner') {
  774. // channel joined/left, message marked as read/(un)starred, chat open/closed
  775. chat_manager.mail_tools.on_partner_notification(notification[1]);
  776. } else if (model === 'bus.presence') {
  777. // update presence of users
  778. chat_manager.mail_tools.on_presence_notification(notification[1]);
  779. } else if (model === 'mail_base.mail_sent') {
  780. // Delete cache in order to fetch new message
  781. // TODO find a solution without deleting cache. Currently
  782. // problem is that on inheriting send_mail in
  783. // mail.compose.message it's not possible to get id of new
  784. // message
  785. chat_manager.mail_tools.clear_cache_all_channels();
  786. }
  787. });
  788. },
  789. on_needaction_notification: function (message) {
  790. message = chat_manager.mail_tools.add_message(message, {
  791. channel_id: 'channel_inbox',
  792. show_notification: true,
  793. increment_unread: true
  794. });
  795. chat_manager.mail_tools.invalidate_caches(message.channel_ids);
  796. needaction_counter++;
  797. _.each(message.channel_ids, function (channel_id) {
  798. var channel = chat_manager.get_channel(channel_id);
  799. if (channel) {
  800. channel.needaction_counter++;
  801. }
  802. });
  803. chat_manager.bus.trigger('update_needaction', needaction_counter);
  804. },
  805. on_channel_notification: function (message) {
  806. var def;
  807. var channel_already_in_cache = true;
  808. if (message.channel_ids.length === 1) {
  809. channel_already_in_cache = !!chat_manager.get_channel(message.channel_ids[0]);
  810. def = chat_manager.join_channel(message.channel_ids[0], {autoswitch: false});
  811. } else {
  812. def = $.when();
  813. }
  814. def.then(function () {
  815. // don't increment unread if channel wasn't in cache yet as its unread counter has just been fetched
  816. chat_manager.mail_tools.add_message(message, { show_notification: true, increment_unread: channel_already_in_cache });
  817. chat_manager.mail_tools.invalidate_caches(message.channel_ids);
  818. });
  819. },
  820. on_partner_notification: function (data) {
  821. if (data.info === "unsubscribe") {
  822. var channel = chat_manager.get_channel(data.id);
  823. if (channel) {
  824. var msg;
  825. if (_.contains(['public', 'private'], channel.type)) {
  826. msg = _.str.sprintf(_t('You unsubscribed from <b>%s</b>.'), channel.name);
  827. } else {
  828. msg = _.str.sprintf(_t('You unpinned your conversation with <b>%s</b>.'), channel.name);
  829. }
  830. this.remove_channel(channel);
  831. chat_manager.bus.trigger("unsubscribe_from_channel", data.id);
  832. web_client.do_notify(_("Unsubscribed"), msg);
  833. }
  834. } else if (data.type === 'toggle_star') {
  835. chat_manager.mail_tools.on_toggle_star_notification(data);
  836. } else if (data.type === 'mark_as_read') {
  837. chat_manager.mail_tools.on_mark_as_read_notification(data);
  838. } else if (data.type === 'mark_as_unread') {
  839. chat_manager.mail_tools.on_mark_as_unread_notification(data);
  840. } else if (data.info === 'channel_seen') {
  841. chat_manager.mail_tools.on_channel_seen_notification(data);
  842. } else if (data.info === 'transient_message') {
  843. chat_manager.mail_tools.on_transient_message_notification(data);
  844. } else {
  845. chat_manager.mail_tools.on_chat_session_notification(data);
  846. }
  847. },
  848. on_toggle_star_notification: function (data) {
  849. _.each(data.message_ids, function (msg_id) {
  850. var message = _.findWhere(messages, { id: msg_id });
  851. if (message) {
  852. chat_manager.mail_tools.invalidate_caches(message.channel_ids);
  853. message.is_starred = data.starred;
  854. if (!message.is_starred) {
  855. chat_manager.mail_tools.remove_message_from_channel("channel_starred", message);
  856. starred_counter--;
  857. } else {
  858. chat_manager.mail_tools.add_to_cache(message, []);
  859. var channel_starred = chat_manager.get_channel('channel_starred');
  860. channel_starred.cache = _.pick(channel_starred.cache, "[]");
  861. starred_counter++;
  862. }
  863. chat_manager.bus.trigger('update_message', message);
  864. }
  865. });
  866. chat_manager.bus.trigger('update_starred', starred_counter);
  867. },
  868. on_mark_as_read_notification: function (data) {
  869. _.each(data.message_ids, function (msg_id) {
  870. var message = _.findWhere(messages, { id: msg_id });
  871. if (message) {
  872. chat_manager.mail_tools.invalidate_caches(message.channel_ids);
  873. chat_manager.mail_tools.remove_message_from_channel("channel_inbox", message);
  874. chat_manager.bus.trigger('update_message', message);
  875. }
  876. });
  877. if (data.channel_ids) {
  878. _.each(data.channel_ids, function (channel_id) {
  879. var channel = chat_manager.get_channel(channel_id);
  880. if (channel) {
  881. channel.needaction_counter = Math.max(channel.needaction_counter - data.message_ids.length, 0);
  882. }
  883. });
  884. } else { // if no channel_ids specified, this is a 'mark all read' in the inbox
  885. _.each(channels, function (channel) {
  886. channel.needaction_counter = 0;
  887. });
  888. }
  889. needaction_counter = Math.max(needaction_counter - data.message_ids.length, 0);
  890. chat_manager.bus.trigger('update_needaction', needaction_counter);
  891. },
  892. on_mark_as_unread_notification: function (data) {
  893. _.each(data.message_ids, function (message_id) {
  894. var message = _.findWhere(messages, { id: message_id });
  895. if (message) {
  896. chat_manager.mail_tools.invalidate_caches(message.channel_ids);
  897. chat_manager.mail_tools.add_channel_to_message(message, 'channel_inbox');
  898. chat_manager.mail_tools.add_to_cache(message, []);
  899. }
  900. });
  901. var channel_inbox = chat_manager.get_channel('channel_inbox');
  902. channel_inbox.cache = _.pick(channel_inbox.cache, "[]");
  903. _.each(data.channel_ids, function (channel_id) {
  904. var channel = chat_manager.get_channel(channel_id);
  905. if (channel) {
  906. channel.needaction_counter += data.message_ids.length;
  907. }
  908. });
  909. needaction_counter += data.message_ids.length;
  910. chat_manager.bus.trigger('update_needaction', needaction_counter);
  911. },
  912. on_channel_seen_notification: function (data) {
  913. var channel = chat_manager.get_channel(data.id);
  914. if (channel) {
  915. channel.last_seen_message_id = data.last_message_id;
  916. if (channel.unread_counter) {
  917. chat_manager.mail_tools.update_channel_unread_counter(channel, 0);
  918. }
  919. }
  920. },
  921. on_chat_session_notification: function (chat_session) {
  922. var channel;
  923. if ((chat_session.channel_type === "channel") && (chat_session.state === "open")) {
  924. chat_manager.mail_tools.add_channel(chat_session, {autoswitch: false});
  925. if (!chat_session.is_minimized && chat_session.info !== 'creation') {
  926. web_client.do_notify(_t("Invitation"), _t("You have been invited to: ") + chat_session.name);
  927. }
  928. }
  929. // partner specific change (open a detached window for example)
  930. if ((chat_session.state === "open") || (chat_session.state === "folded")) {
  931. channel = chat_session.is_minimized && chat_manager.get_channel(chat_session.id);
  932. if (channel) {
  933. channel.is_detached = true;
  934. channel.is_folded = (chat_session.state === "folded");
  935. chat_manager.bus.trigger("open_chat", channel);
  936. }
  937. } else if (chat_session.state === "closed") {
  938. channel = chat_manager.get_channel(chat_session.id);
  939. if (channel) {
  940. channel.is_detached = false;
  941. chat_manager.bus.trigger("close_chat", channel, {keep_open_if_unread: true});
  942. }
  943. }
  944. },
  945. on_presence_notification: function (data) {
  946. var dm = chat_manager.get_dm_from_partner_id(data.id);
  947. if (dm) {
  948. dm.status = data.im_status;
  949. chat_manager.bus.trigger('update_dm_presence', dm);
  950. }
  951. },
  952. on_transient_message_notification: function (data) {
  953. var last_message = _.last(messages);
  954. data.id = (last_message ? last_message.id : 0) + 0.01;
  955. data.author_id = data.author_id || ODOOBOT_ID;
  956. chat_manager.mail_tools.add_message(data);
  957. }
  958. });
  959. var cls = new MailTools();
  960. // Public interface
  961. //----------------------------------------------------------------------------------
  962. chat_manager.mail_tools = cls;
  963. // we add this function this way in order to make them extendable via MailTools.include({...})
  964. chat_manager.make_message = function(){
  965. return chat_manager.mail_tools.make_message.apply(chat_manager.mail_tools, arguments);
  966. };
  967. chat_manager.make_channel = function(){
  968. return chat_manager.mail_tools.make_channel.apply(chat_manager.mail_tools, arguments);
  969. };
  970. chat_manager.post_message = function (data, options) {
  971. options = options || {};
  972. var msg = {
  973. partner_ids: data.partner_ids,
  974. body: _.str.trim(data.content),
  975. attachment_ids: data.attachment_ids
  976. };
  977. if ('subject' in data) {
  978. msg.subject = data.subject;
  979. }
  980. if ('channel_id' in options) {
  981. // post a message in a channel or execute a command
  982. return ChannelModel.call(data.command ? 'execute_command' : 'message_post', [options.channel_id], _.extend(msg, {
  983. message_type: 'comment',
  984. content_subtype: 'html',
  985. subtype: 'mail.mt_comment',
  986. command: data.command,
  987. }));
  988. }
  989. if ('model' in options && 'res_id' in options) {
  990. // post a message in a chatter
  991. _.extend(msg, {
  992. content_subtype: data.content_subtype,
  993. context: data.context,
  994. message_type: data.message_type,
  995. subtype: data.subtype,
  996. subtype_id: data.subtype_id
  997. });
  998. if (options.model && options.res_id){
  999. var model = new Model(options.model);
  1000. return model.call('message_post', [options.res_id], msg).then(function (msg_id) {
  1001. return MessageModel.call('message_format', [msg_id]).then(function (msgs) {
  1002. msgs[0].model = options.model;
  1003. msgs[0].res_id = options.res_id;
  1004. chat_manager.mail_tools.add_message(msgs[0]);
  1005. });
  1006. });
  1007. } else {
  1008. options.model = 'mail.compose.message';
  1009. var compose_model = new Model(options.model);
  1010. return compose_model.call('create', [msg, {default_parent_id: options.parent_id}]).then(function(id){
  1011. return compose_model.call('send_mail_action', [id, {}]);
  1012. });
  1013. }
  1014. }
  1015. };
  1016. chat_manager.get_message = function (id) {
  1017. return _.findWhere(messages, {id: id});
  1018. };
  1019. chat_manager.get_messages = function (options) {
  1020. var channel;
  1021. if ('channel_id' in options && options.load_more) {
  1022. // get channel messages, force load_more
  1023. channel = this.get_channel(options.channel_id);
  1024. return chat_manager.mail_tools.fetch_from_channel(channel, {domain: options.domain || {}, load_more: true});
  1025. }
  1026. if ('channel_id' in options) {
  1027. // channel message, check in cache first
  1028. channel = this.get_channel(options.channel_id);
  1029. var channel_cache = chat_manager.mail_tools.get_channel_cache(channel, options.domain);
  1030. if (channel_cache.loaded) {
  1031. return $.when(channel_cache.messages);
  1032. } else {
  1033. return chat_manager.mail_tools.fetch_from_channel(channel, {domain: options.domain});
  1034. }
  1035. }
  1036. if ('ids' in options) {
  1037. // get messages from their ids (chatter is the main use case)
  1038. return chat_manager.mail_tools.fetch_document_messages(options.ids, options).then(function(result) {
  1039. chat_manager.mark_as_read(options.ids);
  1040. return result;
  1041. });
  1042. }
  1043. if ('model' in options && 'res_id' in options) {
  1044. // get messages for a chatter, when it doesn't know the ids (use
  1045. // case is when using the full composer)
  1046. var domain = [['model', '=', options.model], ['res_id', '=', options.res_id]];
  1047. MessageModel.call('message_fetch', [domain], {limit: 30}).then(function (msgs) {
  1048. return _.map(msgs, chat_manager.mail_tools.add_message);
  1049. });
  1050. }
  1051. };
  1052. chat_manager.toggle_star_status = function (message_id) {
  1053. return MessageModel.call('toggle_message_starred', [[message_id]]);
  1054. };
  1055. chat_manager.unstar_all = function () {
  1056. return MessageModel.call('unstar_all', [[]], {});
  1057. };
  1058. chat_manager.mark_as_read = function (message_ids) {
  1059. var ids = _.filter(message_ids, function (id) {
  1060. var message = _.findWhere(messages, {id: id});
  1061. // If too many messages, not all are fetched, and some might not be found
  1062. return !message || message.is_needaction;
  1063. });
  1064. if (ids.length) {
  1065. return MessageModel.call('set_message_done', [ids]);
  1066. } else {
  1067. return $.when();
  1068. }
  1069. };
  1070. chat_manager.mark_all_as_read = function (channel, domain) {
  1071. if ((channel.id === "channel_inbox" && needaction_counter) || (channel && channel.needaction_counter)) {
  1072. return MessageModel.call('mark_all_as_read', [], {channel_ids: channel.id !== "channel_inbox" ? [channel.id] : [], domain: domain});
  1073. }
  1074. return $.when();
  1075. };
  1076. chat_manager.undo_mark_as_read = function (message_ids, channel) {
  1077. return MessageModel.call('mark_as_unread', [message_ids, [channel.id]]);
  1078. };
  1079. chat_manager.mark_channel_as_seen = function (channel) {
  1080. if (channel.unread_counter > 0 && channel.type !== 'static') {
  1081. chat_manager.mail_tools.update_channel_unread_counter(channel, 0);
  1082. channel_seen(channel);
  1083. }
  1084. };
  1085. chat_manager.get_channels = function () {
  1086. return _.clone(channels);
  1087. };
  1088. chat_manager.get_channel = function (id) {
  1089. return _.findWhere(channels, {id: id});
  1090. };
  1091. chat_manager.get_dm_from_partner_id = function (partner_id) {
  1092. return _.findWhere(channels, {direct_partner_id: partner_id});
  1093. };
  1094. chat_manager.all_history_loaded = function (channel, domain) {
  1095. return chat_manager.mail_tools.get_channel_cache(channel, domain).all_history_loaded;
  1096. };
  1097. chat_manager.get_mention_partner_suggestions = function (channel) {
  1098. if (!channel) {
  1099. return mention_partner_suggestions;
  1100. }
  1101. if (!channel.members_deferred) {
  1102. channel.members_deferred = ChannelModel
  1103. .call("channel_fetch_listeners", [channel.uuid], {}, {shadow: true})
  1104. .then(function (members) {
  1105. var suggestions = [];
  1106. _.each(mention_partner_suggestions, function (partners) {
  1107. suggestions.push(_.filter(partners, function (partner) {
  1108. return !_.findWhere(members, { id: partner.id });
  1109. }));
  1110. });
  1111. return [members];
  1112. });
  1113. }
  1114. return channel.members_deferred;
  1115. };
  1116. chat_manager.get_commands = function (channel) {
  1117. return _.filter(commands, function (command) {
  1118. return !command.channel_types || _.contains(command.channel_types, channel.server_type);
  1119. });
  1120. };
  1121. chat_manager.get_canned_responses = function () {
  1122. return canned_responses;
  1123. };
  1124. chat_manager.get_needaction_counter = function () {
  1125. return needaction_counter;
  1126. };
  1127. chat_manager.get_starred_counter = function () {
  1128. return starred_counter;
  1129. };
  1130. chat_manager.get_chat_unread_counter = function () {
  1131. return chat_unread_counter;
  1132. };
  1133. chat_manager.get_unread_conversation_counter = function () {
  1134. return unread_conversation_counter;
  1135. };
  1136. chat_manager.get_last_seen_message = function (channel) {
  1137. if (channel.last_seen_message_id) {
  1138. var messages = channel.cache['[]'].messages;
  1139. var msg = _.findWhere(messages, {id: channel.last_seen_message_id});
  1140. if (msg) {
  1141. var i = _.sortedIndex(messages, msg, 'id') + 1;
  1142. while (i < messages.length && (messages[i].is_author || messages[i].is_system_notification)) {
  1143. msg = messages[i];
  1144. i++;
  1145. }
  1146. return msg;
  1147. }
  1148. }
  1149. };
  1150. chat_manager.get_discuss_menu_id = function () {
  1151. return discuss_menu_id;
  1152. };
  1153. chat_manager.detach_channel = function (channel) {
  1154. return ChannelModel.call("channel_minimize", [channel.uuid, true], {}, {shadow: true});
  1155. };
  1156. chat_manager.remove_chatter_messages = function (model) {
  1157. messages = _.reject(messages, function (message) {
  1158. return message.channel_ids.length === 0 && message.model === model;
  1159. });
  1160. };
  1161. chat_manager.create_channel = function (name, type) {
  1162. var method = type === "dm" ? "channel_get" : "channel_create";
  1163. var args = type === "dm" ? [[name]] : [name, type];
  1164. return ChannelModel
  1165. .call(method, args)
  1166. .then(chat_manager.mail_tools.add_channel);
  1167. };
  1168. chat_manager.join_channel = function (channel_id, options) {
  1169. if (channel_id in channel_defs) {
  1170. // prevents concurrent calls to channel_join_and_get_info
  1171. return channel_defs[channel_id];
  1172. }
  1173. var channel = this.get_channel(channel_id);
  1174. if (channel) {
  1175. // channel already joined
  1176. channel_defs[channel_id] = $.when(channel);
  1177. } else {
  1178. channel_defs[channel_id] = ChannelModel
  1179. .call('channel_join_and_get_info', [[channel_id]])
  1180. .then(function (result) {
  1181. return chat_manager.mail_tools.add_channel(result, options);
  1182. });
  1183. }
  1184. return channel_defs[channel_id];
  1185. };
  1186. chat_manager.open_and_detach_dm = function (partner_id) {
  1187. return ChannelModel.call('channel_get_and_minimize', [[partner_id]]).then(chat_manager.mail_tools.add_channel);
  1188. };
  1189. chat_manager.open_channel = function (channel) {
  1190. chat_manager.bus.trigger(client_action_open ? 'open_channel' : 'detach_channel', channel);
  1191. };
  1192. chat_manager.unsubscribe = function (channel) {
  1193. var def;
  1194. if (_.contains(['public', 'private'], channel.type)) {
  1195. return ChannelModel.call('action_unfollow', [[channel.id]]);
  1196. } else {
  1197. return ChannelModel.call('channel_pin', [channel.uuid, false]);
  1198. }
  1199. };
  1200. chat_manager.close_chat_session = function (channel_id) {
  1201. var channel = this.get_channel(channel_id);
  1202. ChannelModel.call("channel_fold", [], {uuid : channel.uuid, state : "closed"}, {shadow: true});
  1203. };
  1204. chat_manager.fold_channel = function (channel_id, folded) {
  1205. var args = {
  1206. uuid: this.get_channel(channel_id).uuid
  1207. };
  1208. if (_.isBoolean(folded)) {
  1209. args.state = folded ? 'folded' : 'open';
  1210. }
  1211. return ChannelModel.call("channel_fold", [], args, {shadow: true});
  1212. };
  1213. /**
  1214. * Special redirection handling for given model and id
  1215. *
  1216. * If the model is res.partner, and there is a user associated with this
  1217. * partner which isn't the current user, open the DM with this user.
  1218. * Otherwhise, open the record's form view, if this is not the current user's.
  1219. */
  1220. chat_manager.redirect = function (res_model, res_id, dm_redirection_callback) {
  1221. var self = this;
  1222. var redirect_to_document = function (res_model, res_id, view_id) {
  1223. web_client.do_action({
  1224. type:'ir.actions.act_window',
  1225. view_type: 'form',
  1226. view_mode: 'form',
  1227. res_model: res_model,
  1228. views: [[view_id || false, 'form']],
  1229. res_id: res_id
  1230. });
  1231. };
  1232. if (res_model === "res.partner") {
  1233. var domain = [["partner_id", "=", res_id]];
  1234. UserModel.call("search", [domain]).then(function (user_ids) {
  1235. if (user_ids.length && user_ids[0] !== session.uid && dm_redirection_callback) {
  1236. self.create_channel(res_id, 'dm').then(dm_redirection_callback);
  1237. } else {
  1238. redirect_to_document(res_model, res_id);
  1239. }
  1240. });
  1241. } else {
  1242. new Model(res_model).call('get_formview_id', [[res_id], session.context]).then(function (view_id) {
  1243. redirect_to_document(res_model, res_id, view_id);
  1244. });
  1245. }
  1246. };
  1247. chat_manager.get_channels_preview = function (channels) {
  1248. var channels_preview = _.map(channels, function (channel) {
  1249. var info = _.pick(channel, 'id', 'is_chat', 'name', 'status', 'unread_counter');
  1250. info.last_message = _.last(channel.cache['[]'].messages);
  1251. if (!info.is_chat) {
  1252. info.image_src = '/web/image/mail.channel/'+channel.id+'/image_small';
  1253. } else if (channel.direct_partner_id) {
  1254. info.image_src = '/web/image/res.partner/'+channel.direct_partner_id+'/image_small';
  1255. } else {
  1256. info.image_src = '/mail/static/src/img/smiley/avatar.jpg';
  1257. }
  1258. return info;
  1259. });
  1260. var missing_channels = _.where(channels_preview, {last_message: undefined});
  1261. if (!channels_preview_def) {
  1262. if (missing_channels.length) {
  1263. var missing_channel_ids = _.pluck(missing_channels, 'id');
  1264. channels_preview_def = ChannelModel.call('channel_fetch_preview', [missing_channel_ids], {}, {shadow: true});
  1265. } else {
  1266. channels_preview_def = $.when();
  1267. }
  1268. }
  1269. return channels_preview_def.then(function (channels) {
  1270. _.each(missing_channels, function (channel_preview) {
  1271. var channel = _.findWhere(channels, {id: channel_preview.id});
  1272. if (channel) {
  1273. channel_preview.last_message = chat_manager.mail_tools.add_message(channel.last_message);
  1274. }
  1275. });
  1276. return _.filter(channels_preview, function (channel) {
  1277. return channel.last_message; // remove empty channels
  1278. });
  1279. });
  1280. };
  1281. chat_manager.get_message_body_preview = function (message_body) {
  1282. return utils.parse_and_transform(message_body, utils.inline);
  1283. };
  1284. chat_manager.search_partner = function (search_val, limit) {
  1285. var def = $.Deferred();
  1286. var values = [];
  1287. // search among prefetched partners
  1288. var search_regexp = new RegExp(_.str.escapeRegExp(utils.unaccent(search_val)), 'i');
  1289. _.each(mention_partner_suggestions, function (partners) {
  1290. if (values.length < limit) {
  1291. values = values.concat(_.filter(partners, function (partner) {
  1292. return session.partner_id !== partner.id && search_regexp.test(partner.name);
  1293. })).splice(0, limit);
  1294. }
  1295. });
  1296. if (!values.length) {
  1297. // extend the research to all users
  1298. def = PartnerModel.call('im_search', [search_val, limit || 20], {}, {shadow: true});
  1299. } else {
  1300. def = $.when(values);
  1301. }
  1302. return def.then(function (values) {
  1303. var autocomplete_data = _.map(values, function (value) {
  1304. return { id: value.id, value: value.name, label: value.name };
  1305. });
  1306. return _.sortBy(autocomplete_data, 'label');
  1307. });
  1308. };
  1309. chat_manager.bus.on('client_action_open', null, function (open) {
  1310. client_action_open = open;
  1311. });
  1312. // In order to extend init use chat_manager.is_ready Derrered object. See example in mail_arhive module
  1313. function init () {
  1314. chat_manager.mail_tools.add_channel({
  1315. id: "channel_inbox",
  1316. name: _lt("Inbox"),
  1317. type: "static",
  1318. }, { display_needactions: true });
  1319. chat_manager.mail_tools.add_channel({
  1320. id: "channel_starred",
  1321. name: _lt("Starred"),
  1322. type: "static"
  1323. });
  1324. // unsubscribe and then subscribe to the event, to avoid duplication of new messages
  1325. bus.off('notification');
  1326. bus.on('notification', null, function(){
  1327. chat_manager.mail_tools.on_notification.apply(chat_manager.mail_tools, arguments);
  1328. });
  1329. return session.rpc('/mail/client_action').then(function (result) {
  1330. _.each(result.channel_slots, function (channels) {
  1331. _.each(channels, chat_manager.mail_tools.add_channel);
  1332. });
  1333. needaction_counter = result.needaction_inbox_counter;
  1334. starred_counter = result.starred_counter;
  1335. commands = _.map(result.commands, function (command) {
  1336. return _.extend({ id: command.name }, command);
  1337. });
  1338. mention_partner_suggestions = result.mention_partner_suggestions;
  1339. discuss_menu_id = result.menu_id;
  1340. // Shortcodes: canned responses and emojis
  1341. _.each(result.shortcodes, function (s) {
  1342. if (s.shortcode_type === 'text') {
  1343. canned_responses.push(_.pick(s, ['id', 'source', 'substitution']));
  1344. } else {
  1345. emojis.push(_.pick(s, ['id', 'source', 'substitution', 'description']));
  1346. emoji_substitutions[_.escape(s.source)] = s.substitution;
  1347. }
  1348. });
  1349. bus.start_polling();
  1350. });
  1351. }
  1352. chat_manager.is_ready = init();
  1353. return {
  1354. ODOOBOT_ID: ODOOBOT_ID,
  1355. chat_manager: chat_manager,
  1356. MailTools: MailTools,
  1357. MailComposer: MailComposer
  1358. };
  1359. });