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..967835cc7916852193786cacee45fd61423ed42b 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, 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 0f03e7aeafbc903c6d15c625ade7ee184def7b5f..4acf63c2e2eea61e2145209d9df866be91be1799 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -117,6 +117,27 @@ 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 + + p f.to_json + + f + end + # ~~~~ end followable actions ~~~~ # private 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'