FEATURE: Add bookmarks to the user export (#10591)

FEATURE: Add bookmarks to the user export (#10591)

diff --git a/app/jobs/regular/export_user_archive.rb b/app/jobs/regular/export_user_archive.rb
index 03aa2c9..f2e77b0 100644
--- a/app/jobs/regular/export_user_archive.rb
+++ b/app/jobs/regular/export_user_archive.rb
@@ -14,6 +14,7 @@ module Jobs
       user_archive
       user_archive_profile
       badges
+      bookmarks
       category_preferences
       visits
     )
@@ -22,6 +23,7 @@ module Jobs
       user_archive: ['topic_title', 'categories', 'is_pm', 'post', 'like_count', 'reply_count', 'url', 'created_at'],
       user_archive_profile: ['location', 'website', 'bio', 'views'],
       badges: ['badge_id', 'badge_name', 'granted_at', 'post_id', 'seq', 'granted_manually', 'notification_id', 'featured_rank'],
+      bookmarks: ['post_id', 'topic_id', 'post_number', 'link', 'name', 'created_at', 'updated_at', 'reminder_type', 'reminder_at', 'reminder_last_sent_at', 'reminder_set_at', 'auto_delete_preference'],
       category_preferences: ['category_id', 'category_names', 'notification_level', 'dismiss_new_timestamp'],
       visits: ['visited_at', 'posts_read', 'mobile', 'time_read'],
     )
@@ -153,6 +155,35 @@ module Jobs
       end
     end
 
+    def bookmarks_export
+      return enum_for(:bookmarks_export) unless block_given?
+
+      Bookmark
+        .where(user_id: @current_user.id)
+        .joins(:post)
+        .order(:id)
+        .each do |bkmk|
+        link = ''
+        if guardian.can_see_post?(bkmk.post)
+          link = bkmk.post.full_url
+        end
+        yield [
+          bkmk.post_id,
+          bkmk.topic_id,
+          bkmk.post&.post_number,
+          link,
+          bkmk.name,
+          bkmk.created_at,
+          bkmk.updated_at,
+          Bookmark.reminder_types[bkmk.reminder_type],
+          bkmk.reminder_at,
+          bkmk.reminder_last_sent_at,
+          bkmk.reminder_set_at,
+          Bookmark.auto_delete_preferences[bkmk.auto_delete_preference],
+        ]
+      end
+    end
+
     def category_preferences_export
       return enum_for(:category_preferences_export) unless block_given?
 
@@ -205,6 +236,10 @@ module Jobs
 
     private
 
+    def guardian
+      @guardian ||= Guardian.new(@current_user)
+    end
+
     def piped_category_name(category_id)
       return "-" unless category_id
       category = Category.find_by(id: category_id)
diff --git a/spec/jobs/export_user_archive_spec.rb b/spec/jobs/export_user_archive_spec.rb
index 8a5f1f4..4d305d7 100644
--- a/spec/jobs/export_user_archive_spec.rb
+++ b/spec/jobs/export_user_archive_spec.rb
@@ -61,7 +61,7 @@ describe Jobs::ExportUserArchive do
 
       expect(system_message.first_post.raw).to eq(I18n.t(
         "system_messages.csv_export_succeeded.text_body_template",
-        download_link: "[#{upload.original_filename}|attachment](#{upload.short_url}) (#{upload.filesize} Bytes)"
+        download_link: "[#{upload.original_filename}|attachment](#{upload.short_url}) (#{upload.human_filesize})"
       ).chomp)
 
       expect(system_message.id).to eq(UserExport.last.topic_id)
@@ -191,6 +191,63 @@ describe Jobs::ExportUserArchive do
 
   end
 
+  context 'bookmarks' do
+    let(:component) { 'bookmarks' }
+
+    let(:name) { 'Collect my thoughts on this' }
+    let(:manager) { BookmarkManager.new(user) }
+    let(:topic1) { Fabricate(:topic) }
+    let(:post1) { Fabricate(:post, topic: topic1, post_number: 5) }
+    let(:post2) { Fabricate(:post) }
+    let(:post3) { Fabricate(:post) }
+    let(:message) { Fabricate(:private_message_topic) }
+    let(:post4) { Fabricate(:post, topic: message) }
+    let(:reminder_type) { Bookmark.reminder_types[:tomorrow] }
+    let(:reminder_at) { 1.day.from_now }
+
+    it 'properly includes bookmark records' do
+      now = freeze_time '2017-03-01 12:00'
+
+      bkmk1 = manager.create(post_id: post1.id, name: name)
+      update1_at = now + 1.hours
+      bkmk1.update(name: 'great food recipe', updated_at: update1_at)
+
+      manager.create(post_id: post2.id, name: name, reminder_type: :tomorrow, reminder_at: reminder_at, options: { auto_delete_preference: Bookmark.auto_delete_preferences[:when_reminder_sent] })
+      twelve_hr_ago = freeze_time now - 12.hours
+      pending_reminder = manager.create(post_id: post3.id, name: name, reminder_type: :later_today, reminder_at: now - 8.hours)
+      freeze_time now
+
+      tau_record = message.topic_allowed_users.create!(user_id: user.id)
+      manager.create(post_id: post4.id, name: name)
+      tau_record.destroy!
+
+      BookmarkReminderNotificationHandler.send_notification(pending_reminder)
+
+      data, csv_out = make_component_csv
+
+      expect(data.length).to eq(4)
+
+      expect(data[0]['post_id']).to eq(post1.id.to_s)
+      expect(data[0]['topic_id']).to eq(topic1.id.to_s)
+      expect(data[0]['post_number']).to eq('5')
+      expect(data[0]['link']).to eq(post1.full_url)
+      expect(DateTime.parse(data[0]['updated_at'])).to eq(DateTime.parse(update1_at.to_s))
+
+      expect(data[1]['name']).to eq(name)
+      expect(data[1]['reminder_type']).to eq('tomorrow')
+      expect(DateTime.parse(data[1]['reminder_at'])).to eq(DateTime.parse(reminder_at.to_s))
+      expect(data[1]['auto_delete_preference']).to eq('when_reminder_sent')
+
+      expect(DateTime.parse(data[2]['created_at'])).to eq(DateTime.parse(twelve_hr_ago.to_s))
+      expect(DateTime.parse(data[2]['reminder_last_sent_at'])).to eq(DateTime.parse(now.to_s))
+      expect(data[2]['reminder_set_at']).to eq('')
+
+      expect(data[3]['topic_id']).to eq(message.id.to_s)
+      expect(data[3]['link']).to eq('')
+    end
+
+  end
+
   context 'category_preferences' do
     let(:component) { 'category_preferences' }
 

GitHub sha: e0a09284

This commit appears in #10591 which was approved by ZogStriP. It was merged by SamSaffron.