discourse: add likes edges to the graph (#1299)

A very simple commit; we add a type for likes edges, and add them to the
graph.

Test plan: Unit tests added; yarn test passes.
This commit is contained in:
Dandelion Mané 2019-08-18 19:38:32 +02:00 committed by GitHub
parent bf68a4c01d
commit c082b8faf2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 121 additions and 58 deletions

View File

@ -8,7 +8,13 @@ import {
type Edge, type Edge,
type NodeAddressT, type NodeAddressT,
} from "../../core/graph"; } from "../../core/graph";
import {type PostId, type TopicId, type Post, type Topic} from "./fetch"; import {
type PostId,
type TopicId,
type Post,
type Topic,
type LikeAction,
} from "./fetch";
import {type DiscourseData} from "./mirror"; import {type DiscourseData} from "./mirror";
import { import {
topicNodeType, topicNodeType,
@ -18,6 +24,7 @@ import {
authorsTopicEdgeType, authorsTopicEdgeType,
postRepliesEdgeType, postRepliesEdgeType,
topicContainsPostEdgeType, topicContainsPostEdgeType,
likesEdgeType,
} from "./declaration"; } from "./declaration";
export function topicAddress(serverUrl: string, id: TopicId): NodeAddressT { export function topicAddress(serverUrl: string, id: TopicId): NodeAddressT {
@ -114,7 +121,6 @@ export function postRepliesEdge(
String(post.id), String(post.id),
String(basePostId) String(basePostId)
); );
return { return {
address, address,
timestampMs: post.timestampMs, timestampMs: post.timestampMs,
@ -123,6 +129,21 @@ export function postRepliesEdge(
}; };
} }
export function likesEdge(serverUrl: string, like: LikeAction): Edge {
const address = EdgeAddress.append(
likesEdgeType.prefix,
serverUrl,
like.username,
String(like.postId)
);
return {
address,
timestampMs: like.timestampMs,
src: userAddress(serverUrl, like.username),
dst: postAddress(serverUrl, like.postId),
};
}
export function createGraph(serverUrl: string, data: DiscourseData): Graph { export function createGraph(serverUrl: string, data: DiscourseData): Graph {
if (serverUrl.endsWith("/")) { if (serverUrl.endsWith("/")) {
throw new Error(`by convention, serverUrl should not end with /`); throw new Error(`by convention, serverUrl should not end with /`);
@ -161,5 +182,9 @@ export function createGraph(serverUrl: string, data: DiscourseData): Graph {
} }
} }
for (const like of data.likes()) {
g.addEdge(likesEdge(serverUrl, like));
}
return g; return g;
} }

View File

@ -13,6 +13,9 @@ import {
authorsPostEdge, authorsPostEdge,
topicContainsPostEdge, topicContainsPostEdge,
postRepliesEdge, postRepliesEdge,
likesEdge,
userAddress,
postAddress,
} from "./createGraph"; } from "./createGraph";
import { import {
userNodeType, userNodeType,
@ -22,6 +25,7 @@ import {
authorsPostEdgeType, authorsPostEdgeType,
topicContainsPostEdgeType, topicContainsPostEdgeType,
postRepliesEdgeType, postRepliesEdgeType,
likesEdgeType,
} from "./declaration"; } from "./declaration";
import type {EdgeType, NodeType} from "../../analysis/types"; import type {EdgeType, NodeType} from "../../analysis/types";
@ -98,7 +102,7 @@ describe("plugins/discourse/createGraph", () => {
authorUsername: "mzargham", authorUsername: "mzargham",
}; };
const likes: $ReadOnlyArray<LikeAction> = [ const likes: $ReadOnlyArray<LikeAction> = [
{timestampMs: 3, username: "mzargam", postId: 2}, {timestampMs: 3, username: "mzargham", postId: 2},
{timestampMs: 4, username: "decentralion", postId: 3}, {timestampMs: 4, username: "decentralion", postId: 3},
]; ];
const posts = [post1, post2, post3]; const posts = [post1, post2, post3];
@ -260,6 +264,26 @@ describe("plugins/discourse/createGraph", () => {
] ]
`); `);
}); });
it("for likes", () => {
const {url, likes} = example();
const like = likes[0];
const expectedSrc = userAddress(url, like.username);
const expectedDst = postAddress(url, like.postId);
const edge = likesEdge(url, like);
expect(edge.src).toEqual(expectedSrc);
expect(edge.dst).toEqual(expectedDst);
expect(edge.timestampMs).toEqual(like.timestampMs);
expect(EdgeAddress.toParts(edge.address)).toMatchInlineSnapshot(`
Array [
"sourcecred",
"discourse",
"likes",
"https://url.com",
"mzargham",
"2",
]
`);
});
}); });
describe("has the right nodes", () => { describe("has the right nodes", () => {
@ -322,5 +346,10 @@ describe("plugins/discourse/createGraph", () => {
]; ];
expectEdgesOfType(edges, postRepliesEdgeType); expectEdgesOfType(edges, postRepliesEdgeType);
}); });
it("likes edges", () => {
const {url, likes} = example();
const edges = likes.map((l) => likesEdge(url, l));
expectEdgesOfType(edges, likesEdgeType);
});
}); });
}); });

View File

@ -71,6 +71,14 @@ export const authorsPostEdgeType: EdgeType = deepFreeze({
description: "Connects an author to a post they've created.", description: "Connects an author to a post they've created.",
}); });
export const likesEdgeType: EdgeType = deepFreeze({
forwardName: "likes",
backwardName: "is liked by",
prefix: EdgeAddress.fromParts(["sourcecred", "discourse", "likes"]),
defaultWeight: {forwards: 1, backwards: 0},
description: "Connects a Discourse user to a post they liked.",
});
export const declaration: PluginDeclaration = deepFreeze({ export const declaration: PluginDeclaration = deepFreeze({
name: "discourse", name: "discourse",
nodeTypes: [userNodeType, topicNodeType, postNodeType], nodeTypes: [userNodeType, topicNodeType, postNodeType],
@ -79,5 +87,6 @@ export const declaration: PluginDeclaration = deepFreeze({
authorsTopicEdgeType, authorsTopicEdgeType,
authorsPostEdgeType, authorsPostEdgeType,
topicContainsPostEdgeType, topicContainsPostEdgeType,
likesEdgeType,
], ],
}); });