-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.rb
300 lines (232 loc) · 6.2 KB
/
app.rb
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
require 'sinatra'
require 'json'
require 'active_record'
require 'rumoji'
require './butler'
require './content_view'
require './database'
require './interest'
require './news'
require './reaction'
require './search'
require './source'
require './tag'
require './user'
require './validations'
butler = Butler.new(settings.redis_url)
after do
ActiveRecord::Base.clear_active_connections!
end
before do
# CORS for Sinatra https://gist.github.com/karlcoelho/17b908942c0837a2d534
headers['Access-Control-Allow-Methods'] = 'GET, POST, PUT, DELETE, OPTIONS'
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Headers'] = 'accept, authorization, origin'
end
options '*' do
# CORS for Sinatra https://gist.github.com/karlcoelho/17b908942c0837a2d534
response.headers['Allow'] = 'HEAD,GET,PUT,DELETE,OPTIONS,POST'
response.headers['Access-Control-Allow-Headers'] = 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Cache-Control, Accept'
end
get '/' do
content_type :json
{
version: '1.2.2',
author: '@betzerra'
}.to_json
end
get '/trending/:date' do
content_type :json
redirect to("/trending/#{params[:date]}/3")
end
get '/week' do
content_type :json
News.trending_this_week.to_json
end
get '/trending/:date/:count' do
content_type :json
date = params[:date]
count = params[:count]
unless Validations.is_valid_trending_date(date)
status 400
response = {
error: "invalid trending date, format must be like '2017-01-17' "\
'(year-month-day)'
}
body response.to_json
return
end
unless Validations.is_integer(count)
status 400
response = {
error: 'invalid trending count, must be an integer'
}
body response.to_json
return
end
butler.trending(date, count.to_i)
end
get '/news/:id' do
content_type :json
id = params[:id]
unless Validations.is_integer(id)
status 400
response = {
error: 'invalid identifier'
}
body response.to_json
return
end
News.from_id(id).to_json
end
get '/latest/:date' do
content_type :json
page = params[:page].to_i
butler.latest(params[:date], page)
end
get %r{/popular/([0-9]{4}-[0-9]{2})} do
date = params[:captures].first
unless Validations.is_valid_year_month_date(date)
status 400
response = {
error: "invalid date, format must be like '2019-01' (year-month)"
}
body response.to_json
return
end
page = [params[:page].to_i, 1].max
date_begin = Date.strptime(date, '%Y-%m')
date_end = date_begin >> 1 # >> 1 goes one month in the future
butler.popular_between(date_begin, date_end, page)
end
get '/popular' do
content_type :json
page = [params[:page].to_i, 1].max
butler.popular(page)
end
post '/reactions/:news_id' do
content_type :json
# user_id, source and reaction are mandatory fields
if params[:user_id].nil? || params[:source].nil? || params[:reaction].nil?
status 400
response = {
error: 'invalid payload'
}
body response.to_json
return
end
user = User.find_or_create_by(identifier: params[:user_id], source: params[:source])
emoji = Rumoji.encode(params[:reaction])
Reaction.react(
news_id: params[:news_id],
user_id: user.user_id,
reaction: emoji
)
n = News.find_by_news_id(params[:news_id]).as_json
n['reactions'] = Reaction.raw_reactions_by_news_id(params[:news_id])
n.to_json
end
post '/users/devicetoken' do
content_type :json
user = User.find_or_create_by(identifier: params[:user_id], source: params[:source])
user.device_token = params[:device_token]
user.save
user.to_json
end
get '/search/:keywords' do
content_type :json
user_id = env['HTTP_USER_ID']
user_source = env['HTTP_USER_SOURCE']
keywords = params[:keywords]
# search tracking
if !user_id.nil? && !user_source.nil?
user = User.find_or_create_by(identifier: user_id, source: user_source)
Search.create(criteria: keywords, user_id: user[:user_id])
end
page = [params[:page].to_i, 1].max
butler.search(keywords, page)
end
get '/search/trending/' do
content_type :json
Search
.select('criteria, count(criteria) as quantity')
.where('date >= DATE_SUB(SYSDATE(), INTERVAL 60 DAY)')
.group(:criteria)
.order('quantity DESC')
.limit(10)
.as_json(except: :id)
.to_json
end
get '/reactions/:user_id/:source' do
content_type :json
user = User.where(identifier: params[:user_id], source: params[:source]).first
reactions = Reaction
.joins(:news)
.where(user: user)
.order('date DESC')
.limit(200)
.as_json(include: :news)
reactions.each { |r| r['reaction'] = Rumoji.decode(r['reaction']) }
reactions.to_json
end
get '/news/category/:id' do
content_type :json
page = [params[:page].to_i, 1].max
butler.category(params[:id], page)
end
get '/categories/' do
content_type :json
categories = Category.all.map do |c|
category = c.as_json
news_source = c.sources.joins(:news).where(fetch_enabled: 1).first
unless news_source.nil?
category['img_url'] = news_source.news.last.img_url
end
category
end
categories.to_json
end
post '/content-views/' do
content_type :json
if params[:user_id].nil? || params[:user_source].nil?
status 400
response = { error: 'invalid user' }
body response.to_json
return
end
user = User
.where(identifier: params[:user_id], source: params[:user_source])
.first_or_create
if user.nil? || params[:news_id].nil?
status 400
response = { error: 'invalid or missing parameters' }
body response.to_json
return
end
content_view = ContentView.create(
news_id: params[:news_id],
user_id: user.user_id,
context_from: params[:context_from]
)
{ 'content_view' => content_view }.to_json
end
get '/tags/:name' do
content_type :json
if params[:name].nil?
status 400
response = { error: 'invalid or missing parameters' }
body response.to_json
return
end
Tag
.starting_with(params[:name])
.as_json(except: %i[tag_id blacklisted])
.to_json
end
get '/interests/:user_id/:source' do
content_type :json
user = User
.where(identifier: params[:user_id], source: params[:source])
.first
Interest.from_user(user.user_id).to_json
end