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

Refactor get active threads #9638

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libs/model/src/models/topic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type { ModelInstance } from './types';
export type TopicAttributes = z.infer<typeof Topic> & {
// associations
community?: CommunityAttributes;
threads?: ThreadAttributes[] | TopicAttributes['id'][];
threads?: ThreadAttributes[];
};
export type TopicInstance = ModelInstance<TopicAttributes>;

Expand Down
77 changes: 77 additions & 0 deletions libs/model/src/thread/GetActiveThreads.query.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Query } from '@hicommonwealth/core';
import * as schemas from '@hicommonwealth/schemas';
import { models } from '../database';

export function GetActiveThreads(): Query<typeof schemas.GetActiveThreads> {
return {
...schemas.GetActiveThreads,
auth: [],
secure: false,
body: async ({ payload }) => {
const { community_id, threads_per_topic, withXRecentComments } = payload;

const topThreadsByTopic = await models.Topic.findAll({
where: { community_id },
include: [
{
model: models.Thread,
as: 'threads',
required: true,
order: [
['created_at', 'DESC'],
['last_commented_on', 'DESC'],
],
limit: threads_per_topic ?? 3,
include: [
{
model: models.Address,
as: 'Address',
attributes: ['id', 'address', 'community_id'],
include: [
{
model: models.User,
attributes: ['id', 'profile'],
},
],
},
{ model: models.Address, as: 'collaborators' },
{ model: models.Topic, as: 'topic', required: true },
{
model: models.Comment,
limit: Math.min(withXRecentComments ?? 0, 10), // cap to 10
order: [['created_at', 'DESC']],
attributes: [
'id',
'address_id',
'text',
'created_at',
'updated_at',
'deleted_at',
'marked_as_spam_at',
'discord_meta',
'content_url',
],
include: [
{
model: models.Address,
as: 'Address',
attributes: ['address'],
include: [
{
model: models.User,
attributes: ['id', 'profile'],
},
],
},
],
},
],
},
],
});
return topThreadsByTopic
.map((topic) => topic.toJSON().threads ?? [])
.flat();
},
};
}
1 change: 1 addition & 0 deletions libs/model/src/thread/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './CreateThread.command';
export * from './CreateThreadReaction.command';
export * from './DeleteThread.command';
export * from './GetActiveThreads.query';
export * from './GetThreads.query';
export * from './UpdateThread.command';
9 changes: 9 additions & 0 deletions libs/schemas/src/queries/thread.schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,12 @@ export const DEPRECATED_GetBulkThreads = z.object({
status: z.string().optional(),
withXRecentComments: z.coerce.number().optional(),
});

export const GetActiveThreads = {
input: z.object({
community_id: z.string(),
threads_per_topic: z.coerce.number().min(0).max(10).optional(),
withXRecentComments: z.coerce.number().optional(),
}),
output: z.array(Thread),
};
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function addressToUserProfile(
address: z.infer<typeof Address>,
): UserProfile {
return {
userId: address.user_id!,
userId: address.user_id ?? address.User?.id ?? 0,
avatarUrl: address.User?.profile.avatar_url ?? '',
name: address.User?.profile.name ?? DEFAULT_NAME,
address: address?.address,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ import {
CreateThreadPollResult,
__createThreadPoll,
} from './server_threads_methods/create_thread_poll';
import {
GetActiveThreadsOptions,
GetActiveThreadsResult,
__getActiveThreads,
} from './server_threads_methods/get_active_threads';
import {
GetThreadPollsOptions,
GetThreadPollsResult,
Expand Down Expand Up @@ -43,13 +38,6 @@ export class ServerThreadsController {
return __getThreadsById.call(this, options);
}

async getActiveThreads(
this: ServerThreadsController,
options: GetActiveThreadsOptions,
): Promise<GetActiveThreadsResult> {
return __getActiveThreads.call(this, options);
}

async searchThreads(
this: ServerThreadsController,
options: SearchThreadsOptions,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,13 @@ export const getThreadsHandler = async (
const { threads_per_topic, withXRecentComments } =
req.query as ActiveThreadsRequestQuery;

const activeThreads = await controllers.threads.getActiveThreads({
communityId: community_id,
threadsPerTopic: parseInt(threads_per_topic, 10),
withXRecentComments,
const activeThreads = await query(Thread.GetActiveThreads(), {
actor: { user: { email: '' } },
payload: {
community_id,
threads_per_topic: parseInt(threads_per_topic, 10),
withXRecentComments,
},
});
return success(res, activeThreads);
}
Expand Down
Loading