retrospring/app/controllers/ajax/list_controller.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

62 lines
1.7 KiB
Ruby
Raw Normal View History

2022-07-09 19:16:18 +02:00
# frozen_string_literal: true
class Ajax::ListController < AjaxController
before_action :authenticate_user!
2015-01-17 18:24:36 +01:00
def create
params.require :name
params.require :user
2015-01-17 18:24:36 +01: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])
list = List.create! user: current_user, display_name: params[:name]
2015-01-17 18:24:36 +01:00
@response[:status] = :okay
@response[:success] = true
@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
params.require :list
2015-01-17 18:57:23 +01:00
@response[:status] = :err
List.where(user: current_user, name: params[:list]).first.destroy!
2015-01-17 18:57:23 +01:00
@response[:status] = :okay
@response[:success] = true
@response[:message] = t(".success")
2015-01-17 18:57:23 +01:00
end
def membership
params.require :user
params.require :list
params.require :add
@response[:status] = :err
2022-07-09 19:16:18 +02:00
add = params[:add] == "true"
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])
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)
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?
@response[:checked] = true
@response[:message] = t(".success.add")
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?
@response[:checked] = false
@response[:message] = t(".success.remove")
end
2015-05-09 01:31:31 +02:00
@response[:status] = :okay
@response[:success] = true
end
end