xenforo.ts 8.1 KB

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