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

[WIP] fix rubocop offenses in app/controllers #6417 #6485

Closed
wants to merge 23 commits into from
Closed
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
52 changes: 27 additions & 25 deletions app/controllers/admin_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class AdminController < ApplicationController
before_action :require_user, only: %i(spam spam_revisions mark_comment_spam publish_comment spam_comments)

Expand All @@ -21,7 +23,7 @@ def promote_admin
def promote_moderator
@user = User.find params[:id]
unless @user.nil?
if logged_in_as(['admin', 'moderator'])
if logged_in_as(%w(admin moderator))
@user.role = 'moderator'
@user.save
flash[:notice] = "User '<a href='/profile/" + @user.username + "'>" + @user.username + "</a>' is now a moderator."
Expand All @@ -35,7 +37,7 @@ def promote_moderator
def demote_basic
@user = User.find params[:id]
unless @user.nil?
if logged_in_as(['admin', 'moderator'])
if logged_in_as(%w(admin moderator))
@user.role = 'basic'
@user.save
flash[:notice] = "User '<a href='/profile/" + @user.username + "'>" + @user.username + "</a>' is no longer a moderator."
Expand All @@ -56,17 +58,17 @@ def reset_user_password
PasswordResetMailer.reset_notify(user, key).deliver_later unless user.nil? # respond the same to both successes and failures; security
end
flash[:notice] = "#{user.name} should receive an email with instructions on how to reset their password. If they do not, please double check that they are using the email they registered with."
redirect_to URI.parse("/profile/" + user.name).path
redirect_to URI.parse('/profile/' + user.name).path
end
end

def useremail
if logged_in_as(['admin', 'moderator'])
if logged_in_as(%w(admin moderator))
if params[:address]
# address was submitted. find the username(s) and return.
@address = params[:address]
@users = User.where(email: params[:address])
.where(status: [1, 4])
.where(status: [1, 4])
end
else
# unauthorized. instead of return ugly 403, just send somewhere else
Expand All @@ -75,7 +77,7 @@ def useremail
end

def spam
if logged_in_as(['admin', 'moderator'])
if logged_in_as(%w(admin moderator))
@nodes = Node.paginate(page: params[:page])
.order('nid DESC')
@nodes = if params[:type] == 'wiki'
Expand All @@ -90,7 +92,7 @@ def spam
end

def spam_revisions
if logged_in_as(['admin', 'moderator'])
if logged_in_as(%w(admin moderator))
@revisions = Revision.paginate(page: params[:page])
.order('timestamp DESC')
.where(status: 0)
Expand All @@ -104,8 +106,8 @@ def spam_revisions
def spam_comments
if current_user &. can_moderate?
@comments = Comment.paginate(page: params[:page])
.order('timestamp DESC')
.where(status: 0)
.order('timestamp DESC')
.where(status: 0)
render template: 'admin/spam'
else
flash[:error] = 'Only moderators can moderate comments.'
Expand All @@ -115,7 +117,7 @@ def spam_comments

def mark_spam
@node = Node.find params[:id]
if logged_in_as(['admin', 'moderator'])
if logged_in_as(%w(admin moderator))
if @node.status == 1 || @node.status == 4
@node.spam
@node.author.ban
Expand All @@ -138,15 +140,15 @@ def mark_spam

def mark_comment_spam
@comment = Comment.find params[:id]
if logged_in_as(['admin', 'moderator'])
if logged_in_as(%w(admin moderator))
if @comment.status == 1 || @comment.status == 4
@comment.spam
user = @comment.author
user.ban
AdminMailer.notify_moderators_of_comment_spam(@comment, current_user).deliver_later
flash[:notice] = "Comment has been marked as spam and comment author has been banned. You can undo this on the <a href='/spam/comments'>spam moderation page</a>."
else
flash[:notice] = "Comment already marked as spam."
flash[:notice] = 'Comment already marked as spam.'
end
else
flash[:error] = 'Only moderators can moderate comments.'
Expand All @@ -155,7 +157,7 @@ def mark_comment_spam
end

def publish_comment
if logged_in_as(['admin', 'moderator'])
if logged_in_as(%w(admin moderator))
@comment = Comment.find params[:id]
if @comment.status == 1
flash[:notice] = 'Comment already published.'
Expand All @@ -181,7 +183,7 @@ def publish_comment
end

def publish
if logged_in_as(['admin', 'moderator'])
if logged_in_as(%w(admin moderator))
@node = Node.find params[:id]
if @node.status == 1
flash[:notice] = 'Item already published.'
Expand Down Expand Up @@ -223,7 +225,7 @@ def mark_spam_revision
return
end

if logged_in_as(['admin', 'moderator'])
if logged_in_as(%w(admin moderator))
if @revision.status == 1
@revision.spam
@revision.author.ban
Expand All @@ -244,7 +246,7 @@ def mark_spam_revision
end

def publish_revision
if logged_in_as(['admin', 'moderator'])
if logged_in_as(%w(admin moderator))
@revision = Revision.find params[:vid]
@revision.publish
@revision.author.unban
Expand All @@ -262,7 +264,7 @@ def publish_revision

def moderate
user = User.find params[:id]
if logged_in_as(['admin', 'moderator'])
if logged_in_as(%w(admin moderator))
user.moderate
flash[:notice] = 'The user has been moderated.'
else
Expand All @@ -273,7 +275,7 @@ def moderate

def unmoderate
user = User.find params[:id]
if logged_in_as(['admin', 'moderator'])
if logged_in_as(%w(admin moderator))
user.unmoderate
flash[:notice] = 'The user has been unmoderated.'
else
Expand All @@ -284,7 +286,7 @@ def unmoderate

def ban
user = User.find params[:id]
if logged_in_as(['admin', 'moderator'])
if logged_in_as(%w(admin moderator))
user.ban
flash[:notice] = 'The user has been banned.'
else
Expand All @@ -295,7 +297,7 @@ def ban

def unban
user = User.find params[:id]
if logged_in_as(['admin', 'moderator'])
if logged_in_as(%w(admin moderator))
user.unban
flash[:notice] = 'The user has been unbanned.'
else
Expand All @@ -305,7 +307,7 @@ def unban
end

def users
if logged_in_as(['admin', 'moderator'])
if logged_in_as(%w(admin moderator))
@users = User.order('uid DESC').limit(200)
else
flash[:error] = 'Only moderators can moderate other users.'
Expand All @@ -314,7 +316,7 @@ def users
end

def batch
if logged_in_as(['admin', 'moderator'])
if logged_in_as(%w(admin moderator))
nodes = 0
users = []
params[:ids].split(',').uniq.each do |nid|
Expand Down Expand Up @@ -352,7 +354,7 @@ def migrate
end

def queue
if logged_in_as(['admin', 'moderator'])
if logged_in_as(%w(admin moderator))
@notes = Node.where(status: 4)
.paginate(page: params[:page])
flash[:warning] = "These are notes requiring moderation. <a href='/wiki/moderation'>Community moderators</a> may approve or reject them."
Expand All @@ -376,10 +378,10 @@ def smtp_test
s.print "RCPT TO: <example@publiclab.org>\n"
end
if line.include? '250 Accepted'
render plain: "Email gateway OK"
render plain: 'Email gateway OK'
s.close_write
elsif line.include? '550'
render plain: "Email gateway NOT OK"
render plain: 'Email gateway NOT OK'
render status: 500
s.close_write
end
Expand Down
28 changes: 15 additions & 13 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

include ActionView::Helpers::DateHelper # required for time_ago_in_words()
class ApplicationController < ActionController::Base
protect_from_forgery
Expand Down Expand Up @@ -35,15 +37,15 @@ def set_sidebar(type = :generic, data = :all, args = {})
hidden_nids = Node.where(type: :note, status: 1).select { |n| n.has_a_tag('hidden:response') }.collect(&:nid)
@notes = if params[:controller] == 'questions'
Node.questions
.joins(:revision)
.joins(:revision)
else
Node.research_notes.joins(:revision).order('node.nid DESC').paginate(page: params[:page])
end

@notes = @notes.where('node.nid != (?)', @node.nid) if @node
@notes = @notes.where('node_revisions.status = 1 AND node.nid NOT IN (?)', hidden_nids) unless hidden_nids.empty?

@notes = if logged_in_as(['admin', 'moderator'])
@notes = if logged_in_as(%w(admin moderator))
@notes.where('(node.status = 1 OR node.status = 4)')
elsif current_user
@notes.where('(node.status = 1 OR (node.status = 4 AND node.uid = ?))', current_user.uid)
Expand All @@ -52,11 +54,11 @@ def set_sidebar(type = :generic, data = :all, args = {})
end

@wikis = Node.order('changed DESC')
.joins(:revision)
.where('node_revisions.status = 1 AND node.status = 1 AND type = "page"')
.limit(10)
.group('node_revisions.nid')
.order('node_revisions.timestamp DESC')
.joins(:revision)
.where('node_revisions.status = 1 AND node.status = 1 AND type = "page"')
.limit(10)
.group('node_revisions.nid')
.order('node_revisions.timestamp DESC')
end
end

Expand Down Expand Up @@ -94,9 +96,9 @@ def current_user

end

cookies.signed["user_token"] = nil
cookies.signed['user_token'] = nil
if @current_user
cookies.signed["user_token"] = @current_user.persistence_token
cookies.signed['user_token'] = @current_user.persistence_token
end
@current_user
end
Expand All @@ -108,7 +110,7 @@ def require_user
redirect_to login_url
false
end
return current_user
current_user
end

def require_no_user
Expand Down Expand Up @@ -142,18 +144,18 @@ def redirect_to_node_path?(node)
end

def alert_and_redirect_moderated
if @node.author.status == User::Status::BANNED && !(logged_in_as(['admin', 'moderator']))
if @node.author.status == User::Status::BANNED && !logged_in_as(%w(admin moderator))
flash[:error] = I18n.t('application_controller.author_has_been_banned')
redirect_to '/'
elsif @node.status == 4 && (logged_in_as(['admin', 'moderator']))
elsif @node.status == 4 && logged_in_as(%w(admin moderator))
flash.now[:warning] = "First-time poster <a href='/profile/#{@node.author.name}'>#{@node.author.name}</a> submitted this #{time_ago_in_words(@node.created_at)} ago and it has not yet been approved by a moderator. <a class='btn btn-default btn-sm' href='/moderate/publish/#{@node.id}'>Approve</a> <a class='btn btn-default btn-sm' href='/moderate/spam/#{@node.id}'>Spam</a>"
elsif @node.status == 4 && current_user&.id == @node.author.id && !flash[:first_time_post]
flash.now[:warning] = "Thank you for contributing open research, and thanks for your patience while your post is approved by <a href='/wiki/moderation'>community moderators</a> and we'll email you when it is published. In the meantime, if you have more to contribute, feel free to do so."
elsif @node.status == 3 && (current_user&.is_coauthor?(@node) || current_user&.can_moderate?) && !flash[:first_time_post]
flash.now[:warning] = "This is a draft note. Once you're ready, click <a class='btn btn-success btn-xs' href='/notes/publish_draft/#{@node.id}'>Publish Draft</a> to make it public. You can share it with collaborators using this private link <a href='#{@node.draft_url(request.base_url)}'>#{@node.draft_url(request.base_url)}</a>"
elsif @node.status == 3 && (params[:token].nil? || (params[:token].present? && @node.slug.split('token:').last != params[:token]))
page_not_found
elsif @node.status != 1 && @node.status != 3 && !(logged_in_as(['admin', 'moderator']))
elsif @node.status != 1 && @node.status != 3 && !logged_in_as(%w(admin moderator))
# if it's spam or a draft
# no notification; don't let people easily fish for existing draft titles; we should try to 404 it
redirect_to '/'
Expand Down
18 changes: 10 additions & 8 deletions app/controllers/comment_controller.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# frozen_string_literal: true

class CommentController < ApplicationController
include CommentHelper
respond_to :html, :xml, :json
before_action :require_user, only: %i(create update delete)

def index
comments = Comment.joins(:node, :user)
.order('timestamp DESC')
.where('node.status = ?', 1)
.paginate(page: params[:page], per_page: 30)
.order('timestamp DESC')
.where('node.status = ?', 1)
.paginate(page: params[:page], per_page: 30)

@normal_comments = comments.where('comments.status = 1')
if logged_in_as(%w(admin moderator))
Expand Down Expand Up @@ -44,7 +46,7 @@ def create
"<a href='/subscribe/tag/#{tagname}'>#{tagname}</a>"
end
tagnames = tagnames.join(', ')
tagnames = " Click to subscribe to updates on these tags or topics: " + tagnames unless tagnames.empty?
tagnames = ' Click to subscribe to updates on these tags or topics: ' + tagnames unless tagnames.empty?
flash[:notice] = "Comment posted.#{tagnames}"
redirect_to @node.path + '#last' # to last comment
end
Expand All @@ -60,7 +62,7 @@ def create_by_token
@node = Node.find params[:id]
@user = User.find_by(username: params[:username])
@body = params[:body]
@token = request.headers["HTTP_TOKEN"]
@token = request.headers['HTTP_TOKEN']

if @user && @user.token == @token
begin
Expand Down Expand Up @@ -139,9 +141,9 @@ def delete
end

def like_comment
@comment_id = params["comment_id"].to_i
@user_id = params["user_id"].to_i
@emoji_type = params["emoji_type"]
@comment_id = params['comment_id'].to_i
@user_id = params['user_id'].to_i
@emoji_type = params['emoji_type']
comment = Comment.where(cid: @comment_id).first
like = comment.likes.where(user_id: @user_id, emoji_type: @emoji_type)
@is_liked = like.count.positive?
Expand Down
11 changes: 7 additions & 4 deletions app/controllers/csvfiles_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class CsvfilesController < ApplicationController
before_action :require_user, only: %i(delete user_files)

Expand All @@ -7,7 +9,7 @@ def setter
filetitle: params[:filetitle],
filedescription: params[:filedescription],
filepath: params[:object],
filename: "file" + Time.now.to_i.to_s,
filename: 'file' + Time.now.to_i.to_s,
filestring: params[:filestring]
)
render json: @csvfile if @csvfile.save
Expand All @@ -28,7 +30,7 @@ def add_graphobject
filetitle: params[:filetitle],
filedescription: params[:filedescription],
filepath: params[:object],
filename: "file" + Time.now.to_i.to_s,
filename: 'file' + Time.now.to_i.to_s,
filestring: params[:filestring],
graphobject: params[:graphobject]
)
Expand All @@ -38,11 +40,12 @@ def add_graphobject

def delete
return unless params[:id] && params[:uid].to_i == current_user.uid

file = Csvfile.where(id: params[:id].to_i)
if file.destroy(params[:id].to_i)
flash[:notice] = "Deleted the file"
flash[:notice] = 'Deleted the file'
else
flash[:error] = "Could not delete the file"
flash[:error] = 'Could not delete the file'
end
redirect_to "simple-data-grapher/data/#{params[:uid]}"
end
Expand Down
Loading