gab-social/app/controllers/api/v1/groups_controller.rb

83 lines
2.1 KiB
Ruby
Raw Normal View History

2019-07-02 08:10:25 +01:00
# frozen_string_literal: true
class Api::V1::GroupsController < Api::BaseController
include Authorization
before_action -> { doorkeeper_authorize! :read, :'read:groups' }, only: [:index, :show]
before_action -> { doorkeeper_authorize! :write, :'write:groups' }, except: [:index, :show]
before_action :require_user!
before_action :set_group, except: [:index, :create]
def index
2019-07-15 14:47:05 +01:00
case current_tab
when 'featured'
2019-07-30 15:03:12 +01:00
@groups = Group.where(is_featured: true).limit(50).all
2019-07-15 14:47:05 +01:00
when 'member'
2019-07-20 00:45:14 +01:00
@groups = Group.joins(:group_accounts).where(is_archived: false, group_accounts: { account: current_account }).order('group_accounts.unread_count DESC, group_accounts.id DESC').all
2019-07-15 14:47:05 +01:00
when 'admin'
2019-07-16 13:58:29 +01:00
@groups = Group.joins(:group_accounts).where(is_archived: false, group_accounts: { account: current_account, role: :admin }).all
2019-07-15 14:47:05 +01:00
end
2019-07-02 08:10:25 +01:00
render json: @groups, each_serializer: REST::GroupSerializer
end
2019-07-15 14:47:05 +01:00
def current_tab
tab = 'featured'
tab = params[:tab] if ['featured', 'member', 'admin'].include? params[:tab]
return tab
end
2019-07-02 08:10:25 +01:00
def show
render json: @group, serializer: REST::GroupSerializer
end
def create
authorize :group, :create?
2019-07-02 08:10:25 +01:00
@group = Group.create!(group_params.merge(account: current_account))
render json: @group, serializer: REST::GroupSerializer
end
def update
authorize @group, :update?
@group.update!(group_params)
render json: @group, serializer: REST::GroupSerializer
end
def destroy
authorize @group, :destroy?
@group.is_archived = true
@group.save!
render_empty
end
def destroy_status
authorize @group, :destroy_status?
status = Status.find(params[:status_id])
GroupUnlinkStatusService.new.call(current_account, @group, status)
render_empty
end
def approve_status
authorize @group, :approve_status?
status = Status.find(params[:status_id])
GroupApproveStatusService.new.call(current_account, @group, status)
render_empty
end
private
def set_group
@group = Group.find(params[:id])
end
def group_params
params.permit(:title, :cover_image, :description)
end
end