about summary refs log tree commit diff
path: root/app/models/encrypted_message.rb
blob: 7b4e32283fa125a2313bcb349cc2b9245facfd55 (plain) (blame)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true
# == Schema Information
#
# Table name: encrypted_messages
#
#  id               :bigint(8)        not null, primary key
#  device_id        :bigint(8)
#  from_account_id  :bigint(8)
#  from_device_id   :string           default(""), not null
#  type             :integer          default(0), not null
#  body             :text             default(""), not null
#  digest           :text             default(""), not null
#  message_franking :text             default(""), not null
#  created_at       :datetime         not null
#  updated_at       :datetime         not null
#

class EncryptedMessage < ApplicationRecord
  self.inheritance_column = nil

  include Paginable
  include Redisable

  scope :up_to, ->(id) { where(arel_table[:id].lteq(id)) }

  belongs_to :device
  belongs_to :from_account, class_name: 'Account'

  around_create Mastodon::Snowflake::Callbacks

  after_commit :push_to_streaming_api

  private

  def push_to_streaming_api
    return if destroyed? || !subscribed_to_timeline?

    PushEncryptedMessageWorker.perform_async(id)
  end

  def subscribed_to_timeline?
    redis.exists?("subscribed:#{streaming_channel}")
  end

  def streaming_channel
    "timeline:#{device.account_id}:#{device.device_id}"
  end
end