Show Twitter link on search pages

This commit is contained in:
Zed 2019-10-08 15:07:10 +02:00
parent e090dde1ea
commit 381a8a106c
3 changed files with 33 additions and 6 deletions

View File

@ -1,7 +1,7 @@
import strutils, strformat, sequtils, htmlgen, xmltree, times, uri import strutils, strformat, sequtils, htmlgen, xmltree, times, uri, tables
import regex import regex
import types, utils import types, utils, query
from unicode import Rune, `$` from unicode import Rune, `$`
@ -139,3 +139,30 @@ proc getLink*(tweet: Tweet | Quote): string =
proc getTombstone*(text: string): string = proc getTombstone*(text: string): string =
text.replace(re"\n* *Learn more", "").stripText().strip(chars={' ', '\n'}) 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, "")

View File

@ -110,7 +110,7 @@ proc cleanPos*(pos: string): string =
pos.multiReplace((posPrefix, ""), (posSuffix, "")) pos.multiReplace((posPrefix, ""), (posSuffix, ""))
proc genPos*(pos: string): string = proc genPos*(pos: string): string =
if pos.len == 0: return if pos.len == 0 or pos == "0": return pos
result = posPrefix & pos result = posPrefix & pos
if "A==" notin result: if "A==" notin result:
result &= posSuffix result &= posSuffix

View File

@ -2,7 +2,7 @@ import uri, strutils
import karax/[karaxdsl, vdom] import karax/[karaxdsl, vdom]
import renderutils import renderutils
import ../utils, ../types, ../prefs import ../utils, ../types, ../prefs, ../formatters
import jester import jester
@ -10,7 +10,7 @@ const doctype = "<!DOCTYPE html>\n"
proc renderNavbar*(title, rss: string; req: Request): VNode = proc renderNavbar*(title, rss: string; req: Request): VNode =
let path = $(parseUri(req.path) ? filterParams(req.params)) 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): buildHtml(nav):
tdiv(class="inner-nav"): tdiv(class="inner-nav"):
@ -23,7 +23,7 @@ proc renderNavbar*(title, rss: string; req: Request): VNode =
icon "search", title="Search", href="/search" icon "search", title="Search", href="/search"
if rss.len > 0: if rss.len > 0:
icon "rss-feed", title="RSS Feed", href=rss 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" icon "info-circled", title="About", href="/about"
iconReferer "cog", "/settings", path, title="Preferences" iconReferer "cog", "/settings", path, title="Preferences"