From ebdd2a05c5fdb11496c59a9934be140867af7b16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dandelion=20Man=C3=A9?= Date: Mon, 26 Aug 2019 12:25:42 +0200 Subject: [PATCH] add a loadDiscourse method (#1323) This is the analogue to `github/loadGraph`, but for Discourse. It basically pipes together the mechanisms for loading Discourse data and creating a Discourse graph from them, resulting in a single endpoint for consumption in the API. In contrast to github, the method is called `loadDiscourse` and not `loadGraph`, which seemed more appropriate to me. I haven't changed the corresponding GitHub method's name. (I'm currently knowingly letting conceputal debt accumulate around the plugin interface; I expect to do a full refactor within the next few months.) Test plan: This is the kind of "pipe together tested APIs involving IO" code which I have decided not to write explicit tests for. However, it is still protected by flow, and I have a branch (`discourse-plugin`) which uses this code to do a full Discourse load. --- src/plugins/discourse/loadDiscourse.js | 30 ++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/plugins/discourse/loadDiscourse.js diff --git a/src/plugins/discourse/loadDiscourse.js b/src/plugins/discourse/loadDiscourse.js new file mode 100644 index 0000000..e3edd9c --- /dev/null +++ b/src/plugins/discourse/loadDiscourse.js @@ -0,0 +1,30 @@ +// @flow + +import Database from "better-sqlite3"; +import base64url from "base64url"; +import {Fetcher, type DiscourseFetchOptions} from "./fetch"; +import {Mirror} from "./mirror"; +import {createGraph} from "./createGraph"; +import {TaskReporter} from "../../util/taskReporter"; +import {Graph} from "../../core/graph"; +import path from "path"; + +export type {DiscourseFetchOptions} from "./fetch"; + +export type Options = {| + +fetchOptions: DiscourseFetchOptions, + +cacheDirectory: string, +|}; + +export async function loadDiscourse( + options: Options, + reporter: TaskReporter +): Promise { + const filename = base64url.encode(options.fetchOptions.serverUrl) + ".db"; + const db = new Database(path.join(options.cacheDirectory, filename)); + const fetcher = new Fetcher(options.fetchOptions); + const mirror = new Mirror(db, fetcher, options.fetchOptions.serverUrl); + await mirror.update(reporter); + const graph = createGraph(options.fetchOptions.serverUrl, mirror); + return graph; +}