Class: Gitlab::Email::Handler::UnsubscribeHandler

Inherits:
BaseHandler
  • Object
show all
Defined in:
lib/gitlab/email/handler/unsubscribe_handler.rb

Constant Summary collapse

HANDLER_REGEX_FOR =
-> (suffix) { /\A(?<reply_token>\w+)#{Regexp.escape(suffix)}\z/ }.freeze
HANDLER_REGEX =
HANDLER_REGEX_FOR.call(Gitlab::IncomingEmail::UNSUBSCRIBE_SUFFIX).freeze
HANDLER_REGEX_LEGACY =
HANDLER_REGEX_FOR.call(Gitlab::IncomingEmail::UNSUBSCRIBE_SUFFIX_LEGACY).freeze

Instance Attribute Summary

Attributes inherited from BaseHandler

#mail, #mail_key

Instance Method Summary collapse

Methods inherited from BaseHandler

#metrics_params

Constructor Details

#initialize(mail, mail_key) ⇒ UnsubscribeHandler

Returns a new instance of UnsubscribeHandler.


18
19
20
21
22
23
# File 'lib/gitlab/email/handler/unsubscribe_handler.rb', line 18

def initialize(mail, mail_key)
  super(mail, mail_key)

  matched = HANDLER_REGEX.match(mail_key.to_s) || HANDLER_REGEX_LEGACY.match(mail_key.to_s)
  @reply_token = matched[:reply_token] if matched
end

Instance Method Details

#can_handle?Boolean

Returns:

  • (Boolean)

25
26
27
# File 'lib/gitlab/email/handler/unsubscribe_handler.rb', line 25

def can_handle?
  reply_token.present?
end

#executeObject


29
30
31
32
33
34
35
36
37
# File 'lib/gitlab/email/handler/unsubscribe_handler.rb', line 29

def execute
  raise SentNotificationNotFoundError unless sent_notification
  return unless sent_notification.unsubscribable?

  noteable = sent_notification.noteable
  raise NoteableNotFoundError unless noteable

  noteable.unsubscribe(sent_notification.recipient)
end

#metrics_eventObject


39
40
41
# File 'lib/gitlab/email/handler/unsubscribe_handler.rb', line 39

def metrics_event
  :receive_email_unsubscribe
end