2019-07-02 08:10:25 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Api::V1::GroupsController < Api::BaseController
|
|
|
|
include Authorization
|
|
|
|
|
2020-07-22 05:05:54 +01:00
|
|
|
# before_action -> { doorkeeper_authorize! :read, :'read:groups' }, only: [:index, :show]
|
2019-07-02 08:10:25 +01:00
|
|
|
before_action -> { doorkeeper_authorize! :write, :'write:groups' }, except: [:index, :show]
|
|
|
|
|
2020-07-22 05:05:54 +01:00
|
|
|
before_action :require_user!, except: [:index, :show]
|
2020-09-14 23:12:45 +01:00
|
|
|
before_action :set_group, except: [:index, :create, :by_category, :by_tag]
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
def index
|
2019-07-15 14:47:05 +01:00
|
|
|
case current_tab
|
|
|
|
when 'featured'
|
2020-08-08 19:08:48 +01:00
|
|
|
@groupIds = FetchGroupsService.new.call("featured")
|
|
|
|
@groups = Group.where(id: @groupIds).limit(150).all
|
2020-04-02 04:17:21 +01:00
|
|
|
when 'new'
|
2020-07-22 05:05:54 +01:00
|
|
|
if !current_user
|
2020-11-25 21:22:37 +00:00
|
|
|
return render json: { error: 'This method requires an authenticated user' }, status: 422
|
2020-07-22 05:05:54 +01:00
|
|
|
end
|
2020-04-02 04:17:21 +01:00
|
|
|
@groups = Group.where(is_archived: false).limit(24).order('created_at DESC').all
|
2019-07-15 14:47:05 +01:00
|
|
|
when 'member'
|
2020-07-22 05:05:54 +01:00
|
|
|
if !current_user
|
2020-11-25 21:22:37 +00:00
|
|
|
return render json: { error: 'This method requires an authenticated user' }, status: 422
|
2020-07-22 05:05:54 +01:00
|
|
|
end
|
2020-05-10 04:57:38 +01:00
|
|
|
@groups = Group.joins(:group_accounts).where(is_archived: false, group_accounts: { account: current_account }).order('group_accounts.id DESC').all
|
2019-07-15 14:47:05 +01:00
|
|
|
when 'admin'
|
2020-07-22 05:05:54 +01:00
|
|
|
if !current_user
|
|
|
|
render json: { error: 'This method requires an authenticated user' }, status: 422
|
|
|
|
end
|
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
|
|
|
|
|
2020-09-14 23:12:45 +01:00
|
|
|
def by_category
|
|
|
|
if !current_user
|
2020-11-25 21:22:37 +00:00
|
|
|
return render json: { error: 'This method requires an authenticated user' }, status: 422
|
2020-09-14 23:12:45 +01:00
|
|
|
end
|
|
|
|
|
2020-09-24 22:21:16 +01:00
|
|
|
@groupCategory = nil
|
|
|
|
if !params[:category].empty?
|
|
|
|
@groupCategory = GroupCategories.where("text ILIKE ?", "%#{params[:category]}%")
|
|
|
|
end
|
|
|
|
|
2020-09-14 23:12:45 +01:00
|
|
|
@groups = []
|
2020-09-24 22:21:16 +01:00
|
|
|
if !@groupCategory.nil?
|
|
|
|
@groups = Group.where(is_archived: false, group_categories: @groupCategory).all
|
|
|
|
end
|
2020-09-14 23:12:45 +01:00
|
|
|
|
|
|
|
render json: @groups, each_serializer: REST::GroupSerializer
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_tag
|
|
|
|
if !current_user
|
2020-11-25 21:22:37 +00:00
|
|
|
return render json: { error: 'This method requires an authenticated user' }, status: 422
|
2020-09-14 23:12:45 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@groups = []
|
2020-09-24 22:21:16 +01:00
|
|
|
if !params[:tag].empty?
|
|
|
|
@groups = Group.where(is_archived: false).where("array_to_string(tags, '||') ILIKE :tag", tag: "%#{params[:tag]}%").all
|
|
|
|
end
|
2020-09-14 23:12:45 +01:00
|
|
|
|
|
|
|
render json: @groups, each_serializer: REST::GroupSerializer
|
|
|
|
end
|
|
|
|
|
2019-07-15 14:47:05 +01:00
|
|
|
def current_tab
|
|
|
|
tab = 'featured'
|
2020-04-02 04:17:21 +01:00
|
|
|
tab = params[:tab] if ['featured', 'member', 'admin', 'new'].include? params[:tab]
|
2019-07-15 14:47:05 +01:00
|
|
|
return tab
|
|
|
|
end
|
|
|
|
|
2019-07-02 08:10:25 +01:00
|
|
|
def show
|
2020-09-18 00:21:58 +01:00
|
|
|
render json: @group, serializer: REST::GroupSerializer, individual_group: true
|
2019-07-02 08:10:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2019-08-01 17:46:17 +01:00
|
|
|
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!
|
2020-11-25 21:22:37 +00:00
|
|
|
render_empty_success
|
2019-07-02 08:10:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_status
|
|
|
|
authorize @group, :destroy_status?
|
|
|
|
|
|
|
|
status = Status.find(params[:status_id])
|
|
|
|
GroupUnlinkStatusService.new.call(current_account, @group, status)
|
2020-11-25 21:22:37 +00:00
|
|
|
render_empty_success
|
2019-07-02 08:10:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def approve_status
|
|
|
|
authorize @group, :approve_status?
|
|
|
|
|
|
|
|
status = Status.find(params[:status_id])
|
|
|
|
GroupApproveStatusService.new.call(current_account, @group, status)
|
2020-11-25 21:22:37 +00:00
|
|
|
render_empty_success
|
2019-07-02 08:10:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_group
|
2020-09-11 23:23:13 +01:00
|
|
|
@group = Group.where(id: params[:id], is_archived: false).includes(:group_categories).first
|
2019-07-02 08:10:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def group_params
|
2020-09-10 23:12:43 +01:00
|
|
|
thep = params.permit(:title, :password, :cover_image, :description, :is_private, :tags, :is_visible, :group_category_id, :slug)
|
|
|
|
thep[:tags] = thep[:tags].split(",") unless thep[:tags].nil?
|
|
|
|
thep
|
2019-07-02 08:10:25 +01:00
|
|
|
end
|
|
|
|
end
|