Class: NotificationsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/notifications_controller.rb

Instance Method Summary collapse

Instance Method Details

#archive_selectedObject

Archive selected notifications

:category: Notifications Actions

Parameters

  • :id - An array of IDs of notifications you'd like to archive. If ID is 'all', all notifications will be archived

Example

POST notifications/archive_selected.json?id=all

HEAD 204

190
191
192
193
194
195
196
197
# File 'app/controllers/notifications_controller.rb', line 190

def archive_selected
  Notification.archive(selected_notifications, params[:value])
  if request.xhr?
    head :ok
  else
    redirect_back fallback_location: root_path
  end
end

#commentObject


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/controllers/notifications_controller.rb', line 126

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

#delete_selectedObject

Delete selected notifications

:category: Notifications Actions

Parameters

  • :id - An array of IDs of notifications you'd like to delete. If ID is 'all', all notifications will be deleted

Example

POST notifications/delete_selected.json?id=all

HEAD 204

230
231
232
233
234
235
236
237
# File 'app/controllers/notifications_controller.rb', line 230

def delete_selected
  selected_notifications.delete_all
  if request.xhr?
    head :ok
  else
    redirect_back fallback_location: root_path
  end
end

#expand_commentsObject


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
# File 'app/controllers/notifications_controller.rb', line 100

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

#indexObject

Return a listing of notifications, including a summary of unread repos, notification reasons, and notification types

:category: Notifications CRUD

Parameters

  • :page - The page you would like to request

  • :per_page - The number of results you would like to return per page. Max 100, default 20.

  • :starred - Return only the user's starred notifications

  • :archive - Return only the user's archived notifications

  • :q - Search by subject title of the notification

Notes

If the :per_page paremeter is set to more than 100, a 404 will be returned

Example

GET notifications.json

{
   "pagination" : {
      "total_notifications" : 1,
      "page" : 1,
      "total_pages" : 1,
      "per_page" : 20
   },
   "types" : {
      "PullRequest" : 1,
   },
   "reasons" : {
      "mention" : 1
   },
   "unread_repositories" : {
      "octobox/octobox" : 1
   },
   "notifications" : [
      {
         "id" : 29,
         "github_id" :  320,
         "reason" :  "mention",
         "unread" :  true,
         "archived" :  false,
         "starred" :  false,
         "url" : "https://api.github.com/notifications/threads/320",
         "web_url" : "https://github.com/octobox/octobox/pull/320",
         "last_read_at" : "2017-02-20 22:26:11 UTC",
         "created_at" : "2017-02-22T15:49:33.750Z",
         "updated_at" : "2017-02-22T15:40:21.000Z",
         "subject":{
            "title" : "Add JSON API",
            "url" : "https://api.github.com/repos/octobox/octobox/pulls/320",
            "type" : "PullRequest",
            "state" : "merged"
         },
         "repo":{
            "id": 320,
            "name" : "octobox/octobox",
            "owner" : "octobox",
            "repo_url" : "https://github.com/octobox/octobox"
         }
      }
   ]
}

73
74
75
# File 'app/controllers/notifications_controller.rb', line 73

def index
  load_and_count_notifications
end

#mark_read_selectedObject

Mark selected notifications as read

:category: Notifications Actions

Parameters

  • :id - An array of IDs of notifications you'd like to mark as read. If ID is 'all', all notifications will be marked as read

Example

POST notifications/mark_read_selected.json?id=all

HEAD 204

212
213
214
215
# File 'app/controllers/notifications_controller.rb', line 212

def mark_read_selected
  Notification.mark_read(selected_notifications)
  head :ok
end

#mute_selectedObject

Mute selected notifications, this will also archive them

:category: Notifications Actions

Parameters

  • :id - An array of IDs of notifications you'd like to mute. If ID is 'all', all notifications will be muted

Example

POST notifications/mute_selected.json?id=all

HEAD 204

168
169
170
171
172
173
174
175
# File 'app/controllers/notifications_controller.rb', line 168

def mute_selected
  Notification.mute(selected_notifications)
  if request.xhr?
    head :ok
  else
    redirect_back fallback_location: root_path
  end
end

#showObject


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/controllers/notifications_controller.rb', line 77

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

#starObject

Star a notification

:category: Notifications Actions

Example

POST notifications/:id/star.json

HEAD 204

248
249
250
251
# File 'app/controllers/notifications_controller.rb', line 248

def star
  @notification.update_columns starred: !@notification.starred?
  head :ok
end

#syncObject

Synchronize notifications with GitHub

:category: Notifications Actions

Example

POST notifications/sync.json

HEAD 204

262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'app/controllers/notifications_controller.rb', line 262

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

#syncingObject

Check if user is synchronizing notifications with GitHub

Example

POST notifications/syncing.json

{} 204 (ok)

POST notifications/syncing.json

{} 423 (locked)

291
292
293
294
295
296
297
# File 'app/controllers/notifications_controller.rb', line 291

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

#unread_countObject

Return a count for the number of unread notifications

:category: Notifications CRUD

Example

GET notifications/unread_count.json

{ "count" : 1 }

151
152
153
# File 'app/controllers/notifications_controller.rb', line 151

def unread_count
  render json: { 'count' => user_unread_count }
end