allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From shalithasura...@apache.org
Subject [allura] 01/01: [#8284] Add notifications email support for comments
Date Tue, 09 Jul 2019 16:18:12 GMT
This is an automated email from the ASF dual-hosted git repository.

shalithasuranga pushed a commit to branch ss/8284
in repository https://gitbox.apache.org/repos/asf/allura.git

commit 11bc731fc8fd7c81372fc4f415a7ce0e4d152346
Author: Shalitha Suranga <shalithasuranga@gmail.com>
AuthorDate: Tue Jul 9 21:46:18 2019 +0530

    [#8284] Add notifications email support for comments
---
 Allura/allura/controllers/discuss.py               |  1 +
 Allura/allura/lib/app_globals.py                   |  6 +++++
 Allura/allura/lib/utils.py                         | 10 +++++++-
 Allura/allura/model/auth.py                        | 24 +++++++++++++++++++
 .../allura/templates/mail/usermentions_email.txt   | 28 ++++++++++++++++++++++
 5 files changed, 68 insertions(+), 1 deletion(-)

diff --git a/Allura/allura/controllers/discuss.py b/Allura/allura/controllers/discuss.py
index 7a38484..ba6d693 100644
--- a/Allura/allura/controllers/discuss.py
+++ b/Allura/allura/controllers/discuss.py
@@ -271,6 +271,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)
     redirect(request.referer or '/')
 
 
diff --git a/Allura/allura/lib/app_globals.py b/Allura/allura/lib/app_globals.py
index 2a2581f..4137bf7 100644
--- a/Allura/allura/lib/app_globals.py
+++ b/Allura/allura/lib/app_globals.py
@@ -615,6 +615,12 @@ 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):
 
diff --git a/Allura/allura/lib/utils.py b/Allura/allura/lib/utils.py
index 5d2b4b3..f3419a3 100644
--- a/Allura/allura/lib/utils.py
+++ b/Allura/allura/lib/utils.py
@@ -39,7 +39,8 @@ import emoji
 import json
 from collections import OrderedDict
 
-from tg import redirect
+from bs4 import BeautifulSoup
+from tg import redirect, app_globals as g
 from tg.decorators import before_validate
 from tg.controllers.util import etag_cache
 from paste.deploy.converters import asbool, asint
@@ -789,6 +790,13 @@ def get_reactions_json():
         j[em] =  emoji.emojize(em, use_aliases=True)
     return json.dumps(j)
 
+def get_usernames_from_md(text):
+    usernames = []
+    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('@', ''))
+    return usernames
 
 def get_key_from_value(d, val):
     """ Get key from given value. return empty str if not exists """
diff --git a/Allura/allura/model/auth.py b/Allura/allura/model/auth.py
index ce429b7..3c6d37e 100644
--- a/Allura/allura/model/auth.py
+++ b/Allura/allura/model/auth.py
@@ -451,6 +451,30 @@ class User(MappedClass, ActivityNode, ActivityObject, SearchIndexable):
             cc=cc)
         self.sent_user_message_times.append(datetime.utcnow())
 
+    def send_user_mention_notification(self, mentioned_by, artifact):
+        """Send user mention notification to {self} user.
+
+        """
+        tmpl = g.jinja2_env.get_template('allura:templates/mail/usermentions_email.txt')
+        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,
+            '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'),
+            message_id=h.gen_message_id(),
+            subject=subject,
+            text=tmpl.render(tmpl_context))
+
     @property
     def activity_name(self):
         return self.display_name or self.username
diff --git a/Allura/allura/templates/mail/usermentions_email.txt b/Allura/allura/templates/mail/usermentions_email.txt
new file mode 100644
index 0000000..5d4af58
--- /dev/null
+++ b/Allura/allura/templates/mail/usermentions_email.txt
@@ -0,0 +1,28 @@
+{#-
+       Licensed to the Apache Software Foundation (ASF) under one
+       or more contributor license agreements.  See the NOTICE file
+       distributed with this work for additional information
+       regarding copyright ownership.  The ASF licenses this file
+       to you under the Apache License, Version 2.0 (the
+       "License"); you may not use this file except in compliance
+       with the License.  You may obtain a copy of the License at
+
+         http://www.apache.org/licenses/LICENSE-2.0
+
+       Unless required by applicable law or agreed to in writing,
+       software distributed under the License is distributed on an
+       "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+       KIND, either express or implied.  See the License for the
+       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:
+
+<{{base_url}}{{artifact.url()}}>
+
+---
+
+Sent from {{site_domain}} because your user mention notifications are active,
+
+To unsubscribe from further messages, please visit <{{base_url}}/auth/subscriptions>
\ No newline at end of file


Mime
View raw message