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.

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