From 013e3c7f21de34aacf78d0146926ec3866c7704d Mon Sep 17 00:00:00 2001 From: Connor Turland Date: Wed, 15 Feb 2017 23:01:53 -0500 Subject: [PATCH] follows for maps in the ui for internal testing only still (#1072) * follows for maps in the ui for testers * require user for these actions * match how map follow works --- app/controllers/maps_controller.rb | 30 ++++++++++++- app/controllers/topics_controller.rb | 50 ++++++++++++++-------- app/models/user.rb | 12 +++++- app/policies/map_policy.rb | 8 ++++ app/policies/topic_policy.rb | 8 ++++ app/views/layouts/_foot.html.erb | 2 +- config/routes.rb | 4 ++ frontend/src/Metamaps/DataModel/Map.js | 3 ++ frontend/src/Metamaps/DataModel/Mapper.js | 16 ++++++- frontend/src/Metamaps/DataModel/Topic.js | 3 ++ frontend/src/Metamaps/Util.js | 3 ++ frontend/src/Metamaps/Views/ExploreMaps.js | 14 ++++++ frontend/src/components/Maps/MapCard.js | 15 ++++--- frontend/src/components/Maps/index.js | 7 +-- 14 files changed, 145 insertions(+), 30 deletions(-) diff --git a/app/controllers/maps_controller.rb b/app/controllers/maps_controller.rb index a91d576d..fb4036d3 100644 --- a/app/controllers/maps_controller.rb +++ b/app/controllers/maps_controller.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true class MapsController < ApplicationController - before_action :require_user, only: [:create, :update, :destroy, :events] - before_action :set_map, only: [:show, :conversation, :update, :destroy, :contains, :events, :export] + before_action :require_user, only: [:create, :update, :destroy, :events, :follow, :unfollow] + before_action :set_map, only: [:show, :conversation, :update, :destroy, :contains, :events, :export, :follow, :unfollow] after_action :verify_authorized # GET maps/:id @@ -138,6 +138,32 @@ class MapsController < ApplicationController end end + # POST maps/:id/follow + def follow + follow = FollowService.follow(@map, current_user, 'followed') + + respond_to do |format| + format.json do + if follow + head :ok + else + head :bad_request + end + end + end + end + + # POST maps/:id/unfollow + def unfollow + FollowService.unfollow(@map, current_user) + + respond_to do |format| + format.json do + head :ok + end + end + end + private def set_map diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index b09767d0..779e11cf 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -2,7 +2,8 @@ class TopicsController < ApplicationController include TopicsHelper - before_action :require_user, only: [:create, :update, :destroy] + before_action :require_user, only: [:create, :update, :destroy, :follow, :unfollow] + before_action :set_topic, only: [:show, :update, :relative_numbers, :relatives, :network, :destroy, :follow, :unfollow] after_action :verify_authorized, except: :autocomplete_topic respond_to :html, :js, :json @@ -31,9 +32,6 @@ class TopicsController < ApplicationController # GET topics/:id def show - @topic = Topic.find(params[:id]) - authorize @topic - respond_to do |format| format.html do @alltopics = [@topic].concat(policy_scope(Topic.relatives(@topic.id, current_user)).to_a) @@ -49,9 +47,6 @@ class TopicsController < ApplicationController # GET topics/:id/network def network - @topic = Topic.find(params[:id]) - authorize @topic - @alltopics = [@topic].concat(policy_scope(Topic.relatives(@topic.id, current_user)).to_a) @allsynapses = policy_scope(Synapse.for_topic(@topic.id)) @@ -71,9 +66,6 @@ class TopicsController < ApplicationController # GET topics/:id/relative_numbers def relative_numbers - @topic = Topic.find(params[:id]) - authorize @topic - topics_already_has = params[:network] ? params[:network].split(',').map(&:to_i) : [] alltopics = policy_scope(Topic.relatives(@topic.id, current_user)).to_a @@ -94,9 +86,6 @@ class TopicsController < ApplicationController # GET topics/:id/relatives def relatives - @topic = Topic.find(params[:id]) - authorize @topic - topics_already_has = params[:network] ? params[:network].split(',').map(&:to_i) : [] alltopics = policy_scope(Topic.relatives(@topic.id, current_user)).to_a @@ -149,8 +138,6 @@ class TopicsController < ApplicationController # PUT /topics/1 # PUT /topics/1.json def update - @topic = Topic.find(params[:id]) - authorize @topic @topic.updated_by = current_user @topic.assign_attributes(topic_params) @@ -165,8 +152,6 @@ class TopicsController < ApplicationController # DELETE topics/:id def destroy - @topic = Topic.find(params[:id]) - authorize @topic @topic.updated_by = current_user @topic.destroy respond_to do |format| @@ -174,8 +159,39 @@ class TopicsController < ApplicationController end end + # POST topics/:id/follow + def follow + follow = FollowService.follow(@topic, current_user, 'followed') + + respond_to do |format| + format.json do + if follow + head :ok + else + head :bad_request + end + end + end + end + + # POST topics/:id/unfollow + def unfollow + FollowService.unfollow(@topic, current_user) + + respond_to do |format| + format.json do + head :ok + end + end + end + private + def set_topic + @topic = Topic.find(params[:id]) + authorize @topic + end + def topic_params params.require(:topic).permit(:id, :name, :desc, :link, :permission, :metacode_id, :defer_to_map_id) end diff --git a/app/models/user.rb b/app/models/user.rb index e5fadaa9..33ddc8d3 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -52,10 +52,20 @@ class User < ApplicationRecord # override default as_json def as_json(_options = {}) - { id: id, + json = { id: id, name: name, image: image.url(:sixtyfour), admin: admin } + if (_options[:follows]) + json['follows'] = { + topics: following.where(followed_type: 'Topic').to_a.map(&:followed_id), + maps: following.where(followed_type: 'Map').to_a.map(&:followed_id) + } + end + if (_options[:email]) + json['email'] = email + end + json end def as_json_for_autocomplete diff --git a/app/policies/map_policy.rb b/app/policies/map_policy.rb index fb9cfdca..3aa9994e 100644 --- a/app/policies/map_policy.rb +++ b/app/policies/map_policy.rb @@ -90,4 +90,12 @@ class MapPolicy < ApplicationPolicy def unstar? user.present? end + + def follow? + show? && user.present? + end + + def unfollow? + user.present? + end end diff --git a/app/policies/topic_policy.rb b/app/policies/topic_policy.rb index bc80f657..cc413d51 100644 --- a/app/policies/topic_policy.rb +++ b/app/policies/topic_policy.rb @@ -55,6 +55,14 @@ class TopicPolicy < ApplicationPolicy show? end + def follow? + show? && user.present? + end + + def unfollow? + user.present? + end + # Helpers def map_policy @map_policy ||= Pundit.policy(user, record.defer_to_map) diff --git a/app/views/layouts/_foot.html.erb b/app/views/layouts/_foot.html.erb index ea4ab671..0912b062 100644 --- a/app/views/layouts/_foot.html.erb +++ b/app/views/layouts/_foot.html.erb @@ -3,7 +3,7 @@ <%= render :partial => 'shared/metacodeBgColors' %>