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