Add multi-user timeline support

This commit is contained in:
Zed 2019-08-06 17:41:06 +02:00
parent 4660d23667
commit eeead99e32
8 changed files with 80 additions and 35 deletions

View file

@ -140,7 +140,7 @@ a:hover {
.replying-to { .replying-to {
color: hsla(240,1%,73%,.9); color: hsla(240,1%,73%,.9);
margin: 4px 0; margin: -4px 0 4px 0;
} }
.status-el .status-content { .status-el .status-content {
@ -369,6 +369,20 @@ video {
background-color: #282828; background-color: #282828;
} }
.multi-header {
background-color: #161616;
text-align: center;
padding: 10px;
display: block;
font-weight: bold;
margin-bottom: 5px;
}
.multi-timeline {
max-width: 600px;
margin: 0 auto;
}
.profile-tabs { .profile-tabs {
max-width: 900px; max-width: 900px;
margin: 0 auto; margin: 0 auto;
@ -387,6 +401,9 @@ video {
margin: 0; margin: 0;
text-align: left; text-align: left;
vertical-align: top; vertical-align: top;
}
.profile-tabs > .timeline-tab {
width: 68% !important; width: 68% !important;
} }
@ -756,7 +773,7 @@ video {
} }
.timeline-protected { .timeline-protected {
padding-left: 12px; text-align: center;
} }
.timeline-protected p { .timeline-protected p {

View file

@ -308,7 +308,7 @@ proc getTimeline*(username, after, agent: string): Future[Timeline] {.async.} =
let json = await fetchJson(base / (timelineUrl % username) ? params, headers) let json = await fetchJson(base / (timelineUrl % username) ? params, headers)
result = await finishTimeline(json, none(Query), after, agent) result = await finishTimeline(json, none(Query), after, agent)
proc getTimelineSearch*(username, after, agent: string; query: Query): Future[Timeline] {.async.} = proc getTimelineSearch*(query: Query; after, agent: string): Future[Timeline] {.async.} =
let queryParam = genQueryParam(query) let queryParam = genQueryParam(query)
let queryEncoded = encodeUrl(queryParam, usePlus=false) let queryEncoded = encodeUrl(queryParam, usePlus=false)

View file

@ -9,18 +9,15 @@ import views/[general, profile, status]
const configPath {.strdefine.} = "./nitter.conf" const configPath {.strdefine.} = "./nitter.conf"
let cfg = getConfig(configPath) let cfg = getConfig(configPath)
proc showTimeline(name, after: string; query: Option[Query]): Future[string] {.async.} = proc showSingleTimeline(name, after, agent: string; query: Option[Query]): Future[string] {.async.} =
let let profileFut = getCachedProfile(name, agent)
agent = getAgent() let railFut = getPhotoRail(name, agent)
username = name.strip(chars={'/'})
profileFut = getCachedProfile(username, agent)
railFut = getPhotoRail(username, agent)
var timelineFut: Future[Timeline] var timelineFut: Future[Timeline]
if query.isNone: if query.isNone:
timelineFut = getTimeline(username, after, agent) timelineFut = getTimeline(name, after, agent)
else: else:
timelineFut = getTimelineSearch(username, after, agent, get(query)) timelineFut = getTimelineSearch(get(query), after, agent)
let profile = await profileFut let profile = await profileFut
if profile.username.len == 0: if profile.username.len == 0:
@ -29,6 +26,25 @@ proc showTimeline(name, after: string; query: Option[Query]): Future[string] {.a
let profileHtml = renderProfile(profile, await timelineFut, await railFut) let profileHtml = renderProfile(profile, await timelineFut, await railFut)
return renderMain(profileHtml, title=cfg.title, titleText=pageTitle(profile)) return renderMain(profileHtml, title=cfg.title, titleText=pageTitle(profile))
proc showMultiTimeline(names: seq[string]; after, agent: string; query: Option[Query]): Future[string] {.async.} =
var q = query
if q.isSome:
get(q).fromUser = names
else:
q = some(Query(kind: multi, fromUser: names, excludes: @["replies"]))
var timeline = renderMulti(await getTimelineSearch(get(q), after, agent), names.join(","))
return renderMain(timeline, title=cfg.title, titleText=names.join(" | "))
proc showTimeline(name, after: string; query: Option[Query]): Future[string] {.async.} =
let agent = getAgent()
let names = name.strip(chars={'/'}).split(",")
if names.len == 1:
return await showSingleTimeline(names[0], after, agent, query)
else:
return await showMultiTimeline(names, after, agent, query)
template respTimeline(timeline: typed) = template respTimeline(timeline: typed) =
if timeline.len == 0: if timeline.len == 0:
resp Http404, showError("User \"" & @"name" & "\" not found", cfg.title) resp Http404, showError("User \"" & @"name" & "\" not found", cfg.title)

View file

@ -25,7 +25,7 @@ proc initQuery*(filters, includes, excludes, separator: string; name=""): Query
filters: filters.split(",").filterIt(it in validFilters), filters: filters.split(",").filterIt(it in validFilters),
includes: includes.split(",").filterIt(it in validFilters), includes: includes.split(",").filterIt(it in validFilters),
excludes: excludes.split(",").filterIt(it in validFilters), excludes: excludes.split(",").filterIt(it in validFilters),
fromUser: name, fromUser: @[name],
sep: if sep in separators: sep else: "" sep: if sep in separators: sep else: ""
) )
@ -33,7 +33,7 @@ proc getMediaQuery*(name: string): Query =
Query( Query(
kind: media, kind: media,
filters: @["twimg", "native_video"], filters: @["twimg", "native_video"],
fromUser: name, fromUser: @[name],
sep: "OR" sep: "OR"
) )
@ -41,15 +41,17 @@ proc getReplyQuery*(name: string): Query =
Query( Query(
kind: replies, kind: replies,
includes: @["nativeretweets"], includes: @["nativeretweets"],
fromUser: name fromUser: @[name]
) )
proc genQueryParam*(query: Query): string = proc genQueryParam*(query: Query): string =
var filters: seq[string] var filters: seq[string]
var param: string var param: string
if query.fromUser.len > 0: for i, user in query.fromUser:
param = &"from:{query.fromUser} " param &= &"from:{user} "
if i < query.fromUser.high:
param &= "OR "
for f in query.filters: for f in query.filters:
filters.add "filter:" & f filters.add "filter:" & f

View file

@ -32,14 +32,14 @@ db("cache.db", "", "", ""):
type type
QueryKind* = enum QueryKind* = enum
replies, media, custom = "search" replies, media, multi, custom = "search"
Query* = object Query* = object
kind*: QueryKind kind*: QueryKind
filters*: seq[string] filters*: seq[string]
includes*: seq[string] includes*: seq[string]
excludes*: seq[string] excludes*: seq[string]
fromUser*: string fromUser*: seq[string]
sep*: string sep*: string
VideoType* = enum VideoType* = enum

View file

@ -26,7 +26,7 @@ proc renderSearch*(): VNode =
buildHtml(tdiv(class="panel")): buildHtml(tdiv(class="panel")):
tdiv(class="search-panel"): tdiv(class="search-panel"):
form(`method`="post", action="search"): form(`method`="post", action="search"):
input(`type`="text", name="query", placeholder="Enter username...") input(`type`="text", name="query", placeholder="Enter usernames...")
button(`type`="submit"): text "🔎" button(`type`="submit"): text "🔎"
proc renderError*(error: string): VNode = proc renderError*(error: string): VNode =

View file

@ -64,4 +64,9 @@ proc renderProfile*(profile: Profile; timeline: Timeline;
renderPhotoRail(profile.username, photoRail) renderPhotoRail(profile.username, photoRail)
tdiv(class="timeline-tab"): tdiv(class="timeline-tab"):
renderTimeline(timeline, profile) renderTimeline(timeline, profile.username, profile.protected)
proc renderMulti*(timeline: Timeline; usernames: string): VNode =
buildHtml(tdiv(class="multi-timeline")):
tdiv(class="timeline-tab"):
renderTimeline(timeline, usernames, false, multi=true)

View file

@ -11,16 +11,16 @@ proc getQuery(timeline: Timeline): string =
proc getTabClass(timeline: Timeline; tab: string): string = proc getTabClass(timeline: Timeline; tab: string): string =
var classes = @["tab-item"] var classes = @["tab-item"]
if timeline.query.isNone: if timeline.query.isNone or get(timeline.query).kind == multi:
if tab == "posts": if tab == "posts":
classes.add "active" classes.add "active"
elif $timeline.query.get().kind == tab: elif $get(timeline.query).kind == tab:
classes.add "active" classes.add "active"
return classes.join(" ") return classes.join(" ")
proc renderSearchTabs(timeline: Timeline; profile: Profile): VNode = proc renderSearchTabs(timeline: Timeline; username: string): VNode =
let link = "/" & profile.username let link = "/" & username
buildHtml(ul(class="tab")): buildHtml(ul(class="tab")):
li(class=timeline.getTabClass("posts")): li(class=timeline.getTabClass("posts")):
a(href=link): text "Tweets" a(href=link): text "Tweets"
@ -29,14 +29,14 @@ proc renderSearchTabs(timeline: Timeline; profile: Profile): VNode =
li(class=timeline.getTabClass("media")): li(class=timeline.getTabClass("media")):
a(href=(link & "/media")): text "Media" a(href=(link & "/media")): text "Media"
proc renderNewer(timeline: Timeline; profile: Profile): VNode = proc renderNewer(timeline: Timeline; username: string): VNode =
buildHtml(tdiv(class="status-el show-more")): buildHtml(tdiv(class="status-el show-more")):
a(href=("/" & profile.username & getQuery(timeline).strip(chars={'?'}))): a(href=("/" & username & getQuery(timeline).strip(chars={'?'}))):
text "Load newest tweets" text "Load newest tweets"
proc renderOlder(timeline: Timeline; profile: Profile): VNode = proc renderOlder(timeline: Timeline; username: string): VNode =
buildHtml(tdiv(class="show-more")): buildHtml(tdiv(class="show-more")):
a(href=(&"/{profile.username}{getQuery(timeline)}after={timeline.minId}")): a(href=(&"/{username}{getQuery(timeline)}after={timeline.minId}")):
text "Load older tweets" text "Load older tweets"
proc renderNoMore(): VNode = proc renderNoMore(): VNode =
@ -74,20 +74,25 @@ proc renderTweets(timeline: Timeline): VNode =
renderThread(thread) renderThread(thread)
threads &= tweet.threadId threads &= tweet.threadId
proc renderTimeline*(timeline: Timeline; profile: Profile): VNode = proc renderTimeline*(timeline: Timeline; username: string;
protected: bool; multi=false): VNode =
buildHtml(tdiv): buildHtml(tdiv):
renderSearchTabs(timeline, profile) if multi:
tdiv(class="multi-header"):
text username.replace(",", " | ")
if not profile.protected and not timeline.beginning: if not protected:
renderNewer(timeline, profile) renderSearchTabs(timeline, username)
if not timeline.beginning:
renderNewer(timeline, username)
if profile.protected: if protected:
renderProtected(profile.username) renderProtected(username)
elif timeline.tweets.len == 0: elif timeline.tweets.len == 0:
renderNoneFound() renderNoneFound()
else: else:
renderTweets(timeline) renderTweets(timeline)
if timeline.hasMore or timeline.query.isSome: if timeline.hasMore or timeline.query.isSome:
renderOlder(timeline, profile) renderOlder(timeline, username)
else: else:
renderNoMore() renderNoMore()