summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Legler <alex@a3li.li>2015-12-13 13:29:59 +0100
committerAlex Legler <alex@a3li.li>2015-12-13 13:29:59 +0100
commit1f4743f2ae61a7f443980359303d6d631b2426fc (patch)
treedb1a8aafc861eb8fcbf15993090784fa86a55eea
parentUse protocol-relative URI for the favicon (diff)
downloadfrontend-1f4743f2ae61a7f443980359303d6d631b2426fc.tar.gz
frontend-1f4743f2ae61a7f443980359303d6d631b2426fc.tar.bz2
frontend-1f4743f2ae61a7f443980359303d6d631b2426fc.zip
Style fixes
-rw-r--r--.rubocop.yml23
-rw-r--r--Gemfile2
-rw-r--r--ag-web.rb41
-rw-r--r--lib/cache.rb17
-rw-r--r--lib/helpers.rb28
-rw-r--r--lib/index.rb14
6 files changed, 81 insertions, 44 deletions
diff --git a/.rubocop.yml b/.rubocop.yml
new file mode 100644
index 0000000..e2f07ba
--- /dev/null
+++ b/.rubocop.yml
@@ -0,0 +1,23 @@
+Style/FormatString:
+ Enabled: false
+
+Style/FileName:
+ Enabled: false
+
+Style/GlobalVars:
+ Enabled: false
+
+Style/PerlBackrefs:
+ Enabled: false
+
+Metrics/LineLength:
+ Max: 120
+
+Metrics/MethodLength:
+ Max: 20
+
+Metrics/ModuleLength:
+ Max: 200
+
+Lint/RescueException:
+ Enabled: false
diff --git a/Gemfile b/Gemfile
index 8774b92..a1fc520 100644
--- a/Gemfile
+++ b/Gemfile
@@ -7,4 +7,4 @@ gem 'redcarpet'
gem 'thin'
gem 'elasticsearch'
gem 'rinku'
-gem 'pony' \ No newline at end of file
+gem 'pony'
diff --git a/ag-web.rb b/ag-web.rb
index df6b838..6050329 100644
--- a/ag-web.rb
+++ b/ag-web.rb
@@ -1,7 +1,8 @@
-# /usr/bin/env ruby
-# AgWeb -- displaying all the 'golden' flamewars on -dev
-# Alex Legler <a3li@gentoo.org>
-# AGPLv3
+#!/usr/bin/env ruby
+# archives.gentoo.org: web frontend
+#
+# Copyright 2015 Alex Legler <a3li@gentoo.org>
+# Licensed under the terms of the AGPLv3.
require 'bundler/setup'
require 'yaml'
@@ -36,12 +37,12 @@ get '/:list/report/:msgid' do
if result['hits']['total'] == 0
status 404
- body "Message not found."
+ body 'Message not found.'
return
end
result_data = result['hits']['hits'].first
- @title = "Report %s - %s" % [h(result_data['_source']['subject']), params[:list]]
+ @title = 'Report %s - %s' % [h(result_data['_source']['subject']), params[:list]]
erb :report, locals: { message: result_data, list: params[:list] }
rescue Exception => e
@@ -58,12 +59,12 @@ post '/report' do
if result['hits']['total'] == 0
status 404
- body "Message not found."
+ body 'Message not found.'
return
end
result_data = result['hits']['hits'].first
- @title = "Report %s - %s" % [h(result_data['_source']['subject']), params[:list]]
+ @title = 'Report %s - %s' % [h(result_data['_source']['subject']), params[:list]]
msg = ''
if params[:captcha] == $config['report_captcha']
@@ -110,11 +111,17 @@ get '/:list/threads/:year-:month/:page?' do
max_pages = (result['hits']['total'].to_f / PER_PAGE).ceil
if result['hits']['total'] == 0
- redirect "/%s/messages/%s-%s/?no_threads=1" % [params[:list], params[:year], params[:month]]
+ redirect '/%s/messages/%s-%s/?no_threads=1' % [params[:list], params[:year], params[:month]]
return
end
- erb :listmonth, locals: { results: result, list: params[:list], current_page: current_page, max_pages: max_pages, mode: :threads }
+ erb :listmonth, locals: {
+ results: result,
+ list: params[:list],
+ current_page: current_page,
+ max_pages: max_pages,
+ mode: :threads
+ }
rescue => e
$stderr.puts e.to_s
status 500
@@ -131,11 +138,17 @@ get '/:list/messages/:year-:month/:page?' do
max_pages = (result['hits']['total'].to_f / PER_PAGE).ceil
if result['hits']['total'] == 0
- redirect "/%s/?no_messages=1" % params[:list]
+ redirect '/%s/?no_messages=1' % params[:list]
return
end
- erb :listmonth, locals: { results: result, list: params[:list], current_page: current_page, max_pages: max_pages, mode: :messages }
+ erb :listmonth, locals: {
+ results: result,
+ list: params[:list],
+ current_page: current_page,
+ max_pages: max_pages,
+ mode: :messages
+ }
rescue => e
$stderr.puts e.to_s
status 500
@@ -150,12 +163,12 @@ get '/:list/message/:msgid' do
if result['hits']['total'] == 0
status 404
- body "Message not found."
+ body 'Message not found.'
return
end
result_data = result['hits']['hits'].first
- @title = "%s - %s" % [h(result_data['_source']['subject']), params[:list]]
+ @title = '%s - %s' % [h(result_data['_source']['subject']), params[:list]]
parent_data = get_parent_data(params[:list], result_data['_source']['parent'])
child_data = get_child_data(params[:list], params[:msgid])
diff --git a/lib/cache.rb b/lib/cache.rb
index f635114..262003e 100644
--- a/lib/cache.rb
+++ b/lib/cache.rb
@@ -1,5 +1,7 @@
require 'date'
+# Caches the message count for each email
+# I guess this operation is/was expensive at some point.
class MessageCountCache
include Singleton
CACHE_SECONDS = 3600
@@ -27,16 +29,16 @@ class MessageCountCache
end
private
+
def update?
- if ((DateTime.now - @load_date) * 60 * 60 * 24).to_i > CACHE_SECONDS
- update!
- end
+ update! if ((DateTime.now - @load_date) * 60 * 60 * 24).to_i > CACHE_SECONDS
end
end
+# Caches the most recent messages displayed on the front page
class MostRecentMessagesCache
include Singleton
- CACHE_SECONDS=3600
+ CACHE_SECONDS = 3600
def initialize
update!
@@ -61,9 +63,8 @@ class MostRecentMessagesCache
end
private
+
def update?
- if ((DateTime.now - @load_date) * 60 * 60 * 24).to_i > CACHE_SECONDS
- update!
- end
+ update! if ((DateTime.now - @load_date) * 60 * 60 * 24).to_i > CACHE_SECONDS
end
-end \ No newline at end of file
+end
diff --git a/lib/helpers.rb b/lib/helpers.rb
index 873d50f..3ed4f51 100644
--- a/lib/helpers.rb
+++ b/lib/helpers.rb
@@ -8,7 +8,7 @@ helpers do
def list_check
unless [$config['active_lists'], $config['frozen_lists']].flatten.include?(params[:list])
status 404
- body "List not found"
+ body 'List not found'
return false
end
@@ -16,9 +16,9 @@ helpers do
end
def monthint_to_link(monthint)
- date = DateTime.parse("%s-%s-01" % [monthint[0..3], monthint[4..5]])
+ date = DateTime.parse('%s-%s-01' % [monthint[0..3], monthint[4..5]])
- "<a href=\"threads/%s/\">%s</a>" % [date.strftime('%Y-%m'), date.strftime('%Y %B')]
+ '<a href="threads/%s/">%s</a>' % [date.strftime('%Y-%m'), date.strftime('%Y %B')]
end
def date_format(date)
@@ -26,11 +26,11 @@ helpers do
end
def to_monthint(year, month)
- ("%i%02i" % [year.to_i, month.to_i]).to_i
+ ('%i%02i' % [year.to_i, month.to_i]).to_i
end
def to_month(year, month)
- date = DateTime.parse("%s-%s-01" % [year, month])
+ date = DateTime.parse('%s-%s-01' % [year, month])
date.strftime('%B %Y')
end
@@ -39,19 +39,19 @@ helpers do
end
def u(text)
- ERB::Util::url_encode(text)
+ ERB::Util.url_encode(text)
end
# This method strips domains from header fields.
def strip_email_headers(ary)
[ary].flatten.map do |email|
- email.gsub(/@(\S*?)('|"|>|$)/) do |s|
+ email.gsub(/@(\S*?)('|"|>|$)/) do
if $1 == 'gentoo.org'
"@g.o#{$2}"
elsif $1 == 'lists.gentoo.org'
"@l.g.o#{$2}"
else
- domain, dot, tld = $1.rpartition '.'
+ domain, _dot, tld = $1.rpartition '.'
"@#{'×' * domain.length}.#{tld}#{$2}"
end
end
@@ -60,10 +60,10 @@ helpers do
# This method is meant to strip emails from free text, not header fields
def strip_email(str)
- str.gsub(/([a-zA-Z0-9._%+-]+)@([a-zA-Z0-9.-]+)\.([a-zA-Z]{2,10})/) do |s|
- if $2 == 'gentoo' and $3 == 'org'
+ str.gsub(/([a-zA-Z0-9._%+-]+)@([a-zA-Z0-9.-]+)\.([a-zA-Z]{2,10})/) do
+ if $2 == 'gentoo' && $3 == 'org'
"#{$1}@g.o"
- elsif $2 == 'lists.gentoo' and $3 == 'org'
+ elsif $2 == 'lists.gentoo' && $3 == 'org'
"#{$1}@l.g.o"
else
"#{$1}@#{'×' * $2.length}.#{$3}"
@@ -76,7 +76,7 @@ helpers do
end
def msgid_to_gmane(msgid)
- GMANE_FIND + ERB::Util::url_encode(msgid)
+ GMANE_FIND + ERB::Util.url_encode(msgid)
end
def msgid_to_marc(msgid)
@@ -87,7 +87,7 @@ helpers do
# pan.2009.08.28.00.00.38@cox.net
# http://marc.info/?i=pan.2009.08.28.00.00.38%20()%20cox%20!%20net
local, host = msgid.split('@', 2)
- new_msgid = local + ' () ' + host.gsub('.',' ! ')
- MARC_FIND + ERB::Util::url_encode(new_msgid)
+ new_msgid = local + ' () ' + host.gsub('.', ' ! ')
+ MARC_FIND + ERB::Util.url_encode(new_msgid)
end
end
diff --git a/lib/index.rb b/lib/index.rb
index 328b74b..3e88a43 100644
--- a/lib/index.rb
+++ b/lib/index.rb
@@ -18,7 +18,7 @@ def threads_in_month(list, year, month, page)
}
},
sort: {
- date: 'desc',
+ date: 'desc'
}
}
)
@@ -30,7 +30,7 @@ def most_recent(list, n)
size: n,
body: {
sort: {
- date: 'desc',
+ date: 'desc'
}
}
)
@@ -40,7 +40,7 @@ def most_recent(list, n)
else
return result['hits']['hits']
end
-rescue => e
+rescue => _e
[]
end
@@ -58,7 +58,7 @@ def messages_in_month(list, year, month, page)
}
},
sort: {
- date: 'desc',
+ date: 'desc'
}
}
)
@@ -99,7 +99,7 @@ def get_message_count_internal(list)
size: 1,
body: {}
)['hits']['total']
-rescue => e
+rescue => _e
'?'
end
@@ -120,7 +120,7 @@ def get_message(list, hash)
end
def get_parent_data(list, parent_id)
- return nil if parent_id == nil
+ return nil if parent_id.nil?
parent = $es.search(
index: 'ml-' + list,
@@ -135,7 +135,7 @@ def get_parent_data(list, parent_id)
end
def get_child_data(list, parent_id)
- return nil if parent_id == nil
+ return nil if parent_id.nil?
children = $es.search(
index: 'ml-' + list,