42 lines
1.1 KiB
Ruby
42 lines
1.1 KiB
Ruby
# typed: strict
|
|
class Domain::Bluesky::MonitoredObject < ReduxApplicationRecord
|
|
self.table_name = "domain_bluesky_monitored_objects"
|
|
validates :value, presence: true, uniqueness: true
|
|
|
|
enum :kind, { user_did: 0, hashtag: 1 }
|
|
|
|
ADDED_NOTIFY_CHANNEL = "bluesky_object_added"
|
|
REMOVED_NOTIFY_CHANNEL = "bluesky_object_removed"
|
|
|
|
after_create_commit :notify_monitor_added
|
|
after_destroy_commit :notify_monitor_removed
|
|
|
|
sig do
|
|
params(user: Domain::User::BlueskyUser).returns(
|
|
Domain::Bluesky::MonitoredObject,
|
|
)
|
|
end
|
|
def self.build_for_user(user)
|
|
build(value: user.did!, kind: :user_did)
|
|
end
|
|
|
|
sig { params(hashtag: String).returns(Domain::Bluesky::MonitoredObject) }
|
|
def self.build_for_hashtag(hashtag)
|
|
build(value: hashtag, kind: :hashtag)
|
|
end
|
|
|
|
sig { void }
|
|
def notify_monitor_added
|
|
self.class.connection.execute(
|
|
"NOTIFY #{ADDED_NOTIFY_CHANNEL}, '#{self.kind}/#{self.value}'",
|
|
)
|
|
end
|
|
|
|
sig { void }
|
|
def notify_monitor_removed
|
|
self.class.connection.execute(
|
|
"NOTIFY #{REMOVED_NOTIFY_CHANNEL}, '#{self.kind}/#{self.value}'",
|
|
)
|
|
end
|
|
end
|