diff --git a/src/services/api/routes/communities.ts b/src/services/api/routes/communities.ts index 334ced5..5694d0d 100644 --- a/src/services/api/routes/communities.ts +++ b/src/services/api/routes/communities.ts @@ -17,7 +17,8 @@ import { HydratedCommunityDocument } from '@/types/mongoose/community'; import { SubCommunityQuery } from '@/types/mongoose/subcommunity-query'; import { CommunityPostsQuery } from '@/types/mongoose/community-posts-query'; import { HydratedContentDocument } from '@/types/mongoose/content'; -import {HydratedPostDocument, IPost} from '@/types/mongoose/post'; +import { HydratedPostDocument, IPost } from '@/types/mongoose/post'; +import { ParamPack } from '@/types/common/param-pack'; const createNewCommunitySchema = z.object({ name: z.string(), @@ -28,7 +29,7 @@ const createNewCommunitySchema = z.object({ const router: express.Router = express.Router(); -async function respondCommunityNotFound(response: express.Response) : Promise { +function respondCommunityNotFound(response: express.Response) : void { response.status(404); response.send(xmlbuilder.create({ result: { @@ -41,12 +42,11 @@ async function respondCommunityNotFound(response: express.Response) : Promise { +async function commonGetSubCommunity(paramPack: ParamPack, communityID: string | undefined) : Promise { - const parentCommunity: HydratedCommunityDocument | null = await getCommunityByTitleID(request.paramPack.title_id); + const parentCommunity: HydratedCommunityDocument | null = await getCommunityByTitleID(paramPack.title_id); if (!parentCommunity) { - await respondCommunityNotFound(response); - return parentCommunity; + return null; } const query: SubCommunityQuery = { @@ -56,8 +56,7 @@ async function commonGetSubCommunity(request: express.Request, response: express const community: HydratedCommunityDocument | null = await Community.findOne(query); if (!community) { - await respondCommunityNotFound(response); - return community; + return null; } return community; @@ -302,39 +301,84 @@ router.post('/', multer().none(), async function (request: express.Request, resp }).end({ pretty: true, allowEmpty: true })); }); -router.post('/:community_action', multer().none(), async function (request: express.Request, response: express.Response): Promise { +router.post('/:community_id.delete', multer().none(), async function (request: express.Request, response: express.Response): Promise { response.type('application/xml'); - const parentCommunity: HydratedCommunityDocument | null = await getCommunityByTitleID(request.paramPack.title_id); - if (!parentCommunity) { - await respondCommunityNotFound(response); - return; - } - - const communityActions: string[] = request.params.community_action.split('.'); - const community: HydratedCommunityDocument | null = await commonGetSubCommunity(request, response, communityActions[0]); + const community: HydratedCommunityDocument | null = await commonGetSubCommunity(request.paramPack, request.params.community_id); if (!community) { + respondCommunityNotFound(response); return; } - if (communityActions.length > 1) { - if (communityActions[1] === 'delete') { - await deleteSubCommunity(community, request, response); - } else if (communityActions[1] === 'favorite') { - await addFavoriteSubCommunity(community, request, response); - } else if (communityActions[1] === 'unfavorite') { - await delFavoriteSubCommunity(community, request, response); - } else { // '%s.search' - response.sendStatus(501); // Not Implemented - } - } else { - await updateSubCommunity(community, request, response); + if (community.owner != request.pid) { + response.sendStatus(403); // Forbidden + return; } + + await Community.deleteOne({ _id: community._id }); + + response.send(xmlbuilder.create({ + result: { + has_error: '0', + version: '1', + request_name: 'community', + community: community.json() + } + }).end({ pretty: true, allowEmpty: true })); }); -async function updateSubCommunity(community: HydratedCommunityDocument, request: express.Request, response: express.Response): Promise { +router.post('/:community_id.favorite', multer().none(), async function (request: express.Request, response: express.Response): Promise { response.type('application/xml'); + const community: HydratedCommunityDocument | null = await commonGetSubCommunity(request.paramPack, request.params.community_id); + if (!community) { + respondCommunityNotFound(response); + return; + } + + await community.addUserFavorite(request.pid); + + response.send(xmlbuilder.create({ + result: { + has_error: '0', + version: '1', + request_name: 'community', + community: community.json() + } + }).end({ pretty: true, allowEmpty: true })); +}); + +router.post('/:community_id.unfavorite', multer().none(), async function (request: express.Request, response: express.Response): Promise { + response.type('application/xml'); + + const community: HydratedCommunityDocument | null = await commonGetSubCommunity(request.paramPack, request.params.community_id); + if (!community) { + respondCommunityNotFound(response); + return; + } + + await community.delUserFavorite(request.pid); + + response.send(xmlbuilder.create({ + result: { + has_error: '0', + version: '1', + request_name: 'community', + community: community.json() + } + }).end({ pretty: true, allowEmpty: true })); +}); + + +router.post('/:community_id', multer().none(), async function (request: express.Request, response: express.Response): Promise { + response.type('application/xml'); + + const community: HydratedCommunityDocument | null = await commonGetSubCommunity(request.paramPack, request.params.community_id); + if (!community) { + respondCommunityNotFound(response); + return; + } + if (community.owner != request.pid) { response.sendStatus(403); // Forbidden return; @@ -368,56 +412,6 @@ async function updateSubCommunity(community: HydratedCommunityDocument, request: community: community.json() } }).end({ pretty: true, allowEmpty: true })); -} - -async function deleteSubCommunity(community: HydratedCommunityDocument, request: express.Request, response: express.Response): Promise { - response.type('application/xml'); - - if (community.owner != request.pid) { - response.sendStatus(403); // Forbidden - return; - } - - await Community.deleteOne({ _id: community._id }); - - response.send(xmlbuilder.create({ - result: { - has_error: '0', - version: '1', - request_name: 'community', - community: community.json() - } - }).end({ pretty: true, allowEmpty: true })); -} - -async function addFavoriteSubCommunity(community: HydratedCommunityDocument, request: express.Request, response: express.Response): Promise { - response.type('application/xml'); - - await community.addUserFavorite(request.pid); - - response.send(xmlbuilder.create({ - result: { - has_error: '0', - version: '1', - request_name: 'community', - community: community.json() - } - }).end({ pretty: true, allowEmpty: true })); -} - -async function delFavoriteSubCommunity(community: HydratedCommunityDocument, request: express.Request, response: express.Response): Promise { - response.type('application/xml'); - - await community.delUserFavorite(request.pid); - - response.send(xmlbuilder.create({ - result: { - has_error: '0', - version: '1', - request_name: 'community', - community: community.json() - } - }).end({ pretty: true, allowEmpty: true })); -} +}); export default router; diff --git a/src/types/mongoose/community.ts b/src/types/mongoose/community.ts index 152c3b2..52ea2c7 100644 --- a/src/types/mongoose/community.ts +++ b/src/types/mongoose/community.ts @@ -9,26 +9,26 @@ enum COMMUNITY_TYPE { export interface ICommunity { platform_id: number; - name: string; - description: string; - open: boolean; - allows_comments: boolean; - type: COMMUNITY_TYPE; - parent: string; - admins: Types.Array; - owner: number; - created_at: Date; - empathy_count: number; - followers: number; - has_shop_page: number; - icon: string; - title_ids: Types.Array; - title_id: Types.Array; - community_id: string; - olive_community_id: string; - is_recommended: number; - app_data: string; - user_favorites: Types.Array; + name: string; + description: string; + open: boolean; + allows_comments: boolean; + type: COMMUNITY_TYPE; + parent: string; + admins: Types.Array; + owner: number; + created_at: Date; + empathy_count: number; + followers: number; + has_shop_page: number; + icon: string; + title_ids: Types.Array; + title_id: Types.Array; + community_id: string; + olive_community_id: string; + is_recommended: number; + app_data: string; + user_favorites: Types.Array; } export interface ICommunityMethods { @@ -36,9 +36,9 @@ export interface ICommunityMethods { downEmpathy(): Promise; upFollower(): Promise; downFollower(): Promise; - addUserFavorite(pid: number): Promise; - delUserFavorite(pid: number): Promise; - json(): Record; + addUserFavorite(pid: number): Promise; + delUserFavorite(pid: number): Promise; + json(): Record; } interface ICommunityQueryHelpers {}