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`
This commit is contained in:
Dandelion Mané 2019-08-15 20:12:20 +02:00 committed by GitHub
parent 2f8e1c61e4
commit 69831d6961
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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<Topic>;
/**
* Retrieve every Post available.
*
* The order is unspecified.
*/
posts(): $ReadOnlyArray<Post>;
/**
* 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(