mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 07:53:49 +08:00
DEV: Add support for limit in notifications index w/o recent param (#24423)
Currently to use a limit in the notifications index, you have to also pass recent: true as a param. This PR: Adds optional limit param to be used in the notifications query, regardless of the presence of recent Raises the max limit of the response with recent present from 50 -> 60. It is super weird we have a hard-limit of 50 before with recent param, and 60 without the param.
This commit is contained in:

committed by
GitHub

parent
a5ed0ea5d6
commit
7d35e406ba
@ -5,7 +5,7 @@ class NotificationsController < ApplicationController
|
|||||||
before_action :ensure_admin, only: %i[create update destroy]
|
before_action :ensure_admin, only: %i[create update destroy]
|
||||||
before_action :set_notification, only: %i[update destroy]
|
before_action :set_notification, only: %i[update destroy]
|
||||||
|
|
||||||
INDEX_LIMIT = 50
|
INDEX_LIMIT = 60
|
||||||
|
|
||||||
def index
|
def index
|
||||||
user =
|
user =
|
||||||
@ -72,6 +72,7 @@ class NotificationsController < ApplicationController
|
|||||||
|
|
||||||
render_json_dump(json)
|
render_json_dump(json)
|
||||||
else
|
else
|
||||||
|
limit = fetch_limit_from_params(default: INDEX_LIMIT, max: INDEX_LIMIT)
|
||||||
offset = params[:offset].to_i
|
offset = params[:offset].to_i
|
||||||
|
|
||||||
notifications =
|
notifications =
|
||||||
@ -82,7 +83,7 @@ class NotificationsController < ApplicationController
|
|||||||
notifications = notifications.where(read: false) if params[:filter] == "unread"
|
notifications = notifications.where(read: false) if params[:filter] == "unread"
|
||||||
|
|
||||||
total_rows = notifications.dup.count
|
total_rows = notifications.dup.count
|
||||||
notifications = notifications.offset(offset).limit(60)
|
notifications = notifications.offset(offset).limit(limit)
|
||||||
notifications =
|
notifications =
|
||||||
Notification.filter_inaccessible_topic_notifications(current_user.guardian, notifications)
|
Notification.filter_inaccessible_topic_notifications(current_user.guardian, notifications)
|
||||||
render_json_dump(
|
render_json_dump(
|
||||||
@ -90,7 +91,12 @@ class NotificationsController < ApplicationController
|
|||||||
total_rows_notifications: total_rows,
|
total_rows_notifications: total_rows,
|
||||||
seen_notification_id: user.seen_notification_id,
|
seen_notification_id: user.seen_notification_id,
|
||||||
load_more_notifications:
|
load_more_notifications:
|
||||||
notifications_path(username: user.username, offset: offset + 60, filter: params[:filter]),
|
notifications_path(
|
||||||
|
username: user.username,
|
||||||
|
offset: offset + limit,
|
||||||
|
limit: limit,
|
||||||
|
filter: params[:filter],
|
||||||
|
),
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -90,12 +90,34 @@ RSpec.describe NotificationsController do
|
|||||||
describe "when limit params is invalid" do
|
describe "when limit params is invalid" do
|
||||||
include_examples "invalid limit params",
|
include_examples "invalid limit params",
|
||||||
"/notifications.json",
|
"/notifications.json",
|
||||||
described_class::INDEX_LIMIT,
|
described_class::INDEX_LIMIT + 1,
|
||||||
params: {
|
params: {
|
||||||
recent: true,
|
recent: true,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "respects limit param and properly bumps offset for load_more_notifications URL" do
|
||||||
|
7.times { notification = Fabricate(:notification, user: user) }
|
||||||
|
|
||||||
|
get "/notifications.json", params: { username: user.username, limit: 2 }
|
||||||
|
expect(response.parsed_body["notifications"].count).to eq(2)
|
||||||
|
expect(response.parsed_body["load_more_notifications"]).to eq(
|
||||||
|
"/notifications?limit=2&offset=2&username=#{user.username}",
|
||||||
|
)
|
||||||
|
|
||||||
|
# Same as response above but we need .json added before query params.
|
||||||
|
get "/notifications.json?limit=2&offset=2&username=#{user.username}"
|
||||||
|
expect(response.parsed_body["load_more_notifications"]).to eq(
|
||||||
|
"/notifications?limit=2&offset=4&username=#{user.username}",
|
||||||
|
)
|
||||||
|
|
||||||
|
# We are seeing that the offset is increasing properly and limit is staying the same
|
||||||
|
get "/notifications.json?limit=2&offset=4&username=#{user.username}"
|
||||||
|
expect(response.parsed_body["load_more_notifications"]).to eq(
|
||||||
|
"/notifications?limit=2&offset=6&username=#{user.username}",
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
it "get notifications with all filters" do
|
it "get notifications with all filters" do
|
||||||
notification = Fabricate(:notification, user: user)
|
notification = Fabricate(:notification, user: user)
|
||||||
notification2 = Fabricate(:notification, user: user)
|
notification2 = Fabricate(:notification, user: user)
|
||||||
|
Reference in New Issue
Block a user