diff --git a/src/models/post.ts b/src/models/post.ts index 1d2f0f0..60fd0c8 100644 --- a/src/models/post.ts +++ b/src/models/post.ts @@ -4,7 +4,7 @@ import { Schema, model } from 'mongoose'; import { HydratedPostDocument, IPost, IPostMethods, PostModel } from '@/types/mongoose/post'; import { HydratedCommunityDocument } from '@/types/mongoose/community'; import { PostToJSONOptions } from '@/types/mongoose/post-to-json-options'; -import { PostPainting, PostScreenshot } from '@/types/common/post'; +import { PostData, PostPainting, PostScreenshot, PostTopicTag } from '@/types/miiverse/post'; const PostSchema = new Schema({ id: String, @@ -169,59 +169,60 @@ PostSchema.method('formatScreenshot', function formatScree } }); -PostSchema.method('json', function json(options: PostToJSONOptions, community?: HydratedCommunityDocument): Record { - const json: Record = { +PostSchema.method('formatTopicTag', function formatTopicTag(): PostTopicTag { + return { + name: this.topic_tag, + title_id: this.title_id + }; +}); + +PostSchema.method('json', function json(options: PostToJSONOptions, community?: HydratedCommunityDocument): PostData { + const post: PostData = { + app_data: undefined, // TODO - I try to keep these fields in the real order they show up in, but idk where this one goes body: this.cleanedBody(), - country_id: this.country_id ? this.country_id : 254, + community_id: this.community_id, // TODO - This sucks + country_id: this.country_id, created_at: moment(this.created_at).format('YYYY-MM-DD HH:MM:SS'), feeling_id: this.feeling_id, id: this.id, - is_autopost: this.is_autopost, - is_community_private_autopost: this.is_community_private_autopost, - is_spoiler: this.is_spoiler, - is_app_jumpable: this.is_app_jumpable, - empathy_count: this.empathy_count, + is_autopost: this.is_autopost ? 1 : 0, + is_community_private_autopost: this.is_community_private_autopost ? 1 : 0, + is_spoiler: this.is_spoiler ? 1 : 0, + is_app_jumpable: this.is_app_jumpable ? 1 : 0, + empathy_count: this.empathy_count || 0, language_id: this.language_id, - number: '0', + mii: undefined, // * Conditionally set later + mii_face_url: undefined, // * Conditionally set later + number: 0, + painting: this.formatPainting(), pid: this.pid, platform_id: this.platform_id, region_id: this.region_id, - reply_count: this.reply_count, + reply_count: this.reply_count || 0, screen_name: this.screen_name, - title_id: this.title_id + screenshot: this.formatScreenshot(), + topic_tag: undefined, // * Conditionally set later + title_id: this.title_id, }; - if (this.app_data && options.app_data) { - json.app_data = this.cleanedAppData(); - } - - if (options.topics && community) { - json.community_id = community.community_id; - } else { - json.community_id = this.community_id; + if (options.app_data) { + post.app_data = this.cleanedAppData(); } if (options.with_mii) { - json.mii = this.cleanedMiiData(); - json.mii_face_url = this.mii_face_url; + post.mii = this.cleanedMiiData(); + post.mii_face_url = this.mii_face_url; } - if (this.painting) { - json.painting = this.formatPainting(); + if (options.topic_tag) { + post.topic_tag = this.formatTopicTag(); } - if (this.screenshot && this.screenshot_length) { - json.screenshot = this.formatScreenshot(); + if (community) { + post.community_id = community.community_id; } - if (this.topic_tag && options.topic_tag) { - json.topic_tag = { - name: this.topic_tag, - title_id: this.title_id - }; - } - - return json; + return post; }); PostSchema.pre('save', async function(next) { diff --git a/src/services/api/routes/topics.ts b/src/services/api/routes/topics.ts index a0e5dc1..1451939 100644 --- a/src/services/api/routes/topics.ts +++ b/src/services/api/routes/topics.ts @@ -10,7 +10,7 @@ import { Community } from '@/models/community'; import { IPost } from '@/types/mongoose/post'; import { HydratedEndpointDocument } from '@/types/mongoose/endpoint'; import { HydratedCommunityDocument } from '@/types/mongoose/community'; -import { WWPData, WWPPost, WWPTopic } from '@/types/common/wara-wara-plaza'; +import { WWPData, WWPTopic } from '@/types/miiverse/wara-wara-plaza'; const router = express.Router(); const ONE_HOUR = 60 * 60 * 1000; @@ -115,33 +115,12 @@ async function generateTopicsData(communities: HydratedCommunityDocument[]): Pro const people = await getCommunityPeople(community); for (const person of people) { - const hydratedPost = Post.hydrate(person.post); + const post = Post.hydrate(person.post).json({ + with_mii: true, + topic_tag: true + }); - const post: WWPPost = { - body: hydratedPost.cleanedBody(), - community_id: 0xFFFFFFFF, // * This is how it was in the real WWP. Unsure why, but it works - country_id: hydratedPost.country_id, - created_at: moment(hydratedPost.created_at).format('YYYY-MM-DD HH:MM:SS'), - feeling_id: hydratedPost.feeling_id, - id: hydratedPost.id, - is_autopost: hydratedPost.is_autopost ? 1 : 0, - is_community_private_autopost: hydratedPost.is_community_private_autopost ? 1 : 0, - is_spoiler: hydratedPost.is_spoiler ? 1 : 0, - is_app_jumpable: hydratedPost.is_app_jumpable ? 1 : 0, - empathy_count: hydratedPost.empathy_count || 0, - language_id: hydratedPost.language_id, - mii: hydratedPost.cleanedMiiData(), - mii_face_url: hydratedPost.mii_face_url, - number: 0, - painting: hydratedPost.formatPainting(), - pid: hydratedPost.pid, - platform_id: hydratedPost.platform_id, - region_id: hydratedPost.region_id, - reply_count: hydratedPost.reply_count || 0, - screen_name: hydratedPost.screen_name, - screenshot: hydratedPost.formatScreenshot(), - title_id: hydratedPost.title_id, - }; + post.community_id = 0xFFFFFFFF; // * Make this match above. This is how it was in the real WWP. Unsure why, but it works topic.people.push({ person: { diff --git a/src/types/common/post.ts b/src/types/common/post.ts deleted file mode 100644 index 2807c31..0000000 --- a/src/types/common/post.ts +++ /dev/null @@ -1,11 +0,0 @@ -export type PostPainting = { - format: string; - content: string; - size: number; - url: string; -}; - -export type PostScreenshot = { - size: number; - url: string; -}; \ No newline at end of file diff --git a/src/types/common/wara-wara-plaza.ts b/src/types/common/wara-wara-plaza.ts deleted file mode 100644 index 5454598..0000000 --- a/src/types/common/wara-wara-plaza.ts +++ /dev/null @@ -1,70 +0,0 @@ -// TODO - Maybe this can become more generalized, and not specific to WWP? -export type WWPPost = { - body?: string; - community_id: number; - country_id: number; - created_at: string; - feeling_id: number; - id: string; - is_autopost: 0 | 1; - is_community_private_autopost: 0 | 1; - is_spoiler: 0 | 1; - is_app_jumpable: 0 | 1; - empathy_count: number; - language_id: number; - mii: string; - mii_face_url: string; - number: number; - painting?: { - format: string; - content: string; - size: number; - url: string; - }; - pid: number; - platform_id: number; - region_id: number; - reply_count: number; - screen_name: string; - screenshot?: { - size: number; - url: string; - }; - title_id: string; -}; - -export type WWPPerson = { - person: { - posts: { - post: WWPPost; - }[]; - } -}; - -export type WWPTopic = { - empathy_count: number; - has_shop_page: 0 | 1; - icon: string; - title_ids: { - title_id: string; - }[]; - title_id: string; - community_id: number; - is_recommended: 0 | 1; - name: string; - people: WWPPerson[]; - position: number; -}; - -export type WWPData = { - result: { - has_error: 0 | 1; - version: 1; - expire: string; - request_name: 'topics'; - topics: { - topic: WWPTopic; - }[]; - } -}; - diff --git a/src/types/miiverse/post.ts b/src/types/miiverse/post.ts new file mode 100644 index 0000000..141f7b8 --- /dev/null +++ b/src/types/miiverse/post.ts @@ -0,0 +1,44 @@ +export type PostData = { + app_data?: string; // TODO - I try to keep these fields in the real order they show up in, but idk where this one goes + body?: string; + community_id: number | string; // TODO - Remove this union. Only done to bypass some errors which don't break anything + country_id: number; + created_at: string; + feeling_id: number; + id: string; + is_autopost: 0 | 1; + is_community_private_autopost: 0 | 1; + is_spoiler: 0 | 1; + is_app_jumpable: 0 | 1; + empathy_count: number; + language_id: number; + mii?: string; + mii_face_url?: string; + number: number; + painting?: PostPainting; + pid: number; + platform_id: number; + region_id: number; + reply_count: number; + screen_name: string; + screenshot?: PostScreenshot; + topic_tag?: PostTopicTag; + title_id: string; +}; + +export type PostPainting = { + format: string; + content: string; + size: number; + url: string; +}; + +export type PostScreenshot = { + size: number; + url: string; +}; + +export type PostTopicTag = { + name: string; + title_id: string; +}; \ No newline at end of file diff --git a/src/types/miiverse/wara-wara-plaza.ts b/src/types/miiverse/wara-wara-plaza.ts new file mode 100644 index 0000000..e89b9d3 --- /dev/null +++ b/src/types/miiverse/wara-wara-plaza.ts @@ -0,0 +1,37 @@ +import { PostData } from '@/types/miiverse/post'; + +export type WWPPerson = { + person: { + posts: { + post: PostData; + }[]; + } +}; + +export type WWPTopic = { + empathy_count: number; + has_shop_page: 0 | 1; + icon: string; + title_ids: { + title_id: string; + }[]; + title_id: string; + community_id: number; + is_recommended: 0 | 1; + name: string; + people: WWPPerson[]; + position: number; +}; + +export type WWPData = { + result: { + has_error: 0 | 1; + version: 1; + expire: string; + request_name: 'topics'; + topics: { + topic: WWPTopic; + }[]; + } +}; + diff --git a/src/types/mongoose/post-to-json-options.ts b/src/types/mongoose/post-to-json-options.ts index 209b03d..dc11f9f 100644 --- a/src/types/mongoose/post-to-json-options.ts +++ b/src/types/mongoose/post-to-json-options.ts @@ -1,7 +1,5 @@ export interface PostToJSONOptions { - name?: string; with_mii: boolean; app_data?: boolean; topic_tag?: boolean; - topics?: boolean; } \ No newline at end of file diff --git a/src/types/mongoose/post.ts b/src/types/mongoose/post.ts index 1fe18ef..f3b4d05 100644 --- a/src/types/mongoose/post.ts +++ b/src/types/mongoose/post.ts @@ -1,7 +1,7 @@ import { Model, Types, HydratedDocument } from 'mongoose'; import { HydratedCommunityDocument } from '@/types/mongoose/community'; import { PostToJSONOptions } from '@/types/mongoose/post-to-json-options'; -import { PostPainting, PostScreenshot } from '@/types/common/post'; +import { PostData, PostPainting, PostScreenshot, PostTopicTag } from '@/types/miiverse/post'; export interface IPost { id: string; @@ -51,7 +51,8 @@ export interface IPostMethods { cleanedAppData(): string; formatPainting(): PostPainting | undefined; formatScreenshot(): PostScreenshot | undefined; - json(options: PostToJSONOptions, community?: HydratedCommunityDocument): Record; + formatTopicTag(): PostTopicTag; + json(options: PostToJSONOptions, community?: HydratedCommunityDocument): PostData; } interface IPostQueryHelpers {}