chore: rubocop

chore: fix api test conventions

chore: rubocop -A spec/

chore: more rubocop -A

fix failing test

rubocop fixes

removes helper methods that are in my opinion dead code

more rubocop fixes

rubocop -a --auto-gen-config
This commit is contained in:
Philipp Rothmann 2023-05-12 13:01:12 +02:00 committed by Philipp Rothmann
parent f6fb804bbe
commit fb2b4d8a8a
331 changed files with 4263 additions and 4507 deletions

View file

@ -20,7 +20,7 @@ RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_files.include('lib/**/*.rb')
end
APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
APP_RAKEFILE = File.expand_path('test/dummy/Rakefile', __dir__)
load 'rails/tasks/engine.rake'
Bundler::GemHelper.install_tasks
@ -34,4 +34,4 @@ Rake::TestTask.new(:test) do |t|
t.verbose = false
end
task :default => :test
task default: :test

View file

@ -4,7 +4,7 @@ class Admin::MessagegroupsController < Admin::BaseController
def index
@messagegroups = Messagegroup.order('name ASC')
# if somebody uses the search field:
@messagegroups = @messagegroups.where('name LIKE ?', "%#{params[:query]}%") unless params[:query].blank?
@messagegroups = @messagegroups.where('name LIKE ?', "%#{params[:query]}%") if params[:query].present?
@messagegroups = @messagegroups.page(params[:page]).per(@per_page)
end
@ -13,7 +13,7 @@ class Admin::MessagegroupsController < Admin::BaseController
@messagegroup = Messagegroup.find(params[:id])
@messagegroup.destroy
redirect_to admin_messagegroups_url, notice: t('admin.messagegroups.destroy.notice')
rescue => error
redirect_to admin_messagegroups_url, alert: t('admin.messagegroups.destroy.error', error: error.message)
rescue StandardError => e
redirect_to admin_messagegroups_url, alert: t('admin.messagegroups.destroy.error', error: e.message)
end
end

View file

@ -1,17 +1,17 @@
class MessagegroupsController < ApplicationController
def index
@messagegroups = Messagegroup.order("name")
@messagegroups = Messagegroup.order('name')
end
def join
@messagegroup = Messagegroup.find(params[:id])
@messagegroup.users << current_user
redirect_to messagegroups_url, :notice => I18n.t('messagegroups.join.notice')
redirect_to messagegroups_url, notice: I18n.t('messagegroups.join.notice')
end
def leave
@messagegroup = Messagegroup.find(params[:id])
@messagegroup.users.destroy(current_user)
redirect_to messagegroups_url, :notice => I18n.t('messagegroups.leave.notice')
redirect_to messagegroups_url, notice: I18n.t('messagegroups.leave.notice')
end
end

View file

@ -10,21 +10,20 @@ class MessagesController < ApplicationController
def new
@message = Message.new(params[:message])
if @message.reply_to
original_message = Message.find(@message.reply_to)
if original_message.reply_to
@message.reply_to = original_message.reply_to
end
if original_message.is_readable_for?(current_user)
@message.add_recipients [original_message.sender_id]
@message.group_id = original_message.group_id
@message.private = original_message.private
@message.subject = I18n.t('messages.model.reply_subject', :subject => original_message.subject)
@message.body = I18n.t('messages.model.reply_header', :user => original_message.sender.display, :when => I18n.l(original_message.created_at, :format => :short)) + "\n"
original_message.body.each_line { |l| @message.body += I18n.t('messages.model.reply_indent', :line => l) }
else
redirect_to new_message_url, alert: I18n.t('messages.new.error_private')
end
return unless @message.reply_to
original_message = Message.find(@message.reply_to)
@message.reply_to = original_message.reply_to if original_message.reply_to
if original_message.is_readable_for?(current_user)
@message.add_recipients [original_message.sender_id]
@message.group_id = original_message.group_id
@message.private = original_message.private
@message.subject = I18n.t('messages.model.reply_subject', subject: original_message.subject)
@message.body = I18n.t('messages.model.reply_header', user: original_message.sender.display,
when: I18n.l(original_message.created_at, format: :short)) + "\n"
original_message.body.each_line { |l| @message.body += I18n.t('messages.model.reply_indent', line: l) }
else
redirect_to new_message_url, alert: I18n.t('messages.new.error_private')
end
end
@ -33,18 +32,18 @@ class MessagesController < ApplicationController
@message = @current_user.send_messages.new(params[:message])
if @message.save
DeliverMessageJob.perform_later(@message)
redirect_to messages_url, :notice => I18n.t('messages.create.notice')
redirect_to messages_url, notice: I18n.t('messages.create.notice')
else
render :action => 'new'
render action: 'new'
end
end
# Shows a single message.
def show
@message = Message.find(params[:id])
unless @message.is_readable_for?(current_user)
redirect_to messages_url, alert: I18n.t('messages.new.error_private')
end
return if @message.is_readable_for?(current_user)
redirect_to messages_url, alert: I18n.t('messages.new.error_private')
end
def toggle_private

View file

@ -1,11 +1,11 @@
module MessagesHelper
def format_subject(message, length)
if message.subject.length > length
subject = truncate(message.subject, :length => length)
body = ""
subject = truncate(message.subject, length: length)
body = ''
else
subject = message.subject
body = truncate(message.body, :length => length - subject.length)
body = truncate(message.body, length: length - subject.length)
end
"<b>#{link_to(h(subject), message)}</b> <span style='color:grey'>#{h(body)}</span>".html_safe
end

View file

@ -1,4 +1,4 @@
require "email_reply_trimmer"
require 'email_reply_trimmer'
class MessagesMailReceiver
def self.regexp
@ -9,29 +9,25 @@ class MessagesMailReceiver
@message = Message.find_by_id(match[:message_id])
@user = User.find_by_id(match[:user_id])
raise "Message could not be found" if @message.nil?
raise "User could not be found" if @user.nil?
raise 'Message could not be found' if @message.nil?
raise 'User could not be found' if @user.nil?
hash = @message.mail_hash_for_user(@user)
raise "Hash does not match expectations" unless hash.casecmp(match[:hash]) == 0
raise 'Hash does not match expectations' unless hash.casecmp(match[:hash]) == 0
end
def received(data)
mail = Mail.new data
mail_part = get_mail_part(mail)
raise "No valid content could be found" if mail_part.nil?
raise 'No valid content could be found' if mail_part.nil?
body = mail_part.body.decoded
unless mail_part.content_type_parameters.nil?
body = body.force_encoding mail_part.content_type_parameters[:charset]
end
body = body.force_encoding mail_part.content_type_parameters[:charset] unless mail_part.content_type_parameters.nil?
if MIME::Type.simplified(mail_part.content_type) == "text/html"
body = Nokogiri::HTML(body).text
end
body = Nokogiri::HTML(body).text if MIME::Type.simplified(mail_part.content_type) == 'text/html'
body.encode!(Encoding::default_internal)
body.encode!(Encoding.default_internal)
body = EmailReplyTrimmer.trim(body)
raise BlankBodyException if body.empty?
@ -39,16 +35,16 @@ class MessagesMailReceiver
group: @message.group,
private: @message.private,
received_email: data
if @message.reply_to
message.reply_to_message = @message.reply_to_message
else
message.reply_to_message = @message
end
if mail.subject
message.subject = mail.subject.gsub("[#{FoodsoftConfig[:name]}] ", "")
else
message.subject = I18n.t('messages.model.reply_subject', subject: message.reply_to_message.subject)
end
message.reply_to_message = if @message.reply_to
@message.reply_to_message
else
@message
end
message.subject = if mail.subject
mail.subject.gsub("[#{FoodsoftConfig[:name]}] ", '')
else
I18n.t('messages.model.reply_subject', subject: message.reply_to_message.subject)
end
message.add_recipients [@message.sender_id]
message.save!
@ -64,9 +60,7 @@ class MessagesMailReceiver
for part in mail.parts
part = get_mail_part(part)
content_type = MIME::Type.simplified(part.content_type)
if content_type == "text/plain" || !mail_part && content_type == "text/html"
mail_part = part
end
mail_part = part if content_type == 'text/plain' || (!mail_part && content_type == 'text/html')
end
mail_part
end

View file

@ -1,17 +1,17 @@
require "base32"
require 'base32'
class Message < ApplicationRecord
belongs_to :sender, class_name: 'User', foreign_key: 'sender_id'
belongs_to :group, optional: true, class_name: 'Group', foreign_key: 'group_id'
belongs_to :sender, class_name: 'User'
belongs_to :group, optional: true, class_name: 'Group'
belongs_to :reply_to_message, optional: true, class_name: 'Message', foreign_key: 'reply_to'
has_many :message_recipients, dependent: :destroy
has_many :recipients, through: :message_recipients, source: :user
attr_accessor :send_method, :recipient_tokens, :order_id
scope :threads, -> { where(:reply_to => nil) }
scope :thread, ->(id) { where("id = ? OR reply_to = ?", id, id) }
scope :readable_for, ->(user) {
scope :threads, -> { where(reply_to: nil) }
scope :thread, ->(id) { where('id = ? OR reply_to = ?', id, id) }
scope :readable_for, lambda { |user|
user_id = user.try(&:id)
joins(:message_recipients)
@ -20,7 +20,7 @@ class Message < ApplicationRecord
}
validates_presence_of :message_recipients, :subject, :body
validates_length_of :subject, :in => 1..255
validates_length_of :subject, in: 1..255
after_initialize do
@recipients_ids ||= []
@ -33,7 +33,7 @@ class Message < ApplicationRecord
def create_message_recipients
user_ids = @recipients_ids
user_ids += User.undeleted.pluck(:id) if send_method == 'all'
user_ids += Group.find(group_id).users.pluck(:id) unless group_id.blank?
user_ids += Group.find(group_id).users.pluck(:id) if group_id.present?
user_ids += Order.find(order_id).users_ordered.pluck(:id) if send_method == 'order'
user_ids.uniq.each do |user_id|
@ -47,7 +47,7 @@ class Message < ApplicationRecord
end
def group_id=(group_id)
group = Group.find(group_id) unless group_id.blank?
group = Group.find(group_id) if group_id.present?
if group
@send_method = 'workgroup' if group.type == 'Workgroup'
@send_method = 'ordergroup' if group.type == 'Ordergroup'
@ -96,29 +96,29 @@ class Message < ApplicationRecord
def mail_hash_for_user(user)
digest = Digest::SHA1.new
digest.update self.id.to_s
digest.update ":"
digest.update id.to_s
digest.update ':'
digest.update salt
digest.update ":"
digest.update ':'
digest.update user.id.to_s
Base32.encode digest.digest
end
# Returns true if this message is a system message, i.e. was sent automatically by Foodsoft itself.
def system_message?
self.sender_id.nil?
sender_id.nil?
end
def sender_name
system_message? ? I18n.t('layouts.foodsoft') : sender.display rescue "?"
system_message? ? I18n.t('layouts.foodsoft') : sender.display
rescue StandardError
'?'
end
def recipients_ids
@recipients_ids
end
attr_reader :recipients_ids
def last_reply
Message.where(reply_to: self.id).order(:created_at).last
Message.where(reply_to: id).order(:created_at).last
end
def is_readable_for?(user)
@ -135,6 +135,6 @@ class Message < ApplicationRecord
private
def create_salt
self.salt = [Array.new(6) { rand(256).chr }.join].pack("m").chomp
self.salt = [Array.new(6) { rand(256).chr }.join].pack('m').chomp
end
end

View file

@ -2,5 +2,5 @@ class MessageRecipient < ActiveRecord::Base
belongs_to :message
belongs_to :user
enum email_state: [:pending, :sent, :skipped]
enum email_state: %i[pending sent skipped]
end

View file

@ -1,5 +1,3 @@
class Messagegroup < Group
validates_uniqueness_of :name
protected
end

View file

@ -1,13 +1,13 @@
Rails.application.routes.draw do
scope '/:foodcoop' do
resources :messages, :only => [:index, :show, :new, :create] do
resources :messages, only: %i[index show new create] do
member do
get :thread
post :toggle_private
end
end
resources :message_threads, :only => [:index, :show]
resources :message_threads, only: %i[index show]
resources :messagegroups, only: [:index] do
member do

View file

@ -1,6 +1,6 @@
class AddEmailToMessage < ActiveRecord::Migration[4.2]
def change
add_column :messages, :salt, :string
add_column :messages, :received_email, :binary, :limit => 1.megabyte
add_column :messages, :received_email, :binary, limit: 1.megabyte
end
end

View file

@ -1,25 +1,26 @@
$:.push File.expand_path("../lib", __FILE__)
$:.push File.expand_path('lib', __dir__)
# Maintain your gem's version:
require "foodsoft_messages/version"
require 'foodsoft_messages/version'
# Describe your gem and declare its dependencies:
Gem::Specification.new do |s|
s.name = "foodsoft_messages"
s.name = 'foodsoft_messages'
s.version = FoodsoftMessages::VERSION
s.authors = ["robwa"]
s.email = ["foodsoft-messages@ini.tiative.net"]
s.homepage = "https://github.com/foodcoops/foodsoft"
s.summary = "Messaging plugin for foodsoft."
s.description = "Adds the ability to exchange messages to foodsoft."
s.authors = ['robwa']
s.email = ['foodsoft-messages@ini.tiative.net']
s.homepage = 'https://github.com/foodcoops/foodsoft'
s.summary = 'Messaging plugin for foodsoft.'
s.description = 'Adds the ability to exchange messages to foodsoft.'
s.files = Dir["{app,config,db,lib}/**/*"] + ["Rakefile", "README.md"]
s.files = Dir['{app,config,db,lib}/**/*'] + ['Rakefile', 'README.md']
s.add_dependency "rails"
s.add_dependency "base32"
s.add_dependency "deface", "~> 1.0"
s.add_dependency "email_reply_trimmer"
s.add_dependency "mail"
s.add_dependency 'rails'
s.add_dependency 'base32'
s.add_dependency 'deface', '~> 1.0'
s.add_dependency 'email_reply_trimmer'
s.add_dependency 'mail'
s.add_development_dependency "sqlite3"
s.add_development_dependency 'sqlite3'
s.metadata['rubygems_mfa_required'] = 'true'
end

View file

@ -1,7 +1,7 @@
require "foodsoft_messages/engine"
require "foodsoft_messages/mail_receiver"
require "foodsoft_messages/user_link"
require "deface"
require 'foodsoft_messages/engine'
require 'foodsoft_messages/mail_receiver'
require 'foodsoft_messages/user_link'
require 'deface'
module FoodsoftMessages
# Return whether messages are used or not.

View file

@ -12,15 +12,16 @@ module FoodsoftMessages
sub_nav.items.insert(i, sub_nav.items.delete_at(-1))
end
end
unless primary[:admin].nil?
sub_nav = primary[:admin].sub_navigation
sub_nav.items <<
SimpleNavigation::Item.new(primary, :messagegroups, I18n.t('navigation.admin.messagegroups'), context.admin_messagegroups_path)
# move to right before config item
if i = sub_nav.items.index(sub_nav[:config])
sub_nav.items.insert(i, sub_nav.items.delete_at(-1))
end
end
return if primary[:admin].nil?
sub_nav = primary[:admin].sub_navigation
sub_nav.items <<
SimpleNavigation::Item.new(primary, :messagegroups, I18n.t('navigation.admin.messagegroups'),
context.admin_messagegroups_path)
# move to right before config item
return unless i = sub_nav.items.index(sub_nav[:config])
sub_nav.items.insert(i, sub_nav.items.delete_at(-1))
end
def default_foodsoft_config(cfg)

View file

@ -8,7 +8,7 @@ module FoodsoftMessages
show_user user
else
link_to show_user(user), new_message_path('message[mail_to]' => user.id),
:title => I18n.t('helpers.messages.write_message')
title: I18n.t('helpers.messages.write_message')
end
end
end
@ -18,5 +18,5 @@ end
# modify existing helper
ActiveSupport.on_load(:after_initialize) do
ApplicationHelper.send :include, FoodsoftMessages::UserLink
ApplicationHelper.include FoodsoftMessages::UserLink
end

View file

@ -1,3 +1,3 @@
module FoodsoftMessages
VERSION = "0.0.1"
VERSION = '0.0.1'
end