about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2016-03-27 23:51:18 +0200
committerEugen Rochko <eugen@zeonfederated.com>2016-03-27 23:51:18 +0200
commitc38ad208c361cc77e2fa305a0e929f35363c0fcd (patch)
tree74cd611c7c59e8ffb57747b1db0530d6ca9edc0b
parent039e6f951ca16bd4183fb29d782dd095d2b4120a (diff)
Fix mentions regex to exclude usernames in Medium URLs
-rw-r--r--app/models/account.rb2
-rw-r--r--spec/models/account_spec.rb24
2 files changed, 25 insertions, 1 deletions
diff --git a/app/models/account.rb b/app/models/account.rb
index 007fc77b6..f61f4c765 100644
--- a/app/models/account.rb
+++ b/app/models/account.rb
@@ -31,7 +31,7 @@ class Account < ActiveRecord::Base
   has_many :following, through: :active_relationships,  source: :target_account
   has_many :followers, through: :passive_relationships, source: :account
 
-  MENTION_RE = /(?:^|\W)@([a-z0-9_]+(?:@[a-z0-9\.\-]+)?)/i
+  MENTION_RE = /(?:^|\s|\.)@([a-z0-9_]+(?:@[a-z0-9\.\-]+)?)/i
 
   def follow!(other_account)
     self.active_relationships.where(target_account: other_account).first_or_create!(target_account: other_account)
diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb
index 059e4ee3a..9d19fd3b4 100644
--- a/spec/models/account_spec.rb
+++ b/spec/models/account_spec.rb
@@ -111,4 +111,28 @@ RSpec.describe Account, type: :model do
   describe '.find_local' do
     pending
   end
+
+  describe 'MENTION_RE' do
+    subject { Account::MENTION_RE }
+
+    it 'matches usernames in the middle of a sentence' do
+      expect(subject.match('Hello to @alice from me')[1]).to eq 'alice'
+    end
+
+    it 'matches usernames in the beginning of status' do
+      expect(subject.match('@alice Hey how are you?')[1]).to eq 'alice'
+    end
+
+    it 'matches dot-prepended usernames' do
+      expect(subject.match('.@alice I want everybody to see this')[1]).to eq 'alice'
+    end
+
+    it 'does not match e-mails' do
+      expect(subject.match('Drop me an e-mail at alice@example.com')).to be_nil
+    end
+
+    it 'does not match URLs' do
+      expect(subject.match('Check this out https://medium.com/@alice/some-article#.abcdef123')).to be_nil
+    end
+  end
 end