This repository has been archived by the owner on Feb 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
greader.rb
203 lines (183 loc) · 8.11 KB
/
greader.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
post '/accounts/ClientLogin' do
if Argon2::Password.verify_password(params["Passwd"], Database.new.getOption(params["Email"] + "_clientPassword"))
ctoken = SecureRandom.urlsafe_base64(256)
Database.new.setOption(ctoken, params["Email"])
erb :readerAuth, :layout => false, :locals => {:ctoken => ctoken}
else
return "Error=BadAuthentication"
end
end
get '/reader/ping' do
if ((user = apiAccess!); user)
return "ok"
else
return "Unauthorized"
end
end
get '/reader/api/0/token' do
if ((user = apiAccess!); user)
return request.env["HTTP_AUTHORIZATION"].gsub('GoogleLogin auth=', '')
end
end
get '/reader/directory/search' do
return "Search is not yet supported"
end
get '/reader/api/0/user-info' do
if ((user = apiAccess!); user)
erb :readerUserInfo, :layout => false, :locals => {:user => user}
end
end
get '/reader/api/0/preference/list' do
if params["output"] == "json"
return '{"prefs":[]}'
else
return '<object><list name="prefs"/></object>'
end
end
get '/reader/api/0/friends/list' do
if params["output"] == "json"
return '{"friends":[]}'
else
return '<object><list name="friends"/></object>'
end
end
get '/reader/api/0/preference/stream/list' do
erb :readerStream, :layout => false, :locals => {:output => params["output"]}
end
get '/reader/api/0/preference/stream/set' do
# TODO
return ""
end
get '/reader/api/0/tag/list' do
erb :readerTaglist, :layout => false, :locals => {:output => params["output"]}
end
get '/reader/api/0/subscription/list' do
if ((user = apiAccess!); user)
erb :readerSubscriptionlist, :layout => false, :locals => {:output => params["output"], :feeds => Database.new.getFeeds(onlyUnread: false, user: user)}
end
end
get '/reader/subscriptions/export' do
if ((user = apiAccess!(params[:token])); user)
content_type 'text/x-opml'
erb :export, :layout => false, :locals => {:feeds => Database.new.getFeeds(onlyUnread: false, user: user)}
end
end
post '/reader/api/0/subscription/quickadd' do
if ((user = apiAccess!); user)
Rack::Superfeedr.base_path = url("/superfeedr/feed/", false)
feed = subscribe(url: params["quickadd"], name: params["quickadd"], user: user)
erb :readerQuickadd, :layout => false, :locals => {:feed => feed}
end
end
post '/reader/api/0/subscription/edit' do
if ((user = apiAccess!); user)
if params["ac"] == "unsubscribe" && params["s"]
id = params["s"].gsub("feed/", "")
feed = Feed.new(id: id, user: user)
feed.unsubscribe!
Rack::Superfeedr.unsubscribe(feed.url, id) if (feed.subscribers? == 0)
end
end
end
get '/reader/api/0/unread-count' do
if ((user = apiAccess!); user)
feeds, total = Database.new.getUnreadEntriesCount(user: user)
erb :readerUnread, :layout => false, :locals => {:output => params["output"], :feeds => feeds, :total => total}
end
end
get '/reader/api/0/stream/items/ids' do
if ((user = apiAccess!); user)
db = Database.new
params["s"] ||= params["xt"]
case params["s"]
when "user/-/state/com.google/reading-list"
# TODO: add continuation
erb :readerStreamIds, :layout => false, :locals => {:output => params["output"], :entries => db.getEntries(user: user)}
when "user/-/state/com.google/reading-list"
# TODO: add continuation
erb :readerStreamIds, :layout => false, :locals => {:output => params["output"], :entries => db.getMarkedEntries(nil, user: user)}
when "user/-/state/com.google/read"
# TODO: add continuation
erb :readerStreamIds, :layout => false, :locals => {:output => params["output"], :entries => db.getEntries(nil, nil, true, user: user)}
when "user/-/state/com.google/broadcast", "user/-/state/com.google/created"
if params["output"] == "json"
return '{"itemRefs":[]}'
else
return '<object><list name="itemRefs"></list></object>'
end
when /feed\//
# TODO: add continuation
id = params["s"].gsub("feed/", "")
erb :readerStreamIds, :layout => false, :locals => {:output => params["output"], :entries => Feed.new(id: id, user: user).entries()}
end
end
end
get '/reader/api/0/stream/items/contents' do
if ((user = apiAccess!); user)
# TODO: Add atom output
# we need to manually get the params here, because the syntax of having multiple i=…&i=… collides with how sinatra does things
entries = parseItemIds(request: request, user: user)
feed = Feed.new(id: entries.first.feed_id, user: user)
erb :readerStreamContent, :layout => false, :locals => {:output => params["output"], :entries => entries, :feed => feed}
end
end
get %r{/reader/api/0/stream/contents(.*)}, %r{/reader/atom(.*)} do |feedId|
if ((user = apiAccess!); user)
params["output"] = "atom" if request.env['rack.request.script_name'] == "/reader/atom"
params["s"] = feedId if feedId && feedId.include?('feed/') # legacy greader mode for News+
params["s"] = feedId if feedId && feedId.include?('user/') # legacy greader mode for News+
params["s"] = params["s"][1..-1] if params["s"].chr == "/" # snyc theoretical bazqux api format with regex for next+
db = Database.new
case params["s"]
when "user/-/state/com.google/reading-list"
# TODO: add continuation
feed = Feed.new(id: "reading-list", url: "/reading-list", name: "Reading List", user: user)
erb :readerStreamContent, :layout => false, :locals => {:output => params["output"], :entries => db.getEntries(user: user), :feed => feed}
when "user/-/state/com.google/starred"
# TODO: add continuation
feed = Feed.new(id: "marked", url: "/marked", name: "Marked", user: user)
erb :readerStreamContent, :layout => false, :locals => {:output => params["output"], :entries => db.getMarkedEntries(nil, user: user), :feed => feed}
when "user/-/state/com.google/read"
# TODO: add continuation
feed = Feed.new(id: "read", url: "/read", name: "Read", user: user)
erb :readerStreamContent, :layout => false, :locals => {:output => params["output"], :entries => db.getEntries(nil, nil, true, user: user), :feed => feed}
when "user/-/state/com.google/broadcast", "user/-/state/com.google/created"
if params["output"] == "json"
return '{"itemRefs":[]}'
else
return '<object><list name="itemRefs"></list></object>'
end
when /feed\//
# TODO: add continuation
id = params["s"].gsub("feed/", "")
feed = Feed.new(id: id, user: user)
erb :readerStreamContent, :layout => false, :locals => {:output => params["output"], :entries => feed.entries(), :feed => feed}
end
end
end
post '/reader/api/0/edit-tag' do
if ((user = apiAccess!(params["T"])); user)
entries = parseItemIds(request: request, user: user)
case params["a"]
when "user/-/state/com.google/read" then entries.each{|entry| entry.read? ? entry.unread! : entry.read! }; return "OK" # return something to try to prevent clients from thinking it failed (what is the expected output?)
when "user/-/state/com.google/starred" then entries.each{|entry| entry.marked? ? entry.unmark! : entry.mark! }; return "OK";
end
return ""
end
end
def parseItemIds(request:, user:)
if request.request_method == "GET"
raw = request.env['rack.request.query_string']
else
raw = request.body.read
end
ids = raw.scan(/i=([^&]*)/)
ids.each_with_index do |id, index|
id = id[0]
# we got a long legacy id and need to get the real entry id, but we always want to remove the array
ids[index] = id.gsub("tag%3Agoogle.com%2C2005%3Areader%2Fitem%2F", "")
end
entries = []
ids.each{|id| entries.push(Entry.new(id: id, user: user)) }
return entries
end