debian-mirror-gitlab/lib/gitlab/email/handler/unsubscribe_handler.rb

54 lines
1.6 KiB
Ruby
Raw Normal View History

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