encrypted_message.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: encrypted_messages
  5. #
  6. # id :bigint(8) not null, primary key
  7. # device_id :bigint(8)
  8. # from_account_id :bigint(8)
  9. # from_device_id :string default(""), not null
  10. # type :integer default(0), not null
  11. # body :text default(""), not null
  12. # digest :text default(""), not null
  13. # message_franking :text default(""), not null
  14. # created_at :datetime not null
  15. # updated_at :datetime not null
  16. #
  17. class EncryptedMessage < ApplicationRecord
  18. self.inheritance_column = nil
  19. include Paginable
  20. include Redisable
  21. scope :up_to, ->(id) { where(arel_table[:id].lteq(id)) }
  22. belongs_to :device
  23. belongs_to :from_account, class_name: 'Account'
  24. around_create Mastodon::Snowflake::Callbacks
  25. after_commit :push_to_streaming_api
  26. private
  27. def push_to_streaming_api
  28. return if destroyed? || !subscribed_to_timeline?
  29. PushEncryptedMessageWorker.perform_async(id)
  30. end
  31. def subscribed_to_timeline?
  32. redis.exists?("subscribed:#{streaming_channel}")
  33. end
  34. def streaming_channel
  35. "timeline:#{device.account_id}:#{device.device_id}"
  36. end
  37. end