forked from forem/forem
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add EditorConfig file for all editors * Add article export service * Add setting to request an export of all articles * On the outside they are called posts * Add articles exported email to mailer and service * Refactor to one public method and test flags * Trigger the export if it was requested and send the email * Recreated migration file with generic export fields * Rename fields and switch to a whitelist * Refactor ArticleExportService into a more generic structure * Rename articles_exported_email to export_email * Fix notify mailer spec * Rename Exporter::Exporter to Exporter::Service * Remove commented out line * Removed body_html, coordinates and updated_at from export * Invert DJ config * Update spec
- Loading branch information
1 parent
86c6030
commit 915e2d7
Showing
17 changed files
with
524 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# editorconfig.org | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
indent_size = 2 | ||
indent_style = space | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,4 +94,11 @@ def mentor_email(mentor, mentee) | |
subject = "You have been matched with a new DEV mentee!" | ||
mail(to: @mentor.email, subject: subject, from: "Liana (from dev.to) <[email protected]>") | ||
end | ||
|
||
def export_email(user, attachment) | ||
@user = user | ||
export_filename = "devto-export-#{Date.current.iso8601}.zip" | ||
attachments[export_filename] = attachment | ||
mail(to: @user.email, subject: "The export of your data is ready") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
module Exporter | ||
class Articles | ||
attr_reader :name | ||
attr_reader :user | ||
|
||
def initialize(user) | ||
@name = :articles | ||
@user = user | ||
end | ||
|
||
def export(slug: nil) | ||
articles = user.articles | ||
articles = articles.where(slug: slug) if slug.present? | ||
json_articles = jsonify_articles(articles) | ||
|
||
{ "articles.json" => json_articles } | ||
end | ||
|
||
private | ||
|
||
def whitelisted_attributes | ||
%i[ | ||
body_markdown | ||
cached_tag_list | ||
cached_user_name | ||
cached_user_username | ||
canonical_url | ||
comments_count | ||
created_at | ||
crossposted_at | ||
description | ||
edited_at | ||
feed_source_url | ||
language | ||
last_comment_at | ||
main_image | ||
main_image_background_hex_color | ||
path | ||
positive_reactions_count | ||
processed_html | ||
published | ||
published_at | ||
published_from_feed | ||
reactions_count | ||
show_comments | ||
slug | ||
social_image | ||
title | ||
video | ||
video_closed_caption_track_url | ||
video_code | ||
video_source_url | ||
video_thumbnail_url | ||
] | ||
end | ||
|
||
def jsonify_articles(articles) | ||
articles_to_jsonify = [] | ||
# load articles in batches, we don't want to hog the DB | ||
# if a user has lots and lots of articles | ||
articles.find_each do |article| | ||
articles_to_jsonify << article | ||
end | ||
articles_to_jsonify.to_json(only: whitelisted_attributes) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
require "zip" | ||
|
||
module Exporter | ||
class Service | ||
attr_reader :user | ||
|
||
EXPORTERS = [ | ||
Articles, | ||
].freeze | ||
|
||
def initialize(user) | ||
@user = user | ||
end | ||
|
||
def export(send_email: false, config: {}) | ||
exports = {} | ||
|
||
# export content with filenames | ||
EXPORTERS.each do |exporter| | ||
files = exporter.new(user).export(config.fetch(exporter.name.to_sym, {})) | ||
files.each do |name, content| | ||
exports[name] = content | ||
end | ||
end | ||
|
||
zipped_exports = zip_exports(exports) | ||
|
||
send_exports_by_email(zipped_exports) if send_email | ||
|
||
update_user_export_fields | ||
|
||
zipped_exports.rewind | ||
zipped_exports | ||
end | ||
|
||
private | ||
|
||
def zip_exports(exports) | ||
buffer = StringIO.new | ||
Zip::OutputStream.write_buffer(buffer) do |stream| | ||
exports.each do |name, content| | ||
stream.put_next_entry( | ||
name, | ||
nil, # comment | ||
nil, # extra | ||
Zip::Entry::DEFLATED, | ||
Zlib::BEST_COMPRESSION, | ||
) | ||
stream.write content | ||
end | ||
end | ||
buffer | ||
end | ||
|
||
def send_exports_by_email(zipped_exports) | ||
zipped_exports.rewind | ||
NotifyMailer.export_email(user, zipped_exports.read).deliver | ||
end | ||
|
||
def update_user_export_fields | ||
user.update!(export_requested: false, exported_at: Time.current) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<h3 style="font-weight:300">Your data has been exported.</h3> | ||
<p> | ||
Please check the attached file. | ||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Your data has been exported. | ||
|
||
Please check the attached file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class AddExportFieldsToUsers < ActiveRecord::Migration[5.1] | ||
def change | ||
add_column :users, :export_requested, :boolean, default: false | ||
add_column :users, :exported_at, :datetime | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,6 +120,7 @@ def params(user_email, feedback_message_id) | |
mentee_email = described_class.mentee_email(mentee, mentor) | ||
expect(mentee_email.subject).to eq "You have been matched with a DEV mentor!" | ||
end | ||
|
||
it "renders proper from" do | ||
mentee_email = described_class.mentee_email(mentee, mentor) | ||
expect(mentee_email.from).to include "[email protected]" | ||
|
@@ -134,10 +135,34 @@ def params(user_email, feedback_message_id) | |
mentor_email = described_class.mentor_email(mentor, mentee) | ||
expect(mentor_email.subject).to eq "You have been matched with a new DEV mentee!" | ||
end | ||
it "renders proper subject" do | ||
|
||
it "renders proper from" do | ||
mentor_email = described_class.mentor_email(mentor, mentee) | ||
expect(mentor_email.from).to include "[email protected]" | ||
end | ||
end | ||
|
||
describe "#export_email" do | ||
it "renders proper subject" do | ||
export_email = described_class.export_email(user, "attachment") | ||
expect(export_email.subject).to include("export of your data is ready") | ||
end | ||
|
||
it "renders proper receiver" do | ||
export_email = described_class.export_email(user, "attachment") | ||
expect(export_email.to).to eq([user.email]) | ||
end | ||
|
||
it "attaches a zip file" do | ||
export_email = described_class.export_email(user, "attachment") | ||
expect(export_email.attachments[0].content_type).to include("application/zip") | ||
end | ||
|
||
it "adds the correct filename" do | ||
export_email = described_class.export_email(user, "attachment") | ||
expected_filename = "devto-export-#{Date.current.iso8601}.zip" | ||
expect(export_email.attachments[0].filename).to eq(expected_filename) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.