2019-07-02 08:10:25 +01:00
|
|
|
# == 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
|
2019-07-22 01:46:00 +01:00
|
|
|
# member_count :integer default(0)
|
2020-08-06 05:17:38 +01:00
|
|
|
# slug :text
|
|
|
|
# is_private :boolean default(FALSE)
|
|
|
|
# is_visible :boolean default(FALSE)
|
|
|
|
# tags :string default([]), is an Array
|
2020-09-02 23:56:16 +01:00
|
|
|
# password :string
|
2020-09-10 23:12:43 +01:00
|
|
|
# group_category_id :integer
|
2019-07-02 08:10:25 +01:00
|
|
|
#
|
|
|
|
|
|
|
|
class Group < ApplicationRecord
|
2020-09-10 23:12:43 +01:00
|
|
|
self.ignored_columns = ["group_categories_id"]
|
|
|
|
|
2019-07-02 08:10:25 +01:00
|
|
|
include Paginable
|
|
|
|
include GroupInteractions
|
2019-07-17 19:35:40 +01:00
|
|
|
include GroupCoverImage
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
PER_ACCOUNT_LIMIT = 50
|
|
|
|
|
|
|
|
belongs_to :account, optional: true
|
|
|
|
|
|
|
|
has_many :group_accounts, inverse_of: :group, dependent: :destroy
|
|
|
|
has_many :accounts, through: :group_accounts
|
2020-09-10 23:07:00 +01:00
|
|
|
|
|
|
|
has_many :group_join_requests, inverse_of: :group, dependent: :destroy
|
|
|
|
has_many :join_requests, source: :account, through: :group_join_requests
|
2019-07-02 08:10:25 +01:00
|
|
|
|
2020-09-10 21:07:01 +01:00
|
|
|
has_many :group_pinned_statuses, inverse_of: :group, dependent: :destroy
|
|
|
|
has_many :pinned_statuses, source: :status, through: :group_pinned_statuses
|
|
|
|
|
2019-07-16 07:43:31 +01:00
|
|
|
has_many :group_removed_accounts, inverse_of: :group, dependent: :destroy
|
|
|
|
has_many :removed_accounts, source: :account, through: :group_removed_accounts
|
|
|
|
|
2020-09-10 23:12:43 +01:00
|
|
|
belongs_to :group_categories, optional: true, foreign_key: 'group_category_id'
|
|
|
|
|
2019-07-02 08:10:25 +01:00
|
|
|
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
|
|
|
|
|
2020-09-15 06:14:22 +01:00
|
|
|
before_save :set_password
|
2019-07-02 08:10:25 +01:00
|
|
|
before_destroy :clean_feed_manager
|
|
|
|
after_create :add_owner_to_accounts
|
|
|
|
|
2020-01-14 23:13:05 +00:00
|
|
|
class << self
|
2020-11-04 19:50:39 +00:00
|
|
|
def search_for(term, offset = 0)
|
2020-01-14 23:13:05 +00:00
|
|
|
pattern = sanitize_sql_like(term.strip) + '%'
|
|
|
|
|
2020-08-07 05:11:50 +01:00
|
|
|
Group.where('lower(title) like lower(?) AND is_archived=false AND is_visible=true', pattern)
|
2020-08-12 23:41:55 +01:00
|
|
|
.order('member_count DESC')
|
2020-11-04 19:50:39 +00:00
|
|
|
.limit(25)
|
2020-01-14 23:13:05 +00:00
|
|
|
.offset(offset)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-02 08:10:25 +01:00
|
|
|
private
|
|
|
|
|
2020-09-15 06:14:22 +01:00
|
|
|
def set_password
|
|
|
|
if password.nil? || !password || password.gsub(/\s+/, "").length <= 1 || password == "null"
|
|
|
|
nil
|
|
|
|
else
|
|
|
|
password
|
|
|
|
end
|
|
|
|
end
|
2019-07-02 08:10:25 +01:00
|
|
|
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
|