Pārlūkot izejas kodu

Show Twitter link on search pages

Zed 7 gadi atpakaļ
vecāks
revīzija
381a8a106c
3 mainītis faili ar 33 papildinājumiem un 6 dzēšanām
  1. 29 2
      src/formatters.nim
  2. 1 1
      src/query.nim
  3. 3 3
      src/views/general.nim

+ 29 - 2
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, "")

+ 1 - 1
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

+ 3 - 3
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 = "<!DOCTYPE html>\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"