From 69831d6961571dbd9dadfe67ccdeb77429cb463c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dandelion=20Man=C3=A9?= Date: Thu, 15 Aug 2019 20:12:20 +0200 Subject: [PATCH] mirror: make a data interface (#1291) This is a quick fixup so that the coming createGraph module can be properly tested. Shout out to @Beanow for anticipating this need in a [review comment]. [review comment]: https://github.com/sourcecred/sourcecred/pull/1266#discussion_r314305108 Test plan: trivial refactor, run `yarn test` --- src/plugins/discourse/mirror.js | 39 ++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/src/plugins/discourse/mirror.js b/src/plugins/discourse/mirror.js index 12ab0f8..8adddc0 100644 --- a/src/plugins/discourse/mirror.js +++ b/src/plugins/discourse/mirror.js @@ -15,6 +15,38 @@ import { // so that the cache will be properly invalidated. const VERSION = "discourse_mirror_v1"; +/** + * An interface for retrieving all of the Discourse data at once. + * + * Also has some convenience methods for interpeting the data (e.g. getting + * a post by its index in a topic). + * + * The mirror implements this; it's factored out as an interface for + * ease of testing. + */ +export interface DiscourseData { + /** + * Retrieve every Topic available. + * + * The order is unspecified. + */ + topics(): $ReadOnlyArray; + + /** + * Retrieve every Post available. + * + * The order is unspecified. + */ + posts(): $ReadOnlyArray; + + /** + * Given a TopicId and a post number, find that numbered post within the topic. + * + * Returns undefined if no such post is available. + */ + findPostInTopic(topicId: TopicId, indexWithinTopic: number): ?PostId; +} + /** * Mirrors data from the Discourse API into a local sqlite db. * @@ -32,7 +64,7 @@ const VERSION = "discourse_mirror_v1"; * Each Mirror instance is tied to a particular server. Trying to use a mirror * for multiple Discourse servers is not permitted; use separate Mirrors. */ -export class Mirror { +export class Mirror implements DiscourseData { +_db: Database; +_fetcher: Discourse; @@ -168,11 +200,6 @@ export class Mirror { })); } - /** - * Given a TopicId and a post number, find that numbered post within the topic. - * - * Returns undefined if no such post exists. - */ findPostInTopic(topicId: TopicId, indexWithinTopic: number): ?PostId { return this._db .prepare(