types.nim 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. skipTid*: bool
  16. ApiReq* = object
  17. oauth*: ApiUrl
  18. cookie*: ApiUrl
  19. RateLimit* = object
  20. limit*: int
  21. remaining*: int
  22. reset*: int
  23. SessionKind* = enum
  24. oauth
  25. cookie
  26. Session* = ref object
  27. id*: int64
  28. username*: string
  29. pending*: int
  30. limited*: bool
  31. limitedAt*: int
  32. apis*: Table[string, RateLimit]
  33. case kind*: SessionKind
  34. of oauth:
  35. oauthToken*: string
  36. oauthSecret*: string
  37. of cookie:
  38. authToken*: string
  39. ct0*: string
  40. Error* = enum
  41. null = 0
  42. noUserMatches = 17
  43. protectedUser = 22
  44. missingParams = 25
  45. timeout = 29
  46. couldntAuth = 32
  47. doesntExist = 34
  48. unauthorized = 37
  49. invalidParam = 47
  50. userNotFound = 50
  51. suspended = 63
  52. rateLimited = 88
  53. expiredToken = 89
  54. listIdOrSlug = 112
  55. tweetNotFound = 144
  56. tweetNotAuthorized = 179
  57. forbidden = 200
  58. badRequest = 214
  59. badToken = 239
  60. locked = 326
  61. noCsrf = 353
  62. tweetUnavailable = 421
  63. tweetCensored = 422
  64. VerifiedType* = enum
  65. none = "None"
  66. blue = "Blue"
  67. business = "Business"
  68. government = "Government"
  69. User* = object
  70. id*: string
  71. username*: string
  72. fullname*: string
  73. location*: string
  74. website*: string
  75. bio*: string
  76. userPic*: string
  77. banner*: string
  78. pinnedTweet*: int64
  79. following*: int
  80. followers*: int
  81. tweets*: int
  82. likes*: int
  83. media*: int
  84. verifiedType*: VerifiedType
  85. protected*: bool
  86. suspended*: bool
  87. joinDate*: DateTime
  88. AccountInfo* = object
  89. username*: string
  90. fullname*: string
  91. userPic*: string
  92. joinDate*: DateTime
  93. verifiedType*: VerifiedType
  94. suspended*: bool
  95. basedIn*: string
  96. source*: string
  97. usernameChanges*: int
  98. lastUsernameChange*: DateTime
  99. affiliateUsername*: string
  100. affiliateLabel*: string
  101. isIdentityVerified*: bool
  102. verifiedSince*: DateTime
  103. overrideVerifiedYear*: int
  104. Broadcast* = object
  105. id*: string
  106. title*: string
  107. state*: string
  108. thumb*: string
  109. mediaKey*: string
  110. m3u8Url*: string
  111. totalWatched*: int
  112. startTime*: DateTime
  113. endTime*: DateTime
  114. replayStart*: int
  115. availableForReplay*: bool
  116. user*: User
  117. VideoType* = enum
  118. m3u8 = "application/x-mpegURL"
  119. mp4 = "video/mp4"
  120. vmap = "video/vmap"
  121. VideoVariant* = object
  122. contentType*: VideoType
  123. url*: string
  124. bitrate*: int
  125. resolution*: int
  126. Video* = object
  127. durationMs*: int
  128. url*: string
  129. thumb*: string
  130. available*: bool
  131. reason*: string
  132. title*: string
  133. description*: string
  134. playbackType*: VideoType
  135. variants*: seq[VideoVariant]
  136. QueryKind* = enum
  137. posts, replies, media, users, tweets, userList
  138. Query* = object
  139. kind*: QueryKind
  140. view*: string
  141. text*: string
  142. filters*: seq[string]
  143. includes*: seq[string]
  144. excludes*: seq[string]
  145. fromUser*: seq[string]
  146. since*: string
  147. until*: string
  148. minLikes*: string
  149. sep*: string
  150. Gif* = object
  151. url*: string
  152. thumb*: string
  153. altText*: string
  154. Photo* = object
  155. url*: string
  156. altText*: string
  157. MediaKind* = enum
  158. photoMedia
  159. videoMedia
  160. gifMedia
  161. Media* = object
  162. case kind*: MediaKind
  163. of photoMedia:
  164. photo*: Photo
  165. of videoMedia:
  166. video*: Video
  167. of gifMedia:
  168. gif*: Gif
  169. MediaEntities* = seq[Media]
  170. GalleryPhoto* = object
  171. url*: string
  172. tweetId*: string
  173. color*: string
  174. PhotoRail* = seq[GalleryPhoto]
  175. Article* = ref object
  176. title*: string
  177. coverImage*: string
  178. user*: User
  179. time*: DateTime
  180. stats*: TweetStats
  181. paragraphs*: seq[ArticleParagraph]
  182. entities*: Table[int, ArticleEntity]
  183. media*: Table[string, ArticleMedia]
  184. ArticleParagraph* = object
  185. text*: string
  186. kind*: string
  187. inlineStyles*: seq[ArticleStyle]
  188. entityRanges*: seq[ArticleEntityRange]
  189. ArticleStyle* = object
  190. offset*: int
  191. length*: int
  192. style*: string
  193. ArticleEntityRange* = object
  194. offset*: int
  195. length*: int
  196. key*: int
  197. ArticleEntity* = object
  198. kind*: string
  199. url*: string
  200. mediaIds*: seq[string]
  201. tweetId*: string
  202. markdown*: string
  203. ArticleMedia* = object
  204. kind*: string
  205. url*: string
  206. Poll* = object
  207. options*: seq[string]
  208. values*: seq[int]
  209. votes*: int
  210. leader*: int
  211. status*: string
  212. CardKind* = enum
  213. amplify = "amplify"
  214. app = "app"
  215. appPlayer = "appplayer"
  216. player = "player"
  217. summary = "summary"
  218. summaryLarge = "summary_large_image"
  219. promoWebsite = "promo_website"
  220. promoVideo = "promo_video_website"
  221. promoVideoConvo = "promo_video_convo"
  222. promoImageConvo = "promo_image_convo"
  223. promoImageApp = "promo_image_app"
  224. storeLink = "direct_store_link_app"
  225. liveEvent = "live_event"
  226. broadcast = "broadcast"
  227. periscope = "periscope_broadcast"
  228. unified = "unified_card"
  229. moment = "moment"
  230. messageMe = "message_me"
  231. videoDirectMessage = "video_direct_message"
  232. imageDirectMessage = "image_direct_message"
  233. audiospace = "audiospace"
  234. newsletterPublication = "newsletter_publication"
  235. jobDetails = "job_details"
  236. hidden
  237. unknown
  238. Card* = object
  239. kind*: CardKind
  240. url*: string
  241. title*: string
  242. dest*: string
  243. text*: string
  244. image*: string
  245. video*: Option[Video]
  246. TweetStats* = object
  247. replies*: int
  248. retweets*: int
  249. likes*: int
  250. views*: int
  251. ArticlePreview* = object
  252. title*: string
  253. previewText*: string
  254. coverImage*: string
  255. tweetId*: int64
  256. Tweet* = ref object
  257. id*: int64
  258. threadId*: int64
  259. replyId*: int64
  260. user*: User
  261. text*: string
  262. time*: DateTime
  263. reply*: seq[string]
  264. pinned*: bool
  265. hasThread*: bool
  266. available*: bool
  267. tombstone*: string
  268. location*: string
  269. # Unused, needed for backwards compat
  270. source*: string
  271. stats*: TweetStats
  272. retweet*: Option[Tweet]
  273. attribution*: Option[User]
  274. attributionLink*: string
  275. mediaTags*: seq[User]
  276. quote*: Option[Tweet]
  277. card*: Option[Card]
  278. poll*: Option[Poll]
  279. media*: MediaEntities
  280. history*: seq[int64]
  281. note*: string
  282. isAd*: bool
  283. isAI*: bool
  284. articlePreview*: Option[ArticlePreview]
  285. Tweets* = seq[Tweet]
  286. Result*[T] = object
  287. content*: seq[T]
  288. top*, bottom*: string
  289. beginning*: bool
  290. query*: Query
  291. Chain* = object
  292. content*: Tweets
  293. hasMore*: bool
  294. cursor*: string
  295. Conversation* = ref object
  296. tweet*: Tweet
  297. before*: Chain
  298. after*: Chain
  299. replies*: Result[Chain]
  300. EditHistory* = object
  301. latest*: Tweet
  302. history*: Tweets
  303. Timeline* = Result[Tweets]
  304. Profile* = object
  305. user*: User
  306. photoRail*: PhotoRail
  307. pinned*: Option[Tweet]
  308. tweets*: Timeline
  309. accountInfo*: AccountInfo
  310. List* = object
  311. id*: string
  312. name*: string
  313. userId*: string
  314. username*: string
  315. description*: string
  316. members*: int
  317. banner*: string
  318. GlobalObjects* = ref object
  319. tweets*: Table[string, Tweet]
  320. users*: Table[string, User]
  321. Config* = ref object
  322. address*: string
  323. port*: int
  324. useHttps*: bool
  325. httpMaxConns*: int
  326. title*: string
  327. hostname*: string
  328. staticDir*: string
  329. hmacKey*: string
  330. base64Media*: bool
  331. minTokens*: int
  332. enableRSSUserTweets*: bool
  333. enableRSSUserReplies*: bool
  334. enableRSSUserMedia*: bool
  335. enableRSSSearch*: bool
  336. enableRSSList*: bool
  337. enableDebug*: bool
  338. proxy*: string
  339. proxyAuth*: string
  340. apiProxy*: string
  341. disableTid*: bool
  342. maxConcurrentReqs*: int
  343. maxRetries*: int
  344. retryDelayMs*: int
  345. rssCacheTime*: int
  346. listCacheTime*: int
  347. redisHost*: string
  348. redisPort*: int
  349. redisConns*: int
  350. redisMaxConns*: int
  351. redisPassword*: string
  352. Rss* = object
  353. feed*, cursor*: string
  354. proc contains*(thread: Chain; tweet: Tweet): bool =
  355. thread.content.anyIt(it.id == tweet.id)
  356. proc add*(timeline: var seq[Tweets]; tweet: Tweet) =
  357. timeline.add @[tweet]
  358. proc getPhotos*(tweet: Tweet): seq[Photo] =
  359. tweet.media.filterIt(it.kind == photoMedia).mapIt(it.photo)
  360. proc getVideos*(tweet: Tweet): seq[Video] =
  361. tweet.media.filterIt(it.kind == videoMedia).mapIt(it.video)
  362. proc hasPhotos*(tweet: Tweet): bool =
  363. tweet.media.anyIt(it.kind == photoMedia)
  364. proc hasVideos*(tweet: Tweet): bool =
  365. tweet.media.anyIt(it.kind == videoMedia)
  366. proc hasGifs*(tweet: Tweet): bool =
  367. tweet.media.anyIt(it.kind == gifMedia)
  368. proc getThumb*(media: Media): string =
  369. case media.kind
  370. of photoMedia: media.photo.url
  371. of videoMedia: media.video.thumb
  372. of gifMedia: media.gif.thumb