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