discourse/spec/support/bookmarkable_helper.rb
Martin Brennan 222c8d9b6a
FEATURE: Polymorphic bookmarks pt. 3 (reminders, imports, exports, refactors) (#16591)
A bit of a mixed bag, this addresses several edge areas of bookmarks and makes them compatible with polymorphic bookmarks (hidden behind the `use_polymorphic_bookmarks` site setting). The main ones are:

* ExportUserArchive compatibility
* SyncTopicUserBookmarked job compatibility
* Sending different notifications for the bookmark reminders based on the bookmarkable type
* Import scripts compatibility
* BookmarkReminderNotificationHandler compatibility

This PR also refactors the `register_bookmarkable` API so it accepts a class descended from a `BaseBookmarkable` class instead. This was done because we kept having to add more and more lambdas/properties inline and it was very messy, so a factory pattern is cleaner. The classes can be tested independently as well.

Some later PRs will address some other areas like the discourse narrative bot, advanced search, reports, and the .ics endpoint for bookmarks.
2022-05-09 09:37:23 +10:00

43 lines
959 B
Ruby

# frozen_string_literal: true
class UserTestBookmarkSerializer < UserBookmarkBaseSerializer; end
class UserTestBookmarkable < BaseBookmarkable
def self.model
User
end
def self.serializer
UserTestBookmarkSerializer
end
def self.preload_associations
[:topic_users, :tags, { posts: :user }]
end
def self.list_query(user, guardian)
user.bookmarks.joins(
"INNER JOIN users ON users.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'User'"
).where(bookmarkable_type: "User")
end
def self.search_query(bookmarks, query, ts_query, &bookmarkable_search)
bookmarks.where("users.username ILIKE ?", query)
end
def self.reminder_handler(bookmark)
# noop
end
def self.reminder_conditions(bookmark)
bookmark.bookmarkable.present?
end
def self.can_see?(guardian, bookmark)
true
end
end
def register_test_bookmarkable
Bookmark.register_bookmarkable(UserTestBookmarkable)
end