Skip to content
This repository

add 'favorited' and 'liked' fields to topic api #169

Closed
wants to merge 1 commit into from

2 participants

Donny Jason Lee
Donny

Adding these two fields makes it convenient for api-client to judge one
toppic is favorited or liked by current authenticated user.

Donny add 'favorited' and 'liked' fields to topic api
adding these two fields makes it convenient for api client to judge one
toppic is favorited by current authenticated user.
a43a1fd
Jason Lee
Owner

Like 和 Favorite 是不同的作用,不建议搞出来,有 Favorited 就行了

Jason Lee huacnlee closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Apr 01, 2013
Donny add 'favorited' and 'liked' fields to topic api
adding these two fields makes it convenient for api client to judge one
toppic is favorited by current authenticated user.
a43a1fd
This page is out of date. Refresh to see the latest.
4  app/grape/api_v2.rb
@@ -22,7 +22,7 @@ class APIV2 < Grape::API
22 22
       #   /api/topics/index.json?page=1&per_page=15
23 23
       get do
24 24
         @topics = Topic.last_actived.includes(:user).paginate(:page => params[:page], :per_page => params[:per_page] || 30)
25  
-        present @topics, :with => APIEntities::Topic
  25
+        present @topics, :with => APIEntities::Topic, :current_user => current_user
26 26
       end
27 27
 
28 28
       # Get active topics of the specified node
@@ -35,7 +35,7 @@ class APIV2 < Grape::API
35 35
         @topics = @node.topics.last_actived
36 36
           .limit(page_size)
37 37
           .includes(:user)
38  
-        present @topics, :with => APIEntities::Topic
  38
+        present @topics, :with => APIEntities::Topic, :current_user => current_user
39 39
       end
40 40
 
41 41
       # Post a new topic
6  app/grape/entities.rb
@@ -42,6 +42,12 @@ class Reply < Grape::Entity
42 42
     
43 43
     class Topic < Grape::Entity
44 44
       expose :id, :title, :created_at, :updated_at, :replied_at, :replies_count, :node_name, :node_id, :last_reply_user_id, :last_reply_user_login
  45
+      expose (:favorited){ |topic,options|
  46
+        !!options[:current_user] &&  options[:current_user].favorite_topic_ids.include?(topic.id) 
  47
+      }
  48
+      expose (:liked){ |topic,options|
  49
+        !!options[:current_user] &&  topic.liked_by_user?(options[:current_user])
  50
+      }      
45 51
       expose :user, :using => APIEntities::User
46 52
     end
47 53
 
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.