summaryrefslogtreecommitdiff
path: root/addons/mail/static/src/js/models/threads/document_thread.js
diff options
context:
space:
mode:
authorSébastien Theys <seb@odoo.com>2020-06-15 18:12:21 +0000
committerSébastien Theys <seb@odoo.com>2020-06-15 18:12:21 +0000
commit3fea5b2136627c1d7e83b2a5e111952fe3eb10a1 (patch)
tree5eefad07972a87ce4689b83260f981bc87aadad4 /addons/mail/static/src/js/models/threads/document_thread.js
parent0a82848606151ef60eb6523e9c4c161171f214e7 (diff)
[REF] mail, *: refactor messaging with OWL
This commit is a significant rewriting of client-side discuss, chatter, chat window, and messaging menu using OWL. The behavior should be broadly the same, with some slight functional changes here and there. From a technical standpoint, the code of messaging is mainly organized in 2 main groups of modules: - models, which are logical entities that depict the client-side state of messaging as a whole. - components, which are in charge of displaying information from models. This refactoring also introduces new JS guidelines regarding folder structure (/static) and naming rules for JS modules. Community PR: https://github.com/odoo/odoo/pull/39023 Enterprise PR: https://github.com/odoo/enterprise/pull/6249 Task-1914207 This PR is a collaborative work by Alexandre, Julien, Sébastien and Xavier, with the precious help of Lucas to speed it up towards the end. closes odoo/odoo#39023 Related: odoo/enterprise#6249 Signed-off-by: Alexandre Kühn (aku) <aku@odoo.com> Co-authored-by: Alexandre Kühn <aku@odoo.com> Co-authored-by: Julien Giannone <jgi@odoo.com> Co-authored-by: Lucas Perais <lpe@odoo.com> Co-authored-by: Sébastien Theys <seb@odoo.com> Co-authored-by: Xavier Dubuc <xdu@odoo.com>
Diffstat (limited to 'addons/mail/static/src/js/models/threads/document_thread.js')
-rw-r--r--addons/mail/static/src/js/models/threads/document_thread.js364
1 files changed, 0 insertions, 364 deletions
diff --git a/addons/mail/static/src/js/models/threads/document_thread.js b/addons/mail/static/src/js/models/threads/document_thread.js
deleted file mode 100644
index eefc8f9bd79..00000000000
--- a/addons/mail/static/src/js/models/threads/document_thread.js
+++ /dev/null
@@ -1,364 +0,0 @@
-odoo.define('mail.model.DocumentThread', function (require) {
-"use strict";
-
-var Thread = require('mail.model.Thread');
-
-var session = require('web.session');
-
-/**
- * These threads represent threads that are directly attached to documents.
- * It is sometimes called the "chatter", but technically this is just the
- * part of the chatter containing the thread, as the chatter also contains
- * the activity and listener fields.
- *
- * Note that there are still some hacks in the handling of document threads,
- * due to document threads are instantiated from fetched messages, either from
- * mail.ThreadField or by fetching messages in Inbox.
- *
- * Also, because messages are not sent on the longpoll bus for this kind of
- * threads, it stores the list of message IDs that it contains, which should
- * be up-to-date whenever someone wants to get the messages in the document
- * thread.
- */
-var DocumentThread = Thread.extend({
-
- /**
- * @override
- * @param {Object} params
- * @param {Object} params.data
- * @param {string} [params.data.messagesIDs] the list of message ids linked
- * to the document (if not given, they will be fetched before fetching the
- * messages)
- * @param {string} params.data.model
- * @param {integer} params.data.resID
- */
- init: function (params) {
- var data = params.data;
- data.id = data.resModel + '_' + data.resID;
- data.type = 'document_thread';
-
- this._super.apply(this, arguments);
-
- // ID of the related document
- this._documentID = data.resID;
- // model of the related document
- this._documentModel = data.resModel;
- // used to handle history of messages
- this._messageIDs = data.messageIDs || [];
- // list of loaded messages
- this._messages = [];
-
- // if the messageIDs haven't been given, fetch them before fetching the
- // messages
- this._mustFetchMessageIDs = !_.isArray(data.messageIDs);
- },
-
- //--------------------------------------------------------------------------
- // Public
- //--------------------------------------------------------------------------
-
- /**
- * Overrides to store the thread's state in the LocalStorage, so that it is
- * shared between tabs, and restored on F5.
- *
- * @override
- * @param {Object} [options]
- * @param {boolean} [options.skipCrossTabSync=false] if set, it should
- * not notify other tabs from updating document thread state. This happens
- * in case the `close` operation comes from local storage event.
- */
- close: function (options) {
- this._super.apply(this, arguments);
- if (options && options.skipCrossTabSync) {
- return;
- }
- this.call('mail_service', 'updateDocumentThreadState', this._id, {
- name: this.getName(),
- windowState: 'closed',
- });
- },
- /**
- * Overrides to store the thread's state in the LocalStorage, so that it is
- * shared between tabs, and restored on F5.
- *
- * @override
- * @param {Object} [options]
- * @param {boolean} [options.skipCrossTabSync=false] if set, it should
- * not notify other tabs from updating document thread state. This happens
- * in case the `detach` operation comes from local storage event.
- */
- detach: function (options) {
- this._super.apply(this, arguments);
- if (options && options.skipCrossTabSync) {
- return;
- }
- var windowState = this._folded ? 'folded' : 'open';
- this.call('mail_service', 'updateDocumentThreadState', this._id, {
- name: this.getName(),
- windowState: windowState,
- });
- },
- /**
- *
- * Fetch messages of the document thread
- *
- * @override
- * @param {Object} options
- * @param {boolean} [options.forceFetch] if true, fetch anyway, as user
- * clicked on 'load more'.
- * @returns {Promise<mail.model.Message[]>}
- */
- fetchMessages: function (options) {
- var self = this;
- return this._fetchMessages(options).then(function () {
- return self._messages;
- });
- },
- /**
- * Overrides to store the thread's state in the LocalStorage, so that it is
- * shared between tabs, and restored on F5.
- *
- * @override
- * @param {Object} [options]
- * @param {boolean} [options.skipCrossTabSync=false] if set, it should
- * not notify other tabs from updating document thread state. This happens
- * in case the `detach` operation comes from local storage event.
- */
- fold: function (folded, options) {
- this._super.apply(this, arguments);
- if (options && options.skipCrossTabSync) {
- return;
- }
- var windowState = this._folded ? 'folded' : 'open';
- this.call('mail_service', 'updateDocumentThreadState', this._id, {
- name: this.getName(),
- windowState: windowState,
- });
- },
- /**
- * Get the model name of the document that is linked to this document thread
- *
- * @returns {string}
- */
- getDocumentModel: function () {
- return this._documentModel;
- },
- /**
- * Get the ID of the document that is linked to this document thread
- *
- * @returns {integer}
- */
- getDocumentID: function () {
- return this._documentID;
- },
- /**
- * Get he list of message IDs that this document threads contain.
- *
- * @returns {integer[]}
- */
- getMessageIDs: function () {
- return this._messageIDs;
- },
- /**
- * @override
- * @returns {mail.model.Message[]}
- */
- getMessages: function () {
- return this._messages;
- },
- /**
- * States whether the thread is linked to a document
- * Document thread's are always linked to a document.
- *
- * @override
- * @returns {boolean}
- */
- isLinkedToDocument: function () {
- return true;
- },
- /**
- * @param {integer[]} attachmentIDs
- */
- removeAttachmentsFromMessages: function (attachmentIDs) {
- _.each(this.getMessages(), function (message) {
- message.removeAttachments(attachmentIDs);
- });
- },
- /**
- * Set list of message IDs of this document thread
- *
- * Useful in order to handle message history of the document thread,
- * in particular to fetch messages when necessary and/or display 'load more'.
- *
- * @param {integer[]} messageIDs
- */
- setMessageIDs: function (messageIDs) {
- this._mustFetchMessageIDs = false;
- this._messageIDs = messageIDs;
- },
-
- //--------------------------------------------------------------------------
- // Private
- //--------------------------------------------------------------------------
-
- /**
- * Add this message to this document thread.
- *
- * This is ignored if the message is already linked to this document thread.
- * Also, there is no fetch of unread message counter in a document thread,
- * as this object is built from messages and/or thread field widget. The
- * unread counter of a document thread is the sum of needaction messages
- * linked to this document thread.
- *
- * @override
- * @private
- * @param {mail.model.Message} message
- */
- _addMessage: function (message) {
- this._super.apply(this, arguments);
- if (_.contains(this._messages, message)) {
- return;
- }
- // update internal list of messages
- this._messages.push(message);
- this._messages = _.sortBy(this._messages, function (msg) {
- return msg.getID();
- });
- // update message ids associated to this document thread
- if (!_.contains(this._messageIDs, message.getID())) {
- this._messageIDs.push(message.getID());
- }
- // update unread counter
- if (message.isNeedaction()) {
- this._unreadCounter++;
- }
- },
- /**
- * Get most up to date messageIDs
- *
- * @private
- * @returns {Promise} resolved when message IDs have been fetched and set
- * in the model
- */
- _fetchMessageIDs: function () {
- var self = this;
- var resID = this.getDocumentID();
- if (!resID || !this._mustFetchMessageIDs) {
- return Promise.resolve();
- }
- return this._rpc({
- model: this.getDocumentModel(),
- method: 'read',
- args: [[resID], ['message_ids']],
- }).then(function (result) {
- self.setMessageIDs(result[0].message_ids);
- });
- },
- /**
- * @override
- * @private
- * @param {Object} options
- * @param {boolean} [options.forceFetch]
- * @returns {Promise} resolved when messages have been fetched + document
- * thread has updated messages
- */
- _fetchMessages: function (options) {
- var self = this;
- return this._fetchMessageIDs().then(function () {
- var messageIDs = self._messageIDs;
- var loadedMessages = _.filter(self._messages, function (message) {
- return _.contains(messageIDs, message.getID());
- });
- var loadedMessageIDs = _.map(loadedMessages, function (message) {
- return message.getID();
- });
-
- options = options || {};
- var shouldFetch = _.difference(
- messageIDs.slice(0, self._FETCH_LIMIT),
- loadedMessageIDs
- ).length > 0;
- if (options.forceFetch || shouldFetch) {
- var idsToLoad = _.difference(messageIDs, loadedMessageIDs)
- .slice(0, self._FETCH_LIMIT);
- return self._rpc({
- model: 'mail.message',
- method: 'message_format',
- args: [idsToLoad],
- context: session.user_context,
- })
- .then(function (messagesData) {
- _.each(messagesData, function (messageData) {
- self.call('mail_service', 'addMessage', messageData, { silent: true });
- });
- });
- } else {
- return Promise.resolve();
- }
-
- });
- },
- /**
- * Overrides this method so that all the messages of this document thread
- * are marked as read on the server.
- *
- * @override
- * @private
- * @returns {Promise} resolved when messages have been marked as read on
- * the server.
- */
- _markAsRead: function () {
- var self = this;
- return this._super.apply(this, arguments).then(function () {
- self.call('mail_service', 'markMessagesAsRead', self._messageIDs);
- });
- },
- /**
- * Post message for document thread
- *
- * @override
- * @private
- * @param {Object} data data related to the new message
- * @return {Promise<Object>} resolved when the message has been sent to
- * the server, with the object message that has been sent to the server.
- */
- _postMessage: function (data) {
- var self = this;
- var resModel = this.getDocumentModel();
- var resID = this.getDocumentID();
- return this._super.apply(this, arguments)
- .then(function (messageData) {
- _.extend(messageData, {
- context: data.context,
- message_type: data.message_type,
- subtype_xmlid: data.subtype_xmlid || "mail.mt_comment",
- subtype_id: data.subtype_id,
- });
- return self._rpc({
- model: resModel,
- method: 'message_post',
- args: [resID],
- kwargs: messageData,
- })
- .then(function (messageID) {
- return self._rpc({
- model: 'mail.message',
- method: 'message_format',
- args: [[messageID]],
- })
- .then(function (messages) {
- messages[0].model = resModel;
- messages[0].res_id = resID;
- self.call('mail_service', 'addMessage', messages[0], {
- postedFromDocumentThread: true,
- });
- return messages[0];
- });
- });
- });
- },
-});
-
-return DocumentThread;
-
-});