Class: Api::V3::UsersController

Inherits:
ApplicationController show all
Defined in:
app/controllers/api/v3/users_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#admin?, #can?, #current_ability, #current_user, #error!, #error_404!, #optional!, #requires!

Instance Method Details

#blockObject

屏蔽用户

POST /api/v3/users/:id/block


197
198
199
200
# File 'app/controllers/api/v3/users_controller.rb', line 197

def block
  current_user.block_user(@user.id)
  render json: { ok: 1 }
end

#blockedObject

获取用户的已屏蔽的人(只能获取自己的)

GET /api/v3/users/:id/blocked

params:

offset - default: 0
limit - default: 20, range: 1..150

Raises:


161
162
163
164
165
166
167
168
169
170
# File 'app/controllers/api/v3/users_controller.rb', line 161

def blocked
  optional! :offset, type: Integer, default: 0
  optional! :limit, type: Integer, default: 20, values: 1..150

  raise AccessDenied.new('不可以获取其他人的 blocked_users 列表。') if current_user.id != @user.id

  user_ids = current_user.blocked_user_ids[params[:offset].to_i, params[:limit].to_i]
  @blocked_users = User.where(id: user_ids)
  render json: @blocked_users, root: 'blocked'
end

#favoritesObject

获取某个用户的收藏列表

GET /api/v3/users/:id/favorites

params:

offset - default: 0
limit - default: 20, range: 1..150

106
107
108
109
110
111
112
113
114
115
116
# File 'app/controllers/api/v3/users_controller.rb', line 106

def favorites
  optional! :offset, type: Integer, default: 0
  optional! :limit, type: Integer, default: 20, values: 1..150

  @topic_ids = @user.favorite_topic_ids[params[:offset].to_i, params[:limit].to_i]
  @topics = Topic.where(id: @topic_ids).fields_for_list.includes(:user)
  @topics = @topics.to_a.sort do |a, b|
    @topic_ids.index(a.id) <=> @topic_ids.index(b.id)
  end
  render json: @topics
end

#followObject

关注用户

POST /api/v3/users/:id/follow


177
178
179
180
# File 'app/controllers/api/v3/users_controller.rb', line 177

def follow
  current_user.follow_user(@user)
  render json: { ok: 1 }
end

#followersObject

获取某个用户关注的人的列表

GET /api/v3/users/:id/followers

params:

offset - default: 0
limit - default: 20, range: 1..150

127
128
129
130
131
132
133
# File 'app/controllers/api/v3/users_controller.rb', line 127

def followers
  optional! :offset, type: Integer, default: 0
  optional! :limit, type: Integer, default: 20, values: 1..150

  @users = @user.followers.fields_for_list.offset(params[:offset]).limit(params[:limit])
  render json: @users, root: 'followers'
end

#followingObject

获取某个用户的关注者列表

GET /api/v3/users/:id/following

params:

offset - default: 0
limit - default: 20, range: 1..150

144
145
146
147
148
149
150
# File 'app/controllers/api/v3/users_controller.rb', line 144

def following
  optional! :offset, type: Integer, default: 0
  optional! :limit, type: Integer, default: 20, values: 1..150

  @users = @user.following.fields_for_list.offset(params[:offset]).limit(params[:limit])
  render json: @users, root: 'following'
end

#indexObject

获取热门用户

GET /api/v3/users

params:

limit - default: 20,range: 1..100

15
16
17
18
19
20
21
22
# File 'app/controllers/api/v3/users_controller.rb', line 15

def index
  optional! :limit, default: 20, values: 1..100

  limit = params[:limit].to_i
  limit = 100 if limit > 100
  @users = User.fields_for_list.hot.limit(limit)
  render json: @users
end

#meObject

获取当前用户的完整信息,用于个人设置修改资料

GET /api/v3/users/me


29
30
31
# File 'app/controllers/api/v3/users_controller.rb', line 29

def me
  render json: current_user, serializer: UserDetailSerializer
end

#repliesObject

获取某个用户的回帖列表

GET /api/v3/users/:id/replies

params:

order - 排序方式, default: 'recent', range: %w(recent)
offset - default: 0
limit - default: 20, range: 1..150

86
87
88
89
90
91
92
93
94
95
# File 'app/controllers/api/v3/users_controller.rb', line 86

def replies
  optional! :order, type: String, default: 'recent', values: %w(recent)
  optional! :offset, type: Integer, default: 0
  optional! :limit, type: Integer, default: 20, values: 1..150

  @replies = @user.replies.recent
  @replies = @replies.includes(:user, :topic).offset(params[:offset]).limit(params[:limit])

  render json: @replies, each_serializer: ReplyDetailSerializer
end

#showObject

获取某个用户的详细信息

GET /api/v3/users/:id


38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/api/v3/users_controller.rb', line 38

def show
  meta = { followed: false, blocked: false }

  if current_user
    meta[:followed] = current_user.followed?(@user)
    meta[:blocked] = current_user.blocked_user?(@user)
  end

  render json: @user, serializer: UserDetailSerializer, meta: meta
end

#topicsObject

获取某个用户的话题列表

GET /api/v3/users/:id/topics

params:

order - 排序方式, default: 'recent', range: %w(recent likes replies)
offset - default: 0
limit - default: 20, range: 1..150

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/controllers/api/v3/users_controller.rb', line 59

def topics
  optional! :order, type: String, default: 'recent', values: %w(recent likes replies)
  optional! :offset, type: Integer, default: 0
  optional! :limit, type: Integer, default: 20, values: 1..150

  @topics = @user.topics.fields_for_list
  if params[:order] == 'likes'
    @topics = @topics.high_likes
  elsif params[:order] == 'replies'
    @topics = @topics.high_replies
  else
    @topics = @topics.recent
  end
  @topics = @topics.includes(:user).offset(params[:offset]).limit(params[:limit])
  render json: @topics
end

#unblockObject

取消屏蔽用户

POST /api/v3/users/:id/unblock


207
208
209
210
# File 'app/controllers/api/v3/users_controller.rb', line 207

def unblock
  current_user.unblock_user(@user.id)
  render json: { ok: 1 }
end

#unfollowObject

取消关注用户

POST /api/v3/users/:id/unfollow


187
188
189
190
# File 'app/controllers/api/v3/users_controller.rb', line 187

def unfollow
  current_user.unfollow_user(@user)
  render json: { ok: 1 }
end