Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reactions model semantics #6219

Merged
merged 14 commits into from
Jan 10, 2024
4 changes: 2 additions & 2 deletions packages/commonwealth/client/scripts/models/Reaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export type ReactionType = 'like';
class Reaction {
public readonly id: number;
public readonly author: string;
public readonly chain: string;
public readonly communityId: string;
public readonly reaction: string;
public readonly threadId: number | string;
public readonly commentId: number | string;
Expand All @@ -29,7 +29,7 @@ class Reaction {
}) {
this.id = id;
this.author = Address.address;
this.chain = Address.community_id;
this.communityId = Address.community_id;
this.reaction = reaction;
this.threadId = thread_id;
this.commentId = comment_id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export async function __getStats(
where: whereCommunityId,
}),
this.models.Reaction.count({
where: whereChain,
where: whereCommunityId,
}),
this.models.Vote.count({
where: whereCommunityId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export async function __createCommentReaction(
const reactionData: ReactionAttributes = {
reaction,
address_id: address.id,
chain: community.id,
community_id: community.id,
comment_id: comment.id,
canvas_action: canvasAction,
canvas_session: canvasSession,
Expand Down Expand Up @@ -139,7 +139,7 @@ export async function __createCommentReaction(
comment_text: comment.text,
root_title: thread.title,
root_type: null, // What is this for?
chain_id: finalReaction.chain,
chain_id: finalReaction.community_id,
author_address: finalReaction.Address.address,
author_chain: finalReaction.Address.community_id,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export async function __deleteCommunity(
);

await this.models.Reaction.destroy({
where: { chain: community.id },
where: { community_id: community.id },
transaction: t,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export async function __deleteReaction(

// check if author is banned
const [canInteract, banError] = await this.banCache.checkBan({
communityId: reaction.chain,
communityId: reaction.community_id,
address: address.address,
});
if (!canInteract) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export async function __createThreadReaction(
const reactionData: ReactionAttributes = {
reaction,
address_id: address.id,
chain: community.id,
community_id: community.id,
thread_id: thread.id,
canvas_action: canvasAction,
canvas_session: canvasSession,
Expand Down Expand Up @@ -129,7 +129,7 @@ export async function __createThreadReaction(
thread_id: thread.id,
root_title: thread.title,
root_type: 'discussion',
chain_id: finalReaction.chain,
chain_id: finalReaction.community_id,
author_address: finalReaction.Address.address,
author_chain: finalReaction.Address.community_id,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export async function __getBulkThreads(
}
LEFT JOIN "Addresses" ad
ON r.address_id = ad.id
where r.chain = $community_id
where r.community_id = $community_id
GROUP BY thread_id
) reactions
ON t.id = reactions.thread_id
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
'use strict';

module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.sequelize.transaction(async (transaction) => {
await queryInterface.renameColumn('Reactions', 'chain', 'community_id', {
transaction,
});
await queryInterface.sequelize.query(
`
ALTER INDEX "OffchainReactions_pkey" RENAME TO "Reactions_pkey";
jnaviask marked this conversation as resolved.
Show resolved Hide resolved
`,
{ transaction },
);
await queryInterface.sequelize.query(
`
ALTER TABLE "Reactions" RENAME CONSTRAINT "OffchainReactions_comment_id_fkey" TO "Reactions_comment_id_fkey";
`,
{ transaction },
);
await queryInterface.sequelize.query(
`
ALTER TABLE "Reactions" RENAME CONSTRAINT "OffchainReactions_thread_id_fkey" TO "Reactions_thread_id_fkey";
`,
{ transaction },
);
await queryInterface.sequelize.query(
`
ALTER INDEX "offchain_reactions_address_id" RENAME TO "reactions_address_id";
`,
{ transaction },
);
await queryInterface.sequelize.query(
`
ALTER INDEX "offchain_reactions_chain_comment_id" RENAME TO "reactions_community_id_comment_id";
`,
{ transaction },
);
await queryInterface.sequelize.query(
`
ALTER INDEX "offchain_reactions_chain_thread_id" RENAME TO "reactions_community_id_thread_id";
`,
{ transaction },
);

// remove index already covered by primary key index
await queryInterface.removeIndex('Reactions', 'offchain_reactions_id', {
transaction,
});

await queryInterface.sequelize.query(
`
ALTER INDEX "offchain_reactions_chain_address_id_thread_id_proposal_id_comme" RENAME TO "reactions_unique";
`,
{ transaction },
);
});
},

down: async (queryInterface, Sequelize) => {
await queryInterface.sequelize.transaction(async (transaction) => {
await queryInterface.renameColumn('Reactions', 'community_id', 'chain', {
transaction,
});
await queryInterface.sequelize.query(
`
ALTER INDEX "Reactions_pkey" RENAME TO "OffchainReactions_pkey";
`,
{ transaction },
);
await queryInterface.sequelize.query(
`
ALTER TABLE "Reactions" RENAME CONSTRAINT "Reactions_comment_id_fkey" TO "OffchainReactions_comment_id_fkey";
`,
{ transaction },
);
await queryInterface.sequelize.query(
`
ALTER TABLE "Reactions" RENAME CONSTRAINT "Reactions_thread_id_fkey" TO "OffchainReactions_thread_id_fkey";
`,
{ transaction },
);
await queryInterface.sequelize.query(
`
ALTER INDEX "reactions_address_id" RENAME TO "offchain_reactions_address_id";
`,
{ transaction },
);
await queryInterface.sequelize.query(
`
ALTER INDEX "reactions_community_id_comment_id" RENAME TO "offchain_reactions_chain_comment_id";
`,
{ transaction },
);
await queryInterface.sequelize.query(
`
ALTER INDEX "reactions_community_id_thread_id" RENAME TO "offchain_reactions_chain_thread_id";
`,
{ transaction },
);

await queryInterface.addIndex('Reactions', {
fields: ['id'],
name: 'offchain_reactions_id',
transaction,
});

await queryInterface.sequelize.query(
`
ALTER INDEX "reactions_unique" RENAME TO "offchain_reactions_chain_address_id_thread_id_proposal_id_comme";
`,
{ transaction },
);
});
},
};
24 changes: 11 additions & 13 deletions packages/commonwealth/server/models/reaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import type { DataTypes } from 'sequelize';
import type { AddressAttributes } from './address';
import type { CommunityAttributes } from './community';
import type { ModelInstance, ModelStatic } from './types';

const log = loggerFactory.getLogger(formatFilename(__filename));

export type ReactionAttributes = {
address_id: number;
reaction: string;
id?: number;
chain: string;
community_id: string;
thread_id?: number;
proposal_id?: number;
comment_id?: number;
Expand Down Expand Up @@ -42,7 +43,7 @@ export default (
'Reaction',
{
id: { type: dataTypes.INTEGER, autoIncrement: true, primaryKey: true },
chain: { type: dataTypes.STRING, allowNull: false },
community_id: { type: dataTypes.STRING, allowNull: false },
thread_id: { type: dataTypes.INTEGER, allowNull: true },
proposal_id: { type: dataTypes.STRING, allowNull: true },
comment_id: { type: dataTypes.INTEGER, allowNull: true },
Expand Down Expand Up @@ -86,8 +87,8 @@ export default (
}
} catch (error) {
log.error(
// eslint-disable-next-line max-len
`incrementing thread reaction count afterCreate: thread_id ${thread_id} comment_id ${comment_id} ${error}`,
`incrementing thread reaction count ` +
`afterCreate: thread_id ${thread_id} comment_id ${comment_id} ${error}`,
);
StatsDController.get().increment('cw.reaction-count-error', {
thread_id: String(thread_id),
Expand Down Expand Up @@ -125,8 +126,8 @@ export default (
}
} catch (error) {
log.error(
// eslint-disable-next-line max-len
`incrementing thread reaction count afterDestroy: thread_id ${thread_id} comment_id ${comment_id} ${error}`,
`incrementing thread reaction count afterDestroy: ` +
`thread_id ${thread_id} comment_id ${comment_id} ${error}`,
);
StatsDController.get().increment('cw.hook.reaction-count-error', {
thread_id: String(thread_id),
Expand All @@ -139,12 +140,10 @@ export default (
createdAt: 'created_at',
updatedAt: 'updated_at',
indexes: [
{ fields: ['id'] },
{ fields: ['chain', 'thread_id', 'proposal_id', 'comment_id'] },
{ fields: ['address_id'] },
{
fields: [
'chain',
'community_id',
'address_id',
'thread_id',
'proposal_id',
Expand All @@ -153,16 +152,15 @@ export default (
],
unique: true,
},
{ fields: ['chain', 'thread_id'] },
{ fields: ['chain', 'comment_id'] },
{ fields: ['canvas_hash'] },
jnaviask marked this conversation as resolved.
Show resolved Hide resolved
{ fields: ['community_id', 'thread_id'] },
{ fields: ['community_id', 'comment_id'] },
],
},
);

Reaction.associate = (models) => {
models.Reaction.belongsTo(models.Community, {
foreignKey: 'chain',
foreignKey: 'community_id',
targetKey: 'id',
});
models.Reaction.belongsTo(models.Address, {
Expand Down
2 changes: 1 addition & 1 deletion packages/commonwealth/server/routes/communityStats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ LEFT JOIN (
AND community_id = :chainOrCommunity
UNION
SELECT address_id, created_at FROM "Reactions" WHERE created_at > CURRENT_DATE - ${numberOfPrevDays}
AND ${chain ? 'chain' : 'community'} = :chainOrCommunity
timolegros marked this conversation as resolved.
Show resolved Hide resolved
AND community_id = :chainOrCommunity
) objs
ON objs.created_at::date = seq.date
GROUP BY seq.date
Expand Down
2 changes: 1 addition & 1 deletion packages/commonwealth/server/routes/exportMembersList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const exportMembersList = async (
LEFT JOIN
"Comments" "c" ON "a"."id" = "c"."address_id" AND "c"."community_id" = :chainId
LEFT JOIN
"Reactions" "r" ON "a"."id" = "r"."address_id" AND "r"."chain" = :chainId
"Reactions" "r" ON "a"."id" = "r"."address_id" AND "r"."community_id" = :chainId
WHERE
"a"."community_id" = :chainId
GROUP BY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const getReactions = async (
}
const { community_id, comment_id, addresses, count_only } = req.query;

const where: WhereOptions<ReactionAttributes> = { chain: community_id };
const where: WhereOptions<ReactionAttributes> = { community_id };
if (comment_id) where.comment_id = comment_id;

// if address is included, find which addressIds they correspond to.
Expand Down
4 changes: 2 additions & 2 deletions packages/commonwealth/server/routing/external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function addExternalRoutes(
postReactionsValidation,
addEntities.bind(
this,
'chain',
'community_id',
models,
(a) => models.Reaction.bulkCreate(a),
(req: TypedRequest<PostReactionsReq>) => req.body.reactions,
Expand All @@ -88,7 +88,7 @@ export function addExternalRoutes(
'/reactions',
passport.authenticate('jwt', { session: false }),
onlyIds,
deleteEntities.bind(this, 'chain', models, models.Reaction),
deleteEntities.bind(this, 'community_id', models, models.Reaction),
);

router.get(
Expand Down
2 changes: 1 addition & 1 deletion packages/commonwealth/server/scripts/emitWebhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ async function main() {
});
await models.Reaction.findOrCreate({
where: {
chain: community.id,
community_id: community.id,
thread_id: thread.id,
address_id: anotherAddress.id,
reaction: 'like',
Expand Down
4 changes: 2 additions & 2 deletions packages/commonwealth/test/e2e/hooks/e2eDbEntityHooks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ export async function createTestEntities() {
reaction: 'like',
address_id: -1,
thread_id: -1,
chain: 'cmntest',
community_id: 'cmntest',
},
})
)[0],
Expand All @@ -415,7 +415,7 @@ export async function createTestEntities() {
reaction: 'like',
address_id: -2,
comment_id: -2,
chain: 'cmntest',
community_id: 'cmntest',
},
})
)[0],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ export async function createTestEntities() {
reaction: 'like',
address_id: -1,
thread_id: -1,
chain: 'cmntest',
community_id: 'cmntest',
},
})
)[0],
Expand All @@ -326,7 +326,7 @@ export async function createTestEntities() {
reaction: 'like',
address_id: -2,
comment_id: -2,
chain: 'cmntest',
community_id: 'cmntest',
},
})
)[0],
Expand Down
Loading
Loading