Skip to content

Commit

Permalink
⚡ mail_sent fetching mails from channels
Browse files Browse the repository at this point in the history
  • Loading branch information
KolushovAlexandr committed Jul 14, 2019
1 parent fc03db6 commit 3a44a77
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 11 deletions.
2 changes: 1 addition & 1 deletion mail_sent/__manifest__.py
Expand Up @@ -3,7 +3,7 @@
"summary": """Quick way to find sent messages""",
"category": "Discuss",
"images": ['images/menu.png'],
"version": "12.0.1.0.4",
"version": "12.0.1.1.0",

"author": "IT-Projects LLC, Ivan Yelizariev, Pavel Romanchenko",
"support": "apps@it-projects.info",
Expand Down
6 changes: 6 additions & 0 deletions mail_sent/doc/changelog.rst
@@ -1,3 +1,9 @@
`1.1.0`
-------

- **Imp:** Fetches messages from channels
- **Fix:** Fetching mails from another threads

`1.0.4`
-------
- **FIX:** didn't work for non-admin users
Expand Down
23 changes: 18 additions & 5 deletions mail_sent/models.py
Expand Up @@ -6,14 +6,27 @@ class MailMessage(models.Model):

sent = fields.Boolean('Sent', compute="_compute_sent", help='Was message sent to someone', store=True)

@api.depends('author_id', 'partner_ids')
@api.depends('author_id', 'partner_ids', 'channel_ids')
def _compute_sent(self):
for r in self:
r_sudo = r.sudo()
sent = len(r_sudo.partner_ids) > 1 \
or len(r_sudo.partner_ids) == 1 \
and r_sudo.author_id \
and r_sudo.partner_ids[0].id != r_sudo.author_id.id
recipient_ids = r_sudo.partner_ids
author_id = r_sudo.author_id
res_id = r_sudo.model and r_sudo.res_id and r_sudo.env[r_sudo.model].browse(r_sudo.res_id)
sent = author_id and (
len(recipient_ids) > 1
or (
len(recipient_ids) == 1
and recipient_ids[0].id != author_id.id
)
or (
len(r_sudo.channel_ids)
)
or (
res_id
and len(res_id.message_partner_ids - author_id) > 0
)
)
r.sent = sent

@api.multi
Expand Down
48 changes: 43 additions & 5 deletions mail_sent/static/src/js/sent.js
Expand Up @@ -5,6 +5,7 @@ var core = require('web.core');
var session = require('web.session');
var Manager = require('mail.Manager');
var Mailbox = require('mail.model.Mailbox');
var SearchableThread = require('mail.model.SearchableThread');

var _t = core._t;

Expand All @@ -18,15 +19,52 @@ Manager.include({
this._addMailbox({
id: 'channel_sent',
name: _t("Sent Messages"),
mailboxCounter: data.needaction_inbox_counter || 0,
mailboxCounter: 0,
});
}
},
});

SearchableThread.include({
_fetchMessages: function (pDomain, loadMore) {
var self = this;
if (this._id !== 'mailbox_channel_sent') {
return this._super(pDomain, loadMore);
}

_makeMessage: function (data) {
var message = this._super(data);
message._addThread('mailbox_channel_sent');
return message;
// this is a copy-paste from super method
var domain = this._getThreadDomain();
var cache = this._getCache(pDomain);
if (pDomain) {
domain = domain.concat(pDomain || []);
}
if (loadMore) {
var minMessageID = cache.messages[0].getID();
domain = [['id', '<', minMessageID]].concat(domain);
}
return this._rpc({
model: 'mail.message',
method: 'message_fetch',
args: [domain],
kwargs: this._getFetchMessagesKwargs(),
}).then(function (messages) {
// except this function. It adds the required thread to downloaded messages
_.each(messages, function(m){
m.channel_ids.push('mailbox_channel_sent');
});
if (!cache.allHistoryLoaded) {
cache.allHistoryLoaded = messages.length < self._FETCH_LIMIT;
}
cache.loaded = true;
_.each(messages, function (message) {
self.call('mail_service', 'addMessage', message, {
silent: true,
domain: pDomain,
});
});
cache = self._getCache(pDomain || []);
return cache.messages;
});
},
});

Expand Down

0 comments on commit 3a44a77

Please sign in to comment.