article.nim 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # SPDX-License-Identifier: AGPL-3.0-only
  2. import asyncdispatch, tables, strutils
  3. import jester, karax/vdom
  4. import ".."/[types, api]
  5. import ../views/[article, general]
  6. import router_utils
  7. export api, article, vdom, general, router_utils
  8. proc createArticleRouter*(cfg: Config) =
  9. router articleRoute:
  10. get "/i/article/@id":
  11. cond @"id".allCharsInSet(Digits)
  12. let article = await getGraphArticle(@"id")
  13. if article == nil:
  14. resp Http404, showError("Article not found", cfg)
  15. var tweetIds: seq[string]
  16. for e in article.entities.values:
  17. if e.kind == "TWEET":
  18. tweetIds.add e.tweetId
  19. var tweets = initTable[int64, Tweet]()
  20. if tweetIds.len > 0:
  21. try:
  22. for t in await getGraphTweetResults(tweetIds):
  23. tweets[t.id] = t
  24. except CatchableError:
  25. discard
  26. let
  27. prefs = requestPrefs()
  28. path = getPath()
  29. html = renderArticle(article, tweets, path, prefs, @"id")
  30. twitterUrl = "https://x.com/" & article.user.username & "/article/" & @"id"
  31. resp renderMain(html, request, cfg, prefs, titleText=article.title,
  32. twitterLink=twitterUrl)
  33. get "/@name/article/@id/?":
  34. cond '.' notin @"name"
  35. cond @"id".allCharsInSet(Digits)
  36. redirect("/i/article/" & @"id")
  37. get "/@name/status/@id/article":
  38. cond '.' notin @"name"
  39. cond @"id".allCharsInSet(Digits)
  40. redirect("/i/article/" & @"id")