The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

51 lines
1.4 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. validates :account_id, uniqueness: { scope: [:activity_type, :activity_id] }
  11. STATUS_INCLUDES = [:account, :stream_entry, :media_attachments, :tags, mentions: :account, reblog: [:stream_entry, :account, :media_attachments, :tags, mentions: :account]].freeze
  12. scope :with_includes, -> { includes(status: STATUS_INCLUDES, mention: [status: STATUS_INCLUDES], favourite: [:account, status: STATUS_INCLUDES], follow: :account) }
  13. def activity
  14. send(activity_type.downcase)
  15. end
  16. def type
  17. case activity_type
  18. when 'Status'
  19. :reblog
  20. else
  21. activity_type.downcase.to_sym
  22. end
  23. end
  24. def from_account
  25. case type
  26. when :mention
  27. activity.status.account
  28. when :follow, :favourite, :reblog
  29. activity.account
  30. end
  31. end
  32. def target_status
  33. case type
  34. when :reblog
  35. activity.reblog
  36. when :favourite, :mention
  37. activity.status
  38. end
  39. end
  40. end