The code powering m.abunchtell.com https://m.abunchtell.com
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

121 satır
3.4 KiB

  1. class Account < ActiveRecord::Base
  2. # Local users
  3. has_one :user, inverse_of: :account
  4. validates :username, uniqueness: { scope: :domain, case_sensitive: false }, if: 'local?'
  5. validates :username, uniqueness: { scope: :domain, case_sensitive: true }, unless: 'local?'
  6. # Avatar upload
  7. attr_reader :avatar_remote_url
  8. has_attached_file :avatar, styles: { large: '300x300#', medium: '96x96#', small: '48x48#' }, default_url: 'avatars/missing.png'
  9. validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
  10. # Header upload
  11. has_attached_file :header, styles: { medium: '700x335#' }
  12. validates_attachment_content_type :header, content_type: /\Aimage\/.*\Z/
  13. # Local user profile validations
  14. validates :display_name, length: { maximum: 30 }, if: 'local?'
  15. validates :note, length: { maximum: 124 }, if: 'local?'
  16. # Timelines
  17. has_many :stream_entries, inverse_of: :account
  18. has_many :statuses, inverse_of: :account
  19. has_many :favourites, inverse_of: :account
  20. has_many :mentions, inverse_of: :account
  21. # Follow relations
  22. has_many :active_relationships, class_name: 'Follow', foreign_key: 'account_id', dependent: :destroy
  23. has_many :passive_relationships, class_name: 'Follow', foreign_key: 'target_account_id', dependent: :destroy
  24. has_many :following, through: :active_relationships, source: :target_account
  25. has_many :followers, through: :passive_relationships, source: :account
  26. MENTION_RE = /(?:^|\W)@([a-z0-9_]+(?:@[a-z0-9\.\-]+)?)/i
  27. def follow!(other_account)
  28. self.active_relationships.where(target_account: other_account).first_or_create!(target_account: other_account)
  29. end
  30. def unfollow!(other_account)
  31. follow = self.active_relationships.find_by(target_account: other_account)
  32. follow.destroy unless follow.nil?
  33. end
  34. def following?(other_account)
  35. following.include?(other_account)
  36. end
  37. def local?
  38. self.domain.nil?
  39. end
  40. def acct
  41. local? ? self.username : "#{self.username}@#{self.domain}"
  42. end
  43. def object_type
  44. :person
  45. end
  46. def title
  47. self.username
  48. end
  49. def content
  50. self.note
  51. end
  52. def subscribed?
  53. !(self.secret.blank? || self.verify_token.blank?)
  54. end
  55. def favourited?(status)
  56. (status.reblog? ? status.reblog : status).favourites.where(account: self).count == 1
  57. end
  58. def reblogged?(status)
  59. (status.reblog? ? status.reblog : status).reblogs.where(account: self).count == 1
  60. end
  61. def keypair
  62. self.private_key.nil? ? OpenSSL::PKey::RSA.new(self.public_key) : OpenSSL::PKey::RSA.new(self.private_key)
  63. end
  64. def subscription(webhook_url)
  65. @subscription ||= OStatus2::Subscription.new(self.remote_url, secret: self.secret, token: self.verify_token, webhook: webhook_url, hub: self.hub_url)
  66. end
  67. def ping!(atom_url, hubs)
  68. return unless local?
  69. OStatus2::Publication.new(atom_url, hubs).publish
  70. end
  71. def avatar_remote_url=(url)
  72. self.avatar = URI.parse(url)
  73. @avatar_remote_url = url
  74. end
  75. def to_param
  76. self.username
  77. end
  78. def self.find_local!(username)
  79. table = self.arel_table
  80. self.where(table[:username].matches(username)).where(domain: nil).take!
  81. end
  82. def self.find_local(username)
  83. self.find_local!(username)
  84. rescue ActiveRecord::RecordNotFound
  85. nil
  86. end
  87. before_create do
  88. if local?
  89. keypair = OpenSSL::PKey::RSA.new(Rails.env.test? ? 1024 : 2048)
  90. self.private_key = keypair.to_pem
  91. self.public_key = keypair.public_key.to_pem
  92. end
  93. end
  94. end