From 591205b4433a55471c0d71f6714ccd69296a5f54 Mon Sep 17 00:00:00 2001 From: Timothee Legros Date: Fri, 5 Jan 2024 00:43:33 +0100 Subject: [PATCH 1/8] migration --- ...0104232957-reaction-community-semantics.js | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 packages/commonwealth/server/migrations/20240104232957-reaction-community-semantics.js diff --git a/packages/commonwealth/server/migrations/20240104232957-reaction-community-semantics.js b/packages/commonwealth/server/migrations/20240104232957-reaction-community-semantics.js new file mode 100644 index 00000000000..32ab125c15c --- /dev/null +++ b/packages/commonwealth/server/migrations/20240104232957-reaction-community-semantics.js @@ -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"; + `, + { 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 }, + ); + }); + }, +}; From e33df510f01d3a6a20bc539331c96dd355fa1513 Mon Sep 17 00:00:00 2001 From: Timothee Legros Date: Fri, 5 Jan 2024 00:49:13 +0100 Subject: [PATCH 2/8] Sequelize model + direct references --- .../create_comment_reaction.ts | 4 +-- .../delete_community.ts | 2 +- .../delete_reaction.ts | 10 ++++---- .../create_thread_reaction.ts | 4 +-- .../commonwealth/server/models/reaction.ts | 25 ++++++++----------- .../commonwealth/server/routes/deleteChain.ts | 2 +- .../server/routes/reactions/getReactions.ts | 2 +- .../server/scripts/emitWebhook.ts | 2 +- .../test/e2e/hooks/e2eDbEntityHooks.spec.ts | 4 +-- .../api/external/dbEntityHooks.spec.ts | 4 +-- .../api/external/getReactions.spec.ts | 8 +++--- .../integration/emitNotifications.spec.ts | 2 +- 12 files changed, 33 insertions(+), 36 deletions(-) diff --git a/packages/commonwealth/server/controllers/server_comments_methods/create_comment_reaction.ts b/packages/commonwealth/server/controllers/server_comments_methods/create_comment_reaction.ts index 848e4ac81a3..b315a789873 100644 --- a/packages/commonwealth/server/controllers/server_comments_methods/create_comment_reaction.ts +++ b/packages/commonwealth/server/controllers/server_comments_methods/create_comment_reaction.ts @@ -116,7 +116,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, @@ -149,7 +149,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, }, diff --git a/packages/commonwealth/server/controllers/server_communities_methods/delete_community.ts b/packages/commonwealth/server/controllers/server_communities_methods/delete_community.ts index f0057af1a94..19488c1a6bd 100644 --- a/packages/commonwealth/server/controllers/server_communities_methods/delete_community.ts +++ b/packages/commonwealth/server/controllers/server_communities_methods/delete_community.ts @@ -61,7 +61,7 @@ export async function __deleteCommunity( ); await this.models.Reaction.destroy({ - where: { chain: community.id }, + where: { community_id: community.id }, transaction: t, }); diff --git a/packages/commonwealth/server/controllers/server_reactions_methods/delete_reaction.ts b/packages/commonwealth/server/controllers/server_reactions_methods/delete_reaction.ts index d9976095c68..88ac684629e 100644 --- a/packages/commonwealth/server/controllers/server_reactions_methods/delete_reaction.ts +++ b/packages/commonwealth/server/controllers/server_reactions_methods/delete_reaction.ts @@ -1,8 +1,8 @@ -import { UserInstance } from 'server/models/user'; -import { ServerReactionsController } from '../server_reactions_controller'; import { Op } from 'sequelize'; -import { AppError } from '../../../../common-common/src/errors'; import { AddressInstance } from 'server/models/address'; +import { UserInstance } from 'server/models/user'; +import { AppError } from '../../../../common-common/src/errors'; +import { ServerReactionsController } from '../server_reactions_controller'; const Errors = { ReactionNotFound: 'Reaction not found', @@ -19,7 +19,7 @@ export type DeleteReactionResult = void; export async function __deleteReaction( this: ServerReactionsController, - { user, address, reactionId }: DeleteReactionOptions + { user, address, reactionId }: DeleteReactionOptions, ): Promise { const userOwnedAddressIds = (await user.getAddresses()) .filter((addr) => !!addr.verified) @@ -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) { diff --git a/packages/commonwealth/server/controllers/server_threads_methods/create_thread_reaction.ts b/packages/commonwealth/server/controllers/server_threads_methods/create_thread_reaction.ts index 9bdbb767c30..857e1def2c9 100644 --- a/packages/commonwealth/server/controllers/server_threads_methods/create_thread_reaction.ts +++ b/packages/commonwealth/server/controllers/server_threads_methods/create_thread_reaction.ts @@ -110,7 +110,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, @@ -139,7 +139,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, }, diff --git a/packages/commonwealth/server/models/reaction.ts b/packages/commonwealth/server/models/reaction.ts index c36fce9b074..929e62878ae 100644 --- a/packages/commonwealth/server/models/reaction.ts +++ b/packages/commonwealth/server/models/reaction.ts @@ -1,9 +1,9 @@ +import { StatsDController } from 'common-common/src/statsd'; import type * as Sequelize from 'sequelize'; import type { DataTypes } from 'sequelize'; import type { AddressAttributes } from './address'; import type { CommunityAttributes } from './community'; import type { ModelInstance, ModelStatic } from './types'; -import { StatsDController } from 'common-common/src/statsd'; import { factory, formatFilename } from 'common-common/src/logging'; const log = factory.getLogger(formatFilename(__filename)); @@ -12,7 +12,7 @@ export type ReactionAttributes = { address_id: number; reaction: string; id?: number; - chain: string; + community_id: string; thread_id?: number; proposal_id?: number; comment_id?: number; @@ -34,13 +34,13 @@ export type ReactionModelStatic = ModelStatic; export default ( sequelize: Sequelize.Sequelize, - dataTypes: typeof DataTypes + dataTypes: typeof DataTypes, ): ReactionModelStatic => { const Reaction = sequelize.define( '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 }, @@ -84,7 +84,7 @@ export default ( } } catch (error) { log.error( - `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), @@ -122,7 +122,7 @@ export default ( } } catch (error) { log.error( - `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), @@ -135,12 +135,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', @@ -149,16 +147,15 @@ export default ( ], unique: true, }, - { fields: ['chain', 'thread_id'] }, - { fields: ['chain', 'comment_id'] }, - { fields: ['canvas_hash'] }, + { 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, { diff --git a/packages/commonwealth/server/routes/deleteChain.ts b/packages/commonwealth/server/routes/deleteChain.ts index 19c5f8efae8..5cd621b6f98 100644 --- a/packages/commonwealth/server/routes/deleteChain.ts +++ b/packages/commonwealth/server/routes/deleteChain.ts @@ -74,7 +74,7 @@ const deleteChain = async ( ); await models.Reaction.destroy({ - where: { chain: chain.id }, + where: { community_id: chain.id }, transaction: t, }); diff --git a/packages/commonwealth/server/routes/reactions/getReactions.ts b/packages/commonwealth/server/routes/reactions/getReactions.ts index 80e9036c3b8..91f41d3401d 100644 --- a/packages/commonwealth/server/routes/reactions/getReactions.ts +++ b/packages/commonwealth/server/routes/reactions/getReactions.ts @@ -31,7 +31,7 @@ const getReactions = async ( } const { community_id, comment_id, addresses, count_only } = req.query; - const where: WhereOptions = { chain: community_id }; + const where: WhereOptions = { community_id }; if (comment_id) where.comment_id = comment_id; // if address is included, find which addressIds they correspond to. diff --git a/packages/commonwealth/server/scripts/emitWebhook.ts b/packages/commonwealth/server/scripts/emitWebhook.ts index 335279e1441..74a996a973d 100644 --- a/packages/commonwealth/server/scripts/emitWebhook.ts +++ b/packages/commonwealth/server/scripts/emitWebhook.ts @@ -194,7 +194,7 @@ async function main() { }); await models.Reaction.findOrCreate({ where: { - chain: chain.id, + community_id: chain.id, thread_id: thread.id, address_id: anotherAddress.id, reaction: 'like', diff --git a/packages/commonwealth/test/e2e/hooks/e2eDbEntityHooks.spec.ts b/packages/commonwealth/test/e2e/hooks/e2eDbEntityHooks.spec.ts index a61b9ed39d9..fc3101defe6 100644 --- a/packages/commonwealth/test/e2e/hooks/e2eDbEntityHooks.spec.ts +++ b/packages/commonwealth/test/e2e/hooks/e2eDbEntityHooks.spec.ts @@ -397,7 +397,7 @@ export async function createTestEntities() { reaction: 'like', address_id: -1, thread_id: -1, - chain: 'cmntest', + community_id: 'cmntest', }, }) )[0], @@ -415,7 +415,7 @@ export async function createTestEntities() { reaction: 'like', address_id: -2, comment_id: -2, - chain: 'cmntest', + community_id: 'cmntest', }, }) )[0], diff --git a/packages/commonwealth/test/integration/api/external/dbEntityHooks.spec.ts b/packages/commonwealth/test/integration/api/external/dbEntityHooks.spec.ts index 427944ca5fd..01ce7c4d7ff 100644 --- a/packages/commonwealth/test/integration/api/external/dbEntityHooks.spec.ts +++ b/packages/commonwealth/test/integration/api/external/dbEntityHooks.spec.ts @@ -308,7 +308,7 @@ export async function createTestEntities() { reaction: 'like', address_id: -1, thread_id: -1, - chain: 'cmntest', + community_id: 'cmntest', }, }) )[0], @@ -326,7 +326,7 @@ export async function createTestEntities() { reaction: 'like', address_id: -2, comment_id: -2, - chain: 'cmntest', + community_id: 'cmntest', }, }) )[0], diff --git a/packages/commonwealth/test/integration/api/external/getReactions.spec.ts b/packages/commonwealth/test/integration/api/external/getReactions.spec.ts index 1e13b94187a..ca97709cf94 100644 --- a/packages/commonwealth/test/integration/api/external/getReactions.spec.ts +++ b/packages/commonwealth/test/integration/api/external/getReactions.spec.ts @@ -11,7 +11,7 @@ import { get } from './appHook.spec'; describe('getReactions Tests', () => { it('should return reactions with specified community_id correctly', async () => { - const r: GetReactionsReq = { community_id: testReactions[0].chain }; + const r: GetReactionsReq = { community_id: testReactions[0].community_id }; const resp = await get('/api/reactions', r, true); chai.assert.lengthOf(resp.result.reactions, 5); @@ -19,7 +19,7 @@ describe('getReactions Tests', () => { it('should return reactions with specified addresses correctly', async () => { const r: GetReactionsReq = { - community_id: testReactions[0].chain, + community_id: testReactions[0].community_id, addresses: ['testAddress-1'], }; let resp = await get('/api/reactions', r, true); @@ -34,7 +34,7 @@ describe('getReactions Tests', () => { it('should paginate correctly', async () => { const r: GetReactionsReq = { - community_id: testReactions[0].chain, + community_id: testReactions[0].community_id, addresses: ['testAddress-2'], limit: 2, }; @@ -55,7 +55,7 @@ describe('getReactions Tests', () => { it('should order correctly', async () => { const r: GetReactionsReq = { - community_id: testReactions[0].chain, + community_id: testReactions[0].community_id, addresses: ['testAddress-2'], sort: OrderByOptions.CREATED, }; diff --git a/packages/commonwealth/test/integration/emitNotifications.spec.ts b/packages/commonwealth/test/integration/emitNotifications.spec.ts index 356ab366d50..567b1f542bf 100644 --- a/packages/commonwealth/test/integration/emitNotifications.spec.ts +++ b/packages/commonwealth/test/integration/emitNotifications.spec.ts @@ -104,7 +104,7 @@ describe('emitNotifications tests', () => { //reaction = await models.Reaction.create({ await models.Reaction.create({ - chain, + community_id: chain, thread_id: thread.id, address_id: userAddressId, reaction: 'like', From 3c9cb0d86c26b3c86ec1b51beba63b81cc1681ef Mon Sep 17 00:00:00 2001 From: Timothee Legros Date: Fri, 5 Jan 2024 00:53:53 +0100 Subject: [PATCH 3/8] indirect references --- .../server/controllers/server_admin_methods/get_stats.ts | 2 +- packages/commonwealth/server/routing/external.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/commonwealth/server/controllers/server_admin_methods/get_stats.ts b/packages/commonwealth/server/controllers/server_admin_methods/get_stats.ts index e67493197c6..c6bc7c69c4b 100644 --- a/packages/commonwealth/server/controllers/server_admin_methods/get_stats.ts +++ b/packages/commonwealth/server/controllers/server_admin_methods/get_stats.ts @@ -89,7 +89,7 @@ export async function __getStats( where: whereChain, }), this.models.Reaction.count({ - where: whereChain, + where: whereCommunityId, }), this.models.Vote.count({ where: whereCommunityId, diff --git a/packages/commonwealth/server/routing/external.ts b/packages/commonwealth/server/routing/external.ts index 7636daf1543..9f444363ad6 100644 --- a/packages/commonwealth/server/routing/external.ts +++ b/packages/commonwealth/server/routing/external.ts @@ -92,7 +92,7 @@ export function addExternalRoutes( postReactionsValidation, addEntities.bind( this, - 'chain', + 'community_id', models, (a) => models.Reaction.bulkCreate(a), (req: TypedRequest) => req.body.reactions, @@ -102,7 +102,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( From b4b0c9b47da31884c88d7eeb6779a3a09ccd769a Mon Sep 17 00:00:00 2001 From: Timothee Legros Date: Fri, 5 Jan 2024 00:57:19 +0100 Subject: [PATCH 4/8] Raw SQL updates --- .../get_bulk_threads.ts | 2 +- .../server/routes/communityStats.ts | 12 ++-- .../server/routes/exportMembersList.ts | 6 +- .../integration/api/recomputeCount.spec.ts | 56 +++++++++---------- 4 files changed, 38 insertions(+), 38 deletions(-) diff --git a/packages/commonwealth/server/controllers/server_threads_methods/get_bulk_threads.ts b/packages/commonwealth/server/controllers/server_threads_methods/get_bulk_threads.ts index 02a4ec07bbf..c0ac6d6186e 100644 --- a/packages/commonwealth/server/controllers/server_threads_methods/get_bulk_threads.ts +++ b/packages/commonwealth/server/controllers/server_threads_methods/get_bulk_threads.ts @@ -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 diff --git a/packages/commonwealth/server/routes/communityStats.ts b/packages/commonwealth/server/routes/communityStats.ts index 46ddca50ef6..a52b7141f87 100644 --- a/packages/commonwealth/server/routes/communityStats.ts +++ b/packages/commonwealth/server/routes/communityStats.ts @@ -8,7 +8,7 @@ const communityStats = async ( models: DB, req: Request, res: Response, - next: NextFunction + next: NextFunction, ) => { const chain = req.chain; @@ -24,7 +24,7 @@ const communityStats = async ( models, { where: { address_id: { [Op.in]: userAddressIds } } }, chain.id, - ['admin', 'moderator'] + ['admin', 'moderator'], ); if (!req.user.isAdmin && adminRoles.length === 0) { return next(new AppError('Must be admin')); @@ -44,7 +44,7 @@ ORDER BY seq.date DESC;`, { type: QueryTypes.SELECT, replacements: { chainOrCommunity: chain.id, chainName: chainName }, - } + }, ); }; const roles = await newObjectsQuery('"Addresses"', 'community_id'); @@ -58,7 +58,7 @@ ORDER BY seq.date DESC;`, { type: QueryTypes.SELECT, replacements: { chainOrCommunity: chain.id }, - } + }, ); }; const totalRoles = await totalObjectsQuery('"Addresses"', 'community_id'); @@ -78,7 +78,7 @@ LEFT JOIN ( AND ${chain ? 'chain' : 'community'} = :chainOrCommunity UNION SELECT address_id, created_at FROM "Reactions" WHERE created_at > CURRENT_DATE - ${numberOfPrevDays} - AND ${chain ? 'chain' : 'community'} = :chainOrCommunity + AND community_id = :chainOrCommunity ) objs ON objs.created_at::date = seq.date GROUP BY seq.date @@ -87,7 +87,7 @@ ORDER BY seq.date DESC; { type: QueryTypes.SELECT, replacements: { chainOrCommunity: chain.id }, - } + }, ); return res.json({ diff --git a/packages/commonwealth/server/routes/exportMembersList.ts b/packages/commonwealth/server/routes/exportMembersList.ts index d56ee35caa5..2e066475e4e 100644 --- a/packages/commonwealth/server/routes/exportMembersList.ts +++ b/packages/commonwealth/server/routes/exportMembersList.ts @@ -23,7 +23,7 @@ const exportMembersList = async ( models: DB, req: TypedRequestBody, res: TypedResponse, - next: NextFunction + next: NextFunction, ) => { if (!req.user.isAdmin) { return next(new AppError(Errors.NotAdmin)); @@ -59,7 +59,7 @@ const exportMembersList = async ( LEFT JOIN "Comments" "c" ON "a"."id" = "c"."address_id" AND "c"."chain" = :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 @@ -67,7 +67,7 @@ const exportMembersList = async ( `, { replacements: { chainId }, - } + }, ); return success(res, { data }); diff --git a/packages/commonwealth/test/integration/api/recomputeCount.spec.ts b/packages/commonwealth/test/integration/api/recomputeCount.spec.ts index 1f6b7f6f625..233129fc649 100644 --- a/packages/commonwealth/test/integration/api/recomputeCount.spec.ts +++ b/packages/commonwealth/test/integration/api/recomputeCount.spec.ts @@ -71,7 +71,7 @@ async function calcAllCountsFromSourceTables() { categories: notif_feed_categories, }, type: QueryTypes.SELECT, - } + }, )) as any; let notification_ids = {}; @@ -171,7 +171,7 @@ async function calcCountsFromSourceTable(threadId: number, commentId: number) { async function getCountsFromPreComputedColumns( threadId: number, - commentId: number + commentId: number, ) { const comment = await models.Comment.findOne({ where: { @@ -196,11 +196,11 @@ async function getCountsFromPreComputedColumns( async function getCounts(threadId: number, commentId: number) { const countsFromSourceTable = await calcCountsFromSourceTable( threadId, - commentId + commentId, ); const countsFromPreComputedColumns = await getCountsFromPreComputedColumns( threadId, - commentId + commentId, ); return { countsFromSourceTable, @@ -234,7 +234,7 @@ async function createThreadReactionRaw() { const canvas_hash = '0x0000000000000000000000000000000000000000000000000000000000000000'; await models.sequelize.query(` - INSERT INTO "Reactions" ("id", "chain", "address_id", "reaction", "thread_id", "comment_id", "canvas_action", "canvas_hash", "canvas_session", "created_at", "updated_at") + INSERT INTO "Reactions" ("id", "community_id", "address_id", "reaction", "thread_id", "comment_id", "canvas_action", "canvas_hash", "canvas_session", "created_at", "updated_at") VALUES(-300, '${chain}', '${testAddresses[0].id}', 'like', ${testThreads[0].id}, null, '{}', '${canvas_hash}', '{}', now(), now()), (-400, '${chain}', '${testAddresses[0].id}', 'like', ${testThreads[0].id}, null, '{}', '${canvas_hash}', '{}', now(), now()), (-500, '${chain}', '${testAddresses[0].id}', 'like', ${testThreads[0].id}, null, '{}', '${canvas_hash}', '{}', now(), now()) @@ -246,7 +246,7 @@ async function createCommentReactionRaw() { const canvas_hash = '0x0000000000000000000000000000000000000000000000000000000000000000'; await models.sequelize.query(` - INSERT INTO "Reactions" ("id", "chain", "address_id", "reaction", "thread_id", "comment_id", "canvas_action", "canvas_hash", "canvas_session", "created_at", "updated_at") + INSERT INTO "Reactions" ("id", "community_id", "address_id", "reaction", "thread_id", "comment_id", "canvas_action", "canvas_hash", "canvas_session", "created_at", "updated_at") VALUES(-3000, '${chain}', '${testAddresses[0].id}', 'like', null, ${testComments[0].id}, '{}', '${canvas_hash}', '{}', now(), now()) `); } @@ -254,36 +254,36 @@ async function createCommentReactionRaw() { async function verifyRecomputeCountSingle() { const before = await getCounts(testThreads[0].id, testComments[0].id); expect(before.countsFromSourceTable).to.deep.equal( - before.countsFromPreComputedColumns + before.countsFromPreComputedColumns, ); await recomputeCounts(); const after = await getCounts(testThreads[0].id, testComments[0].id); expect(after.countsFromSourceTable).to.deep.equal( - after.countsFromPreComputedColumns + after.countsFromPreComputedColumns, ); expect(before.countsFromSourceTable).to.deep.equal( - after.countsFromSourceTable + after.countsFromSourceTable, ); expect(before.countsFromPreComputedColumns).to.deep.equal( - after.countsFromPreComputedColumns + after.countsFromPreComputedColumns, ); } async function verifyRecomputeCountAll() { const before = await getCountsAll(); expect(before.countsFromSourceTable).to.deep.equal( - before.countsFromPreComputedColumns + before.countsFromPreComputedColumns, ); await recomputeCounts(); const after = await getCountsAll(); expect(after.countsFromSourceTable).to.deep.equal( - after.countsFromPreComputedColumns + after.countsFromPreComputedColumns, ); expect(before.countsFromSourceTable).to.deep.equal( - after.countsFromSourceTable + after.countsFromSourceTable, ); expect(before.countsFromPreComputedColumns).to.deep.equal( - after.countsFromPreComputedColumns + after.countsFromPreComputedColumns, ); } @@ -308,20 +308,20 @@ describe('recomputeCounts', () => { it('add comment using raw query, count corrected by recompute count', async () => { let before = await getCounts(testThreads[0].id, testComments[0].id); expect(before.countsFromSourceTable).to.deep.equal( - before.countsFromPreComputedColumns + before.countsFromPreComputedColumns, ); await createCommentRaw(); before = await getCounts(testThreads[0].id, testComments[0].id); expect(before.countsFromSourceTable).to.not.deep.equal( - before.countsFromPreComputedColumns + before.countsFromPreComputedColumns, ); expect(before.countsFromSourceTable.commentCount).to.not.deep.equal( - before.countsFromPreComputedColumns.commentCount + before.countsFromPreComputedColumns.commentCount, ); await recomputeCounts(); const after = await getCounts(testThreads[0].id, testComments[0].id); expect(after.countsFromSourceTable).to.deep.equal( - after.countsFromPreComputedColumns + after.countsFromPreComputedColumns, ); await verifyRecomputeCountAll(); }); @@ -331,22 +331,22 @@ describe('recomputeCounts', () => { it('add reaction to thread using raw query, count corrected by recompute count', async () => { let before = await getCounts(testThreads[0].id, testComments[0].id); expect(before.countsFromSourceTable).to.deep.equal( - before.countsFromPreComputedColumns + before.countsFromPreComputedColumns, ); await createThreadReactionRaw(); before = await getCounts(testThreads[0].id, testComments[0].id); expect(before.countsFromSourceTable).to.not.deep.equal( - before.countsFromPreComputedColumns + before.countsFromPreComputedColumns, ); expect( - before.countsFromSourceTable.threadReactionCount + before.countsFromSourceTable.threadReactionCount, ).to.not.deep.equal( - before.countsFromPreComputedColumns.threadReactionCount + before.countsFromPreComputedColumns.threadReactionCount, ); await recomputeCounts(); const after = await getCounts(testThreads[0].id, testComments[0].id); expect(after.countsFromSourceTable).to.deep.equal( - after.countsFromPreComputedColumns + after.countsFromPreComputedColumns, ); await verifyRecomputeCountAll(); }); @@ -354,22 +354,22 @@ describe('recomputeCounts', () => { it('add reaction to comment using raw query, count corrected by recompute count', async () => { let before = await getCounts(testThreads[0].id, testComments[0].id); expect(before.countsFromSourceTable).to.deep.equal( - before.countsFromPreComputedColumns + before.countsFromPreComputedColumns, ); await createCommentReactionRaw(); before = await getCounts(testThreads[0].id, testComments[0].id); expect(before.countsFromSourceTable).to.not.deep.equal( - before.countsFromPreComputedColumns + before.countsFromPreComputedColumns, ); expect( - before.countsFromSourceTable.commentReactionCount + before.countsFromSourceTable.commentReactionCount, ).to.not.deep.equal( - before.countsFromPreComputedColumns.commentReactionCount + before.countsFromPreComputedColumns.commentReactionCount, ); await recomputeCounts(); const after = await getCounts(testThreads[0].id, testComments[0].id); expect(after.countsFromSourceTable).to.deep.equal( - after.countsFromPreComputedColumns + after.countsFromPreComputedColumns, ); await verifyRecomputeCountAll(); }); From 6b366d17977aa71f42920abcf8d8b9dc5bd74ed6 Mon Sep 17 00:00:00 2001 From: Timothee Legros Date: Fri, 5 Jan 2024 00:59:05 +0100 Subject: [PATCH 5/8] client model --- packages/commonwealth/client/scripts/models/Reaction.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/commonwealth/client/scripts/models/Reaction.ts b/packages/commonwealth/client/scripts/models/Reaction.ts index a60fd9e250c..180e4263902 100644 --- a/packages/commonwealth/client/scripts/models/Reaction.ts +++ b/packages/commonwealth/client/scripts/models/Reaction.ts @@ -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; @@ -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; From 3f2fb0a0656f430aa3abb0d25e6d99335846483e Mon Sep 17 00:00:00 2001 From: Timothee Legros Date: Fri, 5 Jan 2024 01:22:45 +0100 Subject: [PATCH 6/8] eslint + test fixes --- packages/commonwealth/server/models/reaction.ts | 6 ++++-- .../test/integration/api/recomputeCount.spec.ts | 7 ++++--- .../server_comments_controller.spec.ts | 4 ++-- .../server_reactions_controller.spec.ts | 16 ++++++++-------- .../server_threads_controller.spec.ts | 4 ++-- 5 files changed, 20 insertions(+), 17 deletions(-) diff --git a/packages/commonwealth/server/models/reaction.ts b/packages/commonwealth/server/models/reaction.ts index 929e62878ae..e4382f31983 100644 --- a/packages/commonwealth/server/models/reaction.ts +++ b/packages/commonwealth/server/models/reaction.ts @@ -84,7 +84,8 @@ export default ( } } catch (error) { log.error( - `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), @@ -122,7 +123,8 @@ export default ( } } catch (error) { log.error( - `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), diff --git a/packages/commonwealth/test/integration/api/recomputeCount.spec.ts b/packages/commonwealth/test/integration/api/recomputeCount.spec.ts index 233129fc649..5731c722a5b 100644 --- a/packages/commonwealth/test/integration/api/recomputeCount.spec.ts +++ b/packages/commonwealth/test/integration/api/recomputeCount.spec.ts @@ -1,3 +1,4 @@ +/* eslint-disable max-len */ import chai, { expect } from 'chai'; import chaiHttp from 'chai-http'; import { Op, QueryTypes } from 'sequelize'; @@ -74,7 +75,7 @@ async function calcAllCountsFromSourceTables() { }, )) as any; - let notification_ids = {}; + const notification_ids = {}; for (const notification of notifications) { if (!notification.thread_id) continue; notification_ids[notification.thread_id] = notification.id; @@ -287,7 +288,7 @@ async function verifyRecomputeCountAll() { ); } -describe('recomputeCounts', () => { +describe.only('recomputeCounts', () => { before(async () => { testVerifiedChainAddress = await modelUtils.createAndVerifyAddress({ chain: 'alex', @@ -390,7 +391,7 @@ describe('recomputeCounts', () => { expect(cRes).not.to.be.null; expect(cRes.error).not.to.be.null; - const before = await getCounts(testThreads[0].id, testComments[0].id); + await getCounts(testThreads[0].id, testComments[0].id); // expect(before.countsFromSourceTable.notification_id).to.be.greaterThan(0); await verifyRecomputeCountAll(); }); diff --git a/packages/commonwealth/test/unit/server_controllers/server_comments_controller.spec.ts b/packages/commonwealth/test/unit/server_controllers/server_comments_controller.spec.ts index 1f4f542c93b..cf17c50fffe 100644 --- a/packages/commonwealth/test/unit/server_controllers/server_comments_controller.spec.ts +++ b/packages/commonwealth/test/unit/server_controllers/server_comments_controller.spec.ts @@ -14,7 +14,7 @@ describe('ServerCommentsController', () => { Reaction: { findOne: sandbox.stub().resolves({ id: 2, - chain: 'ethereum', + community_id: 'ethereum', Address: { address: '0x123', community_id: 'ethereum', @@ -25,7 +25,7 @@ describe('ServerCommentsController', () => { findOrCreate: sandbox.stub().resolves([ { id: 2, - chain: 'ethereum', + community_id: 'ethereum', Address: { address: '0x123', community_id: 'ethereum', diff --git a/packages/commonwealth/test/unit/server_controllers/server_reactions_controller.spec.ts b/packages/commonwealth/test/unit/server_controllers/server_reactions_controller.spec.ts index d7b64a0f6a6..ed0e29763a3 100644 --- a/packages/commonwealth/test/unit/server_controllers/server_reactions_controller.spec.ts +++ b/packages/commonwealth/test/unit/server_controllers/server_reactions_controller.spec.ts @@ -11,7 +11,7 @@ describe('ServerReactionsController', () => { Reaction: { findOne: sandbox.stub().resolves({ id: 777, - chain: 'ethereum', + community_id: 'ethereum', Address: { address: '0x123', }, @@ -29,7 +29,7 @@ describe('ServerReactionsController', () => { }; const serverReactionsController = new ServerReactionsController( db as any, - banCache as any + banCache as any, ); await serverReactionsController.deleteReaction({ user: user as any, @@ -45,7 +45,7 @@ describe('ServerReactionsController', () => { ...(address as any), address: '0xbanned', }, - }) + }), ).to.be.rejectedWith('Ban error: banned'); }); @@ -68,14 +68,14 @@ describe('ServerReactionsController', () => { }; const serverReactionsController = new ServerReactionsController( db as any, - banCache as any + banCache as any, ); expect( serverReactionsController.deleteReaction({ user: user as any, reactionId: 888, address: address as any, - }) + }), ).to.be.rejectedWith(`Reaction not found: 888`); }); @@ -85,7 +85,7 @@ describe('ServerReactionsController', () => { Reaction: { findOne: sandbox.stub().resolves({ id: 999, - chain: 'ethereum', + community_id: 'ethereum', Address: { address: '0x123', }, @@ -105,14 +105,14 @@ describe('ServerReactionsController', () => { }; const serverReactionsController = new ServerReactionsController( db as any, - banCache as any + banCache as any, ); expect( serverReactionsController.deleteReaction({ user: user as any, reactionId: 999, address: address as any, - }) + }), ).to.be.rejectedWith('Ban error: big ban err'); }); }); diff --git a/packages/commonwealth/test/unit/server_controllers/server_threads_controller.spec.ts b/packages/commonwealth/test/unit/server_controllers/server_threads_controller.spec.ts index 1b3e7ea6563..4d14190e454 100644 --- a/packages/commonwealth/test/unit/server_controllers/server_threads_controller.spec.ts +++ b/packages/commonwealth/test/unit/server_controllers/server_threads_controller.spec.ts @@ -11,7 +11,7 @@ describe('ServerThreadsController', () => { Reaction: { findOne: sandbox.stub().resolves({ id: 2, - chain: 'ethereum', + community_id: 'ethereum', Address: { address: '0x123', community_id: 'ethereum', @@ -22,7 +22,7 @@ describe('ServerThreadsController', () => { findOrCreate: sandbox.stub().resolves([ { id: 2, - chain: 'ethereum', + community_id: 'ethereum', Address: { address: '0x123', community_id: 'ethereum', From 8bfcfb29a15975fd7f244c246052840bb5cb375d Mon Sep 17 00:00:00 2001 From: Timothee Legros Date: Mon, 8 Jan 2024 18:26:30 +0100 Subject: [PATCH 7/8] remove .only --- .../test/integration/api/recomputeCount.spec.ts | 2 +- .../evmChainEvents/scheduleNodeProcessing.spec.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/commonwealth/test/integration/api/recomputeCount.spec.ts b/packages/commonwealth/test/integration/api/recomputeCount.spec.ts index c987da5332b..765a26929b7 100644 --- a/packages/commonwealth/test/integration/api/recomputeCount.spec.ts +++ b/packages/commonwealth/test/integration/api/recomputeCount.spec.ts @@ -288,7 +288,7 @@ async function verifyRecomputeCountAll() { ); } -describe.only('recomputeCounts', () => { +describe('recomputeCounts', () => { before(async () => { testVerifiedChainAddress = await modelUtils.createAndVerifyAddress({ chain: 'alex', diff --git a/packages/commonwealth/test/integration/evmChainEvents/scheduleNodeProcessing.spec.ts b/packages/commonwealth/test/integration/evmChainEvents/scheduleNodeProcessing.spec.ts index 65f0681ada0..fef219ad3d0 100644 --- a/packages/commonwealth/test/integration/evmChainEvents/scheduleNodeProcessing.spec.ts +++ b/packages/commonwealth/test/integration/evmChainEvents/scheduleNodeProcessing.spec.ts @@ -1,6 +1,7 @@ import { expect } from 'chai'; -import { scheduleNodeProcessing } from '../../../server/workers/evmChainEvents/nodeProcessing'; import sinon from 'sinon'; +import { scheduleNodeProcessing } from '../../../server/workers/evmChainEvents/nodeProcessing'; +import { resetDatabase } from '../../util/resetDatabase'; import { getTestAbi, getTestCommunityContract, @@ -8,9 +9,8 @@ import { getTestSignatures, getTestSubscription, } from './util'; -import { resetDatabase } from '../../util/resetDatabase'; -describe.only('scheduleNodeProcessing', () => { +describe('scheduleNodeProcessing', () => { const sandbox = sinon.createSandbox(); let processChainStub: sinon.SinonSpy; let clock: sinon.SinonFakeTimers; From 5045817ed749fe21484d983239ed0a898ce42869 Mon Sep 17 00:00:00 2001 From: Timothee Legros Date: Wed, 10 Jan 2024 21:03:40 +0100 Subject: [PATCH 8/8] capitalize foreign keys --- .../20240104232957-reaction-community-semantics.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/commonwealth/server/migrations/20240104232957-reaction-community-semantics.js b/packages/commonwealth/server/migrations/20240104232957-reaction-community-semantics.js index 32ab125c15c..8eaa17dfaa8 100644 --- a/packages/commonwealth/server/migrations/20240104232957-reaction-community-semantics.js +++ b/packages/commonwealth/server/migrations/20240104232957-reaction-community-semantics.js @@ -14,13 +14,13 @@ module.exports = { ); await queryInterface.sequelize.query( ` - ALTER TABLE "Reactions" RENAME CONSTRAINT "OffchainReactions_comment_id_fkey" TO "reactions_comment_id_fkey"; + 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"; + ALTER TABLE "Reactions" RENAME CONSTRAINT "OffchainReactions_thread_id_fkey" TO "Reactions_thread_id_fkey"; `, { transaction }, ); @@ -70,13 +70,13 @@ module.exports = { ); await queryInterface.sequelize.query( ` - ALTER TABLE "Reactions" RENAME CONSTRAINT "reactions_comment_id_fkey" TO "OffchainReactions_comment_id_fkey"; + 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"; + ALTER TABLE "Reactions" RENAME CONSTRAINT "Reactions_thread_id_fkey" TO "OffchainReactions_thread_id_fkey"; `, { transaction }, );