mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
FEATURE: Improving bookmarks part 2 -- Topic Bookmarking (#8954)
### UI Changes If `SiteSetting.enable_bookmarks_with_reminders` is enabled: * Clicking "Bookmark" on a topic will create a new Bookmark record instead of a post + user action * Clicking "Clear Bookmarks" on a topic will delete all the new Bookmark records on a topic * The topic bookmark buttons control the post bookmark flags correctly and vice-versa Disabled selecting the "reminder type" for bookmarks in the UI because the backend functionality is not done yet (of sending users notifications etc.) ### Other Changes * Added delete bookmark route (but no UI yet) * Added a rake task to sync the old PostAction bookmarks to the new Bookmark table, which can be run as many times as we want for a site (it will not create duplicates).
This commit is contained in:
60
lib/tasks/bookmarks.rake
Normal file
60
lib/tasks/bookmarks.rake
Normal file
@@ -0,0 +1,60 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_dependency "rake_helpers"
|
||||
|
||||
##
|
||||
# This will create records in the new bookmarks table from PostAction
|
||||
# records. The task is idempotent, it will not create additional bookmark
|
||||
# records for PostActions that have already been created in the new table.
|
||||
# You can provide a sync_limit for a smaller batch run.
|
||||
#
|
||||
desc "migrates old PostAction bookmarks to the new Bookmark model & table"
|
||||
task "bookmarks:sync_to_table", [:sync_limit] => :environment do |_t, args|
|
||||
sync_limit = args[:sync_limit] || 0
|
||||
post_action_bookmarks = PostAction
|
||||
.select('post_actions.id', 'post_actions.post_id', 'posts.topic_id', 'post_actions.user_id')
|
||||
.where(post_action_type_id: PostActionType.types[:bookmark])
|
||||
.joins(:post)
|
||||
.where(deleted_at: nil)
|
||||
.joins('LEFT JOIN bookmarks ON bookmarks.post_id = post_actions.post_id AND bookmarks.user_id = post_actions.user_id')
|
||||
.where('bookmarks.id IS NULL')
|
||||
|
||||
# if sync_limit is provided as an argument this will cap
|
||||
# the number of bookmarks that will be created in a run of
|
||||
# this task (for huge bookmark count communities)
|
||||
if sync_limit > 0
|
||||
post_action_bookmarks = post_action_bookmarks.limit(sync_limit)
|
||||
end
|
||||
|
||||
post_action_bookmark_count = post_action_bookmarks.count('post_actions.id')
|
||||
bookmarks_to_create = []
|
||||
i = 0
|
||||
|
||||
Bookmark.transaction do
|
||||
post_action_bookmarks.find_each(batch_size: 1000) do |pab|
|
||||
RakeHelpers.print_status_with_label("Creating post new bookmarks.......", i, post_action_bookmark_count)
|
||||
now = Time.zone.now
|
||||
bookmarks_to_create << {
|
||||
topic_id: pab.topic_id,
|
||||
post_id: pab.post_id,
|
||||
user_id: pab.user_id,
|
||||
created_at: now,
|
||||
updated_at: now
|
||||
}
|
||||
|
||||
i += 1
|
||||
end
|
||||
|
||||
# this will ignore conflicts in the bookmarks table so
|
||||
# if the user already has a post bookmarked in the new way,
|
||||
# then we don't error and keep on truckin'
|
||||
#
|
||||
# we shouldn't have duplicates here at any rate because of
|
||||
# the above LEFT JOIN but best to be safe knowing this
|
||||
# won't blow up
|
||||
Bookmark.insert_all(bookmarks_to_create)
|
||||
end
|
||||
|
||||
RakeHelpers.print_status_with_label("Bookmark creation complete! ", i, post_action_bookmark_count)
|
||||
puts ""
|
||||
end
|
||||
Reference in New Issue
Block a user