summaryrefslogtreecommitdiff
blob: 44745eda2447a52bcb7439c03c064bebad2efef5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#!/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'
require 'sinatra'
require 'sinatra/partial'
require 'elasticsearch'
require 'date'
require 'pony'

require_relative 'lib/index.rb'
require_relative 'lib/helpers.rb'
require_relative 'lib/cache.rb'

configure do
  set :partial_template_engine, :erb
  mime_type :atom, 'application/atom+xml'
end

$es = Elasticsearch::Client.new(log: false)
$es.transport.reload_connections!

$config = YAML.load_file('config.yml')

MessageCountCache.instance.update!
MostRecentMessagesCache.instance.update!

get '/:list/report/:msgid' do
  return unless list_check

  begin
    result = get_message(params[:list], params[:msgid])

    if result['hits']['total'] == 0
      status 404
      body 'Message not found.'
      return
    end

    result_data = result['hits']['hits'].first
    @title = 'Report %s - %s' % [h(result_data['_source']['subject']), params[:list]]

    erb :report, locals: { message: result_data, list: params[:list] }
  rescue Exception => e
    $stderr.puts e.to_s
    status 500
  end
end

post '/report' do
  return unless list_check

  begin
    result = get_message(params[:list], params[:msgid])

    if result['hits']['total'] == 0
      status 404
      body 'Message not found.'
      return
    end

    result_data = result['hits']['hits'].first
    @title = 'Report %s - %s' % [h(result_data['_source']['subject']), params[:list]]

    msg = ''
    if params[:captcha] == $config['report_captcha']
      Pony.mail(
        to: $config['report_addr'],
        from: 'archives.gentoo.org <postmaster@gentoo.org>',
        subject: "Reported Message on #{params[:list]}: #{result_data['_source']['subject']}",
        body: erb(:reportmail, locals: { message: result_data, list: params[:list] }, layout: false)
      )
      msg = 'Thanks for your report.'
    else
      msg = 'Invalid CAPTCHA. Report not sent.'
    end

    erb :reportsent, locals: { message: result_data, list: params[:list], msg: msg }
  rescue Exception => e
    $stderr.puts e.to_s
    status 500
  end
end

get '/:list/' do
  return unless list_check

  begin
    result = get_month_listing(params[:list])
    @title = params[:list]
    current_monthint = to_monthint(Date.today.year, Date.today.month)

    erb :listindex, locals: { results: result, list: params[:list], current_monthint: current_monthint }
  rescue => e
    $stderr.puts e.to_s
    status 500
  end
end

get '/:list/threads/?' do
  redirect '/%s/' % [params[:list]], 301
end

get '/:list/messages/?' do
  redirect '/%s/' % [params[:list]], 301
end

get '/:list/threads/:year-:month' do
  redirect '/%s/threads/%s-%s/' % [params[:list], params[:year], params[:month]], 301
end

get '/:list/threads/:year-:month/:page/' do
  redirect '/%s/threads/%s-%s/%s' % [params[:list], params[:year], params[:month], params[:page]], 301
end

get '/:list/threads/:year-:month/:page?' do
  return unless list_check

  begin
    @title = params[:list]
    current_page = [(params[:page] || 1).to_i, 1].max
    result = threads_in_month(params[:list], params[:year], params[:month], current_page)
    no_threads = false

    if result['hits']['total'] == 0
      result = messages_in_month(params[:list], params[:year], params[:month], current_page)
      no_threads = true
    end
    max_pages = (result['hits']['total'].to_f / PER_PAGE).ceil

    erb :listmonth, locals: {
      results: result,
      list: params[:list],
      current_page: current_page,
      max_pages: max_pages,
      mode: :threads,
      no_threads: no_threads
    }
  rescue => e
    $stderr.puts e.to_s
    status 500
  end
end

get '/:list/messages/:year-:month' do
  redirect '/%s/messages/%s-%s/' % [params[:list], params[:year], params[:month]], 301
end

get '/:list/messages/:year-:month/:page/' do
  redirect '/%s/messages/%s-%s/%s' % [params[:list], params[:year], params[:month], params[:page]], 301
end

get '/:list/messages/:year-:month/:page?' do
  return unless list_check

  begin
    @title = params[:list]
    current_page = [(params[:page] || 1).to_i, 1].max
    result = messages_in_month(params[:list], params[:year], params[:month], current_page)
    max_pages = (result['hits']['total'].to_f / PER_PAGE).ceil

    erb :listmonth, locals: {
      results: result,
      list: params[:list],
      current_page: current_page,
      max_pages: max_pages,
      mode: :messages,
      no_threads: false
    }
  rescue => e
    $stderr.puts e.to_s
    status 500
  end
end

get '/:list/message/:msgid/' do
  redirect '/%s/message/%s' % [params[:list], params[:msgid]], 301
end

get '/:list/message/:msgid' do
  return unless list_check

  begin
    result = get_message(params[:list], params[:msgid])

    if result['hits']['total'] == 0
      status 404
      body 'Message not found.'
      return
    end

    result_data = result['hits']['hits'].first
    @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])

    erb :message, locals: { message: result_data, list: params[:list], parent: parent_data, children: child_data }
  rescue Exception => e
    $stderr.puts e.to_s
    status 500
  end
end

get '/lists' do
  @nav = :lists
  erb :lists
end

get '/' do
  @nav = :index
  erb :index
end