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
|
# frozen_string_literal: true
# Copyright (c) 2010-2011, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
class NotificationsController < ApplicationController
before_action :authenticate_user!
def update
note = Notification.where(:recipient_id => current_user.id, :id => params[:id]).first
if note
note.set_read_state(params[:set_unread] != "true" )
respond_to do |format|
format.json { render :json => { :guid => note.id, :unread => note.unread } }
end
else
respond_to do |format|
format.json { render :json => {}.to_json }
end
end
end
def index
conditions = {:recipient_id => current_user.id}
if params[:type] && types.has_key?(params[:type])
conditions[:type] = types[params[:type]]
end
if params[:show] == "unread" then conditions[:unread] = true end
page = params[:page] || 1
per_page = params[:per_page] || 25
@notifications = WillPaginate::Collection.create(page, per_page, Notification.where(conditions).count ) do |pager|
result = Notification.where(conditions)
.includes(:target, :actors => :profile)
.order("updated_at desc")
.limit(pager.per_page)
.offset(pager.offset)
pager.replace(result)
end
@group_days = @notifications.group_by {|note| note.updated_at.strftime("%Y-%m-%d") }
@unread_notification_count = current_user.unread_notifications.count
@grouped_unread_notification_counts = {}
types.each_with_object(current_user.unread_notifications.group_by(&:type)) {|(name, type), notifications|
@grouped_unread_notification_counts[name] = notifications.has_key?(type) ? notifications[type].count : 0
}
respond_to do |format|
format.html
format.xml { render :xml => @notifications.to_xml }
format.json {
render json: render_as_json(@unread_notification_count, @grouped_unread_notification_counts, @notifications)
}
end
end
def default_serializer_options
{
context: self,
root: false
}
end
def read_all
current_type = types[params[:type]]
notifications = Notification.where(recipient_id: current_user.id, unread: true)
notifications = notifications.where(type: current_type) if params[:type]
notifications.update_all(unread: false)
respond_to do |format|
if current_user.unread_notifications.count > 0
format.html { redirect_to notifications_path }
format.mobile { redirect_to notifications_path }
else
format.html { redirect_to stream_path }
format.mobile { redirect_to stream_path }
end
format.xml { render :xml => {}.to_xml }
format.json { render :json => {}.to_json }
end
end
private
def render_as_json(unread_count, unread_count_by_type, notification_list)
{
unread_count: unread_count,
unread_count_by_type: unread_count_by_type,
notification_list: notification_list.map {|note|
NotificationSerializer.new(note, default_serializer_options).as_json
}
}.as_json
end
def types
{
"also_commented" => "Notifications::AlsoCommented",
"comment_on_post" => "Notifications::CommentOnPost",
"liked" => "Notifications::Liked",
"mentioned" => "Notifications::MentionedInPost",
"mentioned_in_comment" => "Notifications::MentionedInComment",
"reshared" => "Notifications::Reshared",
"started_sharing" => "Notifications::StartedSharing",
"contacts_birthday" => "Notifications::ContactsBirthday"
}
end
helper_method :types
end
|