diff --git a/biostar/accounts/models.py b/biostar/accounts/models.py index fd08d08f3..e831e587e 100644 --- a/biostar/accounts/models.py +++ b/biostar/accounts/models.py @@ -240,6 +240,13 @@ def edit_url(self): return reverse('edit_profile') + @property + def no_messages(self): + """ + User has turned all notifications off + """ + return self.message_prefs == self.NO_MESSAGES + @property def mailing_list(self): """ diff --git a/biostar/forum/auth.py b/biostar/forum/auth.py index 8ea8ae9dd..814d3e086 100644 --- a/biostar/forum/auth.py +++ b/biostar/forum/auth.py @@ -232,7 +232,6 @@ def create_post(author, title, content, request=None, root=None, parent=None, pt def diff_ratio(text1, text2): - # Do not match on spaces s = SequenceMatcher(lambda char: re.match(r'\w+', char), text1, text2) return round(s.ratio(), 5) @@ -313,12 +312,8 @@ def create_subscription(post, user, sub_type=None, update=False): subs = Subscription.objects.filter(post=post.root, user=user) sub = subs.first() - default = Subscription.TYPE_MAP.get(user.profile.message_prefs, - Subscription.LOCAL_MESSAGE) + default = Subscription.TYPE_MAP.get(user.profile.message_prefs, Subscription.LOCAL_MESSAGE) - empty = sub_type is None - # Get the current sub type from what's given or the existing sub - sub_type = None if empty else sub_type # No type has been given so default sub_type = sub_type or default diff --git a/biostar/forum/markdown.py b/biostar/forum/markdown.py index e86443c8f..a060610e8 100644 --- a/biostar/forum/markdown.py +++ b/biostar/forum/markdown.py @@ -17,7 +17,7 @@ from mistune import Renderer, InlineLexer, InlineGrammar from mistune import escape as escape_text from bleach.sanitizer import Cleaner -from biostar.forum import auth +from biostar.forum import auth, tasks from biostar.forum.models import Post, Subscription from biostar.accounts.models import Profile, User from bleach.callbacks import nofollow @@ -189,15 +189,24 @@ def rewrite_static(link): return link +def resolve_subtype(user): + """ + Resolve a users subscription type to be LOCAL_MESSAGE or EMAIL_MESSAGE + """ + return Profile.LOCAL_MESSAGE if user.no_messages else user.profile.message_prefs + + class BiostarInlineLexer(MonkeyPatch): grammar_class = BiostarInlineGrammer - def __init__(self, root=None, allow_rewrite=False, *args, **kwargs): + def __init__(self, post=None, allow_rewrite=False, *args, **kwargs): """ :param root: Root post that is being pared :param static_imgs: """ - self.root = root + self.post = post + # Resolve the root if exists. + self.root = post.parent.root if (post and post.parent) else None self.allow_rewrite = allow_rewrite super(BiostarInlineLexer, self).__init__(*args, **kwargs) @@ -247,14 +256,16 @@ def output_mention_link(self, m): profile = reverse("user_profile", kwargs=dict(uid=user.profile.uid)) link = f'{user.profile.name}' # Subscribe mentioned users to post. - if self.root: + if self.post: # Create user subscription if it does not already exist. - auth.create_subscription(post=self.root, user=user, update=True) + sub_type = resolve_subtype(user) + auth.create_subscription(post=self.root, user=user, sub_type=sub_type, update=True) else: link = m.group(0) return link + def output_post_link(self, m): uid = m.group("uid") link = m.group(0) @@ -416,14 +427,11 @@ def parse(text, post=None, clean=True, escape=True, allow_rewrite=False): eg. images/foo.png -> /static/images/foo.png """ - # Resolve the root if exists. - root = post.parent.root if (post and post.parent) else None - # Initialize the renderer renderer = BiostarRenderer(escape=escape) # Initialize the lexer - inline = BiostarInlineLexer(renderer=renderer, root=root, allow_rewrite=allow_rewrite) + inline = BiostarInlineLexer(renderer=renderer, post=post, allow_rewrite=allow_rewrite) markdown = mistune.Markdown(hard_wrap=True, renderer=renderer, inline=inline) diff --git a/biostar/forum/templates/messages/ping.md b/biostar/forum/templates/messages/ping.md new file mode 100644 index 000000000..1f5e7bf38 --- /dev/null +++ b/biostar/forum/templates/messages/ping.md @@ -0,0 +1,2 @@ + +[{{ post.title}}]({{ post.get_absolute_url }}): {{post.content|truncatewords:180}} \ No newline at end of file diff --git a/biostar/forum/templates/messages/ping_email.html b/biostar/forum/templates/messages/ping_email.html new file mode 100644 index 000000000..06d78fafa --- /dev/null +++ b/biostar/forum/templates/messages/ping_email.html @@ -0,0 +1,43 @@ +{% load accounts_tags %} +{% block subject %} + + [Biostar] {{ post.title|truncatechars:60 }} + +{% endblock %} + +{% block html %} + + A user has tagged you on Biostar +
+ User + {{ post.author.profile.name }} wrote + + {{ post.title }}: +
++ {{ post.html|safe }} +
+ ++ You may visit {{ protocol }}://{{ domain }}{{ post.get_absolute_url }} +
+ +The Biostar Team
+ +{% endblock %} + +{% block text %} + + A user has tagged you on {{ domain }}. + + User {{ user.profile.name }} wrote {{ post.title }}: + + {{ post.content }} + + You may visit {{ protocol }}://{{ domain }}{{ post.get_absolute_url }} + + The Biostar Team + + +{% endblock %}