group.rb 2.45 KB
Newer Older
Rob Colbert's avatar
Rob Colbert committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# == Schema Information
#
# Table name: groups
#
#  id                       :bigint(8)        not null, primary key
#  account_id               :bigint(8)
#  title                    :string           not null
#  description              :string           not null
#  cover_image_file_name    :string
#  cover_image_content_type :string
#  cover_image_file_size    :integer
#  cover_image_updated_at   :datetime
#  is_nsfw                  :boolean          default(FALSE), not null
#  is_featured              :boolean          default(FALSE), not null
#  is_archived              :boolean          default(FALSE), not null
#  created_at               :datetime         not null
#  updated_at               :datetime         not null
2458773093's avatar
2458773093 committed
18
#  member_count             :integer          default(0)
Rob Colbert's avatar
Rob Colbert committed
19 20 21 22 23
#

class Group < ApplicationRecord
  include Paginable
  include GroupInteractions
24
  include GroupCoverImage
Rob Colbert's avatar
Rob Colbert committed
25 26 27 28 29 30 31 32

  PER_ACCOUNT_LIMIT = 50

  belongs_to :account, optional: true

  has_many :group_accounts, inverse_of: :group, dependent: :destroy
  has_many :accounts, through: :group_accounts

2458773093's avatar
2458773093 committed
33 34 35
  has_many :group_removed_accounts, inverse_of: :group, dependent: :destroy
  has_many :removed_accounts, source: :account, through: :group_removed_accounts

Rob Colbert's avatar
Rob Colbert committed
36 37 38 39 40 41 42 43 44 45
  validates :title, presence: true
  validates :description, presence: true

  validates_each :account_id, on: :create do |record, _attr, value|
    record.errors.add(:base, I18n.t('groups.errors.limit')) if Group.where(account_id: value).count >= PER_ACCOUNT_LIMIT
  end

  before_destroy :clean_feed_manager
  after_create :add_owner_to_accounts

mgabdev's avatar
mgabdev committed
46 47 48 49 50 51 52 53 54 55 56
  class << self
    def search_for(term, limit = 100, offset = 0)
      pattern = sanitize_sql_like(term.strip) + '%'

      Group.where('lower(title) like lower(?) AND is_archived=false', pattern)
        .order(:title)
        .limit(limit)
        .offset(offset)
    end
  end

Rob Colbert's avatar
Rob Colbert committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  private

  def add_owner_to_accounts
    group_accounts << GroupAccount.new(account: account, role: :admin, write_permissions: true)
  end

  def clean_feed_manager
    reblog_key       = FeedManager.instance.key(:group, id, 'reblogs')
    reblogged_id_set = Redis.current.zrange(reblog_key, 0, -1)

    Redis.current.pipelined do
      Redis.current.del(FeedManager.instance.key(:group, id))
      Redis.current.del(reblog_key)

      reblogged_id_set.each do |reblogged_id|
        reblog_set_key = FeedManager.instance.key(:group, id, "reblogs:#{reblogged_id}")
        Redis.current.del(reblog_set_key)
      end
    end
  end
end