From 0d3469df667818005f60da81beb5310fe229a7bf Mon Sep 17 00:00:00 2001 From: Mitarashi Date: Fri, 14 Jan 2022 19:01:47 +0200 Subject: [PATCH] changed code to be not shit --- src/formatters.nim | 16 ++++++----- src/nitter.nim | 4 +-- src/routes/embed.nim | 17 +++++++++--- src/routes/embedtweet.nim | 22 --------------- src/routes/router_utils.nim | 4 +-- src/views/tweet.nim | 54 +++++++++++++++++++++++++++++++++++++ 6 files changed, 80 insertions(+), 37 deletions(-) delete mode 100644 src/routes/embedtweet.nim diff --git a/src/formatters.nim b/src/formatters.nim index b251ccf..2ae3077 100644 --- a/src/formatters.nim +++ b/src/formatters.nim @@ -32,23 +32,25 @@ proc getUrlPrefix*(cfg: Config): string = if cfg.useHttps: https & cfg.hostname else: "http://" & cfg.hostname -proc stripHtml*(text: string): string = +proc shortLink*(text: string; length=28): string = + result = text.replace(wwwRegex, "") + if result.len > length: + result = result[0 ..< length] & "…" + +proc stripHtml*(text: string; shorten=false): string = var html = parseHtml(text) for el in html.findAll("a"): let link = el.attr("href") if "http" in link: if el.len == 0: continue - el[0].text = link + el[0].text = + if shorten: link.shortLink + else: link html.innerText() proc sanitizeXml*(text: string): string = text.replace(illegalXmlRegex, "") -proc shortLink*(text: string; length=28): string = - result = text.replace(wwwRegex, "") - if result.len > length: - result = result[0 ..< length] & "…" - proc replaceUrls*(body: string; prefs: Prefs; absolute=""): string = result = body diff --git a/src/nitter.nim b/src/nitter.nim index 411c9a3..9f8fcb7 100644 --- a/src/nitter.nim +++ b/src/nitter.nim @@ -10,7 +10,7 @@ import types, config, prefs, formatters, redis_cache, http_pool, tokens import views/[general, about] import routes/[ preferences, timeline, status, media, search, rss, list, debug, - unsupported, embed, resolver, router_utils, embedtweet] + unsupported, embed, resolver, router_utils] const instancesUrl = "https://github.com/zedeus/nitter/wiki/Instances" const issuesUrl = "https://github.com/zedeus/nitter/issues" @@ -51,7 +51,6 @@ createMediaRouter(cfg) createEmbedRouter(cfg) createRssRouter(cfg) createDebugRouter(cfg) -createEmbedTweetRouter(cfg) settings: port = Port(cfg.port) @@ -102,4 +101,3 @@ routes: extend media, "" extend embed, "" extend debug, "" - extend embedtweet, "" diff --git a/src/routes/embed.nim b/src/routes/embed.nim index d9a9ee9..9a4c85b 100644 --- a/src/routes/embed.nim +++ b/src/routes/embed.nim @@ -1,9 +1,12 @@ # SPDX-License-Identifier: AGPL-3.0-only import asyncdispatch, strutils, options -import jester -import ".."/[types, api], ../views/embed +import jester, karax/vdom +import ".."/[types, api], ../views/[embed, tweet, general] +import router_utils -export api, embed +export api, embed, vdom +export tweet, general +export router_utils proc createEmbedRouter*(cfg: Config) = router embed: @@ -13,3 +16,11 @@ proc createEmbedRouter*(cfg: Config) = resp Http404 resp renderVideoEmbed(cfg, convo.tweet) + + get "/@user/status/@id/embedded": + let + tweet = (await getTweet(@"id")).tweet + prefs = cookiePrefs() + path = getPath() + + resp $renderEmbeddedTweet(tweet, cfg, prefs, path) diff --git a/src/routes/embedtweet.nim b/src/routes/embedtweet.nim deleted file mode 100644 index d318e67..0000000 --- a/src/routes/embedtweet.nim +++ /dev/null @@ -1,22 +0,0 @@ -import asyncdispatch, strutils, uri, options -import jester, karax/vdom - -import router_utils -import ".."/views/[general, tweet] -import ".."/[types, api] - -export vdom -export router_utils -export api, tweet, general - -proc createEmbedTweetRouter*(cfg: Config) = - router embedtweet: - get "/embed/Tweet.html": - let - prefs = cookiePrefs() - t = (await getTweet(@"id")).tweet - - resp ($renderHead(prefs, cfg) & $renderTweet(t, prefs, getPath(), mainTweet=true)) - - - diff --git a/src/routes/router_utils.nim b/src/routes/router_utils.nim index 7159890..a071a0d 100644 --- a/src/routes/router_utils.nim +++ b/src/routes/router_utils.nim @@ -4,12 +4,12 @@ from jester import Request, cookies import ../views/general import ".."/[utils, prefs, types] -export utils, prefs, types +export utils, prefs, types, uri template savePref*(pref, value: string; req: Request; expire=false) = if not expire or pref in cookies(req): setCookie(pref, value, daysForward(when expire: -10 else: 360), - httpOnly=true, secure=cfg.useHttps) + httpOnly=true, secure=cfg.useHttps, sameSite=None) template cookiePrefs*(): untyped {.dirty.} = getPrefs(cookies(request)) diff --git a/src/views/tweet.nim b/src/views/tweet.nim index 7494685..3d05993 100644 --- a/src/views/tweet.nim +++ b/src/views/tweet.nim @@ -4,6 +4,7 @@ import karax/[karaxdsl, vdom, vstyles] import renderutils import ".."/[types, utils, formatters] +import general proc getSmallPic(url: string): string = result = url @@ -275,6 +276,59 @@ proc renderLocation*(tweet: Tweet): string = text place return $node +proc renderEmbeddedTweet*(tweet: Tweet; cfg: Config; prefs: Prefs; path: string): VNode = + let fullTweet = tweet + var retweet: string + var tweet = fullTweet + if tweet.retweet.isSome: + tweet = tweet.retweet.get + retweet = fullTweet.profile.fullname + + # handle unavailable + + buildHtml(tdiv(class="timeline-item")): + renderHead(prefs, cfg) + tdiv(class="tweet-body"): + var views = "" + renderHeader(tweet, retweet, prefs) + + var tweetClass = "tweet-content media-body" + if prefs.bidiSupport: + tweetClass &= " tweet-bidi" + + tdiv(class=tweetClass, dir="auto"): + verbatim replaceUrls(tweet.text, prefs) & renderLocation(tweet) + + if tweet.attribution.isSome: + renderAttribution(tweet.attribution.get(), prefs) + + if tweet.card.isSome: + renderCard(tweet.card.get(), prefs, path) + + if tweet.photos.len > 0: + renderAlbum(tweet) + elif tweet.video.isSome: + renderVideo(tweet.video.get(), prefs, path) + views = tweet.video.get().views + elif tweet.gif.isSome: + renderGif(tweet.gif.get(), prefs) + views = "GIF" + + if tweet.poll.isSome: + renderPoll(tweet.poll.get()) + + if tweet.quote.isSome: + renderQuote(tweet.quote.get(), prefs, path) + + p(class="tweet-published"): text getTime(tweet) + + if tweet.mediaTags.len > 0: + renderMediaTags(tweet.mediaTags) + + if not prefs.hideTweetStats: + renderStats(tweet.stats, views) + + proc renderTweet*(tweet: Tweet; prefs: Prefs; path: string; class=""; index=0; last=false; showThread=false; mainTweet=false; afterTweet=false): VNode = var divClass = class