2018-08-23 09:22:23 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-10-12 17:07:36 +00:00
|
|
|
require 'gitlab/email/handler/base_handler'
|
|
|
|
|
2018-12-14 18:37:03 +00:00
|
|
|
# handles unsubscribe emails with these formats:
|
2018-12-13 00:35:01 +00:00
|
|
|
# incoming+1234567890abcdef1234567890abcdef-unsubscribe@incoming.gitlab.com
|
|
|
|
# incoming+1234567890abcdef1234567890abcdef+unsubscribe@incoming.gitlab.com (legacy)
|
2016-10-12 17:07:36 +00:00
|
|
|
module Gitlab
|
|
|
|
module Email
|
|
|
|
module Handler
|
|
|
|
class UnsubscribeHandler < BaseHandler
|
2017-04-20 10:33:51 +00:00
|
|
|
delegate :project, to: :sent_notification, allow_nil: true
|
|
|
|
|
2018-12-20 17:15:31 +00:00
|
|
|
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
|
2018-12-13 00:35:01 +00:00
|
|
|
|
|
|
|
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)
|
2018-12-14 18:37:03 +00:00
|
|
|
@reply_token = matched[:reply_token] if matched
|
2018-12-13 00:35:01 +00:00
|
|
|
end
|
|
|
|
|
2016-10-12 17:07:36 +00:00
|
|
|
def can_handle?
|
2018-12-28 18:53:39 +00:00
|
|
|
reply_token.present?
|
2016-10-12 17:07:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
raise SentNotificationNotFoundError unless sent_notification
|
|
|
|
return unless sent_notification.unsubscribable?
|
|
|
|
|
|
|
|
noteable = sent_notification.noteable
|
|
|
|
raise NoteableNotFoundError unless noteable
|
2017-11-14 09:02:39 +00:00
|
|
|
|
2016-10-12 17:07:36 +00:00
|
|
|
noteable.unsubscribe(sent_notification.recipient)
|
|
|
|
end
|
|
|
|
|
2019-09-13 13:26:31 +00:00
|
|
|
def metrics_event
|
|
|
|
:receive_email_unsubscribe
|
|
|
|
end
|
|
|
|
|
2016-10-12 17:07:36 +00:00
|
|
|
private
|
|
|
|
|
2018-12-20 17:15:31 +00:00
|
|
|
attr_reader :reply_token
|
|
|
|
|
2016-10-12 17:07:36 +00:00
|
|
|
def sent_notification
|
2018-12-20 17:15:31 +00:00
|
|
|
@sent_notification ||= SentNotification.for(reply_token)
|
2016-10-12 17:07:36 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|