Skip to content

Commit

Permalink
Optimize timeline data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
zedeus committed Jul 11, 2023
1 parent 0bc3c15 commit b290f6f
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/parser.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
8 changes: 2 additions & 6 deletions src/routes/timeline.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down
10 changes: 6 additions & 4 deletions src/types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,16 @@ type
video*: Option[Video]
photos*: seq[string]

Tweets* = seq[Tweet]

Result*[T] = object
content*: seq[T]
top*, bottom*: string
beginning*: bool
query*: Query

Chain* = object
content*: seq[Tweet]
content*: Tweets
hasMore*: bool
cursor*: string

Expand All @@ -222,7 +224,7 @@ type
after*: Chain
replies*: Result[Chain]

Timeline* = Result[Chain]
Timeline* = Result[Tweets]

Profile* = object
user*: User
Expand Down Expand Up @@ -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]
16 changes: 8 additions & 8 deletions src/views/rss.nimf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -113,7 +113,7 @@ ${renderRssTweets(profile.tweets.content, cfg, userId=profile.user.id)}
</rss>
#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 = ""
<?xml version="1.0" encoding="UTF-8"?>
Expand All @@ -130,7 +130,7 @@ ${renderRssTweets(tweets, cfg)}
</rss>
#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 = ""
Expand Down
8 changes: 4 additions & 4 deletions src/views/timeline.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit b290f6f

Please sign in to comment.