Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 🎸 methods and db changes to publish single map and batch-publish multiple maps (no interface yet) #1772

Merged
merged 4 commits into from
Jul 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def logged_in?

def logged_in_as(roles, action)
unless current_user && roles.any? { |role| current_user.role == role }
flash[:error] = "Only #{roles.collect { |role| role.pluralize }.join(' and ')} can #{action}."
flash[:error] = "Only #{roles.collect(&:pluralize).join(" and ")} can #{action}."
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ooh cool!

redirect_to('/' + '?_=' + Time.now.to_i.to_s)
end
end
Expand Down
64 changes: 49 additions & 15 deletions app/controllers/spam_controller.rb
Original file line number Diff line number Diff line change
@@ -1,46 +1,80 @@
class SpamController < ApplicationController
module ModerationGuards
def spam_check(map)
#check and spam only unspammed maps
def check_and_spam(map)
# check and spam only unspammed maps
map.spam unless map.status == Map::Status::BANNED
end
def ban_check(map)
#check and ban only unbanned non-anonymous authors

def check_and_ban(map)
# check and ban only unbanned non-anonymous authors
map.user.ban unless map.anonymous? || map.user.status == User::Status::BANNED
end

def check_and_publish(map)
# check and publish only spammed or moderated maps
map.publish unless map.status == Map::Status::NORMAL
end

def check_and_unban(map)
# check and unban only banned non-anonymous authors
map.user.unban unless map.anonymous? || map.user.status != User::Status::BANNED
end
end

include ModerationGuards

require 'set'

before_action :require_login
before_action { logged_in_as(['admin', 'moderator'], 'moderate maps and users') }

def spam_map
@map = Map.find(params[:id])
if spam_check(@map)
if check_and_spam(@map)
notice_text = 'Map marked as spam.'
notice_text.chop! << ' and author banned.' if ban_check(@map)
notice_text.chop! << ' and author banned.' if check_and_ban(@map)
else
notice_text = 'Map already marked as spam.'
end
flash[:notice] = notice_text
redirect_back(fallback_location: root_path)
end

def batch_spam_map
def batch_spam_maps
PeculiarE marked this conversation as resolved.
Show resolved Hide resolved
spammed_maps = 0
banned_authors = Set.new
banned_authors = 0
params[:ids].split(',').uniq.each do |id|
map = Map.find(id)
if spam_check(map)
if check_and_spam(map)
spammed_maps += 1
banned_authors << map.user.id if ban_check(map)
banned_authors += 1 if check_and_ban(map)
end
end
flash[:notice] = helpers.pluralize(spammed_maps, 'map') + ' spammed and ' + helpers.pluralize(banned_authors, 'author') + ' banned.'
redirect_back(fallback_location: root_path)
end

def publish_map
PeculiarE marked this conversation as resolved.
Show resolved Hide resolved
@map = Map.find(params[:id])
if check_and_publish(@map)
notice_text = 'Map published.'
notice_text.chop! << ' and author unbanned.' if check_and_unban(@map)
else
notice_text = 'Map already published.'
end
flash[:notice] = notice_text
redirect_back(fallback_location: root_path)
end

def batch_publish_maps
PeculiarE marked this conversation as resolved.
Show resolved Hide resolved
published_maps = 0
unbanned_authors = 0
params[:ids].split(',').uniq.each do |id|
map = Map.find(id)
if check_and_publish(map)
published_maps += 1
unbanned_authors += 1 if check_and_unban(map)
end
end
flash[:notice] = helpers.pluralize(spammed_maps, 'map') + ' spammed and ' + helpers.pluralize(banned_authors.size, 'author') + ' banned.'
flash[:notice] = helpers.pluralize(published_maps, 'map') + ' published and ' + helpers.pluralize(unbanned_authors, 'author') + ' unbanned.'
redirect_back(fallback_location: root_path)
end
end
4 changes: 4 additions & 0 deletions app/models/map.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,8 @@ def authors
def spam
update!(status: Status::BANNED)
end

def publish
update!(status: Status::NORMAL)
end
end
6 changes: 5 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ def can_edit?(resource)
end

def ban
update!(status: Status::BANNED)
update!({ status: Status::BANNED, status_updated_at: Time.now })
end

def unban
update!({ status: Status::NORMAL, status_updated_at: Time.now })
end
end
11 changes: 9 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,15 @@

get '/warps/:map/:file(.:format)', to: redirect('https://archive.publiclab.org/warps/%{map}/%{file}.%{format}')

patch 'moderate/spam_map/:id' => 'spam#spam_map', as: 'spam_map'
patch 'moderate/batch_spam_map/:ids' => 'spam#batch_spam_map', as: 'batch_spam_map'
scope 'moderate', module: 'spam' do
%w(spam_map publish_map).each do |action|
patch action + '/:id', action: action, as: action
end

%w(batch_spam_maps batch_publish_maps).each do |action|
patch action + '/:ids', action: action, as: action
end
end

# See how all your routes lay out with 'rails routes'

Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20220628190519_add_status_update_time_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddStatusUpdateTimeToUsers < ActiveRecord::Migration[5.2]
def change
add_column :users, :status_updated_at, :datetime, null: true
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2022_06_19_221926) do
ActiveRecord::Schema.define(version: 2022_06_28_190519) do

create_table "annotations", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.integer "map_id"
Expand Down Expand Up @@ -124,6 +124,7 @@
t.string "remember_token", limit: 40
t.datetime "remember_token_expires_at"
t.integer "status", default: 1, null: false
t.datetime "status_updated_at"
t.index ["login"], name: "index_users_on_login", unique: true
end

Expand Down
Loading