profile.nim 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # SPDX-License-Identifier: AGPL-3.0-only
  2. import strutils, strformat
  3. import karax/[karaxdsl, vdom, vstyles]
  4. import renderutils, search
  5. import ".."/[types, utils, formatters]
  6. proc renderStat(num: int; class: string; text=""): VNode =
  7. let t = if text.len > 0: text else: class
  8. buildHtml(li(class=class)):
  9. span(class="profile-stat-header"): text capitalizeAscii(t)
  10. span(class="profile-stat-num"):
  11. text insertSep($num, ',')
  12. proc renderUserCard*(user: User; prefs: Prefs): VNode =
  13. buildHtml(tdiv(class="profile-card")):
  14. tdiv(class="profile-card-info"):
  15. let
  16. url = getPicUrl(user.getUserPic())
  17. size =
  18. if prefs.autoplayGifs and user.userPic.endsWith("gif"): ""
  19. else: "_400x400"
  20. a(class="profile-card-avatar", href=url, target="_blank"):
  21. genImg(user.getUserPic(size))
  22. tdiv(class="profile-card-tabs-name"):
  23. linkUser(user, class="profile-card-fullname")
  24. linkUser(user, class="profile-card-username")
  25. tdiv(class="profile-card-extra"):
  26. if user.bio.len > 0:
  27. tdiv(class="profile-bio"):
  28. p(dir="auto"):
  29. verbatim replaceUrls(user.bio, prefs)
  30. if user.location.len > 0:
  31. tdiv(class="profile-location"):
  32. span: icon "location"
  33. let (place, url) = getLocation(user)
  34. if url.len > 1:
  35. a(href=url): text place
  36. elif "://" in place:
  37. a(href=place): text place
  38. else:
  39. span: text place
  40. if user.website.len > 0:
  41. tdiv(class="profile-website"):
  42. span:
  43. let url = replaceUrls(user.website, prefs)
  44. icon "link"
  45. a(href=url): text shortLink(url)
  46. tdiv(class="profile-joindate"):
  47. span(title=getJoinDateFull(user)):
  48. icon "calendar", getJoinDate(user)
  49. tdiv(class="profile-card-extra-links"):
  50. ul(class="profile-statlist"):
  51. renderStat(user.tweets, "posts", text="Tweets")
  52. renderStat(user.following, "following")
  53. renderStat(user.followers, "followers")
  54. renderStat(user.likes, "likes")
  55. proc renderPhotoRail(profile: Profile): VNode =
  56. let count = insertSep($profile.user.media, ',')
  57. buildHtml(tdiv(class="photo-rail-card")):
  58. tdiv(class="photo-rail-header"):
  59. a(href=(&"/{profile.user.username}/media")):
  60. icon "picture", count & " Photos and videos"
  61. input(id="photo-rail-grid-toggle", `type`="checkbox")
  62. label(`for`="photo-rail-grid-toggle", class="photo-rail-header-mobile"):
  63. icon "picture", count & " Photos and videos"
  64. icon "down"
  65. tdiv(class="photo-rail-grid"):
  66. for i, photo in profile.photoRail:
  67. if i == 16: break
  68. let photoSuffix =
  69. if "format" in photo.url or "placeholder" in photo.url: ""
  70. else: ":thumb"
  71. a(href=(&"/{profile.user.username}/status/{photo.tweetId}#m")):
  72. genImg(photo.url & photoSuffix)
  73. proc renderBanner(banner: string): VNode =
  74. buildHtml():
  75. if banner.len == 0:
  76. a()
  77. elif banner.startsWith('#'):
  78. a(style={backgroundColor: banner})
  79. else:
  80. a(href=getPicUrl(banner), target="_blank"): genImg(banner)
  81. proc renderProtected(username: string): VNode =
  82. buildHtml(tdiv(class="timeline-container")):
  83. tdiv(class="timeline-header timeline-protected"):
  84. h2: text "This account's tweets are protected."
  85. p: text &"Only confirmed followers have access to @{username}'s tweets."
  86. proc renderProfile*(profile: var Profile; prefs: Prefs; path: string): VNode =
  87. profile.tweets.query.fromUser = @[profile.user.username]
  88. buildHtml(tdiv(class="profile-tabs")):
  89. if not prefs.hideBanner:
  90. tdiv(class="profile-banner"):
  91. renderBanner(profile.user.banner)
  92. let sticky = if prefs.stickyProfile: " sticky" else: ""
  93. tdiv(class=(&"profile-tab{sticky}")):
  94. renderUserCard(profile.user, prefs)
  95. if profile.photoRail.len > 0:
  96. renderPhotoRail(profile)
  97. if profile.user.protected:
  98. renderProtected(profile.user.username)
  99. else:
  100. renderTweetSearch(profile.tweets, prefs, path, profile.pinned)