From f73372e7a25cd480e309b31bb818ccb0e93e36cc Mon Sep 17 00:00:00 2001 From: Fabien Pinckaers Date: Mon, 20 Aug 2012 11:06:36 +0200 Subject: [PATCH] [IMP] better implementation of message read, no ancestror yet bzr revid: fp@tinyerp.com-20120820090636-gyyp3n47sjs0fbhc --- addons/mail/TODO.txt | 14 ++++----- addons/mail/mail_message.py | 59 +++++++++++++++++++++++++++++++++++++ addons/mail/mail_thread.py | 40 ------------------------- 3 files changed, 64 insertions(+), 49 deletions(-) diff --git a/addons/mail/TODO.txt b/addons/mail/TODO.txt index 9dd9262888f..eb6d0ccd761 100644 --- a/addons/mail/TODO.txt +++ b/addons/mail/TODO.txt @@ -1,25 +1,21 @@ Backend: -* remove _message_search_ancestor_ids and implement in message_read (must use browse records instead of IDS) * I propose to move message_read in mail.message and rename to message_fetch -* remove message_search (objects can overwrite message_ids if they want a custom list of messages) - - requires to reimplement message_read - * remove _message_find_user_id and use _message_find_partners - it requires to change the route code Web Stuff to Do: * replace comments_structure by a real tree structure, provided by message_read directly * rewrite completly records_struct_add_records, merge existing messages with those coming from another message_read -* remove records_struct_update_after_display: + -> false records with domain message_read +* remove records_struct_update_after_display * remove all is_wall stuff and pass real arguments * remove init_and_fetch_comments * rename fetch_comments into message_read -* implement display_current_user --> check why it's present two times +Not Sure: + * implement display_current_user --> check why it's present two times To Check: * check if addons modules rely on some stuff changed in the aboce backend section -To ask ODO: -* message_forward for mail gateway (redundant with notifications by emails ?) - -> I removed it to rely on notifications + diff --git a/addons/mail/mail_message.py b/addons/mail/mail_message.py index a722dca3797..15cb1218218 100644 --- a/addons/mail/mail_message.py +++ b/addons/mail/mail_message.py @@ -120,6 +120,65 @@ class mail_message(osv.Model): 'author_id': _get_default_author } + + #------------------------------------------------------ + # Message loading for web interface + #------------------------------------------------------ + + _limit = 10 + def _message_read(self, cr, uid, messages, domain=[], thread_level=0, fetch_ancestors=False, context=None): + result = [] + for msg in messages[:-1]: + if len(result)<(self._limit-1): + record = { + 'id': msg.id, + 'type': msg.type, + 'attachment_ids': msg.attachment_ids.name_get(context=context), + 'body': msg.body, + 'model': msg.model, + 'res_id': msg.res_id, + 'record_name': msg.record_name, + 'subject': msg.subject, + 'date': msg.date, + 'author_id': msg.author_id.id + } + if thread_level>0: + dom = [('parent_id','=', msg.id)] + if thread_level==1: + dom = [('parent_id','child_of', [msg.id])] + newids = self.search(cr, uid, domain+dom, context=context, limit=self._limit) + objs = self.browse(cr, uid, newids, context=context) + record['child_ids'] = self._message_read(cr, uid, objs, domain+dom, thread_level-1, context=context) + result.append(record) + else: + result.append({ + 'type': 'expandable', + 'domain': [('id','<=', msg.id)]+domain, + 'context': context + }) + break + + def message_read(self, cr, uid, ids=False, domain=[], thread_level=0, fetch_ancestors=False, context=None): + """ + If IDS are provided, fetch these records, otherwise use the domain to + fetch the matching records. After having fetched the records provided + by IDS, it will fetch children (according to thread_level) and/or the + parents if fetch_ancestrors is True. + + Return [ + + ] + """ + if ids is False: + dom = [] + if thread_level>0: + dom = [('parent_id', '=', False)] + ids = self.search(cr, uid, domain+dom, context=context, limit=10) + + messages = self.browse(cr, uid, ids, context=context) + return self._message_read(cr, uid, messages, thread_level, domain=domain, context=context) + + #------------------------------------------------------ # Email api #------------------------------------------------------ diff --git a/addons/mail/mail_thread.py b/addons/mail/mail_thread.py index 614adcf9697..c962e36fc2c 100644 --- a/addons/mail/mail_thread.py +++ b/addons/mail/mail_thread.py @@ -159,46 +159,6 @@ class mail_thread(osv.Model): return [('message_unread','=',True)] return [] - #------------------------------------------------------ - # Message loading - #------------------------------------------------------ - - def message_read(self, cr, uid, ids, fetch_ancestors=False, ancestor_ids=None, - limit=100, offset=0, domain=None, context=None): - """ OpenChatter feature: read the messages related to some threads. - This method is used mainly the Chatter widget, to directly have - read result instead of searching then reading. - - Please see message_search for more information about the parameters. - """ - print 'MSG READ', uid, ids, fetch_ancestors, ancestor_ids, limit, offset, domain, context - message_ids = self.message_search(cr, uid, ids, fetch_ancestors, ancestor_ids, - limit, offset, domain, context=context) - messages = self.pool.get('mail.message').read(cr, uid, message_ids, context=context) - - """ Retrieve all attachments names """ - map_id_to_name = dict((attachment_id, '') for message in messages for attachment_id in message['attachment_ids']) - - ids = map_id_to_name.keys() - names = self.pool.get('ir.attachment').name_get(cr, uid, ids, context=context) - - # convert the list of tuples into a dictionnary - for name in names: - map_id_to_name[name[0]] = name[1] - - # give corresponding ids and names to each message - for msg in messages: - msg["attachments"] = [] - - for attach_id in msg["attachment_ids"]: - msg["attachments"].append({'id': attach_id, 'name': map_id_to_name[attach_id]}) - - # Set the threads as read - self.message_mark_as_read(cr, uid, ids, context=context) - # Sort and return the messages - messages = sorted(messages, key=lambda d: (-d['id'])) - return messages - #------------------------------------------------------ # Mail gateway #------------------------------------------------------