xenforo.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. import { Dict, tryDo, isHttpError, assertOk } from "./util";
  2. import got, { Method } from "got";
  3. export interface RequestError {
  4. code: string;
  5. message: string;
  6. params: { key: string; value: unknown; }[];
  7. }
  8. export type RequestErrorSet = { errors: RequestError[] };
  9. export class XenforoClient {
  10. constructor(private endpoint: string, private userKey: string) {
  11. }
  12. private async makeRequest<TResult, TData>(uri: string, method: Method, data?: TData): Promise<TResult> {
  13. const result = await tryDo(got<TResult | RequestErrorSet>(`${this.endpoint}/${uri}`, {
  14. responseType: "json",
  15. method: method,
  16. headers: {
  17. "XF-Api-Key": this.userKey
  18. },
  19. form: data
  20. }));
  21. if (!result.ok) {
  22. if (isHttpError(result.error))
  23. throw result.error.response.body as RequestErrorSet;
  24. else
  25. throw { errors: [{ code: "UNK", message: "Unkown error" }] } as RequestErrorSet;
  26. } else {
  27. return result.result?.body as TResult;
  28. }
  29. }
  30. async getMe(): Promise<User> {
  31. const { me }: {me: User} = await assertOk(this.makeRequest("me/", "get"));
  32. return me;
  33. }
  34. async postReply(thread_id: number, message: string, attachment_key?: string): Promise<void> {
  35. return await assertOk(this.makeRequest("posts/", "post", {
  36. thread_id,
  37. message,
  38. attachment_key
  39. }));
  40. }
  41. async editThread(id: number, opts?: EditThreadOptions): Promise<CreateThreadResponse> {
  42. return await assertOk(this.makeRequest(`threads/${id}`, "post", opts));
  43. }
  44. async editPost(id: number, opts?: EditPostOptions): Promise<EditPostResponse> {
  45. return await assertOk(this.makeRequest(`posts/${id}`, "post", opts));
  46. }
  47. async getThread(id: number, opts?: GetThreadOptions): Promise<GetThreadResponse> {
  48. return await assertOk(this.makeRequest(`threads/${id}`, "post", opts));
  49. }
  50. async deleteThread(id: number, opts?: DeleteThreadOptions): Promise<SuccessResponse> {
  51. return await assertOk(this.makeRequest(`threads/${id}`, "delete", opts));
  52. }
  53. async createThread(forumId: number, title: string, message: string, opts?: CreateThreadOptions): Promise<CreateThreadResponse> {
  54. return await assertOk(this.makeRequest("threads/", "post", {
  55. node_id: forumId,
  56. title: title,
  57. message: message,
  58. ...opts
  59. }));
  60. }
  61. async getPost(id: number): Promise<Post> {
  62. const { post }: {post: Post} = await this.makeRequest(`posts/${id}`, "get");
  63. return post;
  64. }
  65. async getForumThreads(id: number): Promise<GetForumThreadsResponse> {
  66. return await this.makeRequest(`forums/${id}/threads`, "get");
  67. }
  68. }
  69. //#region Request types
  70. interface DeleteThreadOptions {
  71. hard_delete?: boolean;
  72. reason?: boolean;
  73. starter_alert?: boolean;
  74. starter_alert_reason?: boolean;
  75. }
  76. interface GetThreadOptions {
  77. with_posts?: boolean;
  78. page?: number;
  79. }
  80. interface CreateThreadOptions {
  81. prefix_id?: number;
  82. tags?: string[];
  83. custom_fields?: Dict<string>;
  84. discussion_open?: boolean;
  85. sticky?: boolean;
  86. attachment_key?: boolean;
  87. }
  88. interface EditThreadOptions {
  89. prefix_id?: number;
  90. title?: string;
  91. discussion_open?: boolean;
  92. sticky?: boolean;
  93. custom_fields?: Dict<string>;
  94. add_tags?: unknown[];
  95. remove_tags?: unknown[];
  96. }
  97. interface EditPostOptions {
  98. message?: string;
  99. silent?: boolean;
  100. clear_edit?: boolean;
  101. author_alert?: boolean;
  102. author_alert_reason?: string;
  103. attachment_key?: string;
  104. }
  105. //#endregion
  106. //#region Response types
  107. type GetThreadResponse = {
  108. thread: Thread;
  109. messages: Post[];
  110. pagination: unknown;
  111. };
  112. type SuccessResponse = {
  113. success: boolean;
  114. }
  115. type EditPostResponse = SuccessResponse & { post: Post };
  116. type CreateThreadResponse = SuccessResponse & { thread: Thread; };
  117. type GetForumThreadsResponse = {
  118. threads: Thread[];
  119. pagination: unknown;
  120. sticky: Thread[];
  121. };
  122. //#endregion
  123. //#region Data types
  124. export interface Forum {
  125. allow_posting: boolean;
  126. allow_poll: boolean;
  127. require_prefix: boolean;
  128. min_tags: number;
  129. }
  130. export interface User {
  131. about?: string;
  132. activity_visible?: boolean;
  133. age?: number;
  134. alert_optout?: unknown[];
  135. allow_post_profile?: string;
  136. allow_receive_news_feed?: string;
  137. allow_send_personal_conversation?: string;
  138. allow_view_identities: string;
  139. allow_view_profile?: string;
  140. avatar_urls: unknown;
  141. can_ban: boolean;
  142. can_converse: boolean;
  143. can_edit: boolean;
  144. can_follow: boolean;
  145. can_ignore: boolean;
  146. can_post_profile: boolean;
  147. can_view_profile: boolean;
  148. can_view_profile_posts: boolean;
  149. can_warn: boolean;
  150. content_show_signature?: boolean;
  151. creation_watch_state?: string;
  152. custom_fields?: unknown;
  153. custom_title?: string;
  154. dob?: unknown;
  155. email?: string;
  156. email_on_conversation?: boolean;
  157. gravatar?: string;
  158. interaction_watch_state?: boolean;
  159. is_admin?: boolean;
  160. is_banned?: boolean;
  161. is_discouraged?: boolean;
  162. is_followed?: boolean;
  163. is_ignored?: boolean;
  164. is_moderator?: boolean;
  165. is_super_admin?: boolean;
  166. last_activity?: number;
  167. location: string;
  168. push_on_conversation?: boolean;
  169. push_optout?: unknown[];
  170. receive_admin_email?: boolean;
  171. secondary_group_ids?: unknown[];
  172. show_dob_date?: boolean;
  173. show_dob_year?: boolean;
  174. signature: string;
  175. timezone?: string;
  176. use_tfa?: unknown[];
  177. user_group_id?: number;
  178. user_state?: string;
  179. user_title: string;
  180. visible?: boolean;
  181. warning_points?: number;
  182. website?: string;
  183. user_id: number;
  184. username: string;
  185. message_count: number;
  186. register_date: number;
  187. trophy_points: number;
  188. is_staff: boolean;
  189. reaction_score: number;
  190. }
  191. export interface Node {
  192. breadcrumbs: unknown[];
  193. type_data: unknown;
  194. node_id: number;
  195. title: string;
  196. node_name: string;
  197. description: string;
  198. node_type_id: string;
  199. parent_node_id: number;
  200. display_order: number;
  201. display_in_list: boolean;
  202. }
  203. export interface Thread {
  204. username: string;
  205. is_watching?: boolean;
  206. visitor_post_count?: number;
  207. custom_fields: unknown;
  208. tags: unknown[];
  209. prefix?: string;
  210. can_edit: boolean;
  211. can_edit_tags: boolean;
  212. can_reply: boolean;
  213. can_soft_delete: boolean;
  214. can_hard_delete: boolean;
  215. can_view_attachments: boolean;
  216. Forum?: Node;
  217. thread_id: number;
  218. node_id: number;
  219. title: string;
  220. reply_count: number;
  221. view_count: number;
  222. user_id: number;
  223. post_date: number;
  224. sticky: boolean;
  225. discussion_state: string;
  226. discussion_open: boolean;
  227. discussion_type: string;
  228. first_post_id: number;
  229. last_post_date: number;
  230. last_post_id: number;
  231. last_post_user_id: number;
  232. last_post_username: string;
  233. first_post_reaction_score: number;
  234. prefix_id: number;
  235. }
  236. export interface Attachment {
  237. filename: string;
  238. file_size: number;
  239. height: number;
  240. width: number;
  241. thumbnail_url: string;
  242. video_url: string;
  243. attachment_id: number;
  244. content_type: string;
  245. content_id: number;
  246. attach_date: number;
  247. view_count: number;
  248. }
  249. export interface Post {
  250. username: string;
  251. is_first_post: boolean;
  252. is_last_post: boolean;
  253. can_edit: boolean;
  254. can_soft_delete: boolean;
  255. can_hard_delete: boolean;
  256. can_react: boolean;
  257. can_view_attachments: boolean;
  258. Thread?: Thread;
  259. Attachments?: Attachment[];
  260. is_reacted_to: boolean;
  261. visitor_reaction_id: number;
  262. post_id: number;
  263. thread_id: number;
  264. user_id: number;
  265. post_date: number;
  266. message: string;
  267. message_state: string;
  268. attach_count: number;
  269. warning_message: string;
  270. position: number;
  271. last_edit_date: number;
  272. reaction_score: number;
  273. User: User;
  274. }
  275. //#endregion