-
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathnotifications_controller.rb
More file actions
75 lines (68 loc) · 2.23 KB
/
notifications_controller.rb
File metadata and controls
75 lines (68 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Provides web and API actions relating to user notifications.
class NotificationsController < ApplicationController
include CommentsHelper
before_action :authenticate_user!, only: [:index]
def index
@notifications = Notification.unscoped.where(user: current_user).paginate(page: params[:page], per_page: 100)
.order(Arel.sql('is_read ASC, created_at DESC'))
if stale?(@notifications)
respond_to do |format|
format.html { render :index, layout: 'without_sidebar' }
format.json do
render json: (@notifications.to_a.map do |notif|
notif.as_json.merge(content: helpers.render_pings_text(notif.content),
community_name: notif.community_name)
end)
end
end
end
end
def read
@notification = Notification.unscoped.find params[:id]
unless @notification.user == current_user
respond_to do |format|
format.html { render template: 'errors/forbidden', status: :forbidden }
format.json { render json: nil, status: :forbidden }
end
return
end
@notification.is_read = !@notification.is_read
if @notification.save
respond_to do |format|
format.html do
flash[:notice] = 'Marked as read.'
render :index
end
format.json { render json: { status: 'success', notification: @notification }, methods: :community_name }
end
else
respond_to do |format|
format.html do
flash[:error] = 'Failed to mark read.'
render :index
end
format.json { render json: { status: 'failed' } }
end
end
end
def read_all
@notifications = Notification.unscoped.where(user: current_user, is_read: false)
if @notifications.update_all(is_read: true)
respond_to do |format|
format.html do
flash[:notice] = 'Marked all as read.'
render :index
end
format.json { render json: { status: 'success' } }
end
else
respond_to do |format|
format.html do
flash[:error] = 'Failed to mark all read.'
render :index
end
format.json { render json: { status: 'failed' } }
end
end
end
end