From b290f6fd29ac20717bad359fc55d32822d3e054d Mon Sep 17 00:00:00 2001 From: Zed Date: Wed, 12 Jul 2023 01:34:39 +0200 Subject: [PATCH] Optimize timeline data structure --- src/parser.nim | 2 +- src/routes/timeline.nim | 8 ++------ src/types.nim | 10 ++++++---- src/views/rss.nimf | 16 ++++++++-------- src/views/timeline.nim | 8 ++++---- 5 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/parser.nim b/src/parser.nim index 7b178f3..5dc96df 100644 --- a/src/parser.nim +++ b/src/parser.nim @@ -486,7 +486,7 @@ proc parseGraphTimeline*(js: JsonNode; root: string; after=""): Profile = result.tweets.content.add tweet elif "-conversation-" in entryId or entryId.startsWith("homeConversation"): let (thread, self) = parseGraphThread(e) - result.tweets.content.add thread + result.tweets.content.add thread.content elif entryId.startsWith("cursor-bottom"): result.tweets.bottom = e{"content", "value"}.getStr if after.len == 0 and i{"__typename"}.getStr == "TimelinePinEntry": diff --git a/src/routes/timeline.nim b/src/routes/timeline.nim index 4ac60d2..e62c9e0 100644 --- a/src/routes/timeline.nim +++ b/src/routes/timeline.nim @@ -56,9 +56,7 @@ proc fetchProfile*(after: string; query: Query; skipRail=false; of posts: await getGraphUserTweets(userId, TimelineKind.tweets, after) of replies: await getGraphUserTweets(userId, TimelineKind.replies, after) of media: await getGraphUserTweets(userId, TimelineKind.media, after) - else: Profile(tweets: Timeline(beginning: true, content: @[Chain(content: - @[Tweet(tombstone: "Tweet search is unavailable for now")] - )])) + else: Profile(tweets: Timeline(beginning: true, content: @[@[Tweet(tombstone: "Tweet search is unavailable for now")]])) # else: await getGraphSearch(query, after) result.user = await user @@ -74,9 +72,7 @@ proc showTimeline*(request: Request; query: Query; cfg: Config; prefs: Prefs; if query.fromUser.len != 1: let # timeline = await getGraphSearch(query, after) - timeline = Profile(tweets: Timeline(beginning: true, content: @[Chain(content: - @[Tweet(tombstone: "This features is unavailable for now")] - )])) + timeline = Profile(tweets: Timeline(beginning: true, content: @[@[Tweet(tombstone: "This features is unavailable for now")]])) html = renderTweetSearch(timeline.tweets, prefs, getPath()) return renderMain(html, request, cfg, prefs, "Multi", rss=rss) diff --git a/src/types.nim b/src/types.nim index e7f3303..f7d5f6b 100644 --- a/src/types.nim +++ b/src/types.nim @@ -205,6 +205,8 @@ type video*: Option[Video] photos*: seq[string] + Tweets* = seq[Tweet] + Result*[T] = object content*: seq[T] top*, bottom*: string @@ -212,7 +214,7 @@ type query*: Query Chain* = object - content*: seq[Tweet] + content*: Tweets hasMore*: bool cursor*: string @@ -222,7 +224,7 @@ type after*: Chain replies*: Result[Chain] - Timeline* = Result[Chain] + Timeline* = Result[Tweets] Profile* = object user*: User @@ -275,5 +277,5 @@ type proc contains*(thread: Chain; tweet: Tweet): bool = thread.content.anyIt(it.id == tweet.id) -proc add*(timeline: var seq[Chain]; tweet: Tweet) = - timeline.add Chain(content: @[tweet]) +proc add*(timeline: var seq[Tweets]; tweet: Tweet) = + timeline.add @[tweet] diff --git a/src/views/rss.nimf b/src/views/rss.nimf index ce2518a..036a7b9 100644 --- a/src/views/rss.nimf +++ b/src/views/rss.nimf @@ -56,16 +56,16 @@ Twitter feed for: ${desc}. Generated by ${cfg.hostname} #end if #end proc # -#proc renderRssTweets(tweets: seq[Chain]; cfg: Config; userId=""): string = +#proc renderRssTweets(tweets: seq[Tweets]; cfg: Config; userId=""): string = #let urlPrefix = getUrlPrefix(cfg) #var links: seq[string] -#for c in tweets: -# for t in c.content: -# if userId.len > 0 and t.user.id != userId: continue +#for thread in tweets: +# for tweet in thread: +# if userId.len > 0 and tweet.user.id != userId: continue # end if # -# let retweet = if t.retweet.isSome: t.user.username else: "" -# let tweet = if retweet.len > 0: t.retweet.get else: t +# let retweet = if tweet.retweet.isSome: tweet.user.username else: "" +# let tweet = if retweet.len > 0: tweet.retweet.get else: tweet # let link = getLink(tweet) # if link in links: continue # end if @@ -113,7 +113,7 @@ ${renderRssTweets(profile.tweets.content, cfg, userId=profile.user.id)} #end proc # -#proc renderListRss*(tweets: seq[Chain]; list: List; cfg: Config): string = +#proc renderListRss*(tweets: seq[Tweets]; list: List; cfg: Config): string = #let link = &"{getUrlPrefix(cfg)}/i/lists/{list.id}" #result = "" @@ -130,7 +130,7 @@ ${renderRssTweets(tweets, cfg)} #end proc # -#proc renderSearchRss*(tweets: seq[Chain]; name, param: string; cfg: Config): string = +#proc renderSearchRss*(tweets: seq[Tweets]; name, param: string; cfg: Config): string = #let link = &"{getUrlPrefix(cfg)}/search" #let escName = xmltree.escape(name) #result = "" diff --git a/src/views/timeline.nim b/src/views/timeline.nim index 8ae888e..abeb6d3 100644 --- a/src/views/timeline.nim +++ b/src/views/timeline.nim @@ -39,7 +39,7 @@ proc renderNoneFound(): VNode = h2(class="timeline-none"): text "No items found" -proc renderThread(thread: seq[Tweet]; prefs: Prefs; path: string): VNode = +proc renderThread(thread: Tweets; prefs: Prefs; path: string): VNode = buildHtml(tdiv(class="thread-line")): let sortedThread = thread.sortedByIt(it.id) for i, tweet in sortedThread: @@ -106,9 +106,9 @@ proc renderTimelineTweets*(results: Timeline; prefs: Prefs; path: string; var retweets: seq[int64] for thread in results.content: - if thread.content.len == 1: + if thread.len == 1: let - tweet = thread.content[0] + tweet = thread[0] retweetId = if tweet.retweet.isSome: get(tweet.retweet).id else: 0 if retweetId in retweets or tweet.id in retweets or @@ -121,7 +121,7 @@ proc renderTimelineTweets*(results: Timeline; prefs: Prefs; path: string; hasThread = get(tweet.retweet).hasThread renderTweet(tweet, prefs, path, showThread=hasThread) else: - renderThread(thread.content, prefs, path) + renderThread(thread, prefs, path) if results.bottom.len > 0: renderMore(results.query, results.bottom)