2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-16 20:27:52 -05:00
|
|
|
class UnsubscribeKey < ActiveRecord::Base
|
|
|
|
belongs_to :user
|
|
|
|
belongs_to :post
|
|
|
|
belongs_to :topic
|
|
|
|
|
|
|
|
before_create :generate_random_key
|
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
ALL_TYPE = "all"
|
|
|
|
DIGEST_TYPE = "digest"
|
|
|
|
TOPIC_TYPE = "topic"
|
2022-06-21 13:49:47 -05:00
|
|
|
|
|
|
|
class << self
|
|
|
|
def create_key_for(user, type, post: nil)
|
|
|
|
unsubscribe_key = new(user_id: user.id, unsubscribe_key_type: type)
|
|
|
|
|
|
|
|
if type == TOPIC_TYPE
|
|
|
|
unsubscribe_key.topic_id = post.topic_id
|
|
|
|
unsubscribe_key.post_id = post.id
|
|
|
|
end
|
|
|
|
|
|
|
|
unsubscribe_key.save!
|
|
|
|
unsubscribe_key.key
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_for_key(key)
|
|
|
|
where(key: key).first&.user
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_unsubscribe_strategy_for(key)
|
|
|
|
strategies = {
|
|
|
|
DIGEST_TYPE => EmailControllerHelper::DigestEmailUnsubscriber,
|
|
|
|
TOPIC_TYPE => EmailControllerHelper::TopicEmailUnsubscriber,
|
2023-01-09 06:20:10 -06:00
|
|
|
ALL_TYPE => EmailControllerHelper::BaseEmailUnsubscriber,
|
2022-06-21 13:49:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
DiscoursePluginRegistry.email_unsubscribers.each do |unsubcriber|
|
|
|
|
strategies.merge!(unsubcriber)
|
|
|
|
end
|
|
|
|
|
|
|
|
strategies[key.unsubscribe_key_type]&.new(key)
|
2016-06-16 20:27:52 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-21 13:49:47 -05:00
|
|
|
def associated_topic
|
|
|
|
@associated_topic ||= topic || post&.topic
|
2016-06-16 20:27:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2018-06-07 00:28:18 -05:00
|
|
|
def generate_random_key
|
|
|
|
self.key = SecureRandom.hex(32)
|
|
|
|
end
|
2016-06-16 20:27:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: unsubscribe_keys
|
|
|
|
#
|
|
|
|
# key :string(64) not null, primary key
|
|
|
|
# user_id :integer not null
|
2019-01-11 13:29:56 -06:00
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
2016-06-16 20:27:52 -05:00
|
|
|
# unsubscribe_key_type :string
|
|
|
|
# topic_id :integer
|
2016-07-22 16:27:30 -05:00
|
|
|
# post_id :integer
|
2016-06-16 20:27:52 -05:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
|
|
|
# index_unsubscribe_keys_on_created_at (created_at)
|
|
|
|
#
|