REFACTOR: Simplify extraction of mentions

REFACTOR: Simplify extraction of mentions

From 886ef7251dffb951279dbb652c5f53ba9946b77a Mon Sep 17 00:00:00 2001
From: Gerhard Schlager <mail@gerhard-schlager.at>
Date: Tue, 27 Nov 2018 12:43:05 +0100
Subject: [PATCH] REFACTOR: Simplify extraction of mentions


diff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rb
index 1ebca65..8cda53c 100644
--- a/app/services/post_alerter.rb
+++ b/app/services/post_alerter.rb
@@ -454,21 +454,17 @@ class PostAlerter
   # TODO: Move to post-analyzer?
   def extract_mentions(post)
     mentions = post.raw_mentions
-
-    return unless mentions && mentions.length > 0
+    return if mentions.blank?
 
     groups = Group.where('LOWER(name) IN (?)', mentions)
+    mentions -= groups.map(&:name).map(&:downcase)
+    groups = nil if groups.empty?
 
-    if groups.empty?
-      groups = nil
-    else
-      mentions -= groups.map(&:name).map(&:downcase)
-      return [groups, nil] unless mentions && mentions.length > 0
+    if mentions.present?
+      users = User.where(username_lower: mentions).where.not(id: post.user_id)
+      users = nil if users.empty?
     end
 
-    users = User.where(username_lower: mentions).where.not(id: post.user_id)
-    users = nil if users.empty?
-
     [groups, users]
   end

GitHub

1 Like

Follow-up to FIX: Notify only invited users about mentions in PMs · discourse/discourse@5640166 · GitHub