2020-06-05 14:27:48 +00:00
|
|
|
import asyncdispatch, strutils, tables, times, sequtils, hashes
|
2019-09-15 10:10:43 +00:00
|
|
|
|
|
|
|
import jester
|
|
|
|
|
|
|
|
import router_utils, timeline
|
2020-06-01 00:22:56 +00:00
|
|
|
import ".."/[redis_cache, query], ../views/general
|
2019-09-15 10:10:43 +00:00
|
|
|
|
|
|
|
include "../views/rss.nimf"
|
|
|
|
|
2020-06-05 14:27:48 +00:00
|
|
|
export times, hashes
|
2020-06-01 00:22:56 +00:00
|
|
|
|
2019-12-08 10:56:20 +00:00
|
|
|
proc showRss*(req: Request; hostname: string; query: Query): Future[(string, string)] {.async.} =
|
2019-12-04 04:58:18 +00:00
|
|
|
var profile: Profile
|
|
|
|
var timeline: Timeline
|
2019-12-08 10:56:20 +00:00
|
|
|
let
|
|
|
|
name = req.params.getOrDefault("name")
|
2020-06-01 00:22:56 +00:00
|
|
|
after = getCursor(req)
|
2019-12-08 10:56:20 +00:00
|
|
|
names = getNames(name)
|
|
|
|
|
2019-12-04 04:58:18 +00:00
|
|
|
if names.len == 1:
|
|
|
|
(profile, timeline) =
|
2020-06-05 14:49:10 +00:00
|
|
|
await fetchSingleTimeline(after, query, skipRail=true)
|
2019-12-04 04:58:18 +00:00
|
|
|
else:
|
2020-01-19 07:34:32 +00:00
|
|
|
let multiQuery = query.getMultiQuery(names)
|
2020-06-01 00:22:56 +00:00
|
|
|
timeline = await getSearch[Tweet](multiQuery, after)
|
2019-12-04 04:58:18 +00:00
|
|
|
# this is kinda dumb
|
|
|
|
profile = Profile(
|
|
|
|
username: name,
|
|
|
|
fullname: names.join(" | "),
|
|
|
|
userpic: "https://abs.twimg.com/sticky/default_profile_images/default_profile.png"
|
|
|
|
)
|
2019-10-21 21:12:40 +00:00
|
|
|
|
2020-04-14 21:56:31 +00:00
|
|
|
if profile.suspended:
|
|
|
|
return (profile.username, "suspended")
|
|
|
|
|
2020-06-05 14:49:30 +00:00
|
|
|
if profile.fullname.len > 0:
|
2019-12-08 10:56:20 +00:00
|
|
|
let rss = renderTimelineRss(timeline, profile, hostname, multi=(names.len > 1))
|
2020-06-01 00:22:56 +00:00
|
|
|
return (rss, timeline.bottom)
|
2019-09-15 10:10:43 +00:00
|
|
|
|
2019-12-08 10:56:20 +00:00
|
|
|
template respRss*(rss, minId) =
|
2019-09-15 10:10:43 +00:00
|
|
|
if rss.len == 0:
|
2019-10-21 05:59:22 +00:00
|
|
|
resp Http404, showError("User \"" & @"name" & "\" not found", cfg)
|
2020-04-14 21:56:31 +00:00
|
|
|
elif minId == "suspended":
|
|
|
|
resp Http404, showError(getSuspended(rss), cfg)
|
2020-06-01 00:22:56 +00:00
|
|
|
let headers = {"Content-Type": "application/rss+xml; charset=utf-8", "Min-Id": minId}
|
2019-12-08 10:56:20 +00:00
|
|
|
resp Http200, headers, rss
|
2019-09-15 10:10:43 +00:00
|
|
|
|
|
|
|
proc createRssRouter*(cfg: Config) =
|
|
|
|
router rss:
|
2019-09-28 01:22:46 +00:00
|
|
|
get "/search/rss":
|
2019-09-30 20:24:01 +00:00
|
|
|
if @"q".len > 200:
|
2019-10-21 03:19:00 +00:00
|
|
|
resp Http400, showError("Search input too long.", cfg)
|
2019-09-28 01:22:46 +00:00
|
|
|
|
|
|
|
let query = initQuery(params(request))
|
2019-10-08 11:54:20 +00:00
|
|
|
if query.kind != tweets:
|
2019-10-21 03:19:00 +00:00
|
|
|
resp Http400, showError("Only Tweet searches are allowed for RSS feeds.", cfg)
|
2019-09-28 01:22:46 +00:00
|
|
|
|
2020-06-01 00:22:56 +00:00
|
|
|
let
|
|
|
|
cursor = getCursor()
|
2020-06-05 14:27:48 +00:00
|
|
|
key = $hash(genQueryUrl(query)) & cursor
|
2020-06-01 00:22:56 +00:00
|
|
|
(cRss, cCursor) = await getCachedRss(key)
|
|
|
|
|
|
|
|
if cRss.len > 0:
|
|
|
|
respRss(cRss, cCursor)
|
|
|
|
|
|
|
|
let
|
|
|
|
tweets = await getSearch[Tweet](query, cursor)
|
|
|
|
rss = renderSearchRss(tweets.content, query.text, genQueryUrl(query), cfg.hostname)
|
|
|
|
|
|
|
|
await cacheRss(key, rss, tweets.bottom)
|
|
|
|
respRss(rss, tweets.bottom)
|
2019-09-28 01:22:46 +00:00
|
|
|
|
2019-09-15 10:10:43 +00:00
|
|
|
get "/@name/rss":
|
|
|
|
cond '.' notin @"name"
|
2020-06-01 00:22:56 +00:00
|
|
|
let
|
|
|
|
cursor = getCursor()
|
|
|
|
name = @"name"
|
|
|
|
(cRss, cCursor) = await getCachedRss(name & cursor)
|
|
|
|
|
|
|
|
if cRss.len > 0:
|
|
|
|
respRss(cRss, cCursor)
|
|
|
|
|
|
|
|
let (rss, rssCursor) = await showRss(request, cfg.hostname,
|
|
|
|
Query(fromUser: @[name]))
|
|
|
|
|
|
|
|
await cacheRss(name & cursor, rss, rssCursor)
|
|
|
|
respRss(rss, rssCursor)
|
2019-09-15 10:10:43 +00:00
|
|
|
|
2019-12-08 10:56:20 +00:00
|
|
|
get "/@name/@tab/rss":
|
2019-09-15 10:10:43 +00:00
|
|
|
cond '.' notin @"name"
|
2019-12-08 11:38:55 +00:00
|
|
|
cond @"tab" in ["with_replies", "media", "search"]
|
2020-05-02 17:22:43 +00:00
|
|
|
let name = @"name"
|
2019-12-08 10:56:20 +00:00
|
|
|
let query =
|
|
|
|
case @"tab"
|
2020-05-02 17:22:43 +00:00
|
|
|
of "with_replies": getReplyQuery(name)
|
|
|
|
of "media": getMediaQuery(name)
|
|
|
|
of "search": initQuery(params(request), name=name)
|
|
|
|
else: Query(fromUser: @[name])
|
2019-09-20 01:35:27 +00:00
|
|
|
|
2020-06-05 14:27:48 +00:00
|
|
|
var key = @"name" & "/" & @"tab"
|
|
|
|
if @"tab" == "search":
|
|
|
|
key &= hash(genQueryUrl(query))
|
|
|
|
key &= getCursor()
|
2020-06-01 00:22:56 +00:00
|
|
|
|
2020-06-05 14:27:48 +00:00
|
|
|
let (cRss, cCursor) = await getCachedRss(key)
|
2020-06-01 00:22:56 +00:00
|
|
|
if cRss.len > 0:
|
|
|
|
respRss(cRss, cCursor)
|
|
|
|
|
|
|
|
let (rss, rssCursor) = await showRss(request, cfg.hostname, query)
|
|
|
|
await cacheRss(key, rss, rssCursor)
|
|
|
|
respRss(rss, rssCursor)
|
2019-09-20 23:08:30 +00:00
|
|
|
|
|
|
|
get "/@name/lists/@list/rss":
|
|
|
|
cond '.' notin @"name"
|
2020-06-01 00:22:56 +00:00
|
|
|
let
|
|
|
|
cursor = getCursor()
|
|
|
|
key = @"name" & "/" & @"list" & cursor
|
|
|
|
(cRss, cCursor) = await getCachedRss(key)
|
|
|
|
|
|
|
|
if cRss.len > 0:
|
|
|
|
respRss(cRss, cCursor)
|
|
|
|
|
|
|
|
let
|
|
|
|
list = await getCachedList(@"name", @"list")
|
|
|
|
timeline = await getListTimeline(list.id, cursor)
|
|
|
|
rss = renderListRss(timeline.content, list, cfg.hostname)
|
|
|
|
|
|
|
|
await cacheRss(key, rss, timeline.bottom)
|
|
|
|
respRss(rss, timeline.bottom)
|