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:
parent
bf68a4c01d
commit
c082b8faf2
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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];
|
||||||
|
@ -116,14 +120,14 @@ describe("plugins/discourse/createGraph", () => {
|
||||||
);
|
);
|
||||||
expect(node.timestampMs).toEqual(null);
|
expect(node.timestampMs).toEqual(null);
|
||||||
expect(NodeAddress.toParts(node.address)).toMatchInlineSnapshot(`
|
expect(NodeAddress.toParts(node.address)).toMatchInlineSnapshot(`
|
||||||
Array [
|
Array [
|
||||||
"sourcecred",
|
"sourcecred",
|
||||||
"discourse",
|
"discourse",
|
||||||
"user",
|
"user",
|
||||||
"https://url.com",
|
"https://url.com",
|
||||||
"decentralion",
|
"decentralion",
|
||||||
]
|
]
|
||||||
`);
|
`);
|
||||||
});
|
});
|
||||||
it("for topics", () => {
|
it("for topics", () => {
|
||||||
const {url, topic} = example();
|
const {url, topic} = example();
|
||||||
|
@ -133,14 +137,14 @@ describe("plugins/discourse/createGraph", () => {
|
||||||
);
|
);
|
||||||
expect(node.timestampMs).toEqual(topic.timestampMs);
|
expect(node.timestampMs).toEqual(topic.timestampMs);
|
||||||
expect(NodeAddress.toParts(node.address)).toMatchInlineSnapshot(`
|
expect(NodeAddress.toParts(node.address)).toMatchInlineSnapshot(`
|
||||||
Array [
|
Array [
|
||||||
"sourcecred",
|
"sourcecred",
|
||||||
"discourse",
|
"discourse",
|
||||||
"topic",
|
"topic",
|
||||||
"https://url.com",
|
"https://url.com",
|
||||||
"1",
|
"1",
|
||||||
]
|
]
|
||||||
`);
|
`);
|
||||||
});
|
});
|
||||||
it("for posts", () => {
|
it("for posts", () => {
|
||||||
const {url, topic, posts} = example();
|
const {url, topic, posts} = example();
|
||||||
|
@ -150,14 +154,14 @@ describe("plugins/discourse/createGraph", () => {
|
||||||
);
|
);
|
||||||
expect(node.timestampMs).toEqual(posts[1].timestampMs);
|
expect(node.timestampMs).toEqual(posts[1].timestampMs);
|
||||||
expect(NodeAddress.toParts(node.address)).toMatchInlineSnapshot(`
|
expect(NodeAddress.toParts(node.address)).toMatchInlineSnapshot(`
|
||||||
Array [
|
Array [
|
||||||
"sourcecred",
|
"sourcecred",
|
||||||
"discourse",
|
"discourse",
|
||||||
"post",
|
"post",
|
||||||
"https://url.com",
|
"https://url.com",
|
||||||
"2",
|
"2",
|
||||||
]
|
]
|
||||||
`);
|
`);
|
||||||
});
|
});
|
||||||
it("gives an [unknown topic] description for posts without a matching topic", () => {
|
it("gives an [unknown topic] description for posts without a matching topic", () => {
|
||||||
const post = {
|
const post = {
|
||||||
|
@ -187,16 +191,16 @@ describe("plugins/discourse/createGraph", () => {
|
||||||
expect(edge.dst).toEqual(expectedDst);
|
expect(edge.dst).toEqual(expectedDst);
|
||||||
expect(edge.timestampMs).toEqual(topic.timestampMs);
|
expect(edge.timestampMs).toEqual(topic.timestampMs);
|
||||||
expect(EdgeAddress.toParts(edge.address)).toMatchInlineSnapshot(`
|
expect(EdgeAddress.toParts(edge.address)).toMatchInlineSnapshot(`
|
||||||
Array [
|
Array [
|
||||||
"sourcecred",
|
"sourcecred",
|
||||||
"discourse",
|
"discourse",
|
||||||
"authors",
|
"authors",
|
||||||
"topic",
|
"topic",
|
||||||
"https://url.com",
|
"https://url.com",
|
||||||
"decentralion",
|
"decentralion",
|
||||||
"1",
|
"1",
|
||||||
]
|
]
|
||||||
`);
|
`);
|
||||||
});
|
});
|
||||||
it("for authorsPost", () => {
|
it("for authorsPost", () => {
|
||||||
const {url, posts, topic} = example();
|
const {url, posts, topic} = example();
|
||||||
|
@ -208,16 +212,16 @@ describe("plugins/discourse/createGraph", () => {
|
||||||
expect(edge.dst).toEqual(expectedDst);
|
expect(edge.dst).toEqual(expectedDst);
|
||||||
expect(edge.timestampMs).toEqual(post.timestampMs);
|
expect(edge.timestampMs).toEqual(post.timestampMs);
|
||||||
expect(EdgeAddress.toParts(edge.address)).toMatchInlineSnapshot(`
|
expect(EdgeAddress.toParts(edge.address)).toMatchInlineSnapshot(`
|
||||||
Array [
|
Array [
|
||||||
"sourcecred",
|
"sourcecred",
|
||||||
"discourse",
|
"discourse",
|
||||||
"authors",
|
"authors",
|
||||||
"post",
|
"post",
|
||||||
"https://url.com",
|
"https://url.com",
|
||||||
"wchargin",
|
"wchargin",
|
||||||
"2",
|
"2",
|
||||||
]
|
]
|
||||||
`);
|
`);
|
||||||
});
|
});
|
||||||
it("for topicContainsPost", () => {
|
it("for topicContainsPost", () => {
|
||||||
const {url, posts, topic} = example();
|
const {url, posts, topic} = example();
|
||||||
|
@ -229,15 +233,15 @@ describe("plugins/discourse/createGraph", () => {
|
||||||
expect(edge.dst).toEqual(expectedDst);
|
expect(edge.dst).toEqual(expectedDst);
|
||||||
expect(edge.timestampMs).toEqual(post.timestampMs);
|
expect(edge.timestampMs).toEqual(post.timestampMs);
|
||||||
expect(EdgeAddress.toParts(edge.address)).toMatchInlineSnapshot(`
|
expect(EdgeAddress.toParts(edge.address)).toMatchInlineSnapshot(`
|
||||||
Array [
|
Array [
|
||||||
"sourcecred",
|
"sourcecred",
|
||||||
"discourse",
|
"discourse",
|
||||||
"topicContainsPost",
|
"topicContainsPost",
|
||||||
"https://url.com",
|
"https://url.com",
|
||||||
"1",
|
"1",
|
||||||
"2",
|
"2",
|
||||||
]
|
]
|
||||||
`);
|
`);
|
||||||
});
|
});
|
||||||
it("for postReplies", () => {
|
it("for postReplies", () => {
|
||||||
const {url, posts, topic} = example();
|
const {url, posts, topic} = example();
|
||||||
|
@ -249,13 +253,33 @@ describe("plugins/discourse/createGraph", () => {
|
||||||
expect(edge.src).toEqual(expectedSrc);
|
expect(edge.src).toEqual(expectedSrc);
|
||||||
expect(edge.dst).toEqual(expectedDst);
|
expect(edge.dst).toEqual(expectedDst);
|
||||||
expect(edge.timestampMs).toEqual(post.timestampMs);
|
expect(edge.timestampMs).toEqual(post.timestampMs);
|
||||||
|
expect(EdgeAddress.toParts(edge.address)).toMatchInlineSnapshot(`
|
||||||
|
Array [
|
||||||
|
"sourcecred",
|
||||||
|
"discourse",
|
||||||
|
"replyTo",
|
||||||
|
"https://url.com",
|
||||||
|
"3",
|
||||||
|
"2",
|
||||||
|
]
|
||||||
|
`);
|
||||||
|
});
|
||||||
|
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(`
|
expect(EdgeAddress.toParts(edge.address)).toMatchInlineSnapshot(`
|
||||||
Array [
|
Array [
|
||||||
"sourcecred",
|
"sourcecred",
|
||||||
"discourse",
|
"discourse",
|
||||||
"replyTo",
|
"likes",
|
||||||
"https://url.com",
|
"https://url.com",
|
||||||
"3",
|
"mzargham",
|
||||||
"2",
|
"2",
|
||||||
]
|
]
|
||||||
`);
|
`);
|
||||||
|
@ -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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -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,
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue