From 5979d7f588626decbd64dc6af5ce45e846da440c Mon Sep 17 00:00:00 2001 From: Zed Date: Wed, 23 Oct 2019 09:03:15 +0200 Subject: [PATCH] Refactoring --- src/api/list.nim | 33 ++++++++++++++++++--------------- src/api/timeline.nim | 3 +-- src/routes/list.nim | 12 ++++-------- src/routes/rss.nim | 2 +- src/routes/timeline.nim | 22 +++++++++++----------- 5 files changed, 35 insertions(+), 37 deletions(-) diff --git a/src/api/list.nim b/src/api/list.nim index 1e7a13b..c92fe6e 100644 --- a/src/api/list.nim +++ b/src/api/list.nim @@ -4,7 +4,7 @@ import sequtils, strutils, json, uri import ".."/[types, parser, parserutils, query] import utils, consts, timeline, search -proc getListTimeline*(username, list, agent, after: string; media=true): Future[Timeline] {.async.} = +proc getListTimeline*(username, list, after, agent: string; media=true): Future[Timeline] {.async.} = let url = base / (listUrl % [username, list]) var params = toSeq({ @@ -23,20 +23,7 @@ proc getListTimeline*(username, list, agent, after: string; media=true): Future[ result.minId = getLastId(result) -proc getListMembers*(username, list, agent: string): Future[Result[Profile]] {.async.} = - let - url = base / (listMembersUrl % [username, list]) - html = await fetchHtml(url, genHeaders(agent, url)) - - result = Result[Profile]( - minId: html.selectAttr(".stream-container", "data-min-position"), - hasMore: html.select(".has-more-items") != nil, - beginning: true, - query: Query(kind: userList), - content: html.selectAll(".account").map(parseListProfile) - ) - -proc getListMembersSearch*(username, list, agent, after: string): Future[Result[Profile]] {.async.} = +proc getListMembersSearch(username, list, after, agent: string): Future[Result[Profile]] {.async.} = let referer = base / (listMembersUrl % [username, list]) url = referer / "timeline" @@ -60,3 +47,19 @@ proc getListMembersSearch*(username, list, agent, after: string): Future[Result[ result.hasMore = html != "\n" for p in parseHtml(html).selectAll(".account"): result.content.add parseListProfile(p) + +proc getListMembers*(username, list, after, agent: string): Future[Result[Profile]] {.async.} = + if after.len > 0: + return await getListMembersSearch(username, list, after, agent) + + let + url = base / (listMembersUrl % [username, list]) + html = await fetchHtml(url, genHeaders(agent, url)) + + result = Result[Profile]( + minId: html.selectAttr(".stream-container", "data-min-position"), + hasMore: html.select(".has-more-items") != nil, + beginning: true, + query: Query(kind: userList), + content: html.selectAll(".account").map(parseListProfile) + ) diff --git a/src/api/timeline.nim b/src/api/timeline.nim index 71f802b..a709da5 100644 --- a/src/api/timeline.nim +++ b/src/api/timeline.nim @@ -23,7 +23,7 @@ proc finishTimeline*(json: JsonNode; query: Query; after, agent: string; if media: await getMedia(timeline, agent) result.content = timeline.content -proc getProfileAndTimeline*(username, agent, after: string; media=true): Future[(Profile, Timeline)] {.async.} = +proc getProfileAndTimeline*(username, after, agent: string; media=true): Future[(Profile, Timeline)] {.async.} = var url = base / username if after.len > 0: url = url ? {"max_position": after} @@ -54,7 +54,6 @@ proc getTimeline*(username, after, agent: string; media=true): Future[Timeline] result = await finishTimeline(json, Query(), after, agent, media) proc getMediaTimeline*(username, after, agent: string; media=true): Future[Timeline] {.async.} = - echo "mediaTimeline" var params = toSeq({ "include_available_features": "1", "include_entities": "1", diff --git a/src/routes/list.nim b/src/routes/list.nim index 855fe9b..ebac6bc 100644 --- a/src/routes/list.nim +++ b/src/routes/list.nim @@ -18,17 +18,13 @@ proc createListRouter*(cfg: Config) = get "/@name/lists/@list": cond '.' notin @"name" let - list = await getListTimeline(@"name", @"list", getAgent(), @"max_position") + list = await getListTimeline(@"name", @"list", @"max_position", getAgent()) tweets = renderTimelineTweets(list, cookiePrefs(), request.path) respList list, tweets get "/@name/lists/@list/members": cond '.' notin @"name" - let list = - if @"max_position".len == 0: - await getListMembers(@"name", @"list", getAgent()) - else: - await getListMembersSearch(@"name", @"list", getAgent(), @"max_position") - - let users = renderTimelineUsers(list, cookiePrefs(), request.path) + let + list = await getListMembers(@"name", @"list", @"max_position", getAgent()) + users = renderTimelineUsers(list, cookiePrefs(), request.path) respList list, users diff --git a/src/routes/rss.nim b/src/routes/rss.nim index e7d98a6..74b91c6 100644 --- a/src/routes/rss.nim +++ b/src/routes/rss.nim @@ -51,5 +51,5 @@ proc createRssRouter*(cfg: Config) = get "/@name/lists/@list/rss": cond '.' notin @"name" - let list = await getListTimeline(@"name", @"list", getAgent(), "", media=false) + let list = await getListTimeline(@"name", @"list", "", getAgent(), media=false) respRss(renderListRss(list.content, @"name", @"list", cfg.hostname)) diff --git a/src/routes/timeline.nim b/src/routes/timeline.nim index fa97a56..4fa6f27 100644 --- a/src/routes/timeline.nim +++ b/src/routes/timeline.nim @@ -24,7 +24,7 @@ proc fetchSingleTimeline*(name, after, agent: string; query: Query; if cachedProfile.isSome: timeline = await getTimeline(name, after, agent, media) else: - (profile, timeline) = await getProfileAndTimeline(name, agent, after, media) + (profile, timeline) = await getProfileAndTimeline(name, after, agent, media) cache(profile) else: var timelineFut = @@ -59,21 +59,21 @@ proc showTimeline*(request: Request; query: Query; cfg: Config; rss: string): Fu after = request.get("max_position") names = name.strip(chars={'/'}).split(",").filterIt(it.len > 0) - if names.len == 1: - let - rail = getPhotoRail(names[0], agent, skip=(query.kind == media)) - (p, t) = await fetchSingleTimeline(names[0], after, agent, query) - r = await rail - if p.username.len == 0: return - let pHtml = renderProfile(p, t, r, prefs, getPath()) - return renderMain(pHtml, request, cfg, pageTitle(p), pageDesc(p), - rss=rss, images = @[p.getUserpic("_200x200")]) - else: + if names.len != 1: let timeline = await fetchMultiTimeline(names, after, agent, query) html = renderTweetSearch(timeline, prefs, getPath()) return renderMain(html, request, cfg, "Multi") + let + rail = getPhotoRail(names[0], agent, skip=(query.kind == media)) + (p, t) = await fetchSingleTimeline(names[0], after, agent, query) + r = await rail + if p.username.len == 0: return + let pHtml = renderProfile(p, t, r, prefs, getPath()) + return renderMain(pHtml, request, cfg, pageTitle(p), pageDesc(p), + rss=rss, images = @[p.getUserpic("_200x200")]) + template respTimeline*(timeline: typed) = if timeline.len == 0: resp Http404, showError("User \"" & @"name" & "\" not found", cfg)