xenforo.ts 8.2 KB

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