status_edit.rb 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # frozen_string_literal: true
  2. # == Schema Information
  3. #
  4. # Table name: status_edits
  5. #
  6. # id :bigint(8) not null, primary key
  7. # status_id :bigint(8) not null
  8. # account_id :bigint(8)
  9. # text :text default(""), not null
  10. # spoiler_text :text default(""), not null
  11. # created_at :datetime not null
  12. # updated_at :datetime not null
  13. # ordered_media_attachment_ids :bigint(8) is an Array
  14. # media_descriptions :text is an Array
  15. # poll_options :string is an Array
  16. # sensitive :boolean
  17. #
  18. class StatusEdit < ApplicationRecord
  19. include RateLimitable
  20. self.ignored_columns = %w(
  21. media_attachments_changed
  22. )
  23. class PreservedMediaAttachment < ActiveModelSerializers::Model
  24. attributes :media_attachment, :description
  25. delegate :id, :type, :url, :preview_url, :remote_url,
  26. :preview_remote_url, :text_url, :meta, :blurhash,
  27. :not_processed?, :needs_redownload?, :local?,
  28. :file, :thumbnail, :thumbnail_remote_url,
  29. :shortcode, to: :media_attachment
  30. end
  31. rate_limit by: :account, family: :statuses
  32. belongs_to :status
  33. belongs_to :account, optional: true
  34. default_scope { order(id: :asc) }
  35. delegate :local?, to: :status
  36. def emojis
  37. return @emojis if defined?(@emojis)
  38. @emojis = CustomEmoji.from_text([spoiler_text, text].join(' '), status.account.domain)
  39. end
  40. def ordered_media_attachments
  41. return @ordered_media_attachments if defined?(@ordered_media_attachments)
  42. @ordered_media_attachments = begin
  43. if ordered_media_attachment_ids.nil?
  44. []
  45. else
  46. map = status.media_attachments.index_by(&:id)
  47. ordered_media_attachment_ids.map.with_index { |media_attachment_id, index| PreservedMediaAttachment.new(media_attachment: map[media_attachment_id], description: media_descriptions[index]) }
  48. end
  49. end
  50. end
  51. end