Skip to content

Commit

Permalink
Discord: Simplify default graph + simplifyGraph config (#3203)
Browse files Browse the repository at this point in the history
  • Loading branch information
blueridger committed Oct 4, 2021
1 parent 00a2f59 commit e905f33
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 68 deletions.
5 changes: 5 additions & 0 deletions packages/sourcecred/src/plugins/discord/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ export type DiscordConfigJson = $ReadOnlyArray<{|
+propsChannels?: $ReadOnlyArray<Model.Snowflake>,
// Whether to include NSFW channels in cred distribution or not
+includeNsfwChannels?: boolean,
// This reduces graph size by replacing reaction nodes with message node weights.
+simplifyGraph?: boolean,
|}>;

const parserJson: C.Parser<DiscordConfigJson> = C.array(
Expand Down Expand Up @@ -97,6 +99,7 @@ const parserJson: C.Parser<DiscordConfigJson> = C.array(
}),
propsChannels: C.array(C.string),
includeNsfwChannels: C.boolean,
simplifyGraph: C.boolean,
}
)
);
Expand All @@ -106,6 +109,7 @@ export type DiscordConfig = {|
+weights: WeightConfig,
+propsChannels: $ReadOnlyArray<Model.Snowflake>,
+includeNsfwChannels: boolean,
+simplifyGraph: boolean,
|};
export type DiscordConfigs = $ReadOnlyArray<DiscordConfig>;

Expand Down Expand Up @@ -139,6 +143,7 @@ export function _upgrade(json: DiscordConfigJson): DiscordConfigs {
},
propsChannels: config.propsChannels || [],
includeNsfwChannels: config.includeNsfwChannels || false,
simplifyGraph: config.simplifyGraph || false,
}));
}

Expand Down
4 changes: 4 additions & 0 deletions packages/sourcecred/src/plugins/discord/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe("plugins/discord/config", () => {
},
},
includeNsfwChannels: true,
simplifyGraph: true,
},
];
const expected = [
Expand Down Expand Up @@ -61,6 +62,7 @@ describe("plugins/discord/config", () => {
},
},
includeNsfwChannels: true,
simplifyGraph: true,
},
];
const parsed: DiscordConfigs = parser.parseOrThrow(raw);
Expand Down Expand Up @@ -90,6 +92,7 @@ describe("plugins/discord/config", () => {
defaultWeight: 1,
});
expect(parsed[0].includeNsfwChannels).toEqual(false);
expect(parsed[0].simplifyGraph).toEqual(false);
});
it("can work with delimiters", () => {
const raw = [
Expand Down Expand Up @@ -149,6 +152,7 @@ describe("plugins/discord/config", () => {
},
},
includeNsfwChannels: true,
simplifyGraph: false,
},
];
const parsed: DiscordConfigs = parser.parseOrThrow(raw);
Expand Down
50 changes: 28 additions & 22 deletions packages/sourcecred/src/plugins/discord/createGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,33 +320,39 @@ export function _createGraphFromMessages(
channelId,
channelParentId,
} = graphMessage;

let messageWeight = 0;
for (const {reaction, reactingMember} of reactions) {
const weight = reactionWeight({
weights,
message,
reaction,
reactingMember,
propsChannels,
reactions,
channelParentId,
});
messageWeight += weight;
if (weight && !config.simplifyGraph) {
const node = reactionNode(reaction, message.timestampMs, guildId);
wg.weights.nodeWeights.set(node.address, weight);
wg.graph.addNode(node);
wg.graph.addNode(memberNode(reactingMember));
wg.graph.addEdge(reactsToEdge(reaction, message));
wg.graph.addEdge(
addsReactionEdge(reaction, reactingMember, message.timestampMs)
);
}
}
if (!messageWeight) continue;

if (author) {
wg.graph.addNode(memberNode(author));
wg.graph.addEdge(authorsMessageEdge(message, author));
}
wg.graph.addNode(messageNode(message, guildId, channelName));

for (const {reaction, reactingMember} of reactions) {
const node = reactionNode(reaction, message.timestampMs, guildId);
wg.weights.nodeWeights.set(
node.address,
reactionWeight(
weights,
message,
reaction,
reactingMember,
propsChannels,
reactions,
channelParentId
)
);
wg.graph.addNode(node);
wg.graph.addNode(memberNode(reactingMember));
wg.graph.addEdge(reactsToEdge(reaction, message));
wg.graph.addEdge(
addsReactionEdge(reaction, reactingMember, message.timestampMs)
);
}
if (config.simplifyGraph)
wg.weights.nodeWeights.set(messageAddress(message), messageWeight);

for (const {member, count} of mentions) {
wg.graph.addNode(memberNode(member));
Expand Down
1 change: 1 addition & 0 deletions packages/sourcecred/src/plugins/discord/mirror.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe("plugins/discord/mirror", () => {
},
},
includeNsfwChannels: true,
simplifyGraph: true,
};

describe("smoke test", () => {
Expand Down
27 changes: 18 additions & 9 deletions packages/sourcecred/src/plugins/discord/reactionWeights.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,24 @@ export type WeightConfig = {|
+emojiWeights: ReactionWeightConfig,
|};

export function reactionWeight(
weights: WeightConfig,
message: Model.Message,
reaction: Model.Reaction,
reactingMember: Model.GuildMember,
propsChannels: Set<Model.Snowflake>,
reactions: $ReadOnlyArray<GraphReaction>,
channelParentId: ?Model.Snowflake
): NodeWeight {
export function reactionWeight(options: {|
+weights: WeightConfig,
+message: Model.Message,
+reaction: Model.Reaction,
+reactingMember: Model.GuildMember,
+propsChannels: Set<Model.Snowflake>,
+reactions: $ReadOnlyArray<GraphReaction>,
+channelParentId: ?Model.Snowflake,
|}): NodeWeight {
const {
weights,
message,
reaction,
reactingMember,
propsChannels,
reactions,
channelParentId,
} = options;
if (
message.authorId === reaction.authorId &&
!propsChannels.has(message.channelId)
Expand Down
80 changes: 43 additions & 37 deletions packages/sourcecred/src/plugins/discord/reactionWeights.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,15 @@ describe("plugins/discord/reactionWeights", () => {
const channelWeights = {defaultWeight: 1, weights: {[channelId]: 6}};
const weights = {emojiWeights, roleWeights, channelWeights};
expect(
reactionWeight(
reactionWeight({
weights,
message,
reacterReaction,
reacterMember,
new Set(),
reactions
)
reaction: reacterReaction,
reactingMember: reacterMember,
propsChannels: new Set(),
reactions,
channelParentId: undefined,
})
).toEqual(4 * 5 * 6);
});
it("averages across role-weighted users when averaging is enabled", () => {
Expand All @@ -188,14 +189,15 @@ describe("plugins/discord/reactionWeights", () => {
// This is the role weight of the reactor and excludes the author
const expectedAveragingModifier = 5;
expect(
reactionWeight(
reactionWeight({
weights,
message,
reacterReaction,
reacterMember,
new Set(),
reactions
)
reaction: reacterReaction,
reactingMember: reacterMember,
propsChannels: new Set(),
reactions,
channelParentId: undefined,
})
).toEqual((4 * 5 * 6) / expectedAveragingModifier);
});
it("averaging is safe against divide-by-zero when all roles are zero", () => {
Expand All @@ -211,14 +213,15 @@ describe("plugins/discord/reactionWeights", () => {
const channelWeights = {defaultWeight: 1, weights: {[channelId]: 6}};
const weights = {emojiWeights, roleWeights, channelWeights};
expect(
reactionWeight(
reactionWeight({
weights,
message,
reacterReaction,
reacterMember,
new Set(),
reactions
)
reaction: reacterReaction,
reactingMember: reacterMember,
propsChannels: new Set(),
reactions,
channelParentId: undefined,
})
).toEqual(0);
});
it("dampens emoji average when dampener >0", () => {
Expand All @@ -237,14 +240,15 @@ describe("plugins/discord/reactionWeights", () => {
// This is the role weight of the reactor and excludes the author
const expectedAveragingModifier = 7;
expect(
reactionWeight(
reactionWeight({
weights,
message,
reacterReaction,
reacterMember,
new Set(),
reactions
)
reaction: reacterReaction,
reactingMember: reacterMember,
propsChannels: new Set(),
reactions,
channelParentId: undefined,
})
).toEqual((4 * 5 * 6) / expectedAveragingModifier);
});
it("sets the weight to 0 for a self-reaction", () => {
Expand All @@ -257,17 +261,18 @@ describe("plugins/discord/reactionWeights", () => {
const channelWeights = {defaultWeight: 1, weights: {}};
const weights = {emojiWeights, roleWeights, channelWeights};
expect(
reactionWeight(
reactionWeight({
weights,
message,
authorSelfReaction,
authorMember,
new Set(),
reactions
)
reaction: authorSelfReaction,
reactingMember: authorMember,
propsChannels: new Set(),
reactions,
channelParentId: undefined,
})
).toEqual(0);
});
it("sets a nonzero-weight for a self-reaction in a props channel", () => {
it("sets a nonzero-weight for a self-reaction in a props channel", () => {
const emojiWeights = {
defaultWeight: 5,
weights: {},
Expand All @@ -278,14 +283,15 @@ describe("plugins/discord/reactionWeights", () => {
const weights = {emojiWeights, roleWeights, channelWeights};
const propsChannelSet = new Set([message.channelId]);
expect(
reactionWeight(
reactionWeight({
weights,
message,
authorSelfReaction,
authorMember,
propsChannelSet,
reactions
)
reaction: authorSelfReaction,
reactingMember: authorMember,
propsChannels: propsChannelSet,
reactions,
channelParentId: undefined,
})
).toEqual(5 * 2 * 3);
});
});
Expand Down

0 comments on commit e905f33

Please sign in to comment.