types.nim 9.7 KB

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