Reduce usage of strformat, minor perf improvement

This commit is contained in:
Zed 2022-11-27 15:50:08 +01:00
parent 92a6eb0339
commit 85316f8f8d
6 changed files with 36 additions and 31 deletions

View file

@ -58,11 +58,11 @@ proc replaceUrls*(body: string; prefs: Prefs; absolute=""): string =
result = result.replace(ytRegex, prefs.replaceYouTube) result = result.replace(ytRegex, prefs.replaceYouTube)
if prefs.replaceTwitter.len > 0 and ("twitter.com" in body or tco in body): if prefs.replaceTwitter.len > 0 and ("twitter.com" in body or tco in body):
result = result.replace(tco, &"{https}{prefs.replaceTwitter}/t.co") result = result.replace(tco, https & prefs.replaceTwitter & "/t.co")
result = result.replace(cards, prefs.replaceTwitter & "/cards") result = result.replace(cards, prefs.replaceTwitter & "/cards")
result = result.replace(twRegex, prefs.replaceTwitter) result = result.replace(twRegex, prefs.replaceTwitter)
result = result.replacef(twLinkRegex, a( result = result.replacef(twLinkRegex, a(
prefs.replaceTwitter & "$2", href = &"{https}{prefs.replaceTwitter}$1")) prefs.replaceTwitter & "$2", href = https & prefs.replaceTwitter & "$1"))
if prefs.replaceReddit.len > 0 and ("reddit.com" in result or "redd.it" in result): if prefs.replaceReddit.len > 0 and ("reddit.com" in result or "redd.it" in result):
result = result.replace(rdShortRegex, prefs.replaceReddit & "/comments/") result = result.replace(rdShortRegex, prefs.replaceReddit & "/comments/")

View file

@ -10,6 +10,11 @@ include "../views/rss.nimf"
export times, hashes export times, hashes
proc redisKey*(page, name, cursor: string): string =
result = page & ":" & name
if cursor.len > 0:
result &= ":" & cursor
proc timelineRss*(req: Request; cfg: Config; query: Query): Future[Rss] {.async.} = proc timelineRss*(req: Request; cfg: Config; query: Query): Future[Rss] {.async.} =
var profile: Profile var profile: Profile
let let
@ -42,8 +47,8 @@ proc timelineRss*(req: Request; cfg: Config; query: Query): Future[Rss] {.async.
template respRss*(rss, page) = template respRss*(rss, page) =
if rss.cursor.len == 0: if rss.cursor.len == 0:
let info = case page let info = case page
of "User": &""" "{@"name"}" """ of "User": " \"" & @"name" & "\" "
of "List": &""" "{@"id"}" """ of "List": " \"" & @"id" & "\" "
else: " " else: " "
resp Http404, showError(page & info & "not found", cfg) resp Http404, showError(page & info & "not found", cfg)
@ -67,7 +72,7 @@ proc createRssRouter*(cfg: Config) =
let let
cursor = getCursor() cursor = getCursor()
key = &"search:{hash(genQueryUrl(query))}:cursor" key = redisKey("search", $hash(genQueryUrl(query)), cursor)
var rss = await getCachedRss(key) var rss = await getCachedRss(key)
if rss.cursor.len > 0: if rss.cursor.len > 0:
@ -84,9 +89,8 @@ proc createRssRouter*(cfg: Config) =
cond cfg.enableRss cond cfg.enableRss
cond '.' notin @"name" cond '.' notin @"name"
let let
cursor = getCursor()
name = @"name" name = @"name"
key = &"twitter:{name}:{cursor}" key = redisKey("twitter", name, getCursor())
var rss = await getCachedRss(key) var rss = await getCachedRss(key)
if rss.cursor.len > 0: if rss.cursor.len > 0:
@ -101,18 +105,20 @@ proc createRssRouter*(cfg: Config) =
cond cfg.enableRss cond cfg.enableRss
cond '.' notin @"name" cond '.' notin @"name"
cond @"tab" in ["with_replies", "media", "search"] cond @"tab" in ["with_replies", "media", "search"]
let name = @"name" let
let query = name = @"name"
case @"tab" tab = @"tab"
query =
case tab
of "with_replies": getReplyQuery(name) of "with_replies": getReplyQuery(name)
of "media": getMediaQuery(name) of "media": getMediaQuery(name)
of "search": initQuery(params(request), name=name) of "search": initQuery(params(request), name=name)
else: Query(fromUser: @[name]) else: Query(fromUser: @[name])
var key = &"""{@"tab"}:{@"name"}:""" let searchKey = if tab != "search": ""
if @"tab" == "search": else: ":" & $hash(genQueryUrl(query))
key &= $hash(genQueryUrl(query)) & ":"
key &= getCursor() let key = redisKey(tab, name & searchKey, getCursor())
var rss = await getCachedRss(key) var rss = await getCachedRss(key)
if rss.cursor.len > 0: if rss.cursor.len > 0:
@ -132,28 +138,27 @@ proc createRssRouter*(cfg: Config) =
cursor = getCursor() cursor = getCursor()
if list.id.len == 0: if list.id.len == 0:
resp Http404, showError(&"""List "{@"slug"}" not found""", cfg) resp Http404, showError("List \"" & @"slug" & "\" not found", cfg)
let url = &"/i/lists/{list.id}/rss" let url = "/i/lists/" & list.id & "/rss"
if cursor.len > 0: if cursor.len > 0:
redirect(&"{url}?cursor={encodeUrl(cursor, false)}") redirect(url & "?cursor=" & encodeUrl(cursor, false))
else: else:
redirect(url) redirect(url)
get "/i/lists/@id/rss": get "/i/lists/@id/rss":
cond cfg.enableRss cond cfg.enableRss
let let
id = @"id"
cursor = getCursor() cursor = getCursor()
key = key = redisKey("lists", id, cursor)
if cursor.len == 0: "lists:" & @"id"
else: &"""lists:{@"id"}:{cursor}"""
var rss = await getCachedRss(key) var rss = await getCachedRss(key)
if rss.cursor.len > 0: if rss.cursor.len > 0:
respRss(rss, "List") respRss(rss, "List")
let let
list = await getCachedList(id=(@"id")) list = await getCachedList(id=id)
timeline = await getListTimeline(list.id, cursor) timeline = await getListTimeline(list.id, cursor)
rss.cursor = timeline.bottom rss.cursor = timeline.bottom
rss.feed = renderListRss(timeline.content, list, cfg) rss.feed = renderListRss(timeline.content, list, cfg)

View file

@ -1,5 +1,5 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
import strutils, strformat, uri import strutils, uri
import jester import jester
@ -37,7 +37,7 @@ proc createSearchRouter*(cfg: Config) =
resp Http404, showError("Invalid search", cfg) resp Http404, showError("Invalid search", cfg)
get "/hashtag/@hash": get "/hashtag/@hash":
redirect(&"""/search?q={encodeUrl("#" & @"hash")}""") redirect("/search?q=" & encodeUrl("#" & @"hash"))
get "/opensearch": get "/opensearch":
let url = getUrlPrefix(cfg) & "/search?q=" let url = getUrlPrefix(cfg) & "/search?q="

View file

@ -1,5 +1,5 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
import asyncdispatch, strutils, strformat, sequtils, uri, options, times import asyncdispatch, strutils, sequtils, uri, options, times
import jester, karax/vdom import jester, karax/vdom
import router_utils import router_utils
@ -102,7 +102,7 @@ proc showTimeline*(request: Request; query: Query; cfg: Config; prefs: Prefs;
template respTimeline*(timeline: typed) = template respTimeline*(timeline: typed) =
let t = timeline let t = timeline
if t.len == 0: if t.len == 0:
resp Http404, showError(&"""User "{@"name"}" not found""", cfg) resp Http404, showError("User \"" & @"name" & "\" not found", cfg)
resp t resp t
template respUserId*() = template respUserId*() =

View file

@ -81,7 +81,7 @@ proc renderHead*(prefs: Prefs; cfg: Config; req: Request; titleText=""; desc="";
title: title:
if titleText.len > 0: if titleText.len > 0:
text &"{titleText}|{cfg.title}" text titleText & " | " & cfg.title
else: else:
text cfg.title text cfg.title

View file

@ -108,7 +108,7 @@ proc renderProfile*(profile: var Profile; prefs: Prefs; path: string): VNode =
renderBanner(profile.user.banner) renderBanner(profile.user.banner)
let sticky = if prefs.stickyProfile: " sticky" else: "" let sticky = if prefs.stickyProfile: " sticky" else: ""
tdiv(class=(&"profile-tab{sticky}")): tdiv(class=("profile-tab" & sticky)):
renderUserCard(profile.user, prefs) renderUserCard(profile.user, prefs)
if profile.photoRail.len > 0: if profile.photoRail.len > 0:
renderPhotoRail(profile) renderPhotoRail(profile)