2022-07-09 10:16:18 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-05-25 09:04:54 -07:00
|
|
|
class Ajax::ListController < AjaxController
|
2022-07-09 10:05:11 -07:00
|
|
|
before_action :authenticate_user!
|
2015-01-17 09:24:36 -08:00
|
|
|
|
2022-07-09 10:05:11 -07:00
|
|
|
def create
|
|
|
|
params.require :name
|
2022-07-09 10:32:04 -07:00
|
|
|
params.require :user
|
2015-01-17 09:24:36 -08:00
|
|
|
|
2022-07-09 10:05:11 -07:00
|
|
|
@response[:status] = :err
|
2015-01-17 09:24:36 -08:00
|
|
|
|
2022-07-09 10:16:18 -07:00
|
|
|
target_user = User.find_by!(screen_name: params[:user])
|
2022-07-09 10:05:11 -07:00
|
|
|
list = List.create! user: current_user, display_name: params[:name]
|
2015-01-17 09:24:36 -08:00
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :okay
|
|
|
|
@response[:success] = true
|
2022-07-06 01:41:34 -07:00
|
|
|
@response[:message] = t(".success")
|
2022-07-09 10:16:18 -07:00
|
|
|
@response[:render] = render_to_string(partial: "modal/list/item", locals: { list: list, user: target_user })
|
2015-01-17 09:24:36 -08:00
|
|
|
end
|
|
|
|
|
2015-01-17 09:57:23 -08:00
|
|
|
def destroy
|
2020-05-25 09:04:54 -07:00
|
|
|
params.require :list
|
2015-01-17 09:57:23 -08:00
|
|
|
|
2022-07-09 10:05:11 -07:00
|
|
|
@response[:status] = :err
|
|
|
|
|
|
|
|
List.where(user: current_user, name: params[:list]).first.destroy!
|
2015-01-17 09:57:23 -08:00
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :okay
|
|
|
|
@response[:success] = true
|
2022-07-06 01:41:34 -07:00
|
|
|
@response[:message] = t(".success")
|
2015-01-17 09:57:23 -08:00
|
|
|
end
|
|
|
|
|
2015-01-17 08:50:25 -08:00
|
|
|
def membership
|
|
|
|
params.require :user
|
2020-05-25 09:04:54 -07:00
|
|
|
params.require :list
|
2015-01-17 08:50:25 -08:00
|
|
|
params.require :add
|
|
|
|
|
2022-07-09 10:05:11 -07:00
|
|
|
@response[:status] = :err
|
2015-01-17 08:50:25 -08:00
|
|
|
|
2022-07-09 10:16:18 -07:00
|
|
|
add = params[:add] == "true"
|
2015-01-17 08:50:25 -08:00
|
|
|
|
2022-07-09 10:16:18 -07:00
|
|
|
target_user = User.find_by!(screen_name: params[:user])
|
|
|
|
list = current_user.lists.find_by!(name: params[:list])
|
2015-01-17 08:50:25 -08:00
|
|
|
|
2022-06-09 10:55:56 -07:00
|
|
|
raise Errors::ListingSelfBlockedOther if current_user.blocking?(target_user)
|
|
|
|
raise Errors::ListingOtherBlockedSelf if target_user.blocking?(current_user)
|
|
|
|
|
2015-01-17 08:50:25 -08:00
|
|
|
if add
|
2022-07-09 10:16:18 -07:00
|
|
|
list.add_member target_user if list.members.find_by(user_id: target_user.id).nil?
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:checked] = true
|
2022-07-06 01:41:34 -07:00
|
|
|
@response[:message] = t(".success.add")
|
2015-01-17 08:50:25 -08:00
|
|
|
else
|
2022-07-09 10:16:18 -07:00
|
|
|
list.remove_member target_user unless list.members.find_by(user_id: target_user.id).nil?
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:checked] = false
|
2022-07-06 01:41:34 -07:00
|
|
|
@response[:message] = t(".success.remove")
|
2015-01-17 08:50:25 -08:00
|
|
|
end
|
2015-05-08 16:31:31 -07:00
|
|
|
|
2020-04-28 11:27:59 -07:00
|
|
|
@response[:status] = :okay
|
|
|
|
@response[:success] = true
|
2015-01-17 08:50:25 -08:00
|
|
|
end
|
|
|
|
end
|