gab-social/app/controllers/api/v1/groups_controller.rb
mgabdev 3b0ec6a270 Updated groups to be public
• Updated:
- groups to be public
- routes for /group and /groups/id to be public
- GroupTimeline to ignore relationships for loading
- Group fetching to be a where with is_archived: false
- GroupControllers to have if current_user, else

• Added:
- Meta and og information for view
- Group fetch
- public route api for featured groups, group timelines

• Removed:
- Doorkeeper for read:groups
2020-07-21 23:05:54 -05:00

94 lines
2.7 KiB
Ruby

# 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!, except: [:index, :show]
before_action :set_group, except: [:index, :create]
def index
case current_tab
when 'featured'
@groups = Group.where(is_featured: true, is_archived: false).limit(100).all
when 'new'
if !current_user
render json: { error: 'This method requires an authenticated user' }, status: 422
end
@groups = Group.where(is_archived: false).limit(24).order('created_at DESC').all
when 'member'
if !current_user
render json: { error: 'This method requires an authenticated user' }, status: 422
end
@groups = Group.joins(:group_accounts).where(is_archived: false, group_accounts: { account: current_account }).order('group_accounts.id DESC').all
when 'admin'
if !current_user
render json: { error: 'This method requires an authenticated user' }, status: 422
end
@groups = Group.joins(:group_accounts).where(is_archived: false, group_accounts: { account: current_account, role: :admin }).all
end
render json: @groups, each_serializer: REST::GroupSerializer
end
def current_tab
tab = 'featured'
tab = params[:tab] if ['featured', 'member', 'admin', 'new'].include? params[:tab]
return tab
end
def show
render json: @group, serializer: REST::GroupSerializer
end
def create
authorize :group, :create?
@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.where(id: params[:id], is_archived: false).first
end
def group_params
params.permit(:title, :cover_image, :description)
end
end