diff --git a/addons/mail/mail_message.py b/addons/mail/mail_message.py index 429c85e91f7..58926079575 100644 --- a/addons/mail/mail_message.py +++ b/addons/mail/mail_message.py @@ -184,6 +184,7 @@ class mail_message(osv.osv): ('comment', 'Comment'), ('notification', 'System notification'), ], 'Type', help="Message type: e-mail for e-mail message, notification for system message, comment for other messages such as user replies"), + 'parent_id': fields.many2one('mail.message', 'Parent message', help="Parent message if message belongs to a thread"), } _defaults = { diff --git a/addons/mail/mail_message_view.xml b/addons/mail/mail_message_view.xml index 759a60cdb89..6b131477e46 100644 --- a/addons/mail/mail_message_view.xml +++ b/addons/mail/mail_message_view.xml @@ -13,7 +13,7 @@ - + @@ -30,13 +30,14 @@ - + + @@ -51,6 +52,7 @@ + diff --git a/addons/mail/mail_thread.py b/addons/mail/mail_thread.py index 901a8b02b3e..fa3a36707f1 100644 --- a/addons/mail/mail_thread.py +++ b/addons/mail/mail_thread.py @@ -25,6 +25,7 @@ import base64 import email from email.utils import parsedate +import re import logging import xmlrpclib from osv import osv, fields @@ -124,7 +125,22 @@ class mail_thread(osv.osv): # push to need_action_user_id if user does not follow the object if obj.need_action_user_id and not need_action_pushed: notification_obj.create(cr, uid, {'user_id': obj.need_action_user_id.id, 'message_id': msg_id}, context=context) + + # parse message to get requested users + user_ids = self.message_parse_users(cr, uid, [msg_id], vals['body_text'], context=context) + for user_id in user_ids: + print 'pushed to %d' % (user_id) + notification_obj.create(cr, uid, {'user_id': user_id, 'message_id': msg_id}, context=context) + return msg_id + + def message_parse_users(self, cr, uid, ids, string, context=None): + '''Parse message content; if find @login -(^|\s)@(\w*)-: returns the related ids''' + regex = re.compile('(^|\s)@(\w*)') + login_lst = [item[1] for item in regex.findall(string)] + if not login_lst: return [] + user_ids = self.pool.get('res.users').search(cr, uid, [('login', 'in', login_lst)], context=context) + return user_ids def message_capable_models(self, cr, uid, context=None): ret_dict = {} @@ -134,7 +150,7 @@ class mail_thread(osv.osv): ret_dict[model_name] = model._description return ret_dict - def message_append(self, cr, uid, threads, subject, body_text=None, type='email', + def message_append(self, cr, uid, threads, subject, parent_id=False, body_text=None, type='email', email_to=False, email_from=False, email_cc=None, email_bcc=None, reply_to=None, email_date=None, message_id=False, references=None, attachments=None, body_html=None, subtype=None, headers=None, @@ -211,6 +227,7 @@ class mail_thread(osv.osv): data = { 'subject': subject, 'user_id': uid, + 'parent_id': parent_id, 'model' : thread._name, 'partner_id': partner_id, 'res_id': thread.id, @@ -274,6 +291,7 @@ class mail_thread(osv.osv): if not 'type' in msg_dict: msg_dict['type'] = 'email' return self.message_append(cr, uid, ids, subject = msg_dict.get('subject'), + parent_id = msg_dict.get('parent_id', False), body_text = msg_dict.get('body_text'), type = msg_dict.get('type'), email_to = msg_dict.get('to'), @@ -293,37 +311,61 @@ class mail_thread(osv.osv): context = context) # Message loading - def message_load_ids(self, cr, uid, ids, limit=100, offset=0, context=None): + def _message_get_parent_ids(self, cr, uid, ids, child_ids, root_ids, context=None): + if context is None: context = {} + msg_obj = self.pool.get('mail.message') + msgs_tmp = msg_obj.read(cr, uid, child_ids, context=context) + parent_ids = [msg['parent_id'][0] for msg in msgs_tmp if msg['parent_id'] not in root_ids and msg['parent_id'][0] not in child_ids] + child_ids += parent_ids + cur_iter = 0; max_iter = 10; + while (parent_ids and (cur_iter < max_iter)): + cur_iter += 1 + msgs_tmp = msg_obj.read(cr, uid, parent_ids, context=context) + parent_ids = [msg['parent_id'][0] for msg in msgs_tmp if msg['parent_id'] not in root_ids and msg['parent_id'][0] not in child_ids] + child_ids += parent_ids + return child_ids + + def message_load_ids(self, cr, uid, ids, limit=100, offset=0, domain=[], ascent=False, root_ids=[False], context=None): """ OpenSocial feature: return thread messages ids (for web compatibility) loading messages: search in mail.messages where res_id = ids, (res_)model = current model + see get_pushed_messages for parameters explanation """ + if context is None: context = {} msg_obj = self.pool.get('mail.message') - msg_ids = msg_obj.search(cr, uid, ['&', ('res_id', 'in', ids), ('model', '=', self._name)], + msg_ids = msg_obj.search(cr, uid, ['&', ('res_id', 'in', ids), ('model', '=', self._name)] + domain, limit=limit, offset=offset, context=context) + if (ascent): msg_ids = self._message_get_parent_ids(cr, uid, ids, msg_ids, root_ids, context=context) return msg_ids - def message_load(self, cr, uid, ids, limit=100, offset=0, context=None): + def message_load(self, cr, uid, ids, limit=100, offset=0, domain=[], ascent=False, root_ids=[False], context=None): """ OpenSocial feature: return thread messages loading messages: search in mail.messages where res_id = ids, (res_)model = current model + see get_pushed_messages for parameters explanation """ - msg_ids = self.message_load_ids(cr, uid, ids, limit=limit, offset=offset, context=context) + msg_ids = self.message_load_ids(cr, uid, ids, limit, offset, domain, ascent, root_ids, context=context) return self.pool.get('mail.message').read(cr, uid, msg_ids, context=context) - def get_pushed_messages(self, cr, uid, ids, limit=100, offset=0, domain = None, context=None): + def get_pushed_messages(self, cr, uid, ids, limit=100, offset=0, domain=[], ascent=False, root_ids=[False], context=None): """OpenSocial: wall: get messages to display (=pushed notifications) - :param filter_search: TODO - :return: list of mail.messages, unsorted + :param domain: domain to add to the search; especially child_of is interesting when dealing with threaded display + :param deep: performs an ascended search; will add to fetched msgs all their parents until root_ids + WARNING: must be used in combinaison with a child_of domain + EXAMPLE: domain = ['id', 'child_of', [32, 33]], root_ids=[32,33] + :param root_ids: root_ids when performing an ascended search + :return: list of mail.messages sorted by date """ + if context is None: context = {} notification_obj = self.pool.get('mail.notification') - message_obj = self.pool.get('mail.message') + msg_obj = self.pool.get('mail.message') # get user notifications notification_ids = notification_obj.search(cr, uid, [('user_id', '=', uid)], context=context) notifications = notification_obj.browse(cr, uid, notification_ids, context=context) msg_ids = [notification.message_id.id for notification in notifications] # search messages: ids in notifications, add domain coming from wall search view - search_domain = [('id', 'in', msg_ids)] if domain == None else [('id', 'in', msg_ids)] + domain - msg_ids = message_obj.search(cr, uid, search_domain, limit=limit, offset=offset, context=context) - msgs = message_obj.read(cr, uid, msg_ids, context=context) + search_domain = [('id', 'in', msg_ids)] + domain + msg_ids = msg_obj.search(cr, uid, search_domain, limit=limit, offset=offset, context=context) + if (ascent): msg_ids = self._message_get_parent_ids(cr, uid, ids, msg_ids, root_ids, context=context) + msgs = msg_obj.read(cr, uid, msg_ids, context=context) return msgs # Message tools @@ -588,8 +630,8 @@ class mail_thread(osv.osv): # Note specific #------------------------------------------------------ - def message_append_note(self, cr, uid, ids, subject, body, type='notification', context=None): - return self.message_append(cr, uid, ids, subject, body_text=body, type=type, context=context) + def message_append_note(self, cr, uid, ids, subject, body, parent_id=False, type='notification', context=None): + return self.message_append(cr, uid, ids, subject, body_text=body, parent_id=parent_id, type=type, context=context) # old log overrided method: now calls message_append_note def log(self, cr, uid, id, message, secondary=False, context=None): diff --git a/addons/mail/mail_thread_view.xml b/addons/mail/mail_thread_view.xml index db180baa2cb..7d71a46c4b0 100644 --- a/addons/mail/mail_thread_view.xml +++ b/addons/mail/mail_thread_view.xml @@ -27,7 +27,6 @@ - diff --git a/addons/mail/static/src/css/mail.css b/addons/mail/static/src/css/mail.css index 6b9b1e5c1c6..526ee7ceb42 100644 --- a/addons/mail/static/src/css/mail.css +++ b/addons/mail/static/src/css/mail.css @@ -1,9 +1,14 @@ -/* Wall */ +/* ------------------------------ */ +/* Wall */ +/* ------------------------------ */ .oe_mail_wall { overflow: auto; - padding: 5px 5px 5px 5px; - color: #4C4C4C; + padding: 5px; +} + +.oe_mail_wall_search { + width: 55%; } /* 2 columns view */ @@ -17,19 +22,20 @@ width: 34%; } -.oe_mail_wall_thread_content { - margin-left: 5%; -} - -.oe_mail_wall_thread_content div.oe_mail_thread_act .oe_mail_action_textarea { - width: 100%; +.oe_mail_wall_thread div.oe_mail_thread_act .oe_mail_action_textarea { height: 20px; padding: 2px; } -/* Thread */ +.oe_mail_wall_more { + text-align: center; +} -.oe_mail_thread_main { +/* ------------------------------ */ +/* RecordThread */ +/* ------------------------------ */ + +.oe_mail_recthread { overflow: auto; padding: 5px 5px 5px 5px; } @@ -46,69 +52,79 @@ .oe_mail_followers_action, .oe_mail_followers_display { } -/* Thread: 2 columns view */ -.oe_mail_thread_left { +/* RecordThread: 2 columns view */ +.oe_mail_recthread_left { float: left; - width: 65%; + width: 55%; } -.oe_mail_thread_right { +.oe_mail_recthread_right { float: right; - width: 34%; + width: 25%; + margin-right: 5%; } -.oe_mail_button_follow, .oe_mail_button_unfollow, .oe_mail_button_followers { +.oe_mail_button_follow, .oe_mail_button_unfollow { width: 120px; } -/* Subscribers */ -.oe_mail_followers_vignette { - float: left; - width: 20%%; +.oe_mail_button_followers { + display: inline; + width: 120px; } -.oe_mail_followers_vignette img { - width: 80%; +.oe_mail_followers h4 { + margin: 0 0 8px; } -.oe_mail_thread_more, .oe_mail_thread_nomore { - margin-left: 20%; - margin-right: 40%; - border: 1px solid #D2D9E7; - background: #E0E0E0; - text-align: center; -} +/* ------------------------------ */ +/* ThreadDisplay */ +/* ------------------------------ */ -/* ThreadDisplay */ - -.oe_mail_thread { -} - -.oe_mail_thread_act { -} - -.oe_mail_thread_display { -} - -.oe_mail_thread_msg, .oe_mail_thread_act { - width: 80%; +div.oe_mail_thread_act { white-space: normal; margin-bottom: 5px; +} + +div.oe_mail_thread_display { + white-space: normal; + margin-bottom: 5px; +} + +div.oe_mail_thread_subthread { + margin-top: 8px; + padding-left: 5%; +} + +div.oe_mail_thread_more { border-bottom: 1px solid #D2D9E7; } -.notification { +div.oe_mail_thread_msg { + padding: 4px; + -moz-border-radius: 4px; + -webkit-border-radius: 4px; + -o-border-radius: 4px; + -ms-border-radius: 4px; + border-radius: 4px; +} + +div.notification { background: #E0E0E0; } -.email { +div.email { background: #E0E0E0; } -.oe_mail_thread_msg:after, .oe_mail_thread_act:after { - content: ""; - display: block; - clear: both; +div.oe_mail_thread_msg:after, div.oe_mail_thread_act:after { + content: ""; + display: block; + clear: both; +} + +.oe_mail_msg_content { + margin-left: 60px; } .oe_mail_button_comment { @@ -116,36 +132,26 @@ } .oe_mail_action_textarea { - width: 80%; height: 50px; padding: 5px; } .oe_mail_msg_image { - float: left; + margin-right: 8px; width: 45px; - height: 40px; + height: 45px; text-align: center; overflow: hidden; - -moz-border-radius: 3px; - -webkit-border-radius: 3px; - -o-border-radius: 3px; - -ms-border-radius: 3px; - border-radius: 3px; + -moz-border-radius: 4px; + -webkit-border-radius: 4px; + -o-border-radius: 4px; + -ms-border-radius: 4px; + border-radius: 4px; -moz-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4); -webkit-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4); -o-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4); -box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4); -} - -.oe_mail_msg_image img { - width: 40px; - height: auto; - clip: rect(10px, 100px, 110px, 0px); -} - -.oe_mail_msg_content { - margin-left: 50px; + clip: rect(5px, 40px, 45px, 0px); } .oe_mail_msg_p { @@ -161,18 +167,64 @@ color: #4E43E7; } -.oe_mail_msg_author { - color: #747474; +/* ------------------------------ */ +/* Styling (should be openerp) */ +/* ------------------------------ */ + +input.oe_mail, textarea.oe_mail { + width: 100%; + padding: 4px; + font-size: 12px; + border: 1px solid #cccccc; + -webkit-transition: border linear 0.2s, box-shadow linear 0.2s; + -moz-transition: border linear 0.2s, box-shadow linear 0.2s; + -ms-transition: border linear 0.2s, box-shadow linear 0.2s; + -o-transition: border linear 0.2s, box-shadow linear 0.2s; + transition: border linear 0.2s, box-shadow linear 0.2s; + -moz-box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1); + -webkit-box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1); + -box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1); + -moz-border-radius: 3px; + -webkit-border-radius: 3px; + border-radius: 3px; } -.oe_mail_msg_author a { - color: #4E43E7; +input.oe_mail:focus, textarea.oe_mail:focus { + outline: 0; + border-color: rgba(82, 168, 236, 0.8); + -moz-box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1), 0 0 8px rgba(82, 168, 236, 0.6); + -webkit-box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1), 0 0 8px rgba(82, 168, 236, 0.6); + -box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.1), 0 0 8px rgba(82, 168, 236, 0.6); } -.oe_mail_msg_need_action, .oe_mail_msg_need_action a { +p.oe_mail_msg { + padding: 0; + margin: 0; +} + +.oe_mail_oe_right { + float: right; + margin-right: 8px; +} + +.oe_mail_oe_left { + float: left; + margin-right: 8px; +} + +.oe_mail_oe_fade { + font-size: 12px; + color: #888888; +} + +.oe_mail_oe_bold { + font-weight: bold; +} + +a.oe_mail_oe_intlink { + color: #8786b7; +} + +.oe_mail_oe_warning, .oe_mail_oe_warning a { color: #C03000; } - -.oe_mail_msg_date { - color: #747474; -} diff --git a/addons/mail/static/src/js/mail.js b/addons/mail/static/src/js/mail.js index e88165045c2..d9f6c3472c4 100644 --- a/addons/mail/static/src/js/mail.js +++ b/addons/mail/static/src/js/mail.js @@ -4,36 +4,54 @@ openerp.mail = function(session) { /* Add ThreadDisplay widget to registry */ session.web.form.widgets.add( - 'ThreadDisplay', 'openerp.mail.ThreadDisplay'); + 'Thread', 'openerp.mail.Thread'); session.web.page.readonly.add( - 'ThreadDisplay', 'openerp.mail.ThreadDisplay'); + 'Thread', 'openerp.mail.Thread'); - /* ThreadDisplay widget: display a thread of comments */ - mail.ThreadDisplay = session.web.Widget.extend({ - template: 'ThreadDisplay', + /** + * ThreadDisplay widget: this widget handles the display of a thread of messages. + * Two displays are managed through the [thread_level] parameter that sets + * the level number in the thread: + * - root message + * - - sub message (parent_id = root message) + * - - - sub sub message (parent id = sub message) + * - - sub message (parent_id = root message) + * This widget has 2 ways of initialization, either you give records to be rendered, + * either it will fetch [limit] messages related to [res_model]:[res_id]. + */ + mail.Thread = session.web.Widget.extend({ + template: 'Thread', /** * - * @params {Object} parent parent - * @params {Object} [params] + * @param {Object} parent parent + * @param {Object} [params] * @param {String} [params.res_model] res_model of mail.thread object * @param {Number} [params.res_id] res_id of record + * @param {Number} [params.parent_id=false] parent_id of message * @param {Number} [params.uid] user id - * @param {Number} [params.char_show_more=100] number of character to display before adding a "show more" + * @param {Number} [params.thread_level=0] number of levels in the thread (only 0 or 1 currently) + * @param {Number} [params.msg_more_limit=100] number of character to display before having a "show more" link; + * note that the text will not be truncated if it does not have 110% of + * the parameter (ex: 110 characters needed to be truncated and be displayed + * as a 100-characters message) * @param {Number} [params.limit=10] maximum number of messages to fetch - * @param {Number} [params.offset=0] offset for fetchign messages + * @param {Number} [params.offset=0] offset for fetching messages * @param {Number} [params.records=null] records to show instead of fetching messages */ init: function(parent, params) { this._super(parent); this.params = params; - this.params.limit = this.params.limit || 10; + this.params.parent_id = this.params.parent_id || false; + this.params.thread_level = this.params.thread_level || 0; + this.params.msg_more_limit = this.params.msg_more_limit || 100; + this.params.limit = this.params.limit || 2; this.params.offset = this.params.offset || 0; this.params.records = this.params.records || null; - this.params.char_show_more = this.params.char_show_more || 100; + /* DataSets and internal vars */ this.map_hash = {}; - this.params.show_more = true; - /* define DataSets */ + this.sorted_comments = {'root_ids': [], 'root_id_msg_list': {}}; + this.display_show_more = true; this.ds = new session.web.DataSet(this, this.params.res_model); this.ds_users = new session.web.DataSet(this, 'res.users'); }, @@ -41,28 +59,30 @@ openerp.mail = function(session) { start: function() { var self = this; this._super.apply(this, arguments); - /* events */ + /* display customization and events */ + this.$element.find('p.oe_mail_p_nomore').hide(); + if (this.params.thread_level > 0) this.$element.find('div.oe_mail_thread_act').hide(); + if (this.params.thread_level > 0) this.$element.find('div.oe_mail_thread_more').hide(); this.$element.find('button.oe_mail_button_comment').bind('click', function () { self.do_comment(); }); this.$element.find('button.oe_mail_button_more').bind('click', function () { self.do_more(); }); this.$element.find('div.oe_mail_thread_display').delegate('a.intlink', 'click', function (event) { // lazy implementation: fetch data and try to redirect if (! event.srcElement.dataset.resModel) return false; else var res_model = event.srcElement.dataset.resModel; - if (! event.srcElement.dataset.resLogin) return false; - else var res_login = event.srcElement.dataset.resLogin; - var ds = new session.web.DataSet(self, res_model); - var defer = ds.call('search', [[['login', '=', res_login]]]).then(function (records) { - if (records[0]) { - self.do_action({ - type: 'ir.actions.act_window', - res_model: res_model, - res_id: parseInt(records[0]), - views: [[false, 'form']] - }); - } - }); + var res_login = event.srcElement.dataset.resLogin; + var res_id = event.srcElement.dataset.resId; + if ((! res_login) && (! res_id)) return false; + if (! res_id) { + var ds = new session.web.DataSet(self, res_model); + var defer = ds.call('search', [[['login', '=', res_login]]]).then(function (records) { + if (records[0]) { + self.do_action({ type: 'ir.actions.act_window', res_model: res_model, res_id: parseInt(records[0]), views: [[false, 'form']]}); + } + else return false; + }); + } + else self.do_action({ type: 'ir.actions.act_window', res_model: res_model, res_id: parseInt(res_id), views: [[false, 'form']]}); }); - this.$element.find('div.oe_mail_thread_nomore').hide(); /* display user, fetch comments */ this.display_current_user(); if (this.params.records) return this.display_comments(this.params.records); @@ -70,84 +90,187 @@ openerp.mail = function(session) { }, stop: function () { - this._super.apply(this, arguments); + //this._super.apply(this, arguments); }, init_comments: function() { var self = this; this.params.offset = 0; + this.sorted_comments = {'root_ids': [], 'root_id_msg_list': {}}; this.$element.find('div.oe_mail_thread_display').empty(); - return this.fetch_comments().then(); + domain = this.get_fetch_domain(this.sorted_comments); + console.log(domain); + return this.fetch_comments(this.params.limit, this.params.offset, domain).then(); }, - fetch_comments: function (limit, offset) { + fetch_comments: function (limit, offset, domain) { + console.log('fetch comments'); var self = this; - var defer = this.ds.call('message_load', [[this.params.res_id], limit=(limit||this.params.limit), offset=(offset||this.params.offset)]); + var defer = this.ds.call('message_load', [[this.params.res_id], (limit||this.params.limit), (offset||this.params.offset), (domain||[]), (this.params.thread_level > 0), (this.sorted_comments['root_ids'])]); $.when(defer).then(function (records) { - if (records.length < self.params.limit) self.params.show_more = false; + if (records.length < self.params.limit) self.display_show_more = false; self.display_comments(records); - if (self.params.show_more == true) { - self.$element.find('div.oe_mail_thread_more').show(); - self.$element.find('div.oe_mail_thread_nomore').hide(); } - else { - self.$element.find('div.oe_mail_thread_more').hide(); - self.$element.find('div.oe_mail_thread_nomore').show(); } + if (self.display_show_more == false) { + self.$element.find('button.oe_mail_button_more:last').hide(); + self.$element.find('p.oe_mail_p_nomore:last').show(); } }); return defer; }, - + display_comments: function (records) { var self = this; - - // temporary verification of need_action_user_id (TODO: better display) - if (this.params.need_action_user_id) { - $('
').text('Need action by ' + this.params.need_action_user_id[1]).appendTo(self.$element.find('div.oe_mail_thread_display')); } - + if (this.params.thread_level > 0) { + this.sc = this.sort_comments(records); + } + /* WIP: map matched regexp -> records to browse with name */ //_(records).each(function (record) { //self.do_check_internal_links(record.body_text); //}); + _(records).each(function (record) { - if (record.type == 'email') { record.mini_url = ('/mail/static/src/img/email_icon.png'); } - else { record.mini_url = self.thread_get_avatar_mini('res.users', 'avatar_mini', record.user_id[0]); } - - // body text manipulation - record.body_text = self.do_clean_text(record.body_text); - record.tr_body_text = self.do_truncate_string(record.body_text, self.params.char_show_more); - record.body_text = self.do_replace_internal_links(record.body_text); - if (record.tr_body_text) record.tr_body_text = self.do_replace_internal_links(record.tr_body_text); - - // render - $(session.web.qweb.render('ThreadMsg', {'record': record})).appendTo(self.$element.find('div.oe_mail_thread_display')); - - // truncated: hide full-text, show summary, add buttons - if (record.tr_body_text) { - var node = self.$element.find('span.oe_mail_msg_body:last').append(' [ ... Show less]'); - self.$element.find('p.oe_mail_msg_p:last').append($('' + record.tr_body_text + ' [ ... Show more]')); - var new_node = self.$element.find('span.oe_mail_msg_body_short:last'); - node.hide(); - node.find('a:last').click(function() { node.hide(); new_node.show(); return false; }); - new_node.find('a:last').click(function() { new_node.hide(); node.show(); return false; }); + var sub_msgs = []; + if ((record.parent_id == false || record.parent_id[0] == self.params.parent_id) && self.params.thread_level > 0 ) { + var sub_list = self.sc['root_id_msg_list'][record.id]; + _(records).each(function (record) { + if (record.parent_id == false || record.parent_id[0] == self.params.parent_id) return; + if (_.indexOf(_.pluck(sub_list, 'id'), record.id) != -1) { + sub_msgs.push(record); + } + }); + self.display_comment(record); + self.thread = new mail.Thread(self, {'res_model': self.params.res_model, 'res_id': self.params.res_id, 'uid': self.params.uid, + 'records': sub_msgs, 'thread_level': (self.params.thread_level-1), 'parent_id': record.id}); + self.$element.find('div.oe_mail_thread_display:last').append('
'); + self.thread.appendTo(self.$element.find('div.oe_mail_thread_subthread:last')); + } + else if (self.params.thread_level == 0) { + self.display_comment(record); } }); // update offset for "More" buttons - this.params.offset += records.length; + if (this.params.thread_level == 0) this.params.offset += records.length; + }, + + /** + * Display a record + */ + display_comment: function (record) { + if (record.type == 'email') { record.mini_url = ('/mail/static/src/img/email_icon.png'); } + else { record.mini_url = this.thread_get_avatar_mini('res.users', 'avatar_mini', record.user_id[0]); } + // body text manipulation + record.body_text = this.do_clean_text(record.body_text); + record.tr_body_text = this.do_truncate_string(record.body_text, this.params.msg_more_limit); + record.body_text = this.do_replace_internal_links(record.body_text); + if (record.tr_body_text) record.tr_body_text = this.do_replace_internal_links(record.tr_body_text); + // render + $(session.web.qweb.render('ThreadMsg', {'record': record})).appendTo(this.$element.children('div.oe_mail_thread_display:first')); + // truncated: hide full-text, show summary, add buttons + if (record.tr_body_text) { + var node_body = this.$element.find('span.oe_mail_msg_body:last').append(' [ ... Show less]'); + var node_body_short = this.$element.find('span.oe_mail_msg_body_short:last').append(' [ ... Show more]'); + node_body.hide(); + node_body.find('a:last').click(function() { node_body.hide(); node_body_short.show(); return false; }); + node_body_short.find('a:last').click(function() { node_body_short.hide(); node_body.show(); return false; }); + } + }, + + /** + * Add records to sorted_comments array + * @param {Array} records records from mail.message sorted by date desc + * @returns {Object} sc sorted_comments: dict { + * 'root_id_list': list or root_ids + * 'root_id_msg_list': {'record_id': [ancestor_ids]}, still sorted by date desc + * 'id_to_root': {'root_id': [records]}, still sorted by date desc + * } + */ + sort_comments: function (records) { + var self = this; + sc = {'root_id_list': [], 'root_id_msg_list': {}, 'id_to_root': {}} + var cur_iter = 0; var max_iter = 10; var modif = true; + /* step1: get roots */ + while ( modif && (cur_iter++) < max_iter) { + modif = false; + _(records).each(function (record) { + if ( (record.parent_id == false || record.parent_id[0] == self.params.parent_id) && (_.indexOf(sc['root_id_list'], record.id) == -1)) { + sc['root_id_list'].push(record.id); + sc['root_id_msg_list'][record.id] = []; + self.sorted_comments['root_ids'].push(record.id); + modif = true; + } + else { + if (_.indexOf(sc['root_id_list'], record.parent_id[0]) != -1) { + sc['id_to_root'][record.id] = record.parent_id[0]; + modif = true; + } + else if ( sc['id_to_root'][record.parent_id[0]] ) { + sc['id_to_root'][record.id] = sc['id_to_root'][record.parent_id[0]]; + modif = true; + } + } + }); + } + /* step2: add records */ + _(records).each(function (record) { + var root_id = sc['id_to_root'][record.id]; + if (! root_id) return; + sc['root_id_msg_list'][root_id].push(record); + //self.sorted_comments['root_id_msg_list'][root_id].push(record.id); + }); + return sc; }, display_current_user: function () { - $('
').html( - '' - ).appendTo(this.$element.find('div.oe_mail_msg_image')); + return this.$element.find('img.oe_mail_msg_image').attr('src', this.thread_get_avatar_mini('res.users', 'avatar_mini', this.params.uid)); }, do_comment: function () { var body_text = this.$element.find('textarea').val(); - return this.ds.call('message_append_note', [[this.params.res_id], 'Reply comment', body_text, type='comment']).then( + //console.log(body_text + '-' + this.params.parent_id + '-' + this.params.res_id); + return this.ds.call('message_append_note', [[this.params.res_id], 'Reply comment', body_text, this.params.parent_id, 'comment']).then( this.proxy('init_comments')); }, + /** + * Create a domain to fetch new comments according to + * comment already present in sorted_comments + * @param {Object} sorted_comments (see sort_comments) + * @returns {Array} fetch_domain (OpenERP domain style) + */ + get_fetch_domain: function (sorted_comments) { + var domain = []; + var ids = []; + var dis2 = []; + if (this.params.parent_id) { + domain.push(['id', 'child_of', this.params.parent_id]); + } + _(sorted_comments.root_ids).each(function (id) { // each record + ids.push(id); + dis2.push(id); + }); + + if (ids.length > 0) { + domain.push('&'); + domain.push('!'); + domain.push(['id', 'child_of', ids]); + } + if (this.params.parent_id != false) { + dis2.push(this.params.parent_id); + } + if (dis2.length > 0) { + domain.push(['id', 'not in', dis2]); + } + + + return domain; + }, + do_more: function () { - return this.fetch_comments(this.limit, this.offset); + domain = this.get_fetch_domain(this.sorted_comments); + console.log(domain); + console.log(this.params.limit + '-' + this.params.offset + '-' + this.params.parent_id + '-' + this.params.res_id); + //return true; + return this.fetch_comments(this.params.limit, this.params.offset, domain); }, do_replace_internal_links: function (string) { @@ -157,7 +280,7 @@ openerp.mail = function(session) { var regex_res = regex_login.exec(string); while (regex_res != null) { var login = regex_res[2]; - string = string.replace(regex_res[0], '@' + login + ''); + string = string.replace(regex_res[0], '@' + login + ''); regex_res = regex_login.exec(string); } return string; @@ -168,7 +291,7 @@ openerp.mail = function(session) { }, do_truncate_string: function(string, max_length) { - if (string.length <= max_length) return false; + if (string.length <= (max_length * 1.2)) return false; else return string.slice(0, max_length); }, @@ -210,20 +333,20 @@ openerp.mail = function(session) { /* Add ThreadView widget to registry */ session.web.form.widgets.add( - 'ThreadView', 'openerp.mail.ThreadView'); + 'ThreadView', 'openerp.mail.RecordThread'); session.web.page.readonly.add( - 'ThreadView', 'openerp.mail.ThreadView'); + 'ThreadView', 'openerp.mail.RecordThread'); /* ThreadView widget: thread of comments */ - mail.ThreadView = session.web.form.Field.extend({ + mail.RecordThread = session.web.form.Field.extend({ // QWeb template to use when rendering the object - form_template: 'Thread', + form_template: 'RecordThread', init: function() { this.is_sub = 0; - this.see_sub = 0; + this.see_sub = 1; this._super.apply(this, arguments); - this.thread_display = null; + this.thread = null; /* DataSets */ this.ds = new session.web.DataSet(this, this.view.model); this.ds_users = new session.web.DataSet(this, 'res.users'); @@ -243,52 +366,53 @@ openerp.mail = function(session) { }, set_value: function() { - console.log(this); + console.log('set_value'); var self = this; this._super.apply(this, arguments); + this.see_sub = 1; /* hide follow/unfollow/see followers buttons */ - self.$element.find('button.oe_mail_button_follow').hide(); - self.$element.find('button.oe_mail_button_unfollow').hide(); + this.$element.find('button.oe_mail_button_followers').html('Hide followers') + this.$element.find('button.oe_mail_button_follow').hide(); + this.$element.find('button.oe_mail_button_unfollow').hide(); if (! this.view.datarecord.id) { return; } /* find wich (un)follow buttons to show */ - var call_res = this.ds.call('message_is_subscriber', [[this.view.datarecord.id]]).then(function (records) { + var fetch_fol_done = this.ds.call('message_is_subscriber', [[this.view.datarecord.id]]).then(function (records) { if (records == true) { self.is_sub = 1; self.$element.find('button.oe_mail_button_unfollow').show(); } else { self.is_sub = 0; self.$element.find('button.oe_mail_button_follow').show(); } }); /* fetch subscribers */ - this.fetch_subscribers(); + var fetch_sub_done = this.fetch_subscribers(); /* create ThreadDisplay widget and render it */ - this.$element.find('div.oe_mail_thread_left').empty(); - this.thread_display = new mail.ThreadDisplay(this, {'res_model': this.view.model, 'res_id': this.view.datarecord.id, - 'uid': this.session.uid, 'need_action_user_id': this.view.datarecord.need_action_user_id}); - this.thread_display.appendTo(this.$element.find('div.oe_mail_thread_left')); + this.$element.find('div.oe_mail_recthread_left').empty(); + if (this.thread) this.thread.stop(); + this.thread = new mail.Thread(this, {'res_model': this.view.model, 'res_id': this.view.datarecord.id, 'uid': this.session.uid}); + this.thread.appendTo(this.$element.find('div.oe_mail_recthread_left')); + return fetch_fol_done && fetch_sub_done; }, fetch_subscribers: function () { - var follow_res = this.ds.call('message_get_subscribers', [[this.view.datarecord.id]]).then( - this.proxy('display_subscribers')); - this.$element.find('div.oe_mail_followers_display').hide(); - return follow_res; + return this.ds.call('message_get_subscribers', [[this.view.datarecord.id]]).then(this.proxy('display_subscribers')); }, display_subscribers: function (records) { - this.$element.find('div.oe_mail_followers_display').empty(); var self = this; + var sub_node = this.$element.find('div.oe_mail_followers') + sub_node.empty(); + $('

').html('Followers (' + records.length + ')').appendTo(sub_node); _(records).each(function (record) { - $('
').html( - '' + record.name + '' - ).appendTo(self.$element.find('div.oe_mail_followers_display')); + var mini_url = self.thread_get_avatar_mini('res.users', 'avatar_mini', record.id); + $('' + record.name + '').appendTo(sub_node); }); }, do_follow: function () { this.do_toggle_follow(); - return this.ds.call('message_subscribe', [[this.view.datarecord.id]]).then(); + return this.ds.call('message_subscribe', [[this.view.datarecord.id]]).when(); }, do_unfollow: function () { this.do_toggle_follow(); - return this.ds.call('message_unsubscribe', [[this.view.datarecord.id]]).then(); + return this.ds.call('message_unsubscribe', [[this.view.datarecord.id]]).when(); }, do_toggle_follow: function () { @@ -301,7 +425,7 @@ openerp.mail = function(session) { this.see_sub = 1 - this.see_sub; if (this.see_sub == 1) { this.$element.find('button.oe_mail_button_followers').html('Hide followers'); } else { this.$element.find('button.oe_mail_button_followers').html('Display followers'); } - this.$element.find('div.oe_mail_followers_display').toggle(); + this.$element.find('div.oe_mail_followers').toggle(); }, thread_get_avatar_mini: function(model, field, id) { @@ -331,11 +455,13 @@ openerp.mail = function(session) { init: function (parent, params) { this._super(parent); this.params = params || {}; - this.params.limit = params.limit || 20; + this.params.limit = params.limit || 2; this.params.search_view_id = params.search_view_id || false; + this.params.thread_level = params.thread_level || 1; this.params.search = {}; this.params.domain = []; - this.sorted_comments = {}; + this.sorted_comments = {'models': {}}; + this.display_show_more = true; /* DataSets */ this.ds_msg = new session.web.DataSet(this, 'mail.message'); this.ds_thread = new session.web.DataSet(this, 'mail.thread'); @@ -346,16 +472,16 @@ openerp.mail = function(session) { var self = this; this._super.apply(this, arguments); /* events and buttons */ - this.$element.find('button.oe_mail_button_comment').bind('click', function () { self.do_comment(); }); + this.$element.find('button.oe_mail_wall_button_comment').bind('click', function () { self.do_comment(); }); this.$element.find('button.oe_mail_wall_button_more').bind('click', function () { self.do_more(); }); - this.$element.find('div.oe_mail_wall_nomore').hide(); + this.$element.find('p.oe_mail_wall_nomore').hide(); /* load mail.message search view */ var search_view_loaded = this.load_search_view(this.params.search_view_id, {}, false); var search_view_ready = $.when(search_view_loaded).then(function () { self.searchview.on_search.add(self.do_searchview_search); }); /* fetch comments */ - var comments_ready = this.init_comments(); + var comments_ready = this.init_comments(this.params.domain, {}, 0, this.params.limit); return (search_view_ready && comments_ready); }, @@ -406,7 +532,8 @@ openerp.mail = function(session) { init_comments: function(domain, context, offset, limit) { var self = this; this.params.domain = []; - this.sorted_comments = {}; + this.display_show_more = true; + this.sorted_comments = {'models': {}}; this.$element.find('div.oe_mail_wall_threads').empty(); return this.fetch_comments(domain, context, offset, limit); }, @@ -419,9 +546,16 @@ openerp.mail = function(session) { * @param {Number} limit number of messages to fetch */ fetch_comments: function (domain, context, offset, limit) { - var load_res = this.ds_thread.call('get_pushed_messages', - [[this.session.uid], limit = (limit || 100), offset = (offset || 0), domain = (domain || null), context = (context || null) ]).then( - this.proxy('display_comments')); + var self = this; + var load_res = this.ds_thread.call('get_pushed_messages', + [[this.session.uid], (limit || 2), (offset || 0), (domain || []), true, [false], (context || null)]).then(function (records) { + if (records.length < self.params.limit) self.display_show_more = false; + self.display_comments(records); + if (! self.display_show_more) { + self.$element.find('button.oe_mail_wall_button_more:last').hide(); + self.$element.find('p.oe_mail_wall_nomore:last').show(); + } + }); return load_res; }, @@ -429,41 +563,76 @@ openerp.mail = function(session) { * @param {Array} records records to show in threads */ display_comments: function (records) { - var sorted_comments = this.sort_comments(records, this.sorted_comments); + var sorted_comments = this.sort_comments(records); + console.log(sorted_comments); var self = this; - _(sorted_comments).each(function (rec_models, model) { // each model - _(rec_models).each(function (record_id, id) { // each record + _(sorted_comments.model_list).each(function (model_name) { + _(sorted_comments.models[model_name].root_ids).each(function (id) { + var records = sorted_comments.models[model_name].msgs[id]; var template = 'WallThreadContainer'; - var render_res = session.web.qweb.render(template, { - 'record_model': model, - 'record_id': id, - }); + var render_res = session.web.qweb.render(template, {}); $('
').html(render_res).appendTo(self.$element.find('div.oe_mail_wall_threads')); - var thread_display = new mail.ThreadDisplay(self, - {'res_model': model, 'res_id': parseInt(id), 'uid': self.session.uid, 'records': record_id} + var thread = new mail.Thread(self, { + 'res_model': model_name, 'res_id': records[0]['res_id'], 'uid': self.session.uid, 'records': records, + 'parent_id': false, 'thread_level': self.params.thread_level} ); - thread_display.appendTo(self.$element.find('div.oe_mail_wall_thread_content:last')); + thread.appendTo(self.$element.find('div.oe_mail_wall_thread:last')); }); }); - $.extend(true, this.sorted_comments, sorted_comments); }, /** * Add records to sorted_comments array - * @param {Array} records records from mail.message - * @returns {Object} sorted_comments: dict - * sorted_comments.res_model = {res_ids} - * sorted_comments.res_model.res_id = [records] + * @param {Array} records records from mail.message sorted by date desc + * @returns {Object} sc sorted_comments: dict + * sc.model_list = [record.model names] + * sc.models[model_name] = { + * 'root_ids': [root record.ids], + * 'id_to_root': {record.id: root.id, ..}, + * 'msgs': {'root_id': [records]}, still sorted by date desc + * }, for each model */ sort_comments: function (records) { - sorted_comments = {} + var self = this; + var sc = {'model_list': [], 'models': {}} + var cur_iter = 0; var max_iter = 10; var modif = true; + /* step1: get roots */ + while ( modif && (cur_iter++) < max_iter) { + modif = false; + _(records).each(function (record) { + if (_.indexOf(sc['model_list'], record.model) == -1) { + sc['model_list'].push(record.model); + sc['models'][record.model] = {'root_ids': [], 'id_to_root': {}, 'msgs': {}}; + } + if (! self.sorted_comments['models'][record.model]) { + self.sorted_comments['models'][record.model] = {'root_ids': []}; + } + var rmod = sc['models'][record.model]; + if (record.parent_id == false && (_.indexOf(rmod['root_ids'], record.id) == -1)) { + rmod['root_ids'].push(record.id); + rmod['msgs'][record.id] = []; + self.sorted_comments['models'][record.model]['root_ids'].push(record.id); + modif = true; + } + else { + if ((_.indexOf(rmod['root_ids'], record.parent_id[0]) != -1) && (! rmod['id_to_root'][record.id])) { + rmod['id_to_root'][record.id] = record.parent_id[0]; + modif = true; + } + else if ( rmod['id_to_root'][record.parent_id[0]] && (! rmod['id_to_root'][record.id])) { + rmod['id_to_root'][record.id] = rmod['id_to_root'][record.parent_id[0]]; + modif = true; + } + } + }); + } + /* step2: add records */ _(records).each(function (record) { - if (! (record.model in sorted_comments)) { sorted_comments[record.model] = {}; } - if (! (record.res_id in sorted_comments[record.model])) { - sorted_comments[record.model][record.res_id] = []; } - sorted_comments[record.model][record.res_id].push(record); + var root_id = sc['models'][record.model]['id_to_root'][record.id]; + if (! root_id) root_id = record.id; + sc['models'][record.model]['msgs'][root_id].push(record); }); - return sorted_comments; + return sc; }, /** @@ -474,26 +643,16 @@ openerp.mail = function(session) { */ get_fetch_domain: function (sorted_comments) { var domain = []; - _(sorted_comments).each(function (rec_models, model) { //each model + _(sorted_comments.models).each(function (sc_model, model_name) { //each model var ids = []; - _(rec_models).each(function (record_id, id) { // each record + _(sc_model.root_ids).each(function (id) { // each record ids.push(id); }); - domain.push('|', ['model', '!=', model], ['res_id', 'not in', ids]); + domain.push('|', ['model', '!=', model_name], '!', ['id', 'child_of', ids]); }); return domain; }, - - /** - * Action: Posts a comment - */ - do_comment: function () { - var body_text = this.$element.find('textarea').val(); - return this.ds_users.call('message_append_note', [[this.session.uid], 'Tweet', body_text, type='comment']).then( - //this.proxy('fetch_comments')); - this.init_comments()); - }, - + /** * Action: Shows more discussions */ @@ -501,14 +660,20 @@ openerp.mail = function(session) { var domain = this.get_fetch_domain(this.sorted_comments); return this.fetch_comments(domain); }, - + + /** + * Action: Posts a comment + */ + do_comment: function () { + var body_text = this.$element.find('textarea.oe_mail_wall_action_textarea').val(); + return this.ds_users.call('message_append_note', [[this.session.uid], 'Tweet', body_text, false, 'comment']).then(this.init_comments()); + }, + /** * Tools: get avatar mini (TODO: should be moved in some tools ?) */ thread_get_mini: function(model, field, id) { - id = id || ''; - var url = this.session.prefix + '/web/binary/image?session_id=' + this.session.session_id + '&model=' + model + '&field=' + field + '&id=' + id; - return url; + return this.session.prefix + '/web/binary/image?session_id=' + this.session.session_id + '&model=' + model + '&field=' + field + '&id=' + (id || ''); }, }); }; diff --git a/addons/mail/static/src/xml/mail.xml b/addons/mail/static/src/xml/mail.xml index c545af05e19..1a04d4e44c0 100644 --- a/addons/mail/static/src/xml/mail.xml +++ b/addons/mail/static/src/xml/mail.xml @@ -7,18 +7,15 @@
-
- +
+