diff --git a/app/assets/javascripts/discourse/app/controllers/user-activity-bookmarks.js b/app/assets/javascripts/discourse/app/controllers/user-activity-bookmarks.js index a4221a0087e..72a316b4b65 100644 --- a/app/assets/javascripts/discourse/app/controllers/user-activity-bookmarks.js +++ b/app/assets/javascripts/discourse/app/controllers/user-activity-bookmarks.js @@ -96,6 +96,7 @@ export default Controller.extend({ _processLoadResponse(searchTerm, response) { if (!response || !response.user_bookmark_list) { + this.model.loadMoreUrl = null; return; } diff --git a/app/models/user_bookmark_list.rb b/app/models/user_bookmark_list.rb index bb6db58bec9..a55a72a8141 100644 --- a/app/models/user_bookmark_list.rb +++ b/app/models/user_bookmark_list.rb @@ -5,7 +5,7 @@ class UserBookmarkList PER_PAGE = 20 - attr_reader :bookmarks, :per_page + attr_reader :bookmarks, :per_page, :has_more attr_accessor :more_bookmarks_url, :bookmark_serializer_opts def initialize(user:, guardian:, params:) @@ -21,7 +21,9 @@ class UserBookmarkList end def load(&blk) - @bookmarks = BookmarkQuery.new(user: @user, guardian: @guardian, params: @params).list_all(&blk) + query = BookmarkQuery.new(user: @user, guardian: @guardian, params: @params) + @bookmarks = query.list_all(&blk) + @has_more = (@params[:page].to_i + 1) * @params[:per_page] < query.count @bookmarks end diff --git a/app/serializers/user_bookmark_list_serializer.rb b/app/serializers/user_bookmark_list_serializer.rb index b9f74ef4b0e..cefdd95114c 100644 --- a/app/serializers/user_bookmark_list_serializer.rb +++ b/app/serializers/user_bookmark_list_serializer.rb @@ -15,6 +15,6 @@ class UserBookmarkListSerializer < ApplicationSerializer end def include_more_bookmarks_url? - @include_more_bookmarks_url ||= object.bookmarks.size == object.per_page + @include_more_bookmarks_url ||= object.has_more end end diff --git a/lib/bookmark_query.rb b/lib/bookmark_query.rb index 50c8b25bcbf..f62ce591fac 100644 --- a/lib/bookmark_query.rb +++ b/lib/bookmark_query.rb @@ -26,7 +26,7 @@ class BookmarkQuery end end - attr_reader :guardian + attr_reader :guardian, :count def initialize(user:, guardian: nil, params: {}) @user = user @@ -34,6 +34,7 @@ class BookmarkQuery @guardian = guardian || Guardian.new(@user) @page = @params[:page].to_i @limit = @params[:limit].present? ? @params[:limit].to_i : @params[:per_page] + @count = 0 end def list_all(&blk) @@ -73,6 +74,8 @@ class BookmarkQuery bookmarks.updated_at DESC" ) + @count = results.count + if @page.positive? results = results.offset(@page * @params[:per_page]) end diff --git a/spec/models/user_bookmark_list_spec.rb b/spec/models/user_bookmark_list_spec.rb index b03a7141d74..03d67f80bb8 100644 --- a/spec/models/user_bookmark_list_spec.rb +++ b/spec/models/user_bookmark_list_spec.rb @@ -20,6 +20,7 @@ RSpec.describe UserBookmarkList do it "returns all types of bookmarks" do list.load expect(list.bookmarks.map(&:id)).to match_array([post_bookmark.id, topic_bookmark.id, user_bookmark.id]) + expect(list.has_more).to eq(false) end it "defaults to 20 per page" do