mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
Feature: Mass award badge (#8694)
* UI: Mass grant a badge from the admin ui * Send the uploaded CSV and badge ID to the backend * Read the CSV and grant badge in batches * UX: Communicate the result to the user * Don't award if badge is disabled * Create a 'send_notification' method to remove duplicated code, slightly shrink badge image. Replace router transition with href. * Dynamically discover current route
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'csv'
|
||||
|
||||
class Admin::BadgesController < Admin::AdminController
|
||||
|
||||
def index
|
||||
@@ -33,6 +35,38 @@ class Admin::BadgesController < Admin::AdminController
|
||||
def show
|
||||
end
|
||||
|
||||
def award
|
||||
end
|
||||
|
||||
def mass_award
|
||||
csv_file = params.permit(:file).fetch(:file, nil)
|
||||
badge = Badge.find_by(id: params[:badge_id])
|
||||
raise Discourse::InvalidParameters if csv_file.try(:tempfile).nil? || badge.nil?
|
||||
|
||||
batch_number = 1
|
||||
batch = []
|
||||
|
||||
File.open(csv_file) do |csv|
|
||||
csv.each_line do |email_line|
|
||||
batch.concat CSV.parse_line(email_line)
|
||||
|
||||
# Split the emails in batches of 200 elements.
|
||||
full_batch = csv.lineno % (BadgeGranter::MAX_ITEMS_FOR_DELTA * batch_number) == 0
|
||||
last_batch_item = full_batch || csv.eof?
|
||||
|
||||
if last_batch_item
|
||||
Jobs.enqueue(:mass_award_badge, user_emails: batch, badge_id: badge.id)
|
||||
batch = []
|
||||
batch_number += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
head :ok
|
||||
rescue CSV::MalformedCSVError
|
||||
raise Discourse::InvalidParameters
|
||||
end
|
||||
|
||||
def badge_types
|
||||
badge_types = BadgeType.all.to_a
|
||||
render_serialized(badge_types, BadgeTypeSerializer, root: "badge_types")
|
||||
|
Reference in New Issue
Block a user