types.nim 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. # SPDX-License-Identifier: AGPL-3.0-only
  2. import times, sequtils, options, tables
  3. import prefs_impl
  4. genPrefsType()
  5. type
  6. RateLimitError* = object of CatchableError
  7. NoSessionsError* = object of CatchableError
  8. InternalError* = object of CatchableError
  9. BadClientError* = object of CatchableError
  10. TimelineKind* {.pure.} = enum
  11. tweets, replies, media
  12. ApiUrl* = object
  13. endpoint*: string
  14. params*: seq[(string, string)]
  15. ApiReq* = object
  16. oauth*: ApiUrl
  17. cookie*: ApiUrl
  18. RateLimit* = object
  19. limit*: int
  20. remaining*: int
  21. reset*: int
  22. SessionKind* = enum
  23. oauth
  24. cookie
  25. Session* = ref object
  26. id*: int64
  27. username*: string
  28. pending*: int
  29. limited*: bool
  30. limitedAt*: int
  31. apis*: Table[string, RateLimit]
  32. case kind*: SessionKind
  33. of oauth:
  34. oauthToken*: string
  35. oauthSecret*: string
  36. of cookie:
  37. authToken*: string
  38. ct0*: string
  39. Error* = enum
  40. null = 0
  41. noUserMatches = 17
  42. protectedUser = 22
  43. missingParams = 25
  44. timeout = 29
  45. couldntAuth = 32
  46. doesntExist = 34
  47. unauthorized = 37
  48. invalidParam = 47
  49. userNotFound = 50
  50. suspended = 63
  51. rateLimited = 88
  52. expiredToken = 89
  53. listIdOrSlug = 112
  54. tweetNotFound = 144
  55. tweetNotAuthorized = 179
  56. forbidden = 200
  57. badRequest = 214
  58. badToken = 239
  59. locked = 326
  60. noCsrf = 353
  61. tweetUnavailable = 421
  62. tweetCensored = 422
  63. VerifiedType* = enum
  64. none = "None"
  65. blue = "Blue"
  66. business = "Business"
  67. government = "Government"
  68. User* = object
  69. id*: string
  70. username*: string
  71. fullname*: string
  72. location*: string
  73. website*: string
  74. bio*: string
  75. userPic*: string
  76. banner*: string
  77. pinnedTweet*: int64
  78. following*: int
  79. followers*: int
  80. tweets*: int
  81. likes*: int
  82. media*: int
  83. verifiedType*: VerifiedType
  84. protected*: bool
  85. suspended*: bool
  86. joinDate*: DateTime
  87. VideoType* = enum
  88. m3u8 = "application/x-mpegURL"
  89. mp4 = "video/mp4"
  90. vmap = "video/vmap"
  91. VideoVariant* = object
  92. contentType*: VideoType
  93. url*: string
  94. bitrate*: int
  95. resolution*: int
  96. Video* = object
  97. durationMs*: int
  98. url*: string
  99. thumb*: string
  100. available*: bool
  101. reason*: string
  102. title*: string
  103. description*: string
  104. playbackType*: VideoType
  105. variants*: seq[VideoVariant]
  106. QueryKind* = enum
  107. posts, replies, media, users, tweets, userList
  108. Query* = object
  109. kind*: QueryKind
  110. view*: string
  111. text*: string
  112. filters*: seq[string]
  113. includes*: seq[string]
  114. excludes*: seq[string]
  115. fromUser*: seq[string]
  116. since*: string
  117. until*: string
  118. minLikes*: string
  119. sep*: string
  120. Gif* = object
  121. url*: string
  122. thumb*: string
  123. altText*: string
  124. Photo* = object
  125. url*: string
  126. altText*: string
  127. MediaKind* = enum
  128. photoMedia
  129. videoMedia
  130. gifMedia
  131. Media* = object
  132. case kind*: MediaKind
  133. of photoMedia:
  134. photo*: Photo
  135. of videoMedia:
  136. video*: Video
  137. of gifMedia:
  138. gif*: Gif
  139. MediaEntities* = seq[Media]
  140. GalleryPhoto* = object
  141. url*: string
  142. tweetId*: string
  143. color*: string
  144. PhotoRail* = seq[GalleryPhoto]
  145. Poll* = object
  146. options*: seq[string]
  147. values*: seq[int]
  148. votes*: int
  149. leader*: int
  150. status*: string
  151. CardKind* = enum
  152. amplify = "amplify"
  153. app = "app"
  154. appPlayer = "appplayer"
  155. player = "player"
  156. summary = "summary"
  157. summaryLarge = "summary_large_image"
  158. promoWebsite = "promo_website"
  159. promoVideo = "promo_video_website"
  160. promoVideoConvo = "promo_video_convo"
  161. promoImageConvo = "promo_image_convo"
  162. promoImageApp = "promo_image_app"
  163. storeLink = "direct_store_link_app"
  164. liveEvent = "live_event"
  165. broadcast = "broadcast"
  166. periscope = "periscope_broadcast"
  167. unified = "unified_card"
  168. moment = "moment"
  169. messageMe = "message_me"
  170. videoDirectMessage = "video_direct_message"
  171. imageDirectMessage = "image_direct_message"
  172. audiospace = "audiospace"
  173. newsletterPublication = "newsletter_publication"
  174. jobDetails = "job_details"
  175. hidden
  176. unknown
  177. Card* = object
  178. kind*: CardKind
  179. url*: string
  180. title*: string
  181. dest*: string
  182. text*: string
  183. image*: string
  184. video*: Option[Video]
  185. TweetStats* = object
  186. replies*: int
  187. retweets*: int
  188. likes*: int
  189. views*: int
  190. Tweet* = ref object
  191. id*: int64
  192. threadId*: int64
  193. replyId*: int64
  194. user*: User
  195. text*: string
  196. time*: DateTime
  197. reply*: seq[string]
  198. pinned*: bool
  199. hasThread*: bool
  200. available*: bool
  201. tombstone*: string
  202. location*: string
  203. # Unused, needed for backwards compat
  204. source*: string
  205. stats*: TweetStats
  206. retweet*: Option[Tweet]
  207. attribution*: Option[User]
  208. mediaTags*: seq[User]
  209. quote*: Option[Tweet]
  210. card*: Option[Card]
  211. poll*: Option[Poll]
  212. media*: MediaEntities
  213. history*: seq[int64]
  214. note*: string
  215. isAd*: bool
  216. isAI*: bool
  217. Tweets* = seq[Tweet]
  218. Result*[T] = object
  219. content*: seq[T]
  220. top*, bottom*: string
  221. beginning*: bool
  222. query*: Query
  223. Chain* = object
  224. content*: Tweets
  225. hasMore*: bool
  226. cursor*: string
  227. Conversation* = ref object
  228. tweet*: Tweet
  229. before*: Chain
  230. after*: Chain
  231. replies*: Result[Chain]
  232. EditHistory* = object
  233. latest*: Tweet
  234. history*: Tweets
  235. Timeline* = Result[Tweets]
  236. Profile* = object
  237. user*: User
  238. photoRail*: PhotoRail
  239. pinned*: Option[Tweet]
  240. tweets*: Timeline
  241. List* = object
  242. id*: string
  243. name*: string
  244. userId*: string
  245. username*: string
  246. description*: string
  247. members*: int
  248. banner*: string
  249. GlobalObjects* = ref object
  250. tweets*: Table[string, Tweet]
  251. users*: Table[string, User]
  252. Config* = ref object
  253. address*: string
  254. port*: int
  255. useHttps*: bool
  256. httpMaxConns*: int
  257. title*: string
  258. hostname*: string
  259. staticDir*: string
  260. hmacKey*: string
  261. base64Media*: bool
  262. minTokens*: int
  263. enableRSSUserTweets*: bool
  264. enableRSSUserReplies*: bool
  265. enableRSSUserMedia*: bool
  266. enableRSSSearch*: bool
  267. enableRSSList*: bool
  268. enableDebug*: bool
  269. proxy*: string
  270. proxyAuth*: string
  271. apiProxy*: string
  272. disableTid*: bool
  273. maxConcurrentReqs*: int
  274. maxRetries*: int
  275. retryDelayMs*: int
  276. rssCacheTime*: int
  277. listCacheTime*: int
  278. redisHost*: string
  279. redisPort*: int
  280. redisConns*: int
  281. redisMaxConns*: int
  282. redisPassword*: string
  283. Rss* = object
  284. feed*, cursor*: string
  285. proc contains*(thread: Chain; tweet: Tweet): bool =
  286. thread.content.anyIt(it.id == tweet.id)
  287. proc add*(timeline: var seq[Tweets]; tweet: Tweet) =
  288. timeline.add @[tweet]
  289. proc getPhotos*(tweet: Tweet): seq[Photo] =
  290. tweet.media.filterIt(it.kind == photoMedia).mapIt(it.photo)
  291. proc getVideos*(tweet: Tweet): seq[Video] =
  292. tweet.media.filterIt(it.kind == videoMedia).mapIt(it.video)
  293. proc hasPhotos*(tweet: Tweet): bool =
  294. tweet.media.anyIt(it.kind == photoMedia)
  295. proc hasVideos*(tweet: Tweet): bool =
  296. tweet.media.anyIt(it.kind == videoMedia)
  297. proc hasGifs*(tweet: Tweet): bool =
  298. tweet.media.anyIt(it.kind == gifMedia)
  299. proc getThumb*(media: Media): string =
  300. case media.kind
  301. of photoMedia: media.photo.url
  302. of videoMedia: media.video.thumb
  303. of gifMedia: media.gif.thumb