闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.3 KiB

  1. # frozen_string_literal: true
  2. class Notification < ApplicationRecord
  3. include Paginable
  4. belongs_to :account
  5. belongs_to :activity, polymorphic: true
  6. belongs_to :mention, foreign_type: 'Mention', foreign_key: 'activity_id'
  7. belongs_to :status, foreign_type: 'Status', foreign_key: 'activity_id'
  8. belongs_to :follow, foreign_type: 'Follow', foreign_key: 'activity_id'
  9. belongs_to :favourite, foreign_type: 'Favourite', foreign_key: 'activity_id'
  10. STATUS_INCLUDES = [:account, :stream_entry, :media_attachments, :tags, mentions: :account, reblog: [:stream_entry, :account, :media_attachments, :tags, mentions: :account]].freeze
  11. scope :with_includes, -> { includes(status: STATUS_INCLUDES, mention: [status: STATUS_INCLUDES], favourite: [:account, status: STATUS_INCLUDES], follow: :account) }
  12. def activity
  13. send(activity_type.downcase)
  14. end
  15. def type
  16. case activity_type
  17. when 'Status'
  18. :reblog
  19. else
  20. activity_type.downcase.to_sym
  21. end
  22. end
  23. def from_account
  24. case type
  25. when :mention
  26. activity.status.account
  27. when :follow, :favourite, :reblog
  28. activity.account
  29. end
  30. end
  31. def target_status
  32. case type
  33. when :reblog
  34. activity.reblog
  35. when :favourite, :mention
  36. activity.status
  37. end
  38. end
  39. end