changed code to be not shit

This commit is contained in:
Mitarashi 2022-01-14 19:01:47 +02:00
parent 7f15993a74
commit 0d3469df66
6 changed files with 80 additions and 37 deletions

View file

@ -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

View file

@ -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, ""

View file

@ -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)

View file

@ -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))

View file

@ -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))

View file

@ -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