allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brond...@apache.org
Subject [allura] 04/07: [#8284] Improvements to notification email sender for user mentions
Date Thu, 08 Aug 2019 21:17:40 GMT
This is an automated email from the ASF dual-hosted git repository.

brondsem pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/allura.git

commit a13a5c7e0f4e76e36b5a685cb7de198df3a34635
Author: Shalitha Suranga <shalithasuranga@gmail.com>
AuthorDate: Thu Jul 18 23:30:54 2019 +0530

    [#8284] Improvements to notification email sender for user mentions
---
 Allura/allura/controllers/discuss.py                      |  6 +++++-
 Allura/allura/lib/app_globals.py                          |  7 -------
 Allura/allura/lib/utils.py                                |  5 +++--
 Allura/allura/model/auth.py                               |  8 ++++----
 Allura/allura/tasks/notification_tasks.py                 | 15 ++++++++++++++-
 .../{usermentions_email.txt => usermentions_email.md}     |  6 +++---
 6 files changed, 29 insertions(+), 18 deletions(-)

diff --git a/Allura/allura/controllers/discuss.py b/Allura/allura/controllers/discuss.py
index ba6d693..d3519c1 100644
--- a/Allura/allura/controllers/discuss.py
+++ b/Allura/allura/controllers/discuss.py
@@ -38,6 +38,8 @@ from allura.lib import helpers as h
 from allura.lib.decorators import require_post, memorable_forget
 from allura.lib.security import has_access, require_access
 
+from allura.tasks import notification_tasks
+
 from allura.lib.widgets import discuss as DW
 from allura.lib.widgets import form_fields as ffw
 
@@ -271,7 +273,7 @@ def handle_post_or_reply(thread, edit_widget, rate_limit, kw, parent_post_id=Non
     if thread.artifact:
         thread.artifact.mod_date = datetime.utcnow()
     flash('Message posted')
-    g.send_usermentions_notification(kw['text'], p)
+    notification_tasks.send_usermentions_notification(p, kw['text'])
     redirect(request.referer or '/')
 
 
@@ -310,6 +312,7 @@ class PostController(BaseController):
     def index(self, version=None, **kw):
         c.post = self.W.post
         if request.method == 'POST':
+            old_text = self.post.text
             require_access(self.post, 'moderate')
             post_fields = self.W.edit_post.to_python(kw, None)  # could raise Invalid, but
doesn't seem like it ever does
             file_info = post_fields.pop('file_info', None)
@@ -324,6 +327,7 @@ class PostController(BaseController):
             self.post.last_edit_by_id = c.user._id
             self.thread.is_spam(self.post)  # run spam checker, nothing to do with result
yet
             self.post.commit()
+            notification_tasks.send_usermentions_notification(self.post, kw['text'], old_text)
             g.director.create_activity(c.user, 'modified', self.post,
                                        target=self.post.thread.artifact or self.post.thread,
                                        related_nodes=[self.post.app_config.project],
diff --git a/Allura/allura/lib/app_globals.py b/Allura/allura/lib/app_globals.py
index d03dcc7..a30607e 100644
--- a/Allura/allura/lib/app_globals.py
+++ b/Allura/allura/lib/app_globals.py
@@ -615,13 +615,6 @@ class Globals(object):
             "image_height": logo['image_height']
         }
 
-    def send_usermentions_notification(self, text, artifact):
-        usernames = utils.get_usernames_from_md(text)
-        for username in usernames:
-            u = M.User.by_username(username)
-            u.send_user_mention_notification(c.user, artifact)
-
-
 class Icon(object):
 
     def __init__(self, css, title=None):
diff --git a/Allura/allura/lib/utils.py b/Allura/allura/lib/utils.py
index 0d63388..0203f7a 100644
--- a/Allura/allura/lib/utils.py
+++ b/Allura/allura/lib/utils.py
@@ -794,11 +794,12 @@ def get_reactions_json():
     return json.dumps(j)
 
 def get_usernames_from_md(text):
-    usernames = []
+    """ Returns a unique usernames set from a text """
+    usernames = set()
     html_text = g.markdown.convert(text)
     soup = BeautifulSoup(html_text, 'html.parser')
     for mention in soup.select('a.user-mention'):
-        usernames.append(mention.get_text().replace('@', ''))
+        usernames.add(mention.get_text().replace('@', ''))
     return usernames
 
 def get_key_from_value(d, val):
diff --git a/Allura/allura/model/auth.py b/Allura/allura/model/auth.py
index a53f8c6..915f4df 100644
--- a/Allura/allura/model/auth.py
+++ b/Allura/allura/model/auth.py
@@ -461,22 +461,22 @@ class User(MappedClass, ActivityNode, ActivityObject, SearchIndexable):
         """Send user mention notification to {self} user.
 
         """
-        tmpl = g.jinja2_env.get_template('allura:templates/mail/usermentions_email.txt')
+        tmpl = g.jinja2_env.get_template('allura:templates/mail/usermentions_email.md')
         subject = '[%s:%s] Your name was mentioned' % (
             c.project.shortname, c.app.config.options.mount_point)
         tmpl_context = {
             'site_domain': config['domain'],
             'base_url': config['base_url'],
             'user': c.user,
-            'artifact': artifact,
+            'artifact_link': h.absurl(artifact.url()),
             'mentioned_by': mentioned_by,
             'project_name': c.project.shortname,
             'mount_point': c.app.config.options.mount_point
         }
         allura.tasks.mail_tasks.sendsimplemail.post(
             toaddr=self.get_pref('email_address'),
-            fromaddr=mentioned_by.get_pref('email_address'),
-            reply_to=mentioned_by.get_pref('email_address'),
+            fromaddr=g.noreply,
+            reply_to=g.noreply,
             message_id=h.gen_message_id(),
             subject=subject,
             text=tmpl.render(tmpl_context))
diff --git a/Allura/allura/tasks/notification_tasks.py b/Allura/allura/tasks/notification_tasks.py
index e370222..58a1ab5 100644
--- a/Allura/allura/tasks/notification_tasks.py
+++ b/Allura/allura/tasks/notification_tasks.py
@@ -16,10 +16,23 @@
 #       under the License.
 
 from allura.lib.decorators import task
-
+from allura.lib import utils
+from tg import tmpl_context as c
 
 @task
 def notify(n_id, ref_ids, topic):
     from allura import model as M
     M.Mailbox.deliver(n_id, ref_ids, topic)
     M.Mailbox.fire_ready()
+
+@task
+def send_usermentions_notification(artifact, text, old_text=None):
+    from allura import model as M
+    usernames = utils.get_usernames_from_md(text)
+    if old_text:
+        old_usernames = utils.get_usernames_from_md(old_text)
+        usernames -= old_usernames
+
+    for username in list(usernames):
+        u = M.User.by_username(username)
+        u.send_user_mention_notification(c.user, artifact)
diff --git a/Allura/allura/templates/mail/usermentions_email.txt b/Allura/allura/templates/mail/usermentions_email.md
similarity index 87%
rename from Allura/allura/templates/mail/usermentions_email.txt
rename to Allura/allura/templates/mail/usermentions_email.md
index 5d4af58..886f16f 100644
--- a/Allura/allura/templates/mail/usermentions_email.txt
+++ b/Allura/allura/templates/mail/usermentions_email.md
@@ -16,10 +16,10 @@
        specific language governing permissions and limitations
        under the License.
 -#}
-Your name was mentioned at [{{project_name}}:{{mount_point}}] by {{mentioned_by.display_name}}
-It can be viewed using the link below:
+Your name was mentioned at [{{project_name}}:{{mount_point}}]({{artifact_link}}) by {{mentioned_by.display_name}}
 
-<{{base_url}}{{artifact.url()}}>
+It can be viewed using the link below:
+{{artifact_link}}
 
 ---
 


Mime
View raw message