Module: Motor::Notes::NotifyMentions

Defined in:
lib/motor/notes/notify_mentions.rb

Constant Summary collapse

EMAIL_REGEXP =
/[a-z0-9][.']?(?:(?:[a-z0-9_-]++[.'])*[a-z0-9_-]++)*@(?:[a-z0-9]++[.-])*[a-z0-9]++\.[a-z]{2,}/i.freeze
NOTIFICATION_DESCRIPTION_LIMIT =
100

Class Method Summary collapse

Class Method Details

.build_mention_description(note, current_user) ⇒ Object



55
56
57
58
59
# File 'lib/motor/notes/notify_mentions.rb', line 55

def build_mention_description(note, current_user)
  I18n.t('motor.user_mentioned_you_with_note',
         user: current_user&.email || 'Anonymous',
         note: note.body.truncate(NOTIFICATION_DESCRIPTION_LIMIT))
end

.build_mention_title(note) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/motor/notes/notify_mentions.rb', line 45

def build_mention_title(note)
  configs = Motor::BuildSchema.for_model(note.record.class)

  display_value = note.record.attributes[configs['display_column']]

  I18n.t('motor.new_mention_for',
         resource: ["#{configs['display_name'].singularize} ##{note.record[note.record.class.primary_key]}",
                    display_value].join(' '))
end

.call(note, current_user) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/motor/notes/notify_mentions.rb', line 12

def call(note, current_user)
  users_class = fetch_users_class(current_user)

  return unless users_class

  emails  = note.body.scan(EMAIL_REGEXP)
  emails -= [current_user&.email]

  users_class.where(email: emails).each do |user|
    notification = find_or_build_notification(note, user, current_user)

    next if notification.persisted?

    notification.save!

    Motor::NotificationsMailer.notify_mention_email(notification).deliver_later!
    Motor::NotificationsChannel.broadcast_to(user, ['notify', notification.as_json(include: %i[record])])
  end
end

.fetch_users_class(current_user) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/motor/notes/notify_mentions.rb', line 61

def fetch_users_class(current_user)
  return current_user.class if current_user
  return Motor::AdminUser if defined?(Motor::AdminUser)
  return AdminUser if defined?(AdminUser)
  return User if defined?(User)

  nil
end

.find_or_build_notification(note, user, current_user) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/motor/notes/notify_mentions.rb', line 32

def find_or_build_notification(note, user, current_user)
  notification = note.notifications.find { |e| e.recipient_id == user.id && e.recipient_type == user.class.name }

  return notification if notification

  Motor::Notification.new(
    title: build_mention_title(note),
    description: build_mention_description(note, current_user),
    recipient: user,
    record: note
  )
end