Class: Api::V3::NotificationsController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

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

Instance Method Details

#allObject

删除当前用户的所有通知

DELETE /api/v3/notifications/all


50
51
52
53
# File 'app/controllers/api/v3/notifications_controller.rb', line 50

def all
  current_user.notifications.delete_all
  render json: { ok: 1 }
end

#destroyObject

删除当前用户的某个通知

DELETE /api/v3/notifications/:id


69
70
71
72
73
# File 'app/controllers/api/v3/notifications_controller.rb', line 69

def destroy
  @notification = current_user.notifications.find(params[:id])
  @notification.destroy
  render json: { ok: 1 }
end

#indexObject

获取用户的通知列表

GET /api/v3/notifications

params:

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

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

def index
  optional! :offset, default: 0
  optional! :limit, default: 20, values: 1..150

  @notifications = Notification.where(user_id: current_user.id).order('id desc')
                               .offset(params[:offset])
                               .limit(params[:limit])

  render json: @notifications
end

#readObject

将当前用户的一些通知设成已读状态

POST /api/v3/notifications/read

params:

ids - <Array> of Notification id, [required]

34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/api/v3/notifications_controller.rb', line 34

def read
  requires! :ids

  if params[:ids].length > 0
    @notifications = current_user.notifications.where(id: params[:ids])
    Notification.read!(@notifications.collect(&:id))
  end

  render json: { ok: 1 }
end

#unread_countObject

获得未读通知数量

GET /api/v3/notifications/unread_count


60
61
62
# File 'app/controllers/api/v3/notifications_controller.rb', line 60

def unread_count
  render json: { count: Notification.unread_count(current_user) }
end