Skip to content

Commit

Permalink
add functionality for exporting user data in json format lobsters#1299
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelyao107 committed Dec 4, 2024
1 parent 06e510f commit 3e899a6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
21 changes: 21 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,25 @@ def show
end
end

def export_data


@user = User.includes(comments: :story, stories: []).find_by(username: params[:id])
@upvoted_comments = @user.votes.where(vote: 1).where.not(comment_id: nil).includes(comment: :story).map(&:comment)
Rails.logger.debug("Current User: #{@user.inspect}")
data = {
comments: @user.comments,
stories: @user.stories,
upvoted_comments: @upvoted_comments,
upvoted_stories: @user.upvoted_stories,

}



send_data data.to_json, filename: "user_data_#{@user.id}.json", type: 'application/json'
end

def tree
@title = "Users"
newest_user = User.last.id
Expand Down Expand Up @@ -178,4 +197,6 @@ def only_user_or_moderator
redirect_to(user_path(params[:username]))
end
end


end
5 changes: 5 additions & 0 deletions app/views/settings/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -339,4 +339,9 @@
<%= f.submit "Yes, Delete My Account", :class => "deletion" %>
<% end %>
</div>

<h2>Data Export</h2>
<div class="boxline">
<%= button_to 'Export My Data', export_data_user_path(@edit_user), method: :get %>
</div>
</div>
12 changes: 12 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,16 @@
get "/stats" => "stats#index"

post "/csp-violation-report" => "csp#violation_report"

resources :users do
member do
get :export_data # Use GET method for direct download
end
end

resources :users do
member do
get 'send_export_email'
end
end
end

0 comments on commit 3e899a6

Please sign in to comment.