diff --git a/app/controllers/concerns/followable_controller.rb b/app/controllers/concerns/followable_controller.rb index 33d81f3e0e408c0e6c522f5220bef1df20e93415..ba1aee20646739b28e44aeb34c8fbbf741f8ed60 100644 --- a/app/controllers/concerns/followable_controller.rb +++ b/app/controllers/concerns/followable_controller.rb @@ -28,5 +28,4 @@ module FollowableController render nothing: true, status: :forbidden end end - -end \ No newline at end of file +end diff --git a/app/controllers/v1/users_controller.rb b/app/controllers/v1/users_controller.rb index 67eccc9d00fead4293d00662eeec264346162176..494609db999de0761fd2f5b7ae9cfdb492003f45 100644 --- a/app/controllers/v1/users_controller.rb +++ b/app/controllers/v1/users_controller.rb @@ -1,8 +1,8 @@ class V1::UsersController < ApplicationController include ::FollowableController - before_action :set_user, only: [:show, :update, :destroy] - before_action :authenticate_user!, only: [:create, :update, :destroy] + before_action :set_user, only: [:show, :update, :destroy, :watching] + before_action :authenticate_user!, only: [:create, :update, :destroy, :watching] # GET /v1/users # GET /v1/users.json @@ -50,6 +50,17 @@ class V1::UsersController < ApplicationController end end + def watching + type = params[:object_type] + is_current = (@user.id == current_user.id) unless current_user.nil? + + render nothing: true, status: :bad_request unless type.in? %w(User Collection) + + w = @user.watching(type, is_current) + + render json: w, root: 'follows', status: :ok + end + private def followable @@ -64,5 +75,4 @@ class V1::UsersController < ApplicationController def user_params params.require(:user).permit(:name, :email, :password, :password_confirmation, :terms_of_service, role_ids: []) end - end diff --git a/app/models/user.rb b/app/models/user.rb index b644992fd56b013d808327a2f9f868a490e5d22e..b8941ca9a858a505f4a52475e74538f96ce1ff46 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -120,6 +120,25 @@ class User < ActiveRecord::Base !follows.where(followable_id: followable.id, followable_type: followable.class.name).blank? end + # This function permits see what person is following. + # For current user, list all users and all collections. But, + # if, isn't current user, list only users, and public collections which this person follows. + def watching(followable_type, is_current_user = true) + f = Follow.where(user_id: id, followable_type: followable_type).all + + # If type collection, and isn't current_user, so should list only public collections + if followable_type.to_s == 'Collection' && !is_current_user + f = Follow.joins('INNER JOIN collections ON follows.followable_id = collections.id').where( + "follows.user_id = #{id} " \ + 'AND follows.followable_type = \'Collection\' ' \ + 'AND collections.id = follows.followable_id ' \ + 'AND collections.privacy = \'public\'' + ) + end + + f + end + # ~~~~ end followable actions ~~~~ # private diff --git a/app/serializers/follow_serializer.rb b/app/serializers/follow_serializer.rb new file mode 100644 index 0000000000000000000000000000000000000000..507f2d9830c7c5c079ee7cbeb44b776a0e76c0d5 --- /dev/null +++ b/app/serializers/follow_serializer.rb @@ -0,0 +1,5 @@ +class FollowSerializer < ActiveModel::Serializer + has_one :followable + + attributes :id, :user_id +end diff --git a/config/routes.rb b/config/routes.rb index 0c9b6275987f925de1a01609a78803b8bc3f7461..2a26346ee7a7992f6e871c5391282711cd009119 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -42,6 +42,8 @@ Rails.application.routes.draw do resources :users, concerns: :followable do member do resources :bookmarks, module: 'users', only: [:index, :create, :destroy] + + get 'watching/:object_type', to: 'users#watching' end end get :search, to: 'search#index'