Class: Api::V3::TopicsController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

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

Instance Method Details

#banObject

屏蔽话题,移到 NoPoint 节点 (Admin only)

POST /api/v3/topics/:id/ban

Raises:


239
240
241
242
243
# File 'app/controllers/api/v3/topics_controller.rb', line 239

def ban
  raise AccessDenied.new('当前用户没有屏蔽别人话题的权限,具体请参考官网的说明。') unless can?(:ban, @topic)
  @topic.ban!
  render json: { ok: 1 }
end

#createObject

创建新话题

POST /api/v3/topics

params:

title - 标题, [required]
node_id - 节点编号, [required]
body - Markdown 格式的正文, [required]

Raises:


81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/controllers/api/v3/topics_controller.rb', line 81

def create
  requires! :title
  requires! :body
  requires! :node_id

  raise AccessDenied.new('当前登录的用户没有发帖权限,具体请参考官网的相关说明。') unless can?(:create, Topic)

  @topic = current_user.topics.new(title: params[:title], body: params[:body])
  @topic.node_id = params[:node_id]
  @topic.save!


  render json: @topic, serializer: TopicDetailSerializer
end

#create_repliesObject

创建对话题的回帖

POST /api/v3/topics/:id/replies

params:

body - 回帖内容,[required]

Raises:


181
182
183
184
185
186
187
188
189
190
191
192
# File 'app/controllers/api/v3/topics_controller.rb', line 181

def create_replies
  doorkeeper_authorize!

  requires! :body

  raise AccessDenied.new('当前用户没有回帖权限,具体请参考官网的说明。') unless can?(:create, Reply)

  @reply = @topic.replies.build(body: params[:body])
  @reply.user_id = current_user.id
  @reply.save!
  render json: @reply
end

#destroyObject

删除话题

DELETE /api/v3/topics/:id

Raises:


134
135
136
137
138
# File 'app/controllers/api/v3/topics_controller.rb', line 134

def destroy
  raise AccessDenied unless can?(:destroy, @topic)
  @topic.destroy_by(current_user)
  render json: { ok: 1 }
end

#favoriteObject

收藏话题

POST /api/v3/topics/:id/favorite


219
220
221
222
# File 'app/controllers/api/v3/topics_controller.rb', line 219

def favorite
  current_user.favorite_topic(@topic.id)
  render json: { ok: 1 }
end

#followObject

关注话题

POST /api/v3/topics/:id/follow


199
200
201
202
# File 'app/controllers/api/v3/topics_controller.rb', line 199

def follow
  @topic.push_follower(current_user.id)
  render json: { ok: 1 }
end

#indexObject

获取话题列表,类似网站的 /topics 的结构,支持多种排序方式。

GET /api/v3/topics

params:

type - 排序类型, default: last_actived, %w(last_actived recent no_reply popular excellent),
node_id - 节点编号,如果有给,就会只去节点下的话题
offset - default: 0
limit - default: 20, range: 1..150

18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/api/v3/topics_controller.rb', line 18

def index
  optional! :type, default: 'last_actived',
                   values: %w(last_actived recent no_reply popular excellent)
  optional! :node_id
  optional! :offset, default: 0
  optional! :limit, default: 20, values: 1..150

  params[:type].downcase!

  if params[:node_id].blank?
    @topics = Topic
    if current_user
      @topics = @topics.without_nodes(current_user.blocked_node_ids)
      @topics = @topics.without_users(current_user.blocked_user_ids)
    else
      @topics = @topics.without_hide_nodes
    end
  else
    @node = Node.find(params[:node_id])
    @topics = @node.topics
  end

  @topics = @topics.fields_for_list.includes(:user).send(params[:type])
  if %w(no_reply popular).index(params[:type])
    @topics = @topics.last_actived
  elsif params[:type] == 'excellent'
    @topics = @topics.recent
  end

  @topics = @topics.offset(params[:offset]).limit(params[:limit])
  render json: @topics
end

#repliesObject

获取话题的回帖列表

GET /api/v3/topics/:id/replies

params:

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

149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'app/controllers/api/v3/topics_controller.rb', line 149

def replies
  if request.post?
    create_replies
    return
  end

  params[:limit] ||= 20

  @replies = Reply.unscoped.where(topic_id: @topic.id).order(:id).includes(:user)
  @replies = @replies.offset(params[:offset].to_i).limit(params[:limit].to_i)

  @user_liked_reply_ids = []
  if current_user
    # 找出用户 like 过的 Reply,给 JS 处理 like 功能的状态
    @replies.each do |r|
      unless r.liked_user_ids.index(current_user.id).nil?
        @user_liked_reply_ids << r.id
      end
    end
  end

  render json: @replies, meta: { user_liked_reply_ids: @user_liked_reply_ids }
end

#showObject

获取话题详情(不含回帖)

GET /api/v3/topics/:id


56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/controllers/api/v3/topics_controller.rb', line 56

def show
  @topic.hits.incr(1)
  meta = { followed: false, liked: false, favorited: false }

  if current_user
    # 处理通知
    current_user.read_topic(@topic)
    meta[:followed] = @topic.followed?(current_user.id)
    meta[:liked] = current_user.liked?(@topic)
    meta[:favorited] = current_user.favorited_topic?(@topic.id)
  end

  render json: @topic, serializer: TopicDetailSerializer, meta: meta
end

#unfavoriteObject

取消收藏话题

POST /api/v3/topics/:id/unfavorite


229
230
231
232
# File 'app/controllers/api/v3/topics_controller.rb', line 229

def unfavorite
  current_user.unfavorite_topic(@topic.id)
  render json: { ok: 1 }
end

#unfollowObject

取消关注话题

POST /api/v3/topics/:id/unfollow


209
210
211
212
# File 'app/controllers/api/v3/topics_controller.rb', line 209

def unfollow
  @topic.pull_follower(current_user.id)
  render json: { ok: 1 }
end

#updateObject

更新话题

POST /api/v3/topics/:id

params:

title - 标题, [required]
node_id - 节点编号, [required]
body - Markdown 格式的正文, [required]

Raises:


106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/controllers/api/v3/topics_controller.rb', line 106

def update
  requires! :title
  requires! :body
  requires! :node_id

  raise AccessDenied unless can?(:update, @topic)

  if @topic.lock_node == false || admin?
    # 锁定接点的时候,只有管理员可以修改节点
    @topic.node_id = params[:node_id]

    if admin? && @topic.node_id_changed?
      # 当管理员修改节点的时候,锁定节点
      @topic.lock_node = true
    end
  end
  @topic.title = params[:title]
  @topic.body = params[:body]
  @topic.save!

  render json: @topic, serializer: TopicDetailSerializer
end