2024-02-25 12:15:55 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2010-03-10 18:21:43 -05:00
|
|
|
class FavoritesController < ApplicationController
|
2018-04-02 13:51:26 -04:00
|
|
|
before_action :member_only, except: [:index]
|
2024-12-08 22:28:31 -05:00
|
|
|
before_action :ensure_lockdown_disabled, except: %i[index]
|
2020-02-14 23:17:19 -05:00
|
|
|
respond_to :html, :json
|
2018-04-02 13:51:26 -04:00
|
|
|
skip_before_action :api_check
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2010-11-19 17:20:13 -05:00
|
|
|
def index
|
2010-12-01 17:21:05 -05:00
|
|
|
if params[:tags]
|
2025-01-26 09:26:11 -05:00
|
|
|
redirect_to(posts_path(tags: params[:tags]))
|
2010-12-01 17:21:05 -05:00
|
|
|
else
|
2013-05-05 07:36:07 -04:00
|
|
|
user_id = params[:user_id] || CurrentUser.user.id
|
2013-07-14 19:44:35 -04:00
|
|
|
@user = User.find(user_id)
|
2016-02-22 15:18:19 -05:00
|
|
|
|
|
|
|
if @user.hide_favorites?
|
2022-09-11 13:49:13 -04:00
|
|
|
raise Favorite::HiddenError
|
2016-02-22 15:18:19 -05:00
|
|
|
end
|
|
|
|
|
2025-01-26 09:26:11 -05:00
|
|
|
@post_set = PostSets::Favorites.new(@user, params[:page], limit: params[:limit])
|
|
|
|
@posts = PostsDecorator.decorate_collection(@post_set.posts)
|
|
|
|
respond_with(@posts) do |fmt|
|
2020-03-30 19:48:24 -04:00
|
|
|
fmt.json do
|
2025-01-26 09:26:11 -05:00
|
|
|
render json: @post_set.api_posts, root: "posts"
|
2020-03-30 19:48:24 -04:00
|
|
|
end
|
|
|
|
end
|
2010-12-01 17:21:05 -05:00
|
|
|
end
|
2010-11-19 17:20:13 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2010-03-10 18:21:43 -05:00
|
|
|
def create
|
2018-08-12 15:22:08 -04:00
|
|
|
@post = Post.find(params[:post_id])
|
2019-03-31 16:25:55 -04:00
|
|
|
FavoriteManager.add!(user: CurrentUser.user, post: @post)
|
2018-08-15 21:14:51 -04:00
|
|
|
flash.now[:notice] = "You have favorited this post"
|
2014-09-05 19:46:22 -04:00
|
|
|
|
2018-08-12 15:22:08 -04:00
|
|
|
respond_with(@post)
|
2020-03-06 18:39:46 -05:00
|
|
|
rescue Favorite::Error, ActiveRecord::RecordInvalid => x
|
|
|
|
render_expected_error(422, x.message)
|
2010-03-10 18:21:43 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2010-03-10 18:21:43 -05:00
|
|
|
def destroy
|
2023-08-01 13:32:24 -04:00
|
|
|
@post = Post.find(params[:id])
|
|
|
|
FavoriteManager.remove!(user: CurrentUser.user, post: @post)
|
2014-09-05 19:46:22 -04:00
|
|
|
|
2018-08-15 21:14:51 -04:00
|
|
|
flash.now[:notice] = "You have unfavorited this post"
|
2018-08-12 15:22:08 -04:00
|
|
|
respond_with(@post)
|
2020-03-06 18:39:46 -05:00
|
|
|
rescue Favorite::Error => x
|
|
|
|
render_expected_error(422, x.message)
|
2010-03-10 18:21:43 -05:00
|
|
|
end
|
2024-12-08 22:28:31 -05:00
|
|
|
|
|
|
|
def ensure_lockdown_disabled
|
|
|
|
access_denied if Security::Lockdown.favorites_disabled? && !CurrentUser.is_staff?
|
|
|
|
end
|
2010-03-10 18:21:43 -05:00
|
|
|
end
|