Class: NotificationsController

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

Constant Summary collapse

INDEX_LIMIT =
50

Constants inherited from ApplicationController

ApplicationController::LEGACY_NO_THEMES, ApplicationController::LEGACY_NO_UNOFFICIAL_PLUGINS, ApplicationController::NO_PLUGINS, ApplicationController::NO_THEMES, ApplicationController::NO_UNOFFICIAL_PLUGINS, ApplicationController::SAFE_MODE

Constants included from CanonicalURL::ControllerExtensions

CanonicalURL::ControllerExtensions::ALLOWED_CANONICAL_PARAMS

Instance Attribute Summary

Attributes inherited from ApplicationController

#theme_id

Instance Method Summary collapse

Methods inherited from ApplicationController

#application_layout, #can_cache_content?, #clear_notifications, #conditionally_allow_site_embedding, #current_homepage, #discourse_expires_in, #dont_cache_page, #ember_cli_required?, #fetch_user_from_params, #guardian, #handle_permalink, #handle_theme, #handle_unverified_request, #has_escaped_fragment?, #immutable_for, #no_cookies, #perform_refresh_session, #post_ids_including_replies, #preload_json, #rate_limit_second_factor!, #redirect_with_client_support, #render_json_dump, #render_serialized, requires_plugin, #rescue_discourse_actions, #resolve_safe_mode, #secure_session, #serialize_data, #set_current_user_for_logs, #set_layout, #set_mobile_view, #set_mp_snapshot_fields, #show_browser_update?, #store_preloaded, #use_crawler_layout?, #with_resolved_locale

Methods included from VaryHeader

#ensure_vary_header

Methods included from ReadOnlyMixin

#add_readonly_header, #allowed_in_staff_writes_only_mode?, #block_if_readonly_mode, #check_readonly_mode, included, #staff_writes_only_mode?

Methods included from Hijack

#hijack

Methods included from GlobalPath

#cdn_path, #cdn_relative_path, #full_cdn_url, #path, #upload_cdn_path

Methods included from JsonError

#create_errors_json

Methods included from CanonicalURL::ControllerExtensions

#canonical_url, #default_canonical, included

Methods included from CurrentUser

#clear_current_user, #current_user, has_auth_cookie?, #is_api?, #is_user_api?, #log_off_user, #log_on_user, lookup_from_env, #refresh_session

Instance Method Details

#createObject



122
123
124
125
# File 'app/controllers/notifications_controller.rb', line 122

def create
  @notification = Notification.consolidate_or_create!(notification_params)
  render_notification
end

#destroyObject



132
133
134
135
# File 'app/controllers/notifications_controller.rb', line 132

def destroy
  @notification.destroy!
  render json: success_json
end

#indexObject



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

def index
  user =
    if params[:username] && !params[:recent]
      user_record = User.find_by(username: params[:username].to_s)
      raise Discourse::NotFound if !user_record
      user_record
    else
      current_user
    end

  guardian.ensure_can_see_notifications!(user)

  if notification_types = params[:filter_by_types]&.split(",").presence
    notification_types.map! do |type|
      Notification.types[type.to_sym] ||
        (raise Discourse::InvalidParameters.new("invalid notification type: #{type}"))
    end
  end

  if params[:recent].present?
    limit = fetch_limit_from_params(default: 15, max: INDEX_LIMIT)

    include_reviewables = false

    notifications =
      Notification.prioritized_list(current_user, count: limit, types: notification_types)
    # notification_types is blank for the "all notifications" user menu tab
    include_reviewables = notification_types.blank? && guardian.can_see_review_queue?

    if notifications.present? && !(params.has_key?(:silent) || @readonly_mode)
      if current_user.bump_last_seen_notification!
        current_user.reload
        current_user.publish_notifications_state
      end
    end

    if !params.has_key?(:silent) && params[:bump_last_seen_reviewable] && !@readonly_mode &&
         include_reviewables
      current_user_id = current_user.id
      Scheduler::Defer.later "bump last seen reviewable for user" do
        # we lookup current_user again in the background thread to avoid
        # concurrency issues where the user object returned by the
        # current_user controller method is changed by the time the deferred
        # block is executed
        User.find_by(id: current_user_id)&.bump_last_seen_reviewable!
      end
    end

    notifications = filter_inaccessible_notifications(notifications)

    json = {
      notifications: serialize_data(notifications, NotificationSerializer),
      seen_notification_id: current_user.seen_notification_id,
    }

    if include_reviewables
      json[:pending_reviewables] = Reviewable.basic_serializers_for_list(
        Reviewable.user_menu_list_for(current_user),
        current_user,
      ).as_json
    end

    render_json_dump(json)
  else
    offset = params[:offset].to_i

    notifications =
      Notification.where(user_id: user.id).visible.includes(:topic).order(created_at: :desc)

    notifications = notifications.where(read: true) if params[:filter] == "read"

    notifications = notifications.where(read: false) if params[:filter] == "unread"

    total_rows = notifications.dup.count
    notifications = notifications.offset(offset).limit(60)
    notifications = filter_inaccessible_notifications(notifications)
    render_json_dump(
      notifications: serialize_data(notifications, NotificationSerializer),
      total_rows_notifications: total_rows,
      seen_notification_id: user.seen_notification_id,
      load_more_notifications:
        notifications_path(username: user.username, offset: offset + 60, filter: params[:filter]),
    )
  end
end

#mark_readObject



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

def mark_read
  if params[:id]
    Notification.read(current_user, [params[:id].to_i])
  else
    if types = params[:dismiss_types]&.split(",").presence
      invalid = []
      types.map! do |type|
        type_id = Notification.types[type.to_sym]
        invalid << type if !type_id
        type_id
      end
      if invalid.size > 0
        raise Discourse::InvalidParameters.new("invalid notification types: #{invalid.inspect}")
      end
    end

    Notification.read_types(current_user, types)
    current_user.bump_last_seen_notification!
  end

  current_user.reload
  current_user.publish_notifications_state

  render json: success_json
end

#updateObject



127
128
129
130
# File 'app/controllers/notifications_controller.rb', line 127

def update
  @notification.update!(notification_params)
  render_notification
end