summaryrefslogtreecommitdiff
path: root/lib/gitlab/email/handler/unsubscribe_handler.rb
blob: 97d7a8d65ff95e168e8d684708c18c75167355ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
require 'gitlab/email/handler/base_handler'

module Gitlab
  module Email
    module Handler
      class UnsubscribeHandler < BaseHandler
        def can_handle?
          mail_key =~ /\A\w+#{Regexp.escape(Gitlab::IncomingEmail::UNSUBSCRIBE_SUFFIX)}\z/
        end

        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

        private

        def sent_notification
          @sent_notification ||= SentNotification.for(reply_key)
        end

        def reply_key
          mail_key.sub(Gitlab::IncomingEmail::UNSUBSCRIBE_SUFFIX, '')
        end
      end
    end
  end
end