Skip to content

Commit

Permalink
Topic model semantics (#6177)
Browse files Browse the repository at this point in the history
* migration

* server model + references

* raw sql queries + discordbot fix

* more server side semantic updates

* client side

* test fixes

* external API /topics fix

* addEntities fix for `community_id`

* fix topic edit
  • Loading branch information
timolegros authored Jan 4, 2024
1 parent adcf1c4 commit 9769629
Show file tree
Hide file tree
Showing 33 changed files with 175 additions and 131 deletions.
34 changes: 18 additions & 16 deletions packages/commonwealth/client/scripts/models/Topic.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
export type TopicAttributes = {
name: string;
id: number;
description: string;
telegram?: string;
community_id?: string;
featured_in_sidebar?: boolean;
featured_in_new_post?: boolean;
order?: number;
default_offchain_template?: string;
total_threads: number;
channel_id?: string;
};

class Topic {
public readonly name: string;
public readonly id: number;
public readonly description: string;
public readonly telegram?: string;
public readonly chainId: string;
public readonly communityId: string;
public readonly channelId?: string;
public readonly featuredInSidebar?: boolean;
public readonly featuredInNewPost?: boolean;
Expand All @@ -16,31 +30,19 @@ class Topic {
id,
description,
telegram,
chain_id,
community_id,
featured_in_sidebar,
featured_in_new_post,
order,
default_offchain_template,
total_threads,
channel_id,
}: {
name: string;
id: number;
description: string;
telegram?: string;
chain_id?: string;
featured_in_sidebar?: boolean;
featured_in_new_post?: boolean;
order?: number;
default_offchain_template?: string;
total_threads: number;
channel_id?: string;
}) {
}: TopicAttributes) {
this.name = name;
this.id = id;
this.description = description;
this.telegram = telegram;
this.chainId = chain_id;
this.communityId = community_id;
this.featuredInSidebar = featured_in_sidebar;
this.featuredInNewPost = featured_in_new_post;
this.order = order;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const useCreateTopicMutation = () => {
mutationFn: createTopic,
onSuccess: async (data) => {
await queryClient.invalidateQueries({
queryKey: [ApiEndpoints.BULK_TOPICS, data.chainId],
queryKey: [ApiEndpoints.BULK_TOPICS, data.communityId],
});
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface EditTopicProps {
const editTopic = async ({ topic, featuredOrder }: EditTopicProps) => {
const response = await axios.patch(`${app.serverUrl()}/topics/${topic.id}`, {
id: topic.id,
community_id: topic.chainId,
community_id: topic.communityId,
name: topic.name,
description: topic.description,
telegram: topic.telegram,
Expand All @@ -32,7 +32,7 @@ const useEditTopicMutation = () => {
mutationFn: editTopic,
onSuccess: async (data, variables) => {
await queryClient.invalidateQueries({
queryKey: [ApiEndpoints.BULK_TOPICS, variables.topic.chainId],
queryKey: [ApiEndpoints.BULK_TOPICS, variables.topic.communityId],
});
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const useUpdateFeaturedTopicsOrderMutation = () => {
return useMutation({
mutationFn: updateFeaturedTopicsOrder,
onSuccess: async (data, variables) => {
const communityId = variables.featuredTopics[0].chainId;
const communityId = variables.featuredTopics[0].communityId;
await queryClient.invalidateQueries({
queryKey: [ApiEndpoints.BULK_TOPICS, communityId],
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { DeltaStatic } from 'quill';
import React, { useState } from 'react';

import { pluralizeWithoutNumberPrefix } from 'helpers';
import Topic from '../../models/Topic';
import Topic, { TopicAttributes } from '../../models/Topic';
import { useCommonNavigate } from '../../navigation/helpers';
import app from '../../state';
import {
Expand Down Expand Up @@ -77,11 +77,11 @@ export const EditTopicModal = ({
return;
}

const topicInfo = {
const topicInfo: TopicAttributes = {
id,
description: description,
name: name,
chain_id: app.activeChainId(),
community_id: app.activeChainId(),
telegram: null,
featured_in_sidebar: featuredInSidebar,
featured_in_new_post: featuredInNewPost,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ export async function __createCommunity(
delete nodeJSON.private_url;

await this.models.Topic.create({
chain_id: createdCommunity.id,
community_id: createdCommunity.id,
name: 'General',
featured_in_sidebar: true,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export async function __deleteCommunity(
});

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export async function __createGroup(
id: {
[Op.in]: topics || [],
},
chain_id: community.id,
community_id: community.id,
},
});
if (topics?.length > 0 && topics.length !== topicsToAssociate.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export async function __getGroups(
if (includeTopics) {
const topics = await this.models.Topic.findAll({
where: {
chain_id: community.id,
community_id: community.id,
group_ids: {
[Op.overlap]: groupsResult.map(({ id }) => id),
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export async function __updateGroup(
id: {
[Op.in]: topics || [],
},
chain_id: community.id,
community_id: community.id,
},
});
if (topics?.length > 0 && topics.length !== topicsToAssociate.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export async function __createThread(
const [topic] = await this.models.Topic.findOrCreate({
where: {
name: topicName,
chain_id: community?.id || null,
community_id: community?.id || null,
},
transaction,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { WhereOptions } from 'sequelize';
import { CommunityInstance } from '../../models/community';
import { ThreadAttributes } from '../../models/thread';
import { TopicAttributes } from '../../models/topic';
import { ServerThreadsController } from '../server_threads_controller';

const MIN_THREADS_PER_TOPIC = 0;
Expand All @@ -14,7 +16,7 @@ export type GetActiveThreadsResult = ThreadAttributes[];

export async function __getActiveThreads(
this: ServerThreadsController,
{ community, threadsPerTopic }: GetActiveThreadsOptions
{ community, threadsPerTopic }: GetActiveThreadsOptions,
): Promise<GetActiveThreadsResult> {
const allThreads = [];
if (
Expand All @@ -26,7 +28,9 @@ export async function __getActiveThreads(
threadsPerTopic = 3;
}

const communityWhere = { chain_id: community.id };
const communityWhere: WhereOptions<TopicAttributes> = {
community_id: community.id,
};
const communityTopics = await this.models.Topic.findAll({
where: communityWhere,
});
Expand All @@ -51,7 +55,7 @@ export async function __getActiveThreads(
['last_commented_on', 'DESC'],
],
});
})
}),
);

allRecentTopicThreadsRaw = allRecentTopicThreadsRaw.flat();
Expand All @@ -64,7 +68,7 @@ export async function __getActiveThreads(

communityTopics.forEach((topic) => {
const threadsWithCommentsCount = allRecentTopicThreads.filter(
(thread) => thread.topic_id === topic.id
(thread) => thread.topic_id === topic.id,
);
allThreads.push(...(threadsWithCommentsCount || []));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export async function __getBulkThreads(
as threads_total_likes,
threads.links as links,
topics.id AS topic_id, topics.name AS topic_name, topics.description AS topic_description,
topics.chain_id AS topic_chain,
topics.community_id AS topic_community_id,
topics.telegram AS topic_telegram,
collaborators
FROM "Addresses" AS addr
Expand Down Expand Up @@ -219,7 +219,7 @@ export async function __getBulkThreads(
id: t.topic_id,
name: t.topic_name,
description: t.topic_description,
chainId: t.topic_chain,
chainId: t.topic_community_id,
telegram: t.telegram,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ async function setThreadTopic(
const [topic] = await models.Topic.findOrCreate({
where: {
name: topicName,
chain_id: thread.chain,
community_id: thread.chain,
},
});
toUpdate.topic_id = topic.id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ export async function __createTopic(
featured_in_sidebar,
featured_in_new_post,
default_offchain_template: default_offchain_template || '',
chain_id: community.id,
community_id: community.id,
};

const [newTopic] = await this.models.Topic.findOrCreate({
where: {
name,
chain_id: community.id,
community_id: community.id,
},
defaults: options,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type GetTopicsResult = TopicWithTotalThreads[];

export async function __getTopics(
this: ServerTopicsController,
{ community }: GetTopicsOptions
{ community }: GetTopicsOptions,
): Promise<GetTopicsResult> {
const topics = await this.models.sequelize.query<TopicWithTotalThreads>(
`SELECT
Expand All @@ -21,11 +21,11 @@ export async function __getTopics(
SELECT COUNT(*)::int FROM "Threads"
WHERE chain = :community_id AND topic_id = t.id AND deleted_at IS NULL
) as total_threads
FROM "Topics" t WHERE chain_id = :community_id AND deleted_at IS NULL`,
FROM "Topics" t WHERE community_id = :community_id AND deleted_at IS NULL`,
{
replacements: { community_id: community.id },
type: QueryTypes.SELECT,
}
},
);
return topics;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const getCommunityQuery = (
model: models.Topic,
as: 'topics',
required: false,
attributes: ['id', 'name', 'chain_id'],
attributes: ['id', 'name', 'community_id'],
},
]
: []),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.sequelize.transaction(async (transaction) => {
await queryInterface.renameColumn('Topics', 'chain_id', 'community_id', {
transaction,
});
await queryInterface.sequelize.query(
`
ALTER INDEX "OffchainThreadCategories_pkey" RENAME TO "Topics_pkey";
`,
{ transaction },
);
});
},

down: async (queryInterface, Sequelize) => {
await queryInterface.sequelize.transaction(async (transaction) => {
await queryInterface.renameColumn('Topics', 'community_id', 'chain_id', {
transaction,
});
await queryInterface.sequelize.query(
`
ALTER INDEX "Topics_pkey" RENAME TO "OffchainThreadCategories_pkey";
`,
{ transaction },
);
});
},
};
2 changes: 1 addition & 1 deletion packages/commonwealth/server/models/community.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export default (
models.Community.hasMany(models.Notification, { foreignKey: 'chain_id' });
models.Community.hasMany(models.Topic, {
as: 'topics',
foreignKey: 'chain_id',
foreignKey: 'community_id',
});
models.Community.hasMany(models.Thread, { foreignKey: 'chain' });
models.Community.hasMany(models.Comment, { foreignKey: 'chain' });
Expand Down
14 changes: 7 additions & 7 deletions packages/commonwealth/server/models/topic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export type TopicAttributes = {
featured_in_new_post: boolean;
order?: number;
id?: number;
chain_id: string;
community_id: string;
description?: string;
telegram?: string;
channel_id?: string;
Expand All @@ -22,7 +22,7 @@ export type TopicAttributes = {
group_ids: number[];

// associations
chain?: CommunityAttributes;
community?: CommunityAttributes;
threads?: ThreadAttributes[] | TopicAttributes['id'][];
};

Expand All @@ -34,7 +34,7 @@ export type TopicModelStatic = ModelStatic<TopicInstance>;

export default (
sequelize: Sequelize.Sequelize,
dataTypes: typeof DataTypes
dataTypes: typeof DataTypes,
): TopicModelStatic => {
const Topic = <TopicModelStatic>sequelize.define(
'Topic',
Expand All @@ -43,7 +43,7 @@ export default (
name: { type: dataTypes.STRING, allowNull: false },
description: { type: dataTypes.TEXT, allowNull: false, defaultValue: '' },
telegram: { type: dataTypes.STRING, allowNull: true },
chain_id: { type: dataTypes.STRING, allowNull: false },
community_id: { type: dataTypes.STRING, allowNull: false },
created_at: { type: dataTypes.DATE, allowNull: false },
updated_at: { type: dataTypes.DATE, allowNull: false },
deleted_at: { type: dataTypes.DATE, allowNull: true },
Expand Down Expand Up @@ -84,13 +84,13 @@ export default (
exclude: ['created_at', 'updated_at', 'deleted_at'],
},
},
}
},
);

Topic.associate = (models) => {
models.Topic.belongsTo(models.Community, {
as: 'chain',
foreignKey: 'chain_id',
as: 'community',
foreignKey: 'community_id',
targetKey: 'id',
});
models.Topic.hasMany(models.Thread, {
Expand Down
Loading

0 comments on commit 9769629

Please sign in to comment.