summaryrefslogtreecommitdiff
path: root/app/controllers/api/v2/notifications_controller.rb
blob: 02b38bef8c61f6c9b1dfb1a48888ae52b35979de (plain)
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
# frozen_string_literal: true

class Api::V2::NotificationsController < Api::V2::ApiController
  skip_authorization_check
  before_action :require_user
  before_action :load_actor, only: [:watch, :unwatch]

  def unread
    render json: { count: current_user.notifications.count }
  end

  def mark_read
    notification = Notification.find_by(id: params[:id])
    return head(404) if notification.nil?

    Notification.mark_read(notification, current_user)
    respond_with head: :ok
  end

  def watch
    Notification.watch(@actor, current_user)
    respond_with head: :ok
  end

  def unwatch
    Notification.mark_all_read(@actor, current_user)
    Notification.unwatch(@actor, current_user)
    respond_with head: :ok
  end

  private

  def load_actor
    # nastiness-proof constantizer
    actor_class = %w[Post Topic Forum Gallery].detect { |cn| params[:actor_class] == cn }.constantize
    @actor = actor_class.find_by(id: params[:id])
    head(404) if @actor.nil?
  end
end