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.

1458 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. },
  650. add_to_cache: function (message, domain) {
  651. _.each(message.channel_ids, function (channel_id) {
  652. var channel = chat_manager.get_channel(channel_id);
  653. if (channel) {
  654. var channel_cache = chat_manager.mail_tools.get_channel_cache(channel, domain);
  655. var index = _.sortedIndex(channel_cache.messages, message, 'id');
  656. if (channel_cache.messages[index] !== message) {
  657. channel_cache.messages.splice(index, 0, message);
  658. }
  659. }
  660. });
  661. },
  662. remove_from_cache: function(message, domain){
  663. var self = this;
  664. _.each(message.channel_ids, function (channel_id) {
  665. var channel = chat_manager.get_channel(channel_id);
  666. if (channel) {
  667. var channel_cache = self.get_channel_cache(channel, domain);
  668. var index = _.sortedIndex(channel_cache.messages, message, 'id');
  669. if (channel_cache.messages[index] === message) {
  670. channel_cache.messages.splice(index, 1);
  671. }
  672. }
  673. });
  674. },
  675. remove_message_from_channel: function (channel_id, message) {
  676. message.channel_ids = _.without(message.channel_ids, channel_id);
  677. var channel = _.findWhere(channels, { id: channel_id });
  678. _.each(channel.cache, function (cache) {
  679. cache.messages = _.without(cache.messages, message);
  680. });
  681. },
  682. get_domain: function(channel){
  683. return (channel.id === "channel_inbox") ? [['needaction', '=', true]] :
  684. (channel.id === "channel_starred") ? [['starred', '=', true]] : false;
  685. },
  686. // options: domain, load_more
  687. fetch_from_channel: function (channel, options) {
  688. options = options || {};
  689. var domain = chat_manager.mail_tools.get_domain(channel) || [['channel_ids', 'in', channel.id]];
  690. var cache = chat_manager.mail_tools.get_channel_cache(channel, options.domain);
  691. if (options.domain) {
  692. domain = new data.CompoundDomain(domain, options.domain || []);
  693. }
  694. if (options.load_more) {
  695. var min_message_id = cache.messages[0].id;
  696. domain = new data.CompoundDomain([['id', '<', min_message_id]], domain);
  697. }
  698. return MessageModel.call('message_fetch', [domain], {limit: LIMIT}).then(function (msgs) {
  699. if (!cache.all_history_loaded) {
  700. cache.all_history_loaded = msgs.length < LIMIT;
  701. }
  702. cache.loaded = true;
  703. _.each(msgs, function (msg) {
  704. chat_manager.mail_tools.add_message(msg, {channel_id: channel.id, silent: true, domain: options.domain});
  705. });
  706. var channel_cache = chat_manager.mail_tools.get_channel_cache(channel, options.domain || []);
  707. return channel_cache.messages;
  708. });
  709. },
  710. // options: force_fetch
  711. fetch_document_messages: function (ids, options) {
  712. var loaded_msgs = _.filter(messages, function (message) {
  713. return _.contains(ids, message.id);
  714. });
  715. var loaded_msg_ids = _.pluck(loaded_msgs, 'id');
  716. options = options || {};
  717. if (options.force_fetch || _.difference(ids.slice(0, LIMIT), loaded_msg_ids).length) {
  718. var ids_to_load = _.difference(ids, loaded_msg_ids).slice(0, LIMIT);
  719. return MessageModel.call('message_format', [ids_to_load]).then(function (msgs) {
  720. var processed_msgs = [];
  721. _.each(msgs, function (msg) {
  722. processed_msgs.push(chat_manager.mail_tools.add_message(msg, {silent: true}));
  723. });
  724. return _.sortBy(loaded_msgs.concat(processed_msgs), function (msg) {
  725. return msg.date;
  726. });
  727. });
  728. } else {
  729. return $.when(loaded_msgs);
  730. }
  731. },
  732. update_channel_unread_counter: function (channel, counter) {
  733. if (channel.unread_counter > 0 && counter === 0) {
  734. unread_conversation_counter = Math.max(0, unread_conversation_counter-1);
  735. } else if (channel.unread_counter === 0 && counter > 0) {
  736. unread_conversation_counter++;
  737. }
  738. if (channel.is_chat) {
  739. chat_unread_counter = Math.max(0, chat_unread_counter - channel.unread_counter + counter);
  740. }
  741. channel.unread_counter = counter;
  742. chat_manager.bus.trigger("update_channel_unread_counter", channel);
  743. },
  744. // Notification handlers
  745. // ---------------------------------------------------------------------------------
  746. on_notification: function (notifications) {
  747. // sometimes, the web client receives unsubscribe notification and an extra
  748. // notification on that channel. This is then followed by an attempt to
  749. // rejoin the channel that we just left. The next few lines remove the
  750. // extra notification to prevent that situation to occur.
  751. var unsubscribed_notif = _.find(notifications, function (notif) {
  752. return notif[1].info === "unsubscribe";
  753. });
  754. if (unsubscribed_notif) {
  755. notifications = _.reject(notifications, function (notif) {
  756. return notif[0][1] === "mail.channel" && notif[0][2] === unsubscribed_notif[1].id;
  757. });
  758. }
  759. _.each(notifications, function (notification) {
  760. var model = notification[0][1];
  761. if (model === 'ir.needaction') {
  762. // new message in the inbox
  763. chat_manager.mail_tools.on_needaction_notification(notification[1]);
  764. } else if (model === 'mail.channel') {
  765. // new message in a channel
  766. chat_manager.mail_tools.on_channel_notification(notification[1]);
  767. } else if (model === 'res.partner') {
  768. // channel joined/left, message marked as read/(un)starred, chat open/closed
  769. chat_manager.mail_tools.on_partner_notification(notification[1]);
  770. } else if (model === 'bus.presence') {
  771. // update presence of users
  772. chat_manager.mail_tools.on_presence_notification(notification[1]);
  773. } else if (model === 'mail_base.mail_sent') {
  774. // Delete cache in order to fetch new message
  775. // TODO find a solution without deleting cache. Currently
  776. // problem is that on inheriting send_mail in
  777. // mail.compose.message it's not possible to get id of new
  778. // message
  779. chat_manager.mail_tools.clear_cache_all_channels();
  780. }
  781. });
  782. },
  783. on_needaction_notification: function (message) {
  784. message = chat_manager.mail_tools.add_message(message, {
  785. channel_id: 'channel_inbox',
  786. show_notification: true,
  787. increment_unread: true
  788. });
  789. chat_manager.mail_tools.invalidate_caches(message.channel_ids);
  790. needaction_counter++;
  791. _.each(message.channel_ids, function (channel_id) {
  792. var channel = chat_manager.get_channel(channel_id);
  793. if (channel) {
  794. channel.needaction_counter++;
  795. }
  796. });
  797. chat_manager.bus.trigger('update_needaction', needaction_counter);
  798. },
  799. on_channel_notification: function (message) {
  800. var def;
  801. var channel_already_in_cache = true;
  802. if (message.channel_ids.length === 1) {
  803. channel_already_in_cache = !!chat_manager.get_channel(message.channel_ids[0]);
  804. def = chat_manager.join_channel(message.channel_ids[0], {autoswitch: false});
  805. } else {
  806. def = $.when();
  807. }
  808. def.then(function () {
  809. // don't increment unread if channel wasn't in cache yet as its unread counter has just been fetched
  810. chat_manager.mail_tools.add_message(message, { show_notification: true, increment_unread: channel_already_in_cache });
  811. chat_manager.mail_tools.invalidate_caches(message.channel_ids);
  812. });
  813. },
  814. on_partner_notification: function (data) {
  815. if (data.info === "unsubscribe") {
  816. var channel = chat_manager.get_channel(data.id);
  817. if (channel) {
  818. var msg;
  819. if (_.contains(['public', 'private'], channel.type)) {
  820. msg = _.str.sprintf(_t('You unsubscribed from <b>%s</b>.'), channel.name);
  821. } else {
  822. msg = _.str.sprintf(_t('You unpinned your conversation with <b>%s</b>.'), channel.name);
  823. }
  824. this.remove_channel(channel);
  825. chat_manager.bus.trigger("unsubscribe_from_channel", data.id);
  826. web_client.do_notify(_("Unsubscribed"), msg);
  827. }
  828. } else if (data.type === 'toggle_star') {
  829. chat_manager.mail_tools.on_toggle_star_notification(data);
  830. } else if (data.type === 'mark_as_read') {
  831. chat_manager.mail_tools.on_mark_as_read_notification(data);
  832. } else if (data.type === 'mark_as_unread') {
  833. chat_manager.mail_tools.on_mark_as_unread_notification(data);
  834. } else if (data.info === 'channel_seen') {
  835. chat_manager.mail_tools.on_channel_seen_notification(data);
  836. } else if (data.info === 'transient_message') {
  837. chat_manager.mail_tools.on_transient_message_notification(data);
  838. } else {
  839. chat_manager.mail_tools.on_chat_session_notification(data);
  840. }
  841. },
  842. on_toggle_star_notification: function (data) {
  843. _.each(data.message_ids, function (msg_id) {
  844. var message = _.findWhere(messages, { id: msg_id });
  845. if (message) {
  846. chat_manager.mail_tools.invalidate_caches(message.channel_ids);
  847. message.is_starred = data.starred;
  848. if (!message.is_starred) {
  849. chat_manager.mail_tools.remove_message_from_channel("channel_starred", message);
  850. starred_counter--;
  851. } else {
  852. chat_manager.mail_tools.add_to_cache(message, []);
  853. var channel_starred = chat_manager.get_channel('channel_starred');
  854. channel_starred.cache = _.pick(channel_starred.cache, "[]");
  855. starred_counter++;
  856. }
  857. chat_manager.bus.trigger('update_message', message);
  858. }
  859. });
  860. chat_manager.bus.trigger('update_starred', starred_counter);
  861. },
  862. on_mark_as_read_notification: function (data) {
  863. _.each(data.message_ids, function (msg_id) {
  864. var message = _.findWhere(messages, { id: msg_id });
  865. if (message) {
  866. chat_manager.mail_tools.invalidate_caches(message.channel_ids);
  867. chat_manager.mail_tools.remove_message_from_channel("channel_inbox", message);
  868. chat_manager.bus.trigger('update_message', message);
  869. }
  870. });
  871. if (data.channel_ids) {
  872. _.each(data.channel_ids, function (channel_id) {
  873. var channel = chat_manager.get_channel(channel_id);
  874. if (channel) {
  875. channel.needaction_counter = Math.max(channel.needaction_counter - data.message_ids.length, 0);
  876. }
  877. });
  878. } else { // if no channel_ids specified, this is a 'mark all read' in the inbox
  879. _.each(channels, function (channel) {
  880. channel.needaction_counter = 0;
  881. });
  882. }
  883. needaction_counter = Math.max(needaction_counter - data.message_ids.length, 0);
  884. chat_manager.bus.trigger('update_needaction', needaction_counter);
  885. },
  886. on_mark_as_unread_notification: function (data) {
  887. _.each(data.message_ids, function (message_id) {
  888. var message = _.findWhere(messages, { id: message_id });
  889. if (message) {
  890. chat_manager.mail_tools.invalidate_caches(message.channel_ids);
  891. chat_manager.mail_tools.add_channel_to_message(message, 'channel_inbox');
  892. chat_manager.mail_tools.add_to_cache(message, []);
  893. }
  894. });
  895. var channel_inbox = chat_manager.get_channel('channel_inbox');
  896. channel_inbox.cache = _.pick(channel_inbox.cache, "[]");
  897. _.each(data.channel_ids, function (channel_id) {
  898. var channel = chat_manager.get_channel(channel_id);
  899. if (channel) {
  900. channel.needaction_counter += data.message_ids.length;
  901. }
  902. });
  903. needaction_counter += data.message_ids.length;
  904. chat_manager.bus.trigger('update_needaction', needaction_counter);
  905. },
  906. on_channel_seen_notification: function (data) {
  907. var channel = chat_manager.get_channel(data.id);
  908. if (channel) {
  909. channel.last_seen_message_id = data.last_message_id;
  910. if (channel.unread_counter) {
  911. chat_manager.mail_tools.update_channel_unread_counter(channel, 0);
  912. }
  913. }
  914. },
  915. on_chat_session_notification: function (chat_session) {
  916. var channel;
  917. if ((chat_session.channel_type === "channel") && (chat_session.state === "open")) {
  918. chat_manager.mail_tools.add_channel(chat_session, {autoswitch: false});
  919. if (!chat_session.is_minimized && chat_session.info !== 'creation') {
  920. web_client.do_notify(_t("Invitation"), _t("You have been invited to: ") + chat_session.name);
  921. }
  922. }
  923. // partner specific change (open a detached window for example)
  924. if ((chat_session.state === "open") || (chat_session.state === "folded")) {
  925. channel = chat_session.is_minimized && chat_manager.get_channel(chat_session.id);
  926. if (channel) {
  927. channel.is_detached = true;
  928. channel.is_folded = (chat_session.state === "folded");
  929. chat_manager.bus.trigger("open_chat", channel);
  930. }
  931. } else if (chat_session.state === "closed") {
  932. channel = chat_manager.get_channel(chat_session.id);
  933. if (channel) {
  934. channel.is_detached = false;
  935. chat_manager.bus.trigger("close_chat", channel, {keep_open_if_unread: true});
  936. }
  937. }
  938. },
  939. on_presence_notification: function (data) {
  940. var dm = chat_manager.get_dm_from_partner_id(data.id);
  941. if (dm) {
  942. dm.status = data.im_status;
  943. chat_manager.bus.trigger('update_dm_presence', dm);
  944. }
  945. },
  946. on_transient_message_notification: function (data) {
  947. var last_message = _.last(messages);
  948. data.id = (last_message ? last_message.id : 0) + 0.01;
  949. data.author_id = data.author_id || ODOOBOT_ID;
  950. chat_manager.mail_tools.add_message(data);
  951. }
  952. });
  953. var cls = new MailTools();
  954. // Public interface
  955. //----------------------------------------------------------------------------------
  956. chat_manager.mail_tools = cls;
  957. // we add this function this way in order to make them extendable via MailTools.include({...})
  958. chat_manager.make_message = function(){
  959. return chat_manager.mail_tools.make_message.apply(chat_manager.mail_tools, arguments);
  960. };
  961. chat_manager.make_channel = function(){
  962. return chat_manager.mail_tools.make_channel.apply(chat_manager.mail_tools, arguments);
  963. };
  964. chat_manager.post_message = function (data, options) {
  965. options = options || {};
  966. var msg = {
  967. partner_ids: data.partner_ids,
  968. body: _.str.trim(data.content),
  969. attachment_ids: data.attachment_ids
  970. };
  971. if ('subject' in data) {
  972. msg.subject = data.subject;
  973. }
  974. if ('channel_id' in options) {
  975. // post a message in a channel or execute a command
  976. return ChannelModel.call(data.command ? 'execute_command' : 'message_post', [options.channel_id], _.extend(msg, {
  977. message_type: 'comment',
  978. content_subtype: 'html',
  979. subtype: 'mail.mt_comment',
  980. command: data.command,
  981. }));
  982. }
  983. if ('model' in options && 'res_id' in options) {
  984. // post a message in a chatter
  985. _.extend(msg, {
  986. content_subtype: data.content_subtype,
  987. context: data.context,
  988. message_type: data.message_type,
  989. subtype: data.subtype,
  990. subtype_id: data.subtype_id
  991. });
  992. if (options.model && options.res_id){
  993. var model = new Model(options.model);
  994. return model.call('message_post', [options.res_id], msg).then(function (msg_id) {
  995. return MessageModel.call('message_format', [msg_id]).then(function (msgs) {
  996. msgs[0].model = options.model;
  997. msgs[0].res_id = options.res_id;
  998. chat_manager.mail_tools.add_message(msgs[0]);
  999. });
  1000. });
  1001. } else {
  1002. options.model = 'mail.compose.message';
  1003. var compose_model = new Model(options.model);
  1004. return compose_model.call('create', [msg, {default_parent_id: options.parent_id}]).then(function(id){
  1005. return compose_model.call('send_mail_action', [id, {}]);
  1006. });
  1007. }
  1008. }
  1009. };
  1010. chat_manager.get_message = function (id) {
  1011. return _.findWhere(messages, {id: id});
  1012. };
  1013. chat_manager.get_messages = function (options) {
  1014. var channel;
  1015. if ('channel_id' in options && options.load_more) {
  1016. // get channel messages, force load_more
  1017. channel = this.get_channel(options.channel_id);
  1018. return chat_manager.mail_tools.fetch_from_channel(channel, {domain: options.domain || {}, load_more: true});
  1019. }
  1020. if ('channel_id' in options) {
  1021. // channel message, check in cache first
  1022. channel = this.get_channel(options.channel_id);
  1023. var channel_cache = chat_manager.mail_tools.get_channel_cache(channel, options.domain);
  1024. if (channel_cache.loaded) {
  1025. return $.when(channel_cache.messages);
  1026. } else {
  1027. return chat_manager.mail_tools.fetch_from_channel(channel, {domain: options.domain});
  1028. }
  1029. }
  1030. if ('ids' in options) {
  1031. // get messages from their ids (chatter is the main use case)
  1032. return chat_manager.mail_tools.fetch_document_messages(options.ids, options).then(function(result) {
  1033. chat_manager.mark_as_read(options.ids);
  1034. return result;
  1035. });
  1036. }
  1037. if ('model' in options && 'res_id' in options) {
  1038. // get messages for a chatter, when it doesn't know the ids (use
  1039. // case is when using the full composer)
  1040. var domain = [['model', '=', options.model], ['res_id', '=', options.res_id]];
  1041. MessageModel.call('message_fetch', [domain], {limit: 30}).then(function (msgs) {
  1042. return _.map(msgs, chat_manager.mail_tools.add_message);
  1043. });
  1044. }
  1045. };
  1046. chat_manager.toggle_star_status = function (message_id) {
  1047. return MessageModel.call('toggle_message_starred', [[message_id]]);
  1048. };
  1049. chat_manager.unstar_all = function () {
  1050. return MessageModel.call('unstar_all', [[]], {});
  1051. };
  1052. chat_manager.mark_as_read = function (message_ids) {
  1053. var ids = _.filter(message_ids, function (id) {
  1054. var message = _.findWhere(messages, {id: id});
  1055. // If too many messages, not all are fetched, and some might not be found
  1056. return !message || message.is_needaction;
  1057. });
  1058. if (ids.length) {
  1059. return MessageModel.call('set_message_done', [ids]);
  1060. } else {
  1061. return $.when();
  1062. }
  1063. };
  1064. chat_manager.mark_all_as_read = function (channel, domain) {
  1065. if ((channel.id === "channel_inbox" && needaction_counter) || (channel && channel.needaction_counter)) {
  1066. return MessageModel.call('mark_all_as_read', [], {channel_ids: channel.id !== "channel_inbox" ? [channel.id] : [], domain: domain});
  1067. }
  1068. return $.when();
  1069. };
  1070. chat_manager.undo_mark_as_read = function (message_ids, channel) {
  1071. return MessageModel.call('mark_as_unread', [message_ids, [channel.id]]);
  1072. };
  1073. chat_manager.mark_channel_as_seen = function (channel) {
  1074. if (channel.unread_counter > 0 && channel.type !== 'static') {
  1075. chat_manager.mail_tools.update_channel_unread_counter(channel, 0);
  1076. channel_seen(channel);
  1077. }
  1078. };
  1079. chat_manager.get_channels = function () {
  1080. return _.clone(channels);
  1081. };
  1082. chat_manager.get_channel = function (id) {
  1083. return _.findWhere(channels, {id: id});
  1084. };
  1085. chat_manager.get_dm_from_partner_id = function (partner_id) {
  1086. return _.findWhere(channels, {direct_partner_id: partner_id});
  1087. };
  1088. chat_manager.all_history_loaded = function (channel, domain) {
  1089. return chat_manager.mail_tools.get_channel_cache(channel, domain).all_history_loaded;
  1090. };
  1091. chat_manager.get_mention_partner_suggestions = function (channel) {
  1092. if (!channel) {
  1093. return mention_partner_suggestions;
  1094. }
  1095. if (!channel.members_deferred) {
  1096. channel.members_deferred = ChannelModel
  1097. .call("channel_fetch_listeners", [channel.uuid], {}, {shadow: true})
  1098. .then(function (members) {
  1099. var suggestions = [];
  1100. _.each(mention_partner_suggestions, function (partners) {
  1101. suggestions.push(_.filter(partners, function (partner) {
  1102. return !_.findWhere(members, { id: partner.id });
  1103. }));
  1104. });
  1105. return [members];
  1106. });
  1107. }
  1108. return channel.members_deferred;
  1109. };
  1110. chat_manager.get_commands = function (channel) {
  1111. return _.filter(commands, function (command) {
  1112. return !command.channel_types || _.contains(command.channel_types, channel.server_type);
  1113. });
  1114. };
  1115. chat_manager.get_canned_responses = function () {
  1116. return canned_responses;
  1117. };
  1118. chat_manager.get_needaction_counter = function () {
  1119. return needaction_counter;
  1120. };
  1121. chat_manager.get_starred_counter = function () {
  1122. return starred_counter;
  1123. };
  1124. chat_manager.get_chat_unread_counter = function () {
  1125. return chat_unread_counter;
  1126. };
  1127. chat_manager.get_unread_conversation_counter = function () {
  1128. return unread_conversation_counter;
  1129. };
  1130. chat_manager.get_last_seen_message = function (channel) {
  1131. if (channel.last_seen_message_id) {
  1132. var messages = channel.cache['[]'].messages;
  1133. var msg = _.findWhere(messages, {id: channel.last_seen_message_id});
  1134. if (msg) {
  1135. var i = _.sortedIndex(messages, msg, 'id') + 1;
  1136. while (i < messages.length && (messages[i].is_author || messages[i].is_system_notification)) {
  1137. msg = messages[i];
  1138. i++;
  1139. }
  1140. return msg;
  1141. }
  1142. }
  1143. };
  1144. chat_manager.get_discuss_menu_id = function () {
  1145. return discuss_menu_id;
  1146. };
  1147. chat_manager.detach_channel = function (channel) {
  1148. return ChannelModel.call("channel_minimize", [channel.uuid, true], {}, {shadow: true});
  1149. };
  1150. chat_manager.remove_chatter_messages = function (model) {
  1151. messages = _.reject(messages, function (message) {
  1152. return message.channel_ids.length === 0 && message.model === model;
  1153. });
  1154. };
  1155. chat_manager.create_channel = function (name, type) {
  1156. var method = type === "dm" ? "channel_get" : "channel_create";
  1157. var args = type === "dm" ? [[name]] : [name, type];
  1158. return ChannelModel
  1159. .call(method, args)
  1160. .then(chat_manager.mail_tools.add_channel);
  1161. };
  1162. chat_manager.join_channel = function (channel_id, options) {
  1163. if (channel_id in channel_defs) {
  1164. // prevents concurrent calls to channel_join_and_get_info
  1165. return channel_defs[channel_id];
  1166. }
  1167. var channel = this.get_channel(channel_id);
  1168. if (channel) {
  1169. // channel already joined
  1170. channel_defs[channel_id] = $.when(channel);
  1171. } else {
  1172. channel_defs[channel_id] = ChannelModel
  1173. .call('channel_join_and_get_info', [[channel_id]])
  1174. .then(function (result) {
  1175. return chat_manager.mail_tools.add_channel(result, options);
  1176. });
  1177. }
  1178. return channel_defs[channel_id];
  1179. };
  1180. chat_manager.open_and_detach_dm = function (partner_id) {
  1181. return ChannelModel.call('channel_get_and_minimize', [[partner_id]]).then(chat_manager.mail_tools.add_channel);
  1182. };
  1183. chat_manager.open_channel = function (channel) {
  1184. chat_manager.bus.trigger(client_action_open ? 'open_channel' : 'detach_channel', channel);
  1185. };
  1186. chat_manager.unsubscribe = function (channel) {
  1187. var def;
  1188. if (_.contains(['public', 'private'], channel.type)) {
  1189. return ChannelModel.call('action_unfollow', [[channel.id]]);
  1190. } else {
  1191. return ChannelModel.call('channel_pin', [channel.uuid, false]);
  1192. }
  1193. };
  1194. chat_manager.close_chat_session = function (channel_id) {
  1195. var channel = this.get_channel(channel_id);
  1196. ChannelModel.call("channel_fold", [], {uuid : channel.uuid, state : "closed"}, {shadow: true});
  1197. };
  1198. chat_manager.fold_channel = function (channel_id, folded) {
  1199. var args = {
  1200. uuid: this.get_channel(channel_id).uuid
  1201. };
  1202. if (_.isBoolean(folded)) {
  1203. args.state = folded ? 'folded' : 'open';
  1204. }
  1205. return ChannelModel.call("channel_fold", [], args, {shadow: true});
  1206. };
  1207. /**
  1208. * Special redirection handling for given model and id
  1209. *
  1210. * If the model is res.partner, and there is a user associated with this
  1211. * partner which isn't the current user, open the DM with this user.
  1212. * Otherwhise, open the record's form view, if this is not the current user's.
  1213. */
  1214. chat_manager.redirect = function (res_model, res_id, dm_redirection_callback) {
  1215. var self = this;
  1216. var redirect_to_document = function (res_model, res_id, view_id) {
  1217. web_client.do_action({
  1218. type:'ir.actions.act_window',
  1219. view_type: 'form',
  1220. view_mode: 'form',
  1221. res_model: res_model,
  1222. views: [[view_id || false, 'form']],
  1223. res_id: res_id
  1224. });
  1225. };
  1226. if (res_model === "res.partner") {
  1227. var domain = [["partner_id", "=", res_id]];
  1228. UserModel.call("search", [domain]).then(function (user_ids) {
  1229. if (user_ids.length && user_ids[0] !== session.uid && dm_redirection_callback) {
  1230. self.create_channel(res_id, 'dm').then(dm_redirection_callback);
  1231. } else {
  1232. redirect_to_document(res_model, res_id);
  1233. }
  1234. });
  1235. } else {
  1236. new Model(res_model).call('get_formview_id', [[res_id], session.context]).then(function (view_id) {
  1237. redirect_to_document(res_model, res_id, view_id);
  1238. });
  1239. }
  1240. };
  1241. chat_manager.get_channels_preview = function (channels) {
  1242. var channels_preview = _.map(channels, function (channel) {
  1243. var info = _.pick(channel, 'id', 'is_chat', 'name', 'status', 'unread_counter');
  1244. info.last_message = _.last(channel.cache['[]'].messages);
  1245. if (!info.is_chat) {
  1246. info.image_src = '/web/image/mail.channel/'+channel.id+'/image_small';
  1247. } else if (channel.direct_partner_id) {
  1248. info.image_src = '/web/image/res.partner/'+channel.direct_partner_id+'/image_small';
  1249. } else {
  1250. info.image_src = '/mail/static/src/img/smiley/avatar.jpg';
  1251. }
  1252. return info;
  1253. });
  1254. var missing_channels = _.where(channels_preview, {last_message: undefined});
  1255. if (!channels_preview_def) {
  1256. if (missing_channels.length) {
  1257. var missing_channel_ids = _.pluck(missing_channels, 'id');
  1258. channels_preview_def = ChannelModel.call('channel_fetch_preview', [missing_channel_ids], {}, {shadow: true});
  1259. } else {
  1260. channels_preview_def = $.when();
  1261. }
  1262. }
  1263. return channels_preview_def.then(function (channels) {
  1264. _.each(missing_channels, function (channel_preview) {
  1265. var channel = _.findWhere(channels, {id: channel_preview.id});
  1266. if (channel) {
  1267. channel_preview.last_message = chat_manager.mail_tools.add_message(channel.last_message);
  1268. }
  1269. });
  1270. return _.filter(channels_preview, function (channel) {
  1271. return channel.last_message; // remove empty channels
  1272. });
  1273. });
  1274. };
  1275. chat_manager.get_message_body_preview = function (message_body) {
  1276. return utils.parse_and_transform(message_body, utils.inline);
  1277. };
  1278. chat_manager.search_partner = function (search_val, limit) {
  1279. var def = $.Deferred();
  1280. var values = [];
  1281. // search among prefetched partners
  1282. var search_regexp = new RegExp(_.str.escapeRegExp(utils.unaccent(search_val)), 'i');
  1283. _.each(mention_partner_suggestions, function (partners) {
  1284. if (values.length < limit) {
  1285. values = values.concat(_.filter(partners, function (partner) {
  1286. return session.partner_id !== partner.id && search_regexp.test(partner.name);
  1287. })).splice(0, limit);
  1288. }
  1289. });
  1290. if (!values.length) {
  1291. // extend the research to all users
  1292. def = PartnerModel.call('im_search', [search_val, limit || 20], {}, {shadow: true});
  1293. } else {
  1294. def = $.when(values);
  1295. }
  1296. return def.then(function (values) {
  1297. var autocomplete_data = _.map(values, function (value) {
  1298. return { id: value.id, value: value.name, label: value.name };
  1299. });
  1300. return _.sortBy(autocomplete_data, 'label');
  1301. });
  1302. };
  1303. chat_manager.bus.on('client_action_open', null, function (open) {
  1304. client_action_open = open;
  1305. });
  1306. // In order to extend init use chat_manager.is_ready Derrered object. See example in mail_arhive module
  1307. function init () {
  1308. chat_manager.mail_tools.add_channel({
  1309. id: "channel_inbox",
  1310. name: _lt("Inbox"),
  1311. type: "static",
  1312. }, { display_needactions: true });
  1313. chat_manager.mail_tools.add_channel({
  1314. id: "channel_starred",
  1315. name: _lt("Starred"),
  1316. type: "static"
  1317. });
  1318. // unsubscribe and then subscribe to the event, to avoid duplication of new messages
  1319. bus.off('notification');
  1320. bus.on('notification', null, function(){
  1321. chat_manager.mail_tools.on_notification.apply(chat_manager.mail_tools, arguments);
  1322. });
  1323. return session.rpc('/mail/client_action').then(function (result) {
  1324. _.each(result.channel_slots, function (channels) {
  1325. _.each(channels, chat_manager.mail_tools.add_channel);
  1326. });
  1327. needaction_counter = result.needaction_inbox_counter;
  1328. starred_counter = result.starred_counter;
  1329. commands = _.map(result.commands, function (command) {
  1330. return _.extend({ id: command.name }, command);
  1331. });
  1332. mention_partner_suggestions = result.mention_partner_suggestions;
  1333. discuss_menu_id = result.menu_id;
  1334. // Shortcodes: canned responses and emojis
  1335. _.each(result.shortcodes, function (s) {
  1336. if (s.shortcode_type === 'text') {
  1337. canned_responses.push(_.pick(s, ['id', 'source', 'substitution']));
  1338. } else {
  1339. emojis.push(_.pick(s, ['id', 'source', 'substitution', 'description']));
  1340. emoji_substitutions[_.escape(s.source)] = s.substitution;
  1341. }
  1342. });
  1343. bus.start_polling();
  1344. });
  1345. }
  1346. chat_manager.is_ready = init();
  1347. return {
  1348. ODOOBOT_ID: ODOOBOT_ID,
  1349. chat_manager: chat_manager,
  1350. MailTools: MailTools,
  1351. MailComposer: MailComposer
  1352. };
  1353. });