From 381a8a106c3ff2bfada6fc24fe0e7680080029d4 Mon Sep 17 00:00:00 2001 From: Zed Date: Tue, 8 Oct 2019 15:07:10 +0200 Subject: [PATCH] Show Twitter link on search pages --- src/formatters.nim | 31 +++++++++++++++++++++++++++++-- src/query.nim | 2 +- src/views/general.nim | 6 +++--- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/formatters.nim b/src/formatters.nim index 61c9085..f34af74 100644 --- a/src/formatters.nim +++ b/src/formatters.nim @@ -1,7 +1,7 @@ -import strutils, strformat, sequtils, htmlgen, xmltree, times, uri +import strutils, strformat, sequtils, htmlgen, xmltree, times, uri, tables import regex -import types, utils +import types, utils, query from unicode import Rune, `$` @@ -139,3 +139,30 @@ proc getLink*(tweet: Tweet | Quote): string = proc getTombstone*(text: string): string = text.replace(re"\n* *Learn more", "").stripText().strip(chars={' ', '\n'}) + +proc getTwitterLink*(path: string; params: Table[string, string]): string = + let + twitter = parseUri("https://twitter.com") + username = params.getOrDefault("name") + query = initQuery(params, username) + + var after = params.getOrDefault("after", "0") + if query.kind notin {userList, users} and "/members" notin path: + after = after.genPos() + + var paramList = filterParams(params).mapIt( + if it[0] == "after": ("max_position", after) else: it) + + if "/search" notin path: + return $(twitter / path ? paramList) + + let p = { + "f": $query.kind, + "q": genQueryParam(query), + "src": "typd", + "max_position": after + } + + result = $(parseUri("https://twitter.com") / path ? p) + if username.len > 0: + result = result.replace("/" & username, "") diff --git a/src/query.nim b/src/query.nim index 232f954..bb5bf97 100644 --- a/src/query.nim +++ b/src/query.nim @@ -110,7 +110,7 @@ proc cleanPos*(pos: string): string = pos.multiReplace((posPrefix, ""), (posSuffix, "")) proc genPos*(pos: string): string = - if pos.len == 0: return + if pos.len == 0 or pos == "0": return pos result = posPrefix & pos if "A==" notin result: result &= posSuffix diff --git a/src/views/general.nim b/src/views/general.nim index d9363a1..0cd70db 100644 --- a/src/views/general.nim +++ b/src/views/general.nim @@ -2,7 +2,7 @@ import uri, strutils import karax/[karaxdsl, vdom] import renderutils -import ../utils, ../types, ../prefs +import ../utils, ../types, ../prefs, ../formatters import jester @@ -10,7 +10,7 @@ const doctype = "\n" proc renderNavbar*(title, rss: string; req: Request): VNode = let path = $(parseUri(req.path) ? filterParams(req.params)) - let twitPath = "https://twitter.com" & path.replace("after=", "max_position=") + let twitterPath = getTwitterLink(req.path, req.params) buildHtml(nav): tdiv(class="inner-nav"): @@ -23,7 +23,7 @@ proc renderNavbar*(title, rss: string; req: Request): VNode = icon "search", title="Search", href="/search" if rss.len > 0: icon "rss-feed", title="RSS Feed", href=rss - icon "bird", title="Open in Twitter", href=twitPath + icon "bird", title="Open in Twitter", href=twitterPath icon "info-circled", title="About", href="/about" iconReferer "cog", "/settings", path, title="Preferences"