[IMP] mail_broker: Black, isort, prettier

pull/1305/head
Enric Tobella 2022-11-30 11:31:29 +01:00 committed by Enric Tobella
parent a4b3ef7346
commit 294aa506d4
6 changed files with 112 additions and 94 deletions

View File

@ -44,7 +44,8 @@ class MailBroker(models.Model):
return (
self.env["mail.broker.channel"]
.search(
[("token", "=", str(chat_token)), ("broker_id", "=", self.id)], limit=1,
[("token", "=", str(chat_token)), ("broker_id", "=", self.id)],
limit=1,
)
.id
)

View File

@ -15,17 +15,30 @@ class MailBrokerChannel(models.Model):
active = fields.Boolean(default=True)
token = fields.Char(required=True)
broker_id = fields.Many2one("mail.broker", required=True)
message_ids = fields.One2many("mail.message.broker", inverse_name="channel_id",)
mail_message_ids = fields.One2many(
"mail.message", inverse_name="broker_channel_id",
message_ids = fields.One2many(
"mail.message.broker",
inverse_name="channel_id",
)
mail_message_ids = fields.One2many(
"mail.message",
inverse_name="broker_channel_id",
)
last_message_date = fields.Datetime(
compute="_compute_message_data",
store=True,
)
unread = fields.Integer(
compute="_compute_message_data",
store=True,
)
last_message_date = fields.Datetime(compute="_compute_message_data", store=True,)
unread = fields.Integer(compute="_compute_message_data", store=True,)
broker_token = fields.Char(related="broker_id.token", store=True, required=False)
show_on_app = fields.Boolean()
partner_id = fields.Many2one("res.partner")
message_main_attachment_id = fields.Many2one(
string="Main Attachment", comodel_name="ir.attachment", index=True, copy=False,
string="Main Attachment",
comodel_name="ir.attachment",
index=True,
copy=False,
)
def message_fetch(self, domain=False, limit=30):
@ -39,13 +52,19 @@ class MailBrokerChannel(models.Model):
)
@api.depends(
"mail_message_ids", "mail_message_ids.date", "mail_message_ids.broker_unread",
"mail_message_ids",
"mail_message_ids.date",
"mail_message_ids.broker_unread",
)
def _compute_message_data(self):
for r in self:
r.last_message_date = (
self.env["mail.message"]
.search([("broker_channel_id", "=", r.id)], limit=1, order="date DESC",)
.search(
[("broker_channel_id", "=", r.id)],
limit=1,
order="date DESC",
)
.date
)
r.unread = self.env["mail.message"].search_count(

View File

@ -1,4 +1,4 @@
odoo.define("mail_broker.Broker", function(require) {
odoo.define("mail_broker.Broker", function (require) {
"use strict";
var BasicComposer = require("mail.composer.Basic");
var ExtendedComposer = require("mail.composer.Extended");
@ -19,7 +19,7 @@ odoo.define("mail_broker.Broker", function(require) {
"click .o_mail_sidebar_title .o_add": "_onSearchThread",
"blur .o_mail_add_thread input": "_onSearchThreadBlur",
},
init: function(parent, action, options) {
init: function (parent, action, options) {
this._super.apply(this, arguments);
this.action = action;
this.action_manager = parent;
@ -37,7 +37,7 @@ odoo.define("mail_broker.Broker", function(require) {
/**
* @override
*/
on_attach_callback: function() {
on_attach_callback: function () {
if (this._thread) {
this._threadWidget.scrollToPosition(
this._threadsScrolltop[this._thread.getID()]
@ -49,17 +49,17 @@ odoo.define("mail_broker.Broker", function(require) {
/**
* @override
*/
on_detach_callback: function() {
on_detach_callback: function () {
if (this._thread) {
this._threadsScrolltop[
this._thread.getID()
] = this._threadWidget.getScrolltop();
}
},
start: function() {
start: function () {
var self = this;
return this._super.apply(this, arguments).then(function() {
return this._super.apply(this, arguments).then(function () {
return self._initRender();
});
/*
@ -93,7 +93,7 @@ odoo.define("mail_broker.Broker", function(require) {
});
*/
},
_initRender: function() {
_initRender: function () {
var self = this;
this._basicComposer = new BasicComposer(this, {
mentionPartnersRestricted: true,
@ -114,19 +114,19 @@ odoo.define("mail_broker.Broker", function(require) {
defs.push(this._renderThread());
defs.push(this._basicComposer.appendTo(this.$(".o_mail_discuss_content")));
return Promise.all(defs)
.then(function() {
.then(function () {
if (self._defaultChatID) {
return self._setThread(self._defaultChatID);
}
})
.then(function() {
.then(function () {
self._updateThreads();
self._startListening();
self._threadWidget.$el.on(
"scroll",
null,
_.debounce(function() {
_.debounce(function () {
var $noContent = self._threadWidget.$(".o_mail_no_content");
if (
self._threadWidget.getScrolltop() < 20 &&
@ -142,14 +142,14 @@ odoo.define("mail_broker.Broker", function(require) {
);
});
},
_startListening: function() {
_startListening: function () {
this.call("mail_service", "getMailBus").on(
"new_message",
this,
this._onNewMessage
);
},
_setThread: function(threadID) {
_setThread: function (threadID) {
this._storeThreadState();
var thread = this.call("mail_service", "getThread", threadID);
if (!thread) {
@ -159,7 +159,7 @@ odoo.define("mail_broker.Broker", function(require) {
var self = this;
this.messagesSeparatorPosition = undefined;
return this._fetchAndRenderThread().then(function() {
return this._fetchAndRenderThread().then(function () {
self._thread.markAsRead();
// Restore scroll position and composer of the new
// current thread
@ -175,14 +175,14 @@ odoo.define("mail_broker.Broker", function(require) {
});
});
},
_storeThreadState: function() {
_storeThreadState: function () {
if (this._thread) {
this._threadsScrolltop[
this._thread.getID()
] = this._threadWidget.getScrolltop();
}
},
_loadEnoughMessages: function() {
_loadEnoughMessages: function () {
var $el = this._threadWidget.el;
var loadMoreMessages =
$el.clientHeight &&
@ -194,7 +194,7 @@ odoo.define("mail_broker.Broker", function(require) {
);
}
},
_getThreadRenderingOptions: function() {
_getThreadRenderingOptions: function () {
if (_.isUndefined(this.messagesSeparatorPosition)) {
if (this._unreadCounter) {
var messageID = this._thread.getLastSeenMessageID();
@ -217,9 +217,9 @@ odoo.define("mail_broker.Broker", function(require) {
displayStars: false,
};
},
_fetchAndRenderThread: function() {
_fetchAndRenderThread: function () {
var self = this;
return this._thread.fetchMessages().then(function() {
return this._thread.fetchMessages().then(function () {
self._threadWidget.render(
self._thread,
self._getThreadRenderingOptions()
@ -227,10 +227,10 @@ odoo.define("mail_broker.Broker", function(require) {
return self._loadEnoughMessages();
});
},
_renderButtons: function() {
_renderButtons: function () {
// This is a hook just in case some buttons are required
},
_renderThread: function() {
_renderThread: function () {
this._threadWidget = new ThreadWidget(this, {
areMessageAttachmentsDeletable: false,
loadMoreOnScroll: true,
@ -238,7 +238,7 @@ odoo.define("mail_broker.Broker", function(require) {
this._threadWidget.on("load_more_messages", this, this._loadMoreMessages);
return this._threadWidget.appendTo(this.$(".o_mail_discuss_content"));
},
_renderSidebar: function(options) {
_renderSidebar: function (options) {
var $sidebar = $(
QWeb.render("mail_broker.broker.Sidebar", {
activeThreadID: this._thread ? this._thread.getID() : undefined,
@ -247,7 +247,7 @@ odoo.define("mail_broker.Broker", function(require) {
);
return $sidebar;
},
_restoreThreadState: function() {
_restoreThreadState: function () {
var $newMessagesSeparator = this.$(".o_thread_new_messages_separator");
if ($newMessagesSeparator.length) {
this._threadWidget.$el.scrollTo($newMessagesSeparator);
@ -256,51 +256,49 @@ odoo.define("mail_broker.Broker", function(require) {
this._threadWidget.scrollToPosition(newThreadScrolltop);
}
},
_updateThreads: function() {
_updateThreads: function () {
var bots = this.call("mail_service", "getBrokerBots");
var $sidebar = this._renderSidebar({
bots: bots,
});
this.$(".o_mail_discuss_sidebar").html($sidebar.contents());
var self = this;
_.each(bots, function(bot, broker_id) {
_.each(bots, function (bot, broker_id) {
var $input = self.$(
".o_mail_add_thread[data-bot=" + broker_id + "] input"
);
self._prepareAddThreadInput($input, broker_id, bot);
});
},
_prepareAddThreadInput: function($input, broker_id) {
_prepareAddThreadInput: function ($input, broker_id) {
var self = this;
$input.autocomplete({
source: function(request, response) {
source: function (request, response) {
self._lastSearchVal = _.escape(request.term);
self._searchChannel(broker_id, self._lastSearchVal).then(function(
self._searchChannel(broker_id, self._lastSearchVal).then(function (
result
) {
response(result);
});
},
select: function(ev, ui) {
select: function (ev, ui) {
self._setThread("broker_thread_" + ui.item.id);
self._updateThreads();
},
focus: function(ev) {
focus: function (ev) {
ev.preventDefault();
},
html: true,
});
},
_loadMoreMessages: function() {
_loadMoreMessages: function () {
var self = this;
var oldestMessageID = this.$(".o_thread_message")
.first()
.data("messageId");
var oldestMessageID = this.$(".o_thread_message").first().data("messageId");
var oldestMessageSelector =
'.o_thread_message[data-message-id="' + oldestMessageID + '"]';
var offset = -dom.getPosition(document.querySelector(oldestMessageSelector))
.top;
return this._thread.fetchMessages({loadMore: true}).then(function() {
return this._thread.fetchMessages({loadMore: true}).then(function () {
if (self.messagesSeparatorPosition === "top") {
// Reset value to re-compute separator position
self.messagesSeparatorPosition = undefined;
@ -314,7 +312,7 @@ odoo.define("mail_broker.Broker", function(require) {
self._threadWidget.scrollToPosition(offset);
});
},
_onSearchThread: function(ev) {
_onSearchThread: function (ev) {
ev.preventDefault();
var bot = $(ev.target).data("bot");
this.$(".o_mail_add_thread[data-bot=" + bot + "]")
@ -322,10 +320,10 @@ odoo.define("mail_broker.Broker", function(require) {
.find("input")
.focus();
},
_onSearchThreadBlur: function() {
_onSearchThreadBlur: function () {
this.$(".o_mail_add_thread").hide();
},
_onChannelSettingsClicked: function(ev) {
_onChannelSettingsClicked: function (ev) {
ev.preventDefault();
ev.stopPropagation();
var threadID = $(ev.target).data("thread-id");
@ -339,13 +337,13 @@ odoo.define("mail_broker.Broker", function(require) {
target: "new",
});
},
_onNewMessage: function(message) {
_onNewMessage: function (message) {
var thread_id = "broker_thread_" + message.broker_channel_id;
if (this._thread && thread_id === this._thread.getID()) {
this._thread.markAsRead();
var shouldScroll = this._threadWidget.isAtBottom();
var self = this;
this._fetchAndRenderThread().then(function() {
this._fetchAndRenderThread().then(function () {
if (shouldScroll) {
self._threadWidget.scrollToMessage({
msgID: message.getID(),
@ -362,14 +360,14 @@ odoo.define("mail_broker.Broker", function(require) {
message.getThreadIDs()
);
},
_searchChannel: function(broker_id, searchVal) {
_searchChannel: function (broker_id, searchVal) {
return this._rpc({
model: "mail.broker",
method: "channel_search",
args: [[parseInt(broker_id, 10)], searchVal],
}).then(function(result) {
}).then(function (result) {
var values = [];
_.each(result, function(channel) {
_.each(result, function (channel) {
var escapedName = _.escape(channel.name);
values.push(
_.extend(channel, {
@ -381,16 +379,16 @@ odoo.define("mail_broker.Broker", function(require) {
return values;
});
},
_onComposerFocused: function() {
_onComposerFocused: function () {
// Hook
},
_onSelectBrokerChannel: function(ev) {
_onSelectBrokerChannel: function (ev) {
ev.preventDefault();
var threadID = $(ev.currentTarget).data("thread-id");
this._setThread(threadID);
this._updateThreads();
},
_onPostMessage: function(messageData) {
_onPostMessage: function (messageData) {
var self = this;
var options = {};
if (this._selectedMessage) {
@ -405,7 +403,7 @@ odoo.define("mail_broker.Broker", function(require) {
}
this._thread
.postMessage(messageData, options)
.then(function() {
.then(function () {
if (self._selectedMessage) {
self._renderSnackbar(
"mail.discuss.MessageSentSnackbar",
@ -419,7 +417,7 @@ odoo.define("mail_broker.Broker", function(require) {
self._threadWidget.scrollToBottom();
}
})
.catch(function() {
.catch(function () {
// TODO: Display notifications
});
},

View File

@ -1,10 +1,10 @@
odoo.define("mail_broker.mail.Manager", function(require) {
odoo.define("mail_broker.mail.Manager", function (require) {
"use strict";
var Manager = require("mail.Manager");
var BrokerThread = require("mail_broker.BrokerThread");
Manager.include({
_addMessageToThreads: function(message, options) {
_addMessageToThreads: function (message, options) {
this._super.apply(this, arguments);
if (message.broker_channel_id) {
var thread = this.getThread(
@ -15,31 +15,31 @@ odoo.define("mail_broker.mail.Manager", function(require) {
}
}
},
_updateInternalStateFromServer: function(result) {
_updateInternalStateFromServer: function (result) {
this._super.apply(this, arguments);
this._updateBrokerChannelFromServer(result);
},
getBrokerBots: function() {
getBrokerBots: function () {
var data = _.extend({}, this._broker_bots);
_.each(data, function(value) {
_.each(data, function (value) {
value.threads = [];
});
_.each(this._threads, function(thread) {
_.each(this._threads, function (thread) {
if (thread.getType() === "broker_thread") {
data[thread.broker_id].threads.push(thread);
}
});
_.each(data, function(value) {
value.threads.sort(function(a, b) {
_.each(data, function (value) {
value.threads.sort(function (a, b) {
return b.last_message_date - a.last_message_date;
});
});
return data;
},
_updateBrokerChannelFromServer: function(data) {
_updateBrokerChannelFromServer: function (data) {
var self = this;
this._broker_bots = {};
_.each(data.broker_slots, function(slot) {
_.each(data.broker_slots, function (slot) {
self._broker_bots[slot.id] = {
name: slot.name,
channel_name: slot.channel_name,
@ -47,16 +47,16 @@ odoo.define("mail_broker.mail.Manager", function(require) {
_.each(slot.threads, self._addChannel.bind(self));
});
},
getMailBrokerThreads: function() {
var data = _.filter(this._threads, function(thread) {
getMailBrokerThreads: function () {
var data = _.filter(this._threads, function (thread) {
return thread.getType() === "broker_thread";
});
data = data.sort(function(a, b) {
data = data.sort(function (a, b) {
return b.last_message_date - a.last_message_date;
});
return data;
},
_makeChannel: function(data, options) {
_makeChannel: function (data, options) {
if (data.channel_type === "broker_thread") {
return new BrokerThread({
parent: this,
@ -67,10 +67,10 @@ odoo.define("mail_broker.mail.Manager", function(require) {
}
return this._super.apply(this, arguments);
},
_onNotification: function(notifs) {
_onNotification: function (notifs) {
var self = this;
var result = this._super.apply(this, arguments);
_.each(notifs, function(notif) {
_.each(notifs, function (notif) {
if (notif[0][1] === "mail.broker") {
if (notif[1].message) {
self.addMessage(notif[1].message, {silent: 0});

View File

@ -1,17 +1,17 @@
odoo.define("mail_broker.model.Message", function(require) {
odoo.define("mail_broker.model.Message", function (require) {
"use strict";
var Message = require("mail.model.Message");
Message.include({
init: function(parent, data) {
init: function (parent, data) {
this._super.apply(this, arguments);
this.broker_channel_id = data.broker_channel_id || false;
this.broker_unread = data.broker_unread || false;
this.broker_type = data.broker_type || false;
this.customer_status = data.customer_status || false;
},
isNeedaction: function() {
isNeedaction: function () {
return this._super.apply(this, arguments) || this.broker_unread;
},
});

View File

@ -1,4 +1,4 @@
odoo.define("mail_broker.BrokerThread", function(require) {
odoo.define("mail_broker.BrokerThread", function (require) {
"use strict";
var Thread = require("mail.model.Thread");
@ -6,7 +6,7 @@ odoo.define("mail_broker.BrokerThread", function(require) {
var field_utils = require("web.field_utils");
var BrokerThread = Thread.extend({
init: function(params) {
init: function (params) {
this._messageIDs = [];
var data = params.data;
data.type = "broker_thread";
@ -18,26 +18,26 @@ odoo.define("mail_broker.BrokerThread", function(require) {
this.broker_id = data.broker_id;
this._unreadCounter = data.unread;
},
getMessages: function() {
getMessages: function () {
return this._messages;
},
getLastSeenMessageID: function() {
getLastSeenMessageID: function () {
return null;
},
getNeedactionCounter: function() {
getNeedactionCounter: function () {
return this._unreadCounter;
},
isGroupBasedSubscription: function() {
isGroupBasedSubscription: function () {
return true;
},
_addMessage: function(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) {
this._messages = _.sortBy(this._messages, function (msg) {
return msg.getID();
});
// Update message ids associated to this document thread
@ -51,13 +51,13 @@ odoo.define("mail_broker.BrokerThread", function(require) {
this._unreadCounter++;
}
},
isAllHistoryLoaded: function() {
isAllHistoryLoaded: function () {
return this.allHistoryLoaded;
},
fetchMessages: function(options) {
fetchMessages: function (options) {
return this._fetchMessages(options);
},
_fetchMessages: function(options) {
_fetchMessages: function (options) {
var self = this;
var domain = [];
if (options && options.loadMore) {
@ -69,26 +69,26 @@ odoo.define("mail_broker.BrokerThread", function(require) {
method: "message_fetch",
args: [[this.resId], domain],
kwargs: this._getFetchMessagesKwargs(options),
}).then(function(messages) {
}).then(function (messages) {
if (!self.allHistoryLoaded) {
self.allHistoryLoaded = messages.length < self._FETCH_LIMIT;
}
_.each(messages, function(messageData) {
_.each(messages, function (messageData) {
self.call("mail_service", "addMessage", messageData, {
silent: true,
});
});
});
},
_getFetchMessagesKwargs: function() {
_getFetchMessagesKwargs: function () {
return {
limit: this._FETCH_LIMIT,
context: session.user_context,
};
},
_postMessage: function(data) {
_postMessage: function (data) {
var self = this;
return this._super.apply(this, arguments).then(function(messageData) {
return this._super.apply(this, arguments).then(function (messageData) {
_.extend(messageData, {
broker_type: "comment",
subtype: "mail.mt_comment",
@ -101,14 +101,14 @@ odoo.define("mail_broker.BrokerThread", function(require) {
args: [[self.resId]],
kwargs: messageData,
})
.then(function() {
.then(function () {
return messageData;
});
});
},
_markAsRead: function() {
_markAsRead: function () {
var self = this;
return this._super.apply(this, arguments).then(function() {
return this._super.apply(this, arguments).then(function () {
self.call("mail_service", "markMessagesAsRead", self._messageIDs);
});
},