From f6fdb0b6b3d996dcde7de4ba09b68833b85c702c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Thu, 19 Nov 2015 17:43:10 +0100 Subject: [PATCH] [FIX] website_forum: new post notifications Better message management, linked to the validation feature added in v9 - post a "new post to see" message only when the post is validated - post a "new post to validate" message when the post has to be validated. This message is sent only to followers having sufficient karma. --- addons/website_forum/models/forum.py | 54 ++++++++++++++++++---------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/addons/website_forum/models/forum.py b/addons/website_forum/models/forum.py index a428b1578db..ba7dc132988 100644 --- a/addons/website_forum/models/forum.py +++ b/addons/website_forum/models/forum.py @@ -411,22 +411,12 @@ class Post(models.Model): raise KarmaError('Not enough karma to answer to a question') if not post.parent_id and not post.can_post: post.state = 'pending' - # messaging and chatter - base_url = self.env['ir.config_parameter'].get_param('web.base.url') - if post.parent_id: - body = _( - '

A new answer for %s has been posted. Click here to access the post.

' % - (post.parent_id.name, base_url, slug(post.parent_id.forum_id), slug(post.parent_id)) - ) - post.parent_id.message_post(subject=_('Re: %s') % post.parent_id.name, body=body, subtype='website_forum.mt_answer_new') - else: - body = _( - '

A new question %s has been asked on %s. Click here to access the question.

' % - (post.name, post.forum_id.name, base_url, slug(post.forum_id), slug(post)) - ) - post.message_post(subject=post.name, body=body, subtype='website_forum.mt_question_new') - if post.state == 'active': - self.env.user.sudo().add_karma(post.forum_id.karma_gen_question_new) + + # add karma for posting new questions + if not post.parent_id and post.state == 'active': + self.env.user.sudo().add_karma(post.forum_id.karma_gen_question_new) + + post.post_notification() return post @api.model @@ -483,6 +473,34 @@ class Post(models.Model): obj_id.message_post(body=body, subtype=subtype) return res + @api.multi + def post_notification(self): + base_url = self.env['ir.config_parameter'].get_param('web.base.url') + for post in self: + if post.state == 'active' and post.parent_id: + body = _( + '

A new answer for %s has been posted. Click here to access the post.

' % + (post.parent_id.name, base_url, slug(post.parent_id.forum_id), slug(post.parent_id)) + ) + post.parent_id.message_post(subject=_('Re: %s') % post.parent_id.name, body=body, subtype='website_forum.mt_answer_new') + elif post.state == 'active' and not post.parent_id: + body = _( + '

A new question %s has been asked on %s. Click here to access the question.

' % + (post.name, post.forum_id.name, base_url, slug(post.forum_id), slug(post)) + ) + post.message_post(subject=post.name, body=body, subtype='website_forum.mt_question_new') + elif post.state == 'pending' and not post.parent_id: + # TDE FIXME: in master, you should probably use a subtype; + # however here we remove subtype but set partner_ids + partners = post.message_partner_ids.filtered(lambda partner: partner.user_ids and partner.user_ids.karma >= post.forum_id.karma_moderate) + note_subtype = self.sudo().env.ref('mail.mt_note') + body = _( + '

A new question %s has been asked on %s and require your validation. Click here to access the question.

' % + (post.name, post.forum_id.name, base_url, slug(post.forum_id), slug(post)) + ) + post.message_post(subject=post.name, body=body, subtype_id=note_subtype.id, partner_ids=partners.ids) + return True + @api.multi def reopen(self): if any(post.parent_id or post.state != 'close' for post in self): @@ -528,12 +546,12 @@ class Post(models.Model): if self.state == 'pending': self.create_uid.sudo().add_karma(self.forum_id.karma_gen_question_new) - self.moderator_id = self.env.user - self.write({ 'state': 'active', 'active': True, + 'moderator_id': self.env.user.id, }) + self.post_notification() return True @api.one