2021-12-27 01:37:38 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
2022-11-26 23:03:11 +00:00
|
|
|
import asyncdispatch, tables, times, hashes, uri
|
2019-09-15 10:10:43 +00:00
|
|
|
|
2022-01-27 13:36:12 +00:00
|
|
|
import jester
|
2019-09-15 10:10:43 +00:00
|
|
|
|
|
|
|
import router_utils, timeline
|
2020-06-09 14:45:21 +00:00
|
|
|
import ../query
|
2019-09-15 10:10:43 +00:00
|
|
|
|
|
|
|
include "../views/rss.nimf"
|
|
|
|
|
2022-01-27 13:36:12 +00:00
|
|
|
export times, hashes
|
2020-06-01 00:22:56 +00:00
|
|
|
|
2022-11-27 14:50:08 +00:00
|
|
|
proc redisKey*(page, name, cursor: string): string =
|
|
|
|
result = page & ":" & name
|
|
|
|
if cursor.len > 0:
|
|
|
|
result &= ":" & cursor
|
|
|
|
|
2021-01-08 01:25:43 +00:00
|
|
|
proc timelineRss*(req: Request; cfg: Config; query: Query): Future[Rss] {.async.} =
|
2019-12-04 04:58:18 +00:00
|
|
|
var profile: Profile
|
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:
|
2022-01-23 06:04:50 +00:00
|
|
|
profile = await fetchProfile(after, query, skipRail=true, skipPinned=true)
|
2019-12-04 04:58:18 +00:00
|
|
|
else:
|
2020-06-17 12:12:38 +00:00
|
|
|
var q = query
|
|
|
|
q.fromUser = names
|
2019-12-04 04:58:18 +00:00
|
|
|
profile = Profile(
|
2023-04-21 12:41:30 +00:00
|
|
|
tweets: await getGraphSearch(q, after),
|
2022-01-23 06:04:50 +00:00
|
|
|
# this is kinda dumb
|
|
|
|
user: User(
|
|
|
|
username: name,
|
|
|
|
fullname: names.join(" | "),
|
|
|
|
userpic: "https://abs.twimg.com/sticky/default_profile_images/default_profile.png"
|
|
|
|
)
|
2019-12-04 04:58:18 +00:00
|
|
|
)
|
2019-10-21 21:12:40 +00:00
|
|
|
|
2022-01-23 06:04:50 +00:00
|
|
|
if profile.user.suspended:
|
|
|
|
return Rss(feed: profile.user.username, cursor: "suspended")
|
2020-04-14 21:56:31 +00:00
|
|
|
|
2022-01-23 06:04:50 +00:00
|
|
|
if profile.user.fullname.len > 0:
|
2022-01-27 13:36:12 +00:00
|
|
|
let rss = renderTimelineRss(profile, cfg, multi=(names.len > 1))
|
2022-01-23 06:04:50 +00:00
|
|
|
return Rss(feed: rss, cursor: profile.tweets.bottom)
|
2019-09-15 10:10:43 +00:00
|
|
|
|
2021-12-30 22:47:31 +00:00
|
|
|
template respRss*(rss, page) =
|
2020-06-06 07:27:25 +00:00
|
|
|
if rss.cursor.len == 0:
|
2021-12-30 22:47:31 +00:00
|
|
|
let info = case page
|
2022-11-27 14:50:08 +00:00
|
|
|
of "User": " \"" & @"name" & "\" "
|
|
|
|
of "List": " \"" & @"id" & "\" "
|
2021-12-30 22:47:31 +00:00
|
|
|
else: " "
|
|
|
|
|
|
|
|
resp Http404, showError(page & info & "not found", cfg)
|
2020-06-06 07:27:25 +00:00
|
|
|
elif rss.cursor.len == 9 and rss.cursor == "suspended":
|
2022-01-27 13:46:24 +00:00
|
|
|
resp Http404, showError(getSuspended(@"name"), cfg)
|
2021-01-08 01:25:43 +00:00
|
|
|
|
2020-06-06 07:27:25 +00:00
|
|
|
let headers = {"Content-Type": "application/rss+xml; charset=utf-8",
|
|
|
|
"Min-Id": rss.cursor}
|
2022-01-27 13:36:12 +00:00
|
|
|
resp Http200, headers, rss.feed
|
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":
|
2021-12-28 05:21:22 +00:00
|
|
|
cond cfg.enableRss
|
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()
|
2022-11-27 14:50:08 +00:00
|
|
|
key = redisKey("search", $hash(genQueryUrl(query)), cursor)
|
2020-06-01 00:22:56 +00:00
|
|
|
|
2020-06-06 07:27:25 +00:00
|
|
|
var rss = await getCachedRss(key)
|
|
|
|
if rss.cursor.len > 0:
|
2021-12-30 22:47:31 +00:00
|
|
|
respRss(rss, "Search")
|
2020-06-01 00:22:56 +00:00
|
|
|
|
2023-04-21 12:41:30 +00:00
|
|
|
let tweets = await getGraphSearch(query, cursor)
|
2020-06-06 07:27:25 +00:00
|
|
|
rss.cursor = tweets.bottom
|
2022-01-27 13:36:12 +00:00
|
|
|
rss.feed = renderSearchRss(tweets.content, query.text, genQueryUrl(query), cfg)
|
2020-06-01 00:22:56 +00:00
|
|
|
|
2020-06-06 07:27:25 +00:00
|
|
|
await cacheRss(key, rss)
|
2021-12-30 22:47:31 +00:00
|
|
|
respRss(rss, "Search")
|
2019-09-28 01:22:46 +00:00
|
|
|
|
2019-09-15 10:10:43 +00:00
|
|
|
get "/@name/rss":
|
2021-12-28 05:21:22 +00:00
|
|
|
cond cfg.enableRss
|
2019-09-15 10:10:43 +00:00
|
|
|
cond '.' notin @"name"
|
2020-06-01 00:22:56 +00:00
|
|
|
let
|
|
|
|
name = @"name"
|
2022-11-27 14:50:08 +00:00
|
|
|
key = redisKey("twitter", name, getCursor())
|
2020-06-01 00:22:56 +00:00
|
|
|
|
2020-06-06 07:27:25 +00:00
|
|
|
var rss = await getCachedRss(key)
|
|
|
|
if rss.cursor.len > 0:
|
2021-12-30 22:47:31 +00:00
|
|
|
respRss(rss, "User")
|
2020-06-01 00:22:56 +00:00
|
|
|
|
2021-01-08 01:25:43 +00:00
|
|
|
rss = await timelineRss(request, cfg, Query(fromUser: @[name]))
|
2020-06-01 00:22:56 +00:00
|
|
|
|
2020-06-06 07:27:25 +00:00
|
|
|
await cacheRss(key, rss)
|
2021-12-30 22:47:31 +00:00
|
|
|
respRss(rss, "User")
|
2019-09-15 10:10:43 +00:00
|
|
|
|
2019-12-08 10:56:20 +00:00
|
|
|
get "/@name/@tab/rss":
|
2021-12-28 05:21:22 +00:00
|
|
|
cond cfg.enableRss
|
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"]
|
2022-11-27 14:50:08 +00:00
|
|
|
let
|
|
|
|
name = @"name"
|
|
|
|
tab = @"tab"
|
|
|
|
query =
|
|
|
|
case tab
|
|
|
|
of "with_replies": getReplyQuery(name)
|
|
|
|
of "media": getMediaQuery(name)
|
|
|
|
of "search": initQuery(params(request), name=name)
|
|
|
|
else: Query(fromUser: @[name])
|
|
|
|
|
|
|
|
let searchKey = if tab != "search": ""
|
|
|
|
else: ":" & $hash(genQueryUrl(query))
|
|
|
|
|
|
|
|
let key = redisKey(tab, name & searchKey, getCursor())
|
2020-06-01 00:22:56 +00:00
|
|
|
|
2020-06-06 07:27:25 +00:00
|
|
|
var rss = await getCachedRss(key)
|
|
|
|
if rss.cursor.len > 0:
|
2021-12-30 22:47:31 +00:00
|
|
|
respRss(rss, "User")
|
2020-06-06 07:27:25 +00:00
|
|
|
|
2021-01-08 01:25:43 +00:00
|
|
|
rss = await timelineRss(request, cfg, query)
|
2020-06-01 00:22:56 +00:00
|
|
|
|
2020-06-06 07:27:25 +00:00
|
|
|
await cacheRss(key, rss)
|
2021-12-30 22:47:31 +00:00
|
|
|
respRss(rss, "User")
|
2019-09-20 23:08:30 +00:00
|
|
|
|
2021-12-30 19:55:10 +00:00
|
|
|
get "/@name/lists/@slug/rss":
|
|
|
|
cond cfg.enableRss
|
|
|
|
cond @"name" != "i"
|
|
|
|
let
|
|
|
|
slug = decodeUrl(@"slug")
|
|
|
|
list = await getCachedList(@"name", slug)
|
|
|
|
cursor = getCursor()
|
|
|
|
|
|
|
|
if list.id.len == 0:
|
2022-11-27 14:50:08 +00:00
|
|
|
resp Http404, showError("List \"" & @"slug" & "\" not found", cfg)
|
2021-12-30 19:55:10 +00:00
|
|
|
|
2022-11-27 14:50:08 +00:00
|
|
|
let url = "/i/lists/" & list.id & "/rss"
|
2021-12-30 19:55:10 +00:00
|
|
|
if cursor.len > 0:
|
2022-11-27 14:50:08 +00:00
|
|
|
redirect(url & "?cursor=" & encodeUrl(cursor, false))
|
2021-12-30 19:55:10 +00:00
|
|
|
else:
|
|
|
|
redirect(url)
|
|
|
|
|
2021-10-02 08:13:56 +00:00
|
|
|
get "/i/lists/@id/rss":
|
2021-12-28 05:21:22 +00:00
|
|
|
cond cfg.enableRss
|
2020-06-01 00:22:56 +00:00
|
|
|
let
|
2022-11-27 14:50:08 +00:00
|
|
|
id = @"id"
|
2020-06-01 00:22:56 +00:00
|
|
|
cursor = getCursor()
|
2022-11-27 14:50:08 +00:00
|
|
|
key = redisKey("lists", id, cursor)
|
2020-06-01 00:22:56 +00:00
|
|
|
|
2020-06-06 07:27:25 +00:00
|
|
|
var rss = await getCachedRss(key)
|
|
|
|
if rss.cursor.len > 0:
|
2021-12-30 22:47:31 +00:00
|
|
|
respRss(rss, "List")
|
2020-06-01 00:22:56 +00:00
|
|
|
|
|
|
|
let
|
2022-11-27 14:50:08 +00:00
|
|
|
list = await getCachedList(id=id)
|
2023-04-21 12:41:30 +00:00
|
|
|
timeline = await getGraphListTweets(list.id, cursor)
|
2020-06-06 07:27:25 +00:00
|
|
|
rss.cursor = timeline.bottom
|
2022-01-27 13:36:12 +00:00
|
|
|
rss.feed = renderListRss(timeline.content, list, cfg)
|
2020-06-01 00:22:56 +00:00
|
|
|
|
2020-06-06 07:27:25 +00:00
|
|
|
await cacheRss(key, rss)
|
2021-12-30 22:47:31 +00:00
|
|
|
respRss(rss, "List")
|