diff --git a/addons/mail/mail_group.py b/addons/mail/mail_group.py index 50f636c3c5b..25654bfed8b 100644 --- a/addons/mail/mail_group.py +++ b/addons/mail/mail_group.py @@ -136,15 +136,14 @@ class mail_group(osv.Model): 'domain': [('model', '=', 'mail.group'), ('res_id', '=', mail_group_id)], 'context': {'default_model': 'mail.group', 'default_res_id': mail_group_id, - 'search_default_message_unread': True, - 'mail_read_keep_unread':True}, + 'search_default_message_unread': True}, 'res_model': 'mail.message', 'thread_level': 1, 'header_description': vals.get('description'), } cobj = self.pool.get('ir.actions.client') newref = cobj.copy(cr, SUPERUSER_ID, ref[1], default={'params': str(params), 'name': vals['name']}, context=context) - mobj.write(cr, SUPERUSER_ID, menu_id, { 'action': 'ir.actions.client,' + str(newref), 'mail_group_id': mail_group_id}, context=context) + mobj.write(cr, SUPERUSER_ID, menu_id, {'action': 'ir.actions.client,' + str(newref), 'mail_group_id': mail_group_id}, context=context) if vals.get('group_ids'): self._subscribe_users(cr, uid, [mail_group_id], context=context) diff --git a/addons/mail/mail_message.py b/addons/mail/mail_message.py index 831e1e4b162..207b46de5d5 100644 --- a/addons/mail/mail_message.py +++ b/addons/mail/mail_message.py @@ -492,6 +492,7 @@ class mail_message(osv.Model): message_unload_ids = message_unload_ids if message_unload_ids is not None else [] if message_unload_ids: domain += [('id', 'not in', message_unload_ids)] + notification_obj = self.pool.get('mail.notification') limit = limit or self._message_read_limit message_tree = {} message_list = [] @@ -501,11 +502,6 @@ class mail_message(osv.Model): if ids is None: ids = self.search(cr, uid, domain, context=context, limit=limit) - # if requested: mark messages as read - if context and context.has_key('mail_read_set_read'): - notif_ids = notif_obj.search(cr, uid, [('message_id', '=', msg.id)], context=context) - notif_obj.write(cr, uid, notif_ids, {'read': True}, context=context) - # fetch parent if threaded, sort messages for message in self.browse(cr, uid, ids, context=context): message_id = message.id @@ -534,10 +530,17 @@ class mail_message(osv.Model): message_id_list.sort(key=lambda item: item['id']) message_id_list.insert(0, self._message_read_dict(cr, uid, message_tree[key], context=context)) + # create final ordered message_list based on parent_tree parent_list = parent_tree.items() parent_list = sorted(parent_list, key=lambda item: max([msg.get('id') for msg in item[1]]) if item[1] else item[0], reverse=True) message_list = [message for (key, msg_list) in parent_list for message in msg_list] + # if requested: mark messages as read + if context and context.get('mail_read_set_read'): + message_ids = [message.get('id') for message in message_list] + notif_ids = notification_obj.search(cr, uid, [('partner_id.user_ids', 'in', uid), ('message_id', 'in', message_ids)], context=context) + notification_obj.write(cr, uid, notif_ids, {'read': True}, context=context) + # get the child expandable messages for the tree self._message_read_dict_postprocess(cr, uid, message_list, message_tree, context=context) self._message_read_add_expandables(cr, uid, message_list, message_tree, parent_tree, @@ -605,12 +608,14 @@ class mail_message(osv.Model): model_ids.setdefault(message.get('model'), {}).setdefault(message.get('res_id'), set()).add(message.get('id')) allowed_ids = self._find_allowed_doc_ids(cr, uid, model_ids, context=context) - final_ids = author_ids | partner_ids | allowed_ids + if count: return len(final_ids) else: - return list(final_ids) + # re-construct a list based on ids, because set did not keep the original order + id_list = [id for id in ids if id in final_ids] + return id_list def check_access_rule(self, cr, uid, ids, operation, context=None): """ Access rules of mail.message: diff --git a/addons/mail/static/src/js/mail.js b/addons/mail/static/src/js/mail.js index f873daff9c3..c5e28fe6fb0 100644 --- a/addons/mail/static/src/js/mail.js +++ b/addons/mail/static/src/js/mail.js @@ -1540,6 +1540,7 @@ openerp.mail = function (session) { 'show_compose_message': this.view.is_action_enabled('edit'), }); this.node.context = { + 'mail_read_set_read': true, // set messages as read in Chatter 'default_res_id': this.view.datarecord.id || false, 'default_model': this.view.model || false, }; diff --git a/addons/mail/tests/test_message_read.py b/addons/mail/tests/test_message_read.py index 46dd29a5431..fb195eaec2b 100644 --- a/addons/mail/tests/test_message_read.py +++ b/addons/mail/tests/test_message_read.py @@ -26,7 +26,8 @@ class test_mail_access_rights(TestMailBase): def test_00_message_read(self): """ Tests for message_read and expandables. """ - cr, uid, user_admin, group_pigs = self.cr, self.uid, self.user_admin, self.group_pigs + cr, uid, user_admin, user_raoul, group_pigs = self.cr, self.uid, self.user_admin, self.user_raoul, self.group_pigs + self.mail_group.message_subscribe_users(cr, uid, [group_pigs.id], [user_raoul.id]) pigs_domain = [('model', '=', 'mail.group'), ('res_id', '=', self.group_pigs_id)] # Data: create a discussion in Pigs (3 threads, with respectively 0, 4 and 4 answers) @@ -44,20 +45,32 @@ class test_mail_access_rights(TestMailBase): msg_ids = [msg_id10, msg_id9, msg_id8, msg_id7, msg_id6, msg_id5, msg_id4, msg_id3, msg_id2, msg_id1, msg_id0] ordered_msg_ids = [msg_id2, msg_id4, msg_id6, msg_id8, msg_id10, msg_id1, msg_id3, msg_id5, msg_id7, msg_id9, msg_id0] + # Test: raoul received notifications + raoul_notification_ids = self.mail_notification.search(cr, user_raoul.id, [('read', '=', False), ('message_id', 'in', msg_ids), ('partner_id', '=', user_raoul.partner_id.id)]) + self.assertEqual(len(raoul_notification_ids), 11, 'message_post: wrong number of produced notifications') + # Test: read some specific ids - read_msg_list = self.mail_message.message_read(cr, uid, ids=msg_ids[2:4], domain=[('body', 'like', 'dummy')]) + read_msg_list = self.mail_message.message_read(cr, user_raoul.id, ids=msg_ids[2:4], domain=[('body', 'like', 'dummy')], context={'mail_read_set_read': True}) read_msg_ids = [msg.get('id') for msg in read_msg_list] self.assertEqual(msg_ids[2:4], read_msg_ids, 'message_read with direct ids should read only the requested ids') + # Test: raoul notifications are read + raoul_notification_ids = self.mail_notification.search(cr, user_raoul.id, [('read', '=', False), ('message_id', 'in', msg_ids), ('partner_id', '=', user_raoul.partner_id.id)]) + self.assertEqual(len(raoul_notification_ids), 9, 'message_post: wrong number of produced and/or read notifications') + # Test: read messages of Pigs through a domain, being thread or not threaded - read_msg_list = self.mail_message.message_read(cr, uid, domain=pigs_domain, limit=200) + read_msg_list = self.mail_message.message_read(cr, user_raoul.id, domain=pigs_domain, limit=200) read_msg_ids = [msg.get('id') for msg in read_msg_list] self.assertEqual(msg_ids, read_msg_ids, 'message_read flat with domain on Pigs should equal all messages of Pigs') - read_msg_list = self.mail_message.message_read(cr, uid, domain=pigs_domain, limit=200, thread_level=1) + read_msg_list = self.mail_message.message_read(cr, user_raoul.id, domain=pigs_domain, limit=200, thread_level=1) read_msg_ids = [msg.get('id') for msg in read_msg_list] self.assertEqual(ordered_msg_ids, read_msg_ids, 'message_read threaded with domain on Pigs should equal all messages of Pigs, and sort them with newer thread first, last message last in thread') + # Test: raoul notifications did not change + raoul_notification_ids = self.mail_notification.search(cr, user_raoul.id, [('read', '=', False), ('message_id', 'in', msg_ids), ('partner_id', '=', user_raoul.partner_id.id)]) + self.assertEqual(len(raoul_notification_ids), 9, 'message_post: wrong number of produced and/or read notifications') + # ---------------------------------------- # CASE1: message_read with domain, threaded # We simulate an entire flow, using the expandables to test them @@ -80,14 +93,6 @@ class test_mail_access_rights(TestMailBase): elif msg.get('type') == 'expandable': new_msg_exp = msg - # #check notification For message readable or unread. - # notif_msg1_ids1 = self.mail_notification.search(cr, uid, [('partner_id', '=', user_bert.partner_id.id),('message_id', '=', message1.id)], context=context) - # notif_data1 = self.mail_notification.read(cr, uid, notif_msg1_ids1, ['read'])[0]['read'] - # # Test: Message1 are marked as read. - # self.assertTrue(notif_data1, 'Message1 are set as read for the user Bert that read it') - # #Pass key in context for unread message.. - # context.update({'default_model': 'mail.group', 'default_res_id': [self.group_pigs_id], 'mail_keep_unread': True}) - # Do: fetch new messages in first thread, domain from expandable self.assertIsNotNone(new_msg_exp, 'message_read on last Pigs message should have returned a new messages expandable') domain = new_msg_exp.get('domain', [])