diff --git a/src/pages/User/content/SpaceProfile.tsx b/src/pages/User/content/SpaceProfile.tsx index 39d31697d6..bc47f96097 100644 --- a/src/pages/User/content/SpaceProfile.tsx +++ b/src/pages/User/content/SpaceProfile.tsx @@ -290,6 +290,7 @@ export const SpaceProfile = ({ user, docs }: IProps) => { flexDirection: ['column', 'column', 'row'], justifyContent: 'space-between', alignItems: 'flex-start', + gap: [0, 0, 6], }} > + +export const updateDiscussionMetadata = ( + db: DatabaseV2, + discussion: IDiscussion, +) => { + const collectionName = getCollectionName(discussion.sourceType) + + if (!collectionName) { + logger.trace( + `Unable to find collection. Discussion metadata was not updated. sourceType: ${discussion.sourceType}`, + ) + return + } + + const commentCount = discussion.comments.length + const latestCommentDate = new Date( + Math.max( + ...discussion.comments.map((comment) => + new Date(comment._created).valueOf(), + ), + ), + ) + + return db + .collection(collectionName) + .doc(discussion.sourceId) + .update({ commentCount, latestCommentDate }) +} + +const getCollectionName = (sourceType: string): DiscussionEndpoints | null => { + switch (sourceType) { + case 'question': + return 'questions' + case 'howto': + return 'howtos' + case 'research': + return 'research' + default: + return null + } +} diff --git a/src/stores/Discussions/discussions.store.tsx b/src/stores/Discussions/discussions.store.tsx index bf23968719..9aa919a7aa 100644 --- a/src/stores/Discussions/discussions.store.tsx +++ b/src/stores/Discussions/discussions.store.tsx @@ -7,6 +7,7 @@ import { getUserCountry } from 'src/utils/getUserCountry' import { hasAdminRights, randomID } from 'src/utils/helpers' import { ModuleStore } from '../common/module.store' +import { updateDiscussionMetadata } from './discussionEvents' import type { IUserPPDB } from 'src/models' import type { @@ -209,6 +210,8 @@ export class DiscussionStore extends ModuleStore { ) { await dbRef.set({ ...cloneDeep(discussion) }) + updateDiscussionMetadata(this.db, discussion) + return toJS(dbRef.get()) }