From 0d4c074e84f2489151d4d43ba8deb8d355be2845 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Thu, 5 Jan 2023 17:05:40 +0000 Subject: [PATCH] Tools: Refactor Discourse code --- .eslintignore | 3 + .gitignore | 3 + packages/tools/utils/discourse.ts | 83 +++++++++++++++++++++++++++ packages/tools/website/updateNews.ts | 84 +--------------------------- 4 files changed, 90 insertions(+), 83 deletions(-) create mode 100644 packages/tools/utils/discourse.ts diff --git a/.eslintignore b/.eslintignore index a04dd0f0f..1ab0d68ee 100644 --- a/.eslintignore +++ b/.eslintignore @@ -2328,6 +2328,9 @@ packages/tools/update-readme-sponsors.js.map packages/tools/updateMarkdownDoc.d.ts packages/tools/updateMarkdownDoc.js packages/tools/updateMarkdownDoc.js.map +packages/tools/utils/discourse.d.ts +packages/tools/utils/discourse.js +packages/tools/utils/discourse.js.map packages/tools/utils/translation.d.ts packages/tools/utils/translation.js packages/tools/utils/translation.js.map diff --git a/.gitignore b/.gitignore index f71d922ab..b28b45df3 100644 --- a/.gitignore +++ b/.gitignore @@ -2316,6 +2316,9 @@ packages/tools/update-readme-sponsors.js.map packages/tools/updateMarkdownDoc.d.ts packages/tools/updateMarkdownDoc.js packages/tools/updateMarkdownDoc.js.map +packages/tools/utils/discourse.d.ts +packages/tools/utils/discourse.js +packages/tools/utils/discourse.js.map packages/tools/utils/translation.d.ts packages/tools/utils/translation.js packages/tools/utils/translation.js.map diff --git a/packages/tools/utils/discourse.ts b/packages/tools/utils/discourse.ts new file mode 100644 index 000000000..42b0617db --- /dev/null +++ b/packages/tools/utils/discourse.ts @@ -0,0 +1,83 @@ +import fetch from 'node-fetch'; + +interface ApiConfig { + baseUrl: string; + key: string; + username: string; + newsCategoryId: number; +} + +export enum HttpMethod { + GET = 'GET', + POST = 'POST', + DELETE = 'DELETE', + PUT = 'PUT', + PATCH = 'PATCH', +} + +interface ForumTopPost { + id: number; + raw: string; + title: string; +} + +export const config: ApiConfig = { + baseUrl: 'https://discourse.joplinapp.org', + key: '', + username: '', + newsCategoryId: 9, +}; + +export const execApi = async (method: HttpMethod, path: string, body: Record = null) => { + interface Request { + method: HttpMethod; + headers: Record; + body?: string; + } + + const headers: Record = { + 'Api-Key': config.key, + 'Api-Username': config.username, + }; + + if (method !== HttpMethod.GET) headers['Content-Type'] = 'application/json;'; + + const request: Request = { + method, + headers, + }; + + if (body) request.body = JSON.stringify(body); + + const response = await fetch(`${config.baseUrl}/${path}`, request); + + if (!response.ok) { + const errorText = await response.text(); + const error = new Error(`On ${method} ${path}: ${errorText}`); + let apiObject = null; + try { + apiObject = JSON.parse(errorText); + } catch (error) { + // Ignore - it just means that the error object is a plain string + } + (error as any).apiObject = apiObject; + throw error; + } + + return response.json() as any; +}; + +export const getForumTopPostByExternalId = async (externalId: string): Promise => { + try { + const existingForumTopic = await execApi(HttpMethod.GET, `t/external_id/${externalId}.json`); + const existingForumPost = await execApi(HttpMethod.GET, `posts/${existingForumTopic.post_stream.posts[0].id}.json`); + return { + id: existingForumPost.id, + title: existingForumTopic.title, + raw: existingForumPost.raw, + }; + } catch (error) { + if (error.apiObject && error.apiObject.error_type === 'not_found') return null; + throw error; + } +}; diff --git a/packages/tools/website/updateNews.ts b/packages/tools/website/updateNews.ts index 0212ed706..b4f781e58 100644 --- a/packages/tools/website/updateNews.ts +++ b/packages/tools/website/updateNews.ts @@ -5,19 +5,12 @@ import { readdir, readFile, writeFile } from 'fs-extra'; import { basename } from 'path'; import { rootDir } from '../tool-utils'; -import fetch from 'node-fetch'; import { compileWithFrontMatter, MarkdownAndFrontMatter, stripOffFrontMatter } from './utils/frontMatter'; import { markdownToHtml } from './utils/render'; import { getNewsDate } from './utils/news'; +import { config, execApi, getForumTopPostByExternalId, HttpMethod } from '../utils/discourse'; const RSS = require('rss'); -interface ApiConfig { - baseUrl: string; - key: string; - username: string; - newsCategoryId: number; -} - interface Post { id: string; path: string; @@ -29,29 +22,8 @@ interface PostContent { parsed: MarkdownAndFrontMatter; } -enum HttpMethod { - GET = 'GET', - POST = 'POST', - DELETE = 'DELETE', - PUT = 'PUT', - PATCH = 'PATCH', -} - -interface ForumTopPost { - id: number; - raw: string; - title: string; -} - const ignoredPostIds = ['20180621-172112','20180621-182112','20180906-101039','20180906-111039','20180916-200431','20180916-210431','20180929-111053','20180929-121053','20181004-081123','20181004-091123','20181101-174335','20181213-173459','20190130-230218','20190404-064157','20190404-074157','20190424-102410','20190424-112410','20190523-221026','20190523-231026','20190610-230711','20190611-000711','20190613-192613','20190613-202613','20190814-215957','20190814-225957','20190924-230254','20190925-000254','20190929-142834','20190929-152834','20191012-223121','20191012-233121','20191014-155136','20191014-165136','20191101-131852','20191117-183855','20191118-072700','20200220-190804','20200301-125055','20200314-001555','20200406-214254','20200406-224254','20200505-181736','20200606-151446','20200607-112720','20200613-103545','20200616-191918','20200620-114515','20200622-084127','20200626-134029','20200708-192444','20200906-172325','20200913-163730','20200915-091108','20201030-114530','20201126-114649','20201130-145937','20201212-172039','20201228-112150','20210104-131645','20210105-153008','20210130-144626','20210309-111950','20210310-100852','20210413-091132','20210430-083248','20210506-083359','20210513-095238','20210518-085514','20210621-104753','20210624-171844','20210705-094247','20210706-140228','20210711-095626','20210718-103538','20210729-103234','20210804-085003','20210831-154354','20210901-113415','20210929-144036','20210930-163458','20211031-115215','20211102-150403','20211217-120324','20220215-142000','20220224-release-2-7','20220308-gsoc2022-start','20220405-gsoc-contributor-proposals']; -const config: ApiConfig = { - baseUrl: 'https://discourse.joplinapp.org', - key: '', - username: '', - newsCategoryId: 9, -}; - const getPosts = async (newsDir: string): Promise => { const filenames = await readdir(newsDir); const output: Post[] = []; @@ -92,60 +64,6 @@ const getPostContent = async (post: Post): Promise => { } }; -const execApi = async (method: HttpMethod, path: string, body: Record = null) => { - interface Request { - method: HttpMethod; - headers: Record; - body?: string; - } - - const headers: Record = { - 'Api-Key': config.key, - 'Api-Username': config.username, - }; - - if (method !== HttpMethod.GET) headers['Content-Type'] = 'application/json;'; - - const request: Request = { - method, - headers, - }; - - if (body) request.body = JSON.stringify(body); - - const response = await fetch(`${config.baseUrl}/${path}`, request); - - if (!response.ok) { - const errorText = await response.text(); - const error = new Error(`On ${method} ${path}: ${errorText}`); - let apiObject = null; - try { - apiObject = JSON.parse(errorText); - } catch (error) { - // Ignore - it just means that the error object is a plain string - } - (error as any).apiObject = apiObject; - throw error; - } - - return response.json() as any; -}; - -const getForumTopPostByExternalId = async (externalId: string): Promise => { - try { - const existingForumTopic = await execApi(HttpMethod.GET, `t/external_id/${externalId}.json`); - const existingForumPost = await execApi(HttpMethod.GET, `posts/${existingForumTopic.post_stream.posts[0].id}.json`); - return { - id: existingForumPost.id, - title: existingForumTopic.title, - raw: existingForumPost.raw, - }; - } catch (error) { - if (error.apiObject && error.apiObject.error_type === 'not_found') return null; - throw error; - } -}; - const generateRssFeed = async (posts: Post[]) => { let pubDate = null; let postCount = 0;