Module: Mentionable

Extended by:
ActiveSupport::Concern
Included in:
Reply, Topic
Defined in:
app/models/concerns/mentionable.rb

Instance Method Summary collapse

Instance Method Details

#delete_notifiaction_mentionsObject


11
12
13
# File 'app/models/concerns/mentionable.rb', line 11

def delete_notifiaction_mentions
  Notification.where(notify_type: 'mention', target: self).delete_all
end

#extract_mentioned_usersObject


27
28
29
30
31
32
# File 'app/models/concerns/mentionable.rb', line 27

def extract_mentioned_users
  logins = body.scan(/@(\w{3,20})/).flatten
  if logins.any?
    self.mentioned_user_ids = User.where("login IN (?) AND id != (?)", logins, user.id).limit(5).pluck(:id)
  end
end

#mentioned_user_loginsObject


19
20
21
22
23
24
25
# File 'app/models/concerns/mentionable.rb', line 19

def mentioned_user_logins
  # 用于作为缓存 key
  ids_md5 = Digest::MD5.hexdigest(mentioned_user_ids.to_s)
  Rails.cache.fetch("#{self.class.name.downcase}:#{id}:mentioned_user_logins:#{ids_md5}") do
    User.where(id: mentioned_user_ids).pluck(:login)
  end
end

#mentioned_usersObject


15
16
17
# File 'app/models/concerns/mentionable.rb', line 15

def mentioned_users
  User.where(id: mentioned_user_ids)
end

#no_mention_usersObject


34
35
36
# File 'app/models/concerns/mentionable.rb', line 34

def no_mention_users
  [user]
end

#send_mention_notificationObject


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/concerns/mentionable.rb', line 38

def send_mention_notification
  Notification.bulk_insert(set_size: 100) do |worker|
    (mentioned_users - no_mention_users).each do |user|
      note = {
        notify_type: 'mention',
        actor_id: self.user_id,
        user_id: user.id,
        target_type: self.class.name,
        target_id: self.id
      }
      if self.class.name == 'Reply'
        note[:second_target_type] = 'Topic'
        note[:second_target_id] = self.send(:topic_id)
      end
      worker.add(note)
    end
  end
end