Re-purposing a Discourse installation for a yearly event

Another year has passed, and we followed the same procedure. This time, we used actual Ruby scripts instead of my crazy API-based approach – maybe this is useful to someone else:

Log out and deactivate (almost) all users:

protected_users = ["system", "codinghorror"] # do not process these users

# get all users that should be logged out
affected_users = User.all.select { |u| !(protected_users.include? u.username) }

affected_users.each { |u|
        u.admin = false
        u.moderator = false
        u.active = false
        u.save!
        u.user_auth_tokens.destroy_all
        u.logged_out
};

Move all topics to an archive category and tag them appropriately:

protected_topics = [] # topics that will be ignored

# create a tag by running: Tag.create(name: "archive-2016")
year_tag = 19 # annotate all topics with this tag (represents the year)

categories = { # all categories that should be processed
    1 => {                 # move everything from category 1
        "target" => 2,     # to category 2
        "tags" => [10, 11] # and add tags 10 and 11
    }
    # define additional entries as needed
}

categories.each { |id, data|
    c = Category.find(id)
    topics = c.topics.select { |t| t.id != c.topic_id } # get non-description topics

    topics.each { |t|
        if !(protected_topics.include? t.id)
        tags = t.tag_ids
        tags.push(year_tag)
        data["tags"].each { |tag| tags.push(tag) }
        t.tag_ids = tags
        t.category_id = data["target"]
        t.save!
        end
    }
}


# update all topic counts

Category.all.each { |c|
    c.topic_count = c.topics.length - 1 # -1 for about post
    c.save!
}
6 Likes