/
notifications_controller.rb
161 lines (138 loc) · 4.31 KB
/
notifications_controller.rb
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# frozen_string_literal: true
class NotificationsController < ApplicationController
include NotificationsConcern
skip_before_action :authenticate_user!
before_action :authenticate_web_or_api!
before_action :find_notification, only: [:star]
def index
load_and_count_notifications
respond_to do |format|
format.html
format.json { render 'api/notifications/index' }
end
end
def show
scope = notifications_for_presentation.newest
scope = load_and_count_notifications(scope) unless request.xhr?
ids = scope.pluck(:id)
position = ids.index(params[:id].to_i)
@notification = current_user.notifications.find(params[:id])
@previous = ids[position-1] unless position.nil? || position-1 < 0
@next = ids[position+1] unless position.nil? || position+1 > ids.length
if @notification.subject && @notification.subject.commentable?
comments_loaded = 5
@comments = @notification.subject.comments.order('created_at DESC').limit(comments_loaded).reverse
@comments_left_to_load = @notification.subject.comment_count - comments_loaded
@comments_left_to_load = 0 if @comments_left_to_load < 0
else
@comments = []
end
render partial: "notifications/thread", layout: false if request.xhr?
end
def expand_comments
scope = notifications_for_presentation.newest
scope = load_and_count_notifications(scope) unless request.xhr?
ids = scope.pluck(:id)
position = ids.index(params[:id].to_i)
@notification = current_user.notifications.find(params[:id])
@previous = ids[position-1] unless position.nil? || position-1 < 0
@next = ids[position+1] unless position.nil? || position+1 > ids.length
@comments_left_to_load = 0
if @notification.subject
@comments = @notification.subject.comments.order('created_at ASC')
else
@comments = []
end
if request.xhr?
render partial: "notifications/comments", locals:{comments: @comments}, layout: false
else
render 'notifications/show'
end
end
def comment
subject = current_user.notifications.find(params[:id]).subject
if current_user.can_comment?(subject)
subject.comment(current_user, params[:comment][:body]) if subject.commentable?
if request.xhr?
render partial: "notifications/comments", locals:{comments: subject.comments.last}, layout: false
else
redirect_back fallback_location: notification_path
end
else
flash[:error] = 'Could not post your comment'
redirect_back fallback_location: notification_path
end
end
def unread_count
render json: { 'count' => user_unread_count }
end
def lookup
if params[:url].present?
url = Octobox::SubjectUrlParser.new(params[:url]).to_api_url
@notification = current_user.notifications.where(subject_url: url).first
if @notification.nil?
render json: {}
else
render 'api/notifications/lookup'
end
else
render json: {}
end
end
def mute_selected
Notification.mute(selected_notifications)
if request.xhr?
head :ok
else
redirect_back fallback_location: root_path
end
end
def archive_selected
Notification.archive(selected_notifications, params[:value])
if request.xhr?
head :ok
else
redirect_back fallback_location: root_path
end
end
def mark_read_selected
Notification.mark_read(selected_notifications)
head :ok
end
def delete_selected
selected_notifications.delete_all
if request.xhr?
head :ok
else
redirect_back fallback_location: root_path
end
end
def star
@notification.update_columns starred: !@notification.starred?
head :ok
end
def sync
if Octobox.background_jobs_enabled?
current_user.sync_notifications
else
current_user.sync_notifications_in_foreground
end
respond_to do |format|
format.html do
if request.referer && !request.referer.match('/notifications/sync')
redirect_back fallback_location: root_path
else
redirect_to root_path
end
end
format.json { {} }
end
end
def syncing
if current_user.syncing?
render json: {}, status: :locked
else
render json: { error: Sidekiq::Status::get(current_user.sync_job_id, :exception) }, status: :ok
end
end
end