Skip to content

Commit

Permalink
Discourse: remove update mode 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Beanow committed Nov 20, 2019
1 parent 136369f commit d7678de
Show file tree
Hide file tree
Showing 10 changed files with 278 additions and 2,956 deletions.
169 changes: 0 additions & 169 deletions src/plugins/discourse/__snapshots__/fetch.test.js.snap
@@ -1,18 +1,5 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`plugins/discourse/fetch snapshot testing loads a particular post from snapshot 1`] = `
Object {
"authorUsername": "d11",
"cooked": "<p>This is a test post.</p>
<p>EDIT: This test post was edited on October 24, 2019.</p>",
"id": 14,
"indexWithinTopic": 1,
"replyToPostIndex": null,
"timestampMs": 1564744349476,
"topicId": 11,
}
`;

exports[`plugins/discourse/fetch snapshot testing loads a particular topic from snapshot 1`] = `
Object {
"posts": Array [
Expand Down Expand Up @@ -299,162 +286,6 @@ Needing at least &gt; 20 posts.</p>",
}
`;
exports[`plugins/discourse/fetch snapshot testing loads latest posts from snapshot 1`] = `
Array [
Object {
"authorUsername": "d11",
"cooked": "<p>Here is a mention: <a class=\\"mention\\" href=\\"/u/dl-proto\\">@dl-proto</a><br>
Here is a topic reference: <a href=\\"https://sourcecred-test.discourse.group/t/123-a-post-with-numbers-in-slug/20/\\" class=\\"inline-onebox\\">123 a post with numbers in slug</a><br>
Here is a post reference with a slug: <a href=\\"https://sourcecred-test.discourse.group/t/my-first-test-post/11/4\\" class=\\"inline-onebox\\">My First Test Post</a></p>",
"id": 29,
"indexWithinTopic": 1,
"replyToPostIndex": null,
"timestampMs": 1571855191281,
"topicId": 21,
},
Object {
"authorUsername": "d11",
"cooked": "<p>Oh the post won’t be empty I assure you.</p>",
"id": 28,
"indexWithinTopic": 1,
"replyToPostIndex": null,
"timestampMs": 1570420159922,
"topicId": 20,
},
Object {
"authorUsername": "d11",
"cooked": "<p>I’m replying to a wiki.</p>",
"id": 27,
"indexWithinTopic": 2,
"replyToPostIndex": null,
"timestampMs": 1570049836311,
"topicId": 19,
},
Object {
"authorUsername": "d11",
"cooked": "<p>So I’m starting the wiki content.</p>
<p>No remove that line.</p>
<p>Ah now I’ve added another line.</p>",
"id": 26,
"indexWithinTopic": 1,
"replyToPostIndex": null,
"timestampMs": 1570049491743,
"topicId": 19,
},
Object {
"authorUsername": "d11",
"cooked": "<p>Testing a poll. Let’s see how it works.</p>
<div class=\\"poll\\" data-poll-status=\\"open\\" data-poll-max=\\"5\\" data-poll-min=\\"1\\" data-poll-public=\\"true\\" data-poll-results=\\"always\\" data-poll-type=\\"multiple\\" data-poll-name=\\"poll\\">
<div>
<div class=\\"poll-container\\">
<ul>
<li data-poll-option-id=\\"159d8e412be4679f2d98fb5f0d0cce91\\">One</li>
<li data-poll-option-id=\\"2cbb44549228f1cf22e44fba86b6db21\\">Two</li>
<li data-poll-option-id=\\"7016bd7a00c7caff922822b2910a564a\\">Three</li>
<li data-poll-option-id=\\"d2bb44ccb6b19c98cc57f5b8bb87effe\\">Four</li>
<li data-poll-option-id=\\"c677eae47d33ca9b527e7ea7bb29612c\\">Five</li>
</ul>
</div>
<div class=\\"poll-info\\">
<p>
<span class=\\"info-number\\">0</span>
<span class=\\"info-label\\">voters</span>
</p>
</div>
</div>
</div>",
"id": 25,
"indexWithinTopic": 1,
"replyToPostIndex": null,
"timestampMs": 1570049410625,
"topicId": 18,
},
Object {
"authorUsername": "d11",
"cooked": "<p>Adding another post, after having deleted a whole thread.</p>",
"id": 22,
"indexWithinTopic": 3,
"replyToPostIndex": null,
"timestampMs": 1564951839667,
"topicId": 11,
},
Object {
"authorUsername": "d11",
"cooked": "<aside class=\\"quote no-group\\" data-post=\\"1\\" data-topic=\\"13\\">
<div class=\\"title\\">
<div class=\\"quote-controls\\"></div>
<img alt width=\\"20\\" height=\\"20\\" src=\\"https://avatars.discourse.org/v4/letter/d/8797f3/40.png\\" class=\\"avatar\\"> dl-proto:</div>
<blockquote>
<p>Here is a link to another discourse thread:</p>
</blockquote>
</aside>
<p>Excellent link. I’ve quoted you.</p>",
"id": 18,
"indexWithinTopic": 2,
"replyToPostIndex": null,
"timestampMs": 1564744591313,
"topicId": 13,
},
Object {
"authorUsername": "dl-proto",
"cooked": "<p>Here is a link to another discourse thread: <a href=\\"https://sourcecred-test.discourse.group/t/my-first-test-post/11/3\\" class=\\"inline-onebox\\">My First Test Post</a></p>
<p>Here is a link to a GitHub issue: <a href=\\"https://github.com/sourcecred-test/example-github/issues/1\\" rel=\\"nofollow noopener\\">https://github.com/sourcecred-test/example-github/issues/1</a></p>",
"id": 17,
"indexWithinTopic": 1,
"replyToPostIndex": null,
"timestampMs": 1564744553905,
"topicId": 13,
},
Object {
"authorUsername": "dl-proto",
"cooked": "<p>Ah, an excellent post. I will like it and reply.</p>",
"id": 16,
"indexWithinTopic": 2,
"replyToPostIndex": null,
"timestampMs": 1564744496285,
"topicId": 11,
},
Object {
"authorUsername": "d11",
"cooked": "<p>This is a test post.</p>
<p>EDIT: This test post was edited on October 24, 2019.</p>",
"id": 14,
"indexWithinTopic": 1,
"replyToPostIndex": null,
"timestampMs": 1564744349476,
"topicId": 11,
},
Object {
"authorUsername": "system",
"cooked": "<p>This is a test instance.<br>
The first paragraph of this pinned topic will be visible as a welcome message to all new visitors on your homepage. Its important!</p>
<p><strong>Edit this</strong> into a brief description of your community:</p>
<ul>
<li>Who is it for?</li>
<li>What can they find here?</li>
<li>Why should they come here?</li>
<li>Where can they read more (links, resources, etc)?</li>
</ul>
<p><img src=\\"https://sjc3.discourse-cdn.com/free1/images/welcome/discourse-edit-post-animated.gif\\" width=\\"508\\" height=\\"106\\"></p>
<p>You may want to close this topic via the admin <img src=\\"https://sjc3.discourse-cdn.com/free1/images/emoji/twitter/wrench.png?v=9\\" title=\\":wrench:\\" class=\\"emoji\\" alt=\\":wrench:\\"> (at the upper right and bottom), so that replies don’t pile up on an announcement.</p>",
"id": 10,
"indexWithinTopic": 1,
"replyToPostIndex": null,
"timestampMs": 1554236412933,
"topicId": 7,
},
Object {
"authorUsername": "system",
"cooked": "<p>Discussion about this site, its organization, how it works, and how we can improve it.</p>",
"id": 1,
"indexWithinTopic": 1,
"replyToPostIndex": null,
"timestampMs": 1554236409844,
"topicId": 1,
},
]
`;
exports[`plugins/discourse/fetch snapshot testing loads topic IDs that are category definition topics 1`] = `
Set {
24,
Expand Down
48 changes: 0 additions & 48 deletions src/plugins/discourse/fetch.js
Expand Up @@ -10,7 +10,6 @@
* on the results to simplify it to data that is relevant for us.
*/

import stringify from "json-stable-stringify";
import fetch from "isomorphic-fetch";
import Bottleneck from "bottleneck";
import * as NullUtil from "../../util/null";
Expand Down Expand Up @@ -93,27 +92,12 @@ export type LikeAction = {|
* testing.
*/
export interface Discourse {
// Get the `id` of the latest topic on the server.
// Vital so that we can then enumerate and fetch every Topic we haven't seen yet.
// May reject on not OK status like 404 or 403.
latestTopicId(): Promise<TopicId>;
// Retrieve the Topic with Posts for a given id.
// Will resolve to null if the response status is 403 or 404. 403 because the
// topic may be hidden from the API user; 404 because we sometimes see
// 404s in prod and want to ignore those topic ids. (Not sure why it happens.)
// May reject if the status is not OK and is not 404 or 403.
topicWithPosts(id: TopicId): Promise<TopicWithPosts | null>;
// Retrieve an individual Post by its id.
// Will resolve to null if the response status is 403 or 404. 403 because the
// topic may be hidden from the API user; 404 because we sometimes see
// 404s in prod and want to ignore those topic ids. (Not sure why it happens.)
// May reject if the status is not OK and is not 404 or 403.
post(id: PostId): Promise<Post | null>;
// Retrieve the latest posts from the server.
// Vital so that we can then enumerate and fetch every Post that we haven't
// encountered.
// May reject on not OK status like 404 or 403.
latestPosts(): Promise<Post[]>;

/**
* Retrieves the like actions that were initiated by the target user.
Expand Down Expand Up @@ -245,26 +229,6 @@ export class Fetcher implements Discourse {
return new Set(ids);
}

async latestTopicId(): Promise<TopicId> {
const response = await this._fetch("/latest.json?order=created");
failIfMissing(response);
failForNotOk(response);
const json = await response.json();
if (json.topic_list.topics.length === 0) {
throw new Error(`no topics! got ${stringify(json)} as latest topics.`);
}

return json.topic_list.topics[0].id;
}

async latestPosts(): Promise<Post[]> {
const response = await this._fetch("/posts.json");
failIfMissing(response);
failForNotOk(response);
const json = await response.json();
return json.latest_posts.map(parsePost);
}

async topicWithPosts(id: TopicId): Promise<TopicWithPosts | null> {
const response = await this._fetch(`/t/${id}.json`);
const {status} = response;
Expand Down Expand Up @@ -300,18 +264,6 @@ export class Fetcher implements Discourse {
return {topic, posts};
}

async post(id: PostId): Promise<Post | null> {
const response = await this._fetch(`/posts/${id}.json`);
const {status} = response;
if (status === 403 || status === 404 || status === 410) {
// The post is hidden, deleted, or otherwise missing.
return null;
}
failForNotOk(response);
const json = await response.json();
return parsePost(json);
}

async likesByUser(
username: string,
offset: number
Expand Down
26 changes: 0 additions & 26 deletions src/plugins/discourse/fetch.test.js
Expand Up @@ -5,22 +5,12 @@ import {options, snapshotFetcher} from "./mockSnapshotFetcher";

describe("plugins/discourse/fetch", () => {
describe("snapshot testing", () => {
it("loads LatestTopicId from snapshot", async () => {
const topicId = await snapshotFetcher().latestTopicId();
expect(topicId).toMatchInlineSnapshot(`57`);
});
it("loads latest posts from snapshot", async () => {
expect(await snapshotFetcher().latestPosts()).toMatchSnapshot();
});
it("loads a particular topic from snapshot", async () => {
expect(await snapshotFetcher().topicWithPosts(11)).toMatchSnapshot();
});
it("loads a topic with pagination from snapshot", async () => {
expect(await snapshotFetcher().topicWithPosts(26)).toMatchSnapshot();
});
it("loads a particular post from snapshot", async () => {
expect(await snapshotFetcher().post(14)).toMatchSnapshot();
});
it("loads user likes from snapshot", async () => {
expect(
await snapshotFetcher().likesByUser("dl-proto", 0)
Expand Down Expand Up @@ -55,22 +45,8 @@ describe("plugins/discourse/fetch", () => {
return result.catch((e) => expect(e.message).toMatch(String(status)));
});
}
expectError("latestTopicId", (x) => x.latestTopicId(), 404);
expectError("latestTopicId", (x) => x.latestTopicId(), 403);
expectError("latestTopicId", (x) => x.latestTopicId(), 429);

expectError("latestPosts", (x) => x.latestPosts(), 404);
expectError("latestPosts", (x) => x.latestPosts(), 403);
expectError("latestPosts", (x) => x.latestPosts(), 429);

expectError("topicWithPosts", (x) => x.topicWithPosts(14), 429);
expectError("post", (x) => x.post(14), 429);
expectError(
"categoryDefinitionTopicIds",
(x) => x.categoryDefinitionTopicIds(),
429
);

expectError("topicsBumpedSince", (x) => x.topicsBumpedSince(0), 429);
expectError(
"categoryDefinitionTopicIds",
Expand All @@ -90,8 +66,6 @@ describe("plugins/discourse/fetch", () => {

expectNull("topicWithPosts", (x) => x.topicWithPosts(14), 404);
expectNull("topicWithPosts", (x) => x.topicWithPosts(14), 403);
expectNull("post", (x) => x.post(14), 404);
expectNull("post", (x) => x.post(14), 403);
});

describe("fetch headers", () => {
Expand Down

0 comments on commit d7678de

Please sign in to comment.