瀏覽代碼

Rework list api

jackyzy823 5 年之前
父節點
當前提交
35bb5f9132
共有 7 個文件被更改,包括 38 次插入36 次删除
  1. 2 2
      src/api.nim
  2. 1 1
      src/parser.nim
  3. 6 6
      src/redis_cache.nim
  4. 24 21
      src/routes/list.nim
  5. 3 4
      src/routes/rss.nim
  6. 1 1
      src/views/list.nim
  7. 1 1
      src/views/rss.nimf

+ 2 - 2
src/api.nim

@@ -9,13 +9,13 @@ proc getGraphProfile*(username: string): Future[Profile] {.async.} =
     js = await fetch(graphUser ? {"variables": $variables})
   result = parseGraphProfile(js, username)
 
-proc getGraphList*(name, list: string): Future[List] {.async.} =
+proc getGraphListBySlug*(name, list: string): Future[List] {.async.} =
   let
     variables = %*{"screenName": name, "listSlug": list, "withHighlightedLabel": false}
     js = await fetch(graphList ? {"variables": $variables})
   result = parseGraphList(js)
 
-proc getGraphListById*(id: string): Future[List] {.async.} =
+proc getGraphList*(id: string): Future[List] {.async.} =
   let
     variables = %*{"listId": id, "withHighlightedLabel": false}
     js = await fetch(graphListId ? {"variables": $variables})

+ 1 - 1
src/parser.nim

@@ -73,7 +73,7 @@ proc parseGraphList*(js: JsonNode): List =
 
   result = List(
     id: list{"id_str"}.getStr,
-    name: list{"name"}.getStr.replace(' ', '-'),
+    name: list{"name"}.getStr,
     username: list{"user", "legacy", "screen_name"}.getStr,
     userId: list{"user", "legacy", "id_str"}.getStr,
     description: list{"description"}.getStr,

+ 6 - 6
src/redis_cache.nim

@@ -56,7 +56,7 @@ proc initRedisPool*(cfg: Config) {.async.} =
 
 template pidKey(name: string): string = "pid:" & $(hash(name) div 1_000_000)
 template profileKey(name: string): string = "p:" & name
-template listKey(l: List): string = toLower("l:" & l.username & '/' & l.name)
+template listKey(l: List): string = "l:" & l.id
 
 proc get(query: string): Future[string] {.async.} =
   pool.withAcquire(r):
@@ -129,17 +129,17 @@ proc getCachedPhotoRail*(name: string): Future[PhotoRail] {.async.} =
     result = await getPhotoRail(name)
     await cache(result, name)
 
-proc getCachedList*(username=""; name=""; id=""): Future[List] {.async.} =
-  let list = if id.len > 0: redisNil
-             else: await get(toLower("l:" & username & '/' & name))
+proc getCachedList*(username=""; slug=""; id=""): Future[List] {.async.} =
+  let list = if id.len == 0: redisNil
+             else: await get("l:" & id)
 
   if list != redisNil:
     result = fromFlatty(uncompress(list), List)
   else:
     if id.len > 0:
-      result = await getGraphListById(id)
+      result = await getGraphList(id)
     else:
-      result = await getGraphList(username, name)
+      result = await getGraphListBySlug(username, slug)
     await cache(result)
 
 proc getCachedRss*(key: string): Future[Rss] {.async.} =

+ 24 - 21
src/routes/list.nim

@@ -1,5 +1,5 @@
 # SPDX-License-Identifier: AGPL-3.0-only
-import strutils
+import strutils, uri
 
 import jester
 
@@ -8,41 +8,44 @@ import ".."/[types, redis_cache, api]
 import ../views/[general, timeline, list]
 export getListTimeline, getGraphList
 
-template respList*(list, timeline, vnode: typed) =
+template respList*(list, timeline, title, vnode: typed) =
   if list.id.len == 0:
-    resp Http404, showError("List \"" & @"list" & "\" not found", cfg)
+    resp Http404, showError("List \"" & @"id" & "\" not found", cfg)
 
   let
     html = renderList(vnode, timeline.query, list)
-    rss = "/$1/lists/$2/rss" % [@"name", @"list"]
+    rss = "/i/lists/$1/rss" % [@"id"]
 
-  resp renderMain(html, request, cfg, prefs, rss=rss, banner=list.banner)
+  resp renderMain(html, request, cfg, prefs, titleText=title, rss=rss, banner=list.banner)
 
 proc createListRouter*(cfg: Config) =
   router list:
-    get "/@name/lists/@list/?":
+    get "/@name/lists/@slug/?":
       cond '.' notin @"name"
       cond @"name" != "i"
+      cond @"slug" != "memberships"
+      let
+        slug = decodeUrl(@"slug")
+        list = await getCachedList(@"name", slug)
+      if list.id.len == 0:
+        resp Http404, showError("List \"" & @"slug" & "\" not found", cfg)
+      redirect("/i/lists/" & list.id)
+
+    get "/i/lists/@id/?":
+      cond '.' notin @"id"
       let
         prefs = cookiePrefs()
-        list = await getCachedList(@"name", @"list")
+        list = await getCachedList(id=(@"id"))
+        title = "@" & list.username & "/" & list.name
         timeline = await getListTimeline(list.id, getCursor())
         vnode = renderTimelineTweets(timeline, prefs, request.path)
-      respList(list, timeline, vnode)
+      respList(list, timeline, title, vnode)
 
-    get "/@name/lists/@list/members":
-      cond '.' notin @"name"
-      cond @"name" != "i"
+    get "/i/lists/@id/members":
+      cond '.' notin @"id"
       let
         prefs = cookiePrefs()
-        list = await getCachedList(@"name", @"list")
+        list = await getCachedList(id=(@"id"))
+        title = "@" & list.username & "/" & list.name
         members = await getListMembers(list, getCursor())
-      respList(list, members, renderTimelineUsers(members, prefs, request.path))
-
-    get "/i/lists/@id/?":
-      cond '.' notin @"id"
-      let list = await getCachedList(id=(@"id"))
-      if list.id.len == 0:
-        resp Http404
-      await cache(list)
-      redirect("/" & list.username & "/lists/" & list.name)
+      respList(list, members, title, renderTimelineUsers(members, prefs, request.path))

+ 3 - 4
src/routes/rss.nim

@@ -119,19 +119,18 @@ proc createRssRouter*(cfg: Config) =
       await cacheRss(key, rss)
       respRss(rss)
 
-    get "/@name/lists/@list/rss":
+    get "/i/lists/@id/rss":
       cond cfg.enableRss
-      cond '.' notin @"name"
       let
         cursor = getCursor()
-        key = @"name" & "/" & @"list" & cursor
+        key = @"id" & cursor
 
       var rss = await getCachedRss(key)
       if rss.cursor.len > 0:
         respRss(rss)
 
       let
-        list = await getCachedList(@"name", @"list")
+        list = await getCachedList(id=(@"id"))
         timeline = await getListTimeline(list.id, cursor)
       rss.cursor = timeline.bottom
       rss.feed = compress renderListRss(timeline.content, list, cfg)

+ 1 - 1
src/views/list.nim

@@ -25,5 +25,5 @@ proc renderList*(body: VNode; query: Query; list: List): VNode =
       tdiv(class="timeline-description"):
         text list.description
 
-    renderListTabs(query, &"/{list.username}/lists/{list.name}")
+    renderListTabs(query, &"/i/lists/{list.id}")
     body

+ 1 - 1
src/views/rss.nimf

@@ -109,7 +109,7 @@ ${renderRssTweets(timeline.content, cfg)}
 #end proc
 #
 #proc renderListRss*(tweets: seq[Tweet]; list: List; cfg: Config): string =
-#let link = &"{getUrlPrefix(cfg)}/{list.username}/lists/{list.name}"
+#let link = &"{getUrlPrefix(cfg)}/i/lists/{list.id}"
 #result = ""
 <?xml version="1.0" encoding="UTF-8"?>
 <rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">