Download apps/backend/src/api/routes/posts.controller.ts from Leon4gr45/postio: direct link, hf CLI and curl.
- Browser
- Download file 8.72 kB
-
https://huggingface.co/spaces/Leon4gr45/postio/resolve/main/apps/backend/src/api/routes/posts.controller.ts
- Command line
-
hf download hf://spaces/Leon4gr45/postio/apps/backend/src/api/routes/posts.controller.ts
-
curl -L -o posts.controller.ts https://huggingface.co/spaces/Leon4gr45/postio/resolve/main/apps/backend/src/api/routes/posts.controller.ts
8.72 kB
| import { | |
| Body, | |
| Controller, | |
| Delete, | |
| Get, | |
| HttpException, | |
| Param, | |
| Post, | |
| Put, | |
| Query, | |
| Res, | |
| } from '@nestjs/common'; | |
| import { PostsService } from '@gitroom/nestjs-libraries/database/prisma/posts/posts.service'; | |
| import { GetOrgFromRequest } from '@gitroom/nestjs-libraries/user/org.from.request'; | |
| import { Organization, User } from '@prisma/client'; | |
| import { GetPostsDto } from '@gitroom/nestjs-libraries/dtos/posts/get.posts.dto'; | |
| import { GetPostsListDto } from '@gitroom/nestjs-libraries/dtos/posts/get.posts.list.dto'; | |
| import { CheckPolicies } from '@gitroom/backend/services/auth/permissions/permissions.ability'; | |
| import { ApiTags } from '@nestjs/swagger'; | |
| import { GeneratorDto } from '@gitroom/nestjs-libraries/dtos/generator/generator.dto'; | |
| import { CreateGeneratedPostsDto } from '@gitroom/nestjs-libraries/dtos/generator/create.generated.posts.dto'; | |
| import { AgentGraphService } from '@gitroom/nestjs-libraries/agent/agent.graph.service'; | |
| import { Response } from 'express'; | |
| import { GetUserFromRequest } from '@gitroom/nestjs-libraries/user/user.from.request'; | |
| import { ShortLinkService } from '@gitroom/nestjs-libraries/short-linking/short.link.service'; | |
| import { CreateTagDto } from '@gitroom/nestjs-libraries/dtos/posts/create.tag.dto'; | |
| import { | |
| AuthorizationActions, | |
| Sections, | |
| } from '@gitroom/backend/services/auth/permissions/permission.exception.class'; | |
| import { PostValidationException } from '@gitroom/backend/api/routes/posts.validation.exception'; | |
| ('Posts') | |
| ('/posts') | |
| export class PostsController { | |
| constructor( | |
| private _postsService: PostsService, | |
| private _agentGraphService: AgentGraphService, | |
| private _shortLinkService: ShortLinkService | |
| ) {} | |
| ('/:id/statistics') | |
| async getStatistics( | |
| () org: Organization, | |
| ('id') id: string | |
| ) { | |
| return this._postsService.getStatistics(org.id, id); | |
| } | |
| ('/:id/missing') | |
| async getMissingContent( | |
| () org: Organization, | |
| ('id') id: string | |
| ) { | |
| return this._postsService.getMissingContent(org.id, id); | |
| } | |
| ('/:id/release-id') | |
| async updateReleaseId( | |
| () org: Organization, | |
| ('id') id: string, | |
| ('releaseId') releaseId: string | |
| ) { | |
| return this._postsService.updateReleaseId(org.id, id, releaseId); | |
| } | |
| ('/should-shortlink') | |
| async shouldShortlink(() body: { messages: string[] }) { | |
| return { ask: this._shortLinkService.askShortLinkedin(body.messages) }; | |
| } | |
| ('/:id/comments') | |
| async createComment( | |
| () org: Organization, | |
| () user: User, | |
| ('id') id: string, | |
| () body: { comment: string } | |
| ) { | |
| return this._postsService.createComment(org.id, user.id, id, body.comment); | |
| } | |
| ('/tags') | |
| async getTags(() org: Organization) { | |
| return { tags: await this._postsService.getTags(org.id) }; | |
| } | |
| ('/tags') | |
| async createTag( | |
| () org: Organization, | |
| () body: CreateTagDto | |
| ) { | |
| return this._postsService.createTag(org.id, body); | |
| } | |
| ('/tags/:id') | |
| async editTag( | |
| () org: Organization, | |
| () body: CreateTagDto, | |
| ('id') id: string | |
| ) { | |
| return this._postsService.editTag(id, org.id, body); | |
| } | |
| ('/tags/:id') | |
| async deleteTag( | |
| () org: Organization, | |
| ('id') id: string | |
| ) { | |
| return this._postsService.deleteTag(id, org.id); | |
| } | |
| ('/') | |
| async getPosts( | |
| () org: Organization, | |
| () query: GetPostsDto | |
| ) { | |
| return this._postsService.getPostsMinified(org.id, query); | |
| } | |
| ('/find-slot') | |
| async findSlot(() org: Organization) { | |
| return { date: await this._postsService.findFreeDateTime(org.id) }; | |
| } | |
| ('/find-slot/:id') | |
| async findSlotIntegration( | |
| () org: Organization, | |
| ('id') id?: string | |
| ) { | |
| return { date: await this._postsService.findFreeDateTime(org.id, id) }; | |
| } | |
| ('/list') | |
| async getPostsList( | |
| () org: Organization, | |
| () query: GetPostsListDto | |
| ) { | |
| return this._postsService.getPostsList(org.id, query); | |
| } | |
| ('/old') | |
| oldPosts( | |
| () org: Organization, | |
| ('date') date: string | |
| ) { | |
| return this._postsService.getOldPosts(org.id, date); | |
| } | |
| ('/group/:group/debug-export') | |
| async getPostGroupDebugExport( | |
| () org: Organization, | |
| () user: User, | |
| ('group') group: string | |
| ) { | |
| if (!user.isSuperAdmin) { | |
| throw new HttpException('Forbidden', 403); | |
| } | |
| return this._postsService.getPostGroupDebugExport(org.id, group); | |
| } | |
| ('/group/:group') | |
| getPostsByGroup(() org: Organization, ('group') group: string) { | |
| return this._postsService.getPostsByGroup(org.id, group); | |
| } | |
| ('/:id') | |
| getPost(() org: Organization, ('id') id: string) { | |
| return this._postsService.getPost(org.id, id); | |
| } | |
| ('/valid') | |
| async validatePosts( | |
| () org: Organization, | |
| () rawBody: any | |
| ) { | |
| return this._postsService.validatePosts(org.id, rawBody?.posts || []); | |
| } | |
| ('/') | |
| ([AuthorizationActions.Create, Sections.POSTS_PER_MONTH]) | |
| async createPost( | |
| () org: Organization, | |
| () rawBody: any | |
| ) { | |
| // Server-side validation — never trust the client to have validated. | |
| const validation = await this._postsService.validatePosts( | |
| org.id, | |
| rawBody?.posts || [] | |
| ); | |
| const fail = (item: (typeof validation)[number], error: string) => { | |
| throw new PostValidationException({ | |
| provider: item.identifier, | |
| name: item.name, | |
| error, | |
| }); | |
| }; | |
| for (const item of validation) { | |
| if (item.emptyContent) { | |
| fail( | |
| item, | |
| 'Your post should have at least one character or one image.' | |
| ); | |
| } | |
| } | |
| if (rawBody?.type !== 'draft') { | |
| for (const item of validation) { | |
| if (!item.valid) { | |
| fail(item, item.settingsError || 'Please fix your settings'); | |
| } | |
| if (item.errors !== true) { | |
| fail(item, item.errors as string); | |
| } | |
| if (item.tooLong) { | |
| fail(item, 'post is too long, please fix it'); | |
| } | |
| } | |
| } | |
| const body = await this._postsService.mapTypeToPost(rawBody, org.id); | |
| return this._postsService.createPost(org.id, body, 'WEB'); | |
| } | |
| ('/generator/draft') | |
| ([AuthorizationActions.Create, Sections.POSTS_PER_MONTH]) | |
| generatePostsDraft( | |
| () org: Organization, | |
| () body: CreateGeneratedPostsDto | |
| ) { | |
| return this._postsService.generatePostsDraft(org.id, body); | |
| } | |
| ('/generator') | |
| ([AuthorizationActions.Create, Sections.POSTS_PER_MONTH]) | |
| async generatePosts( | |
| () org: Organization, | |
| () body: GeneratorDto, | |
| ({ passthrough: false }) res: Response | |
| ) { | |
| res.setHeader('Content-Type', 'application/json; charset=utf-8'); | |
| try { | |
| for await (const event of this._agentGraphService.start(org.id, body)) { | |
| res.write(JSON.stringify(event) + '\n'); | |
| } | |
| } catch (err) { | |
| // The stream has already started, so we cannot surface a normal HTTP | |
| // error here. Emit a final error event on the open stream instead, so the | |
| // client can stop and show the message rather than hang on a truncated | |
| // stream. HttpExceptions carry a curated, user-facing message (e.g. the | |
| // AI safety rejection); anything else gets a generic message. | |
| const message = | |
| err instanceof HttpException | |
| ? err.message | |
| : 'Something went wrong while generating your posts, please try again.'; | |
| res.write(JSON.stringify({ name: 'error', error: true, message }) + '\n'); | |
| } | |
| res.end(); | |
| } | |
| ('/:group') | |
| deletePost( | |
| () org: Organization, | |
| ('group') group: string | |
| ) { | |
| return this._postsService.deletePost(org.id, group); | |
| } | |
| ('/:id/date') | |
| changeDate( | |
| () org: Organization, | |
| ('id') id: string, | |
| ('date') date: string, | |
| ('action') action: 'schedule' | 'update' = 'schedule' | |
| ) { | |
| return this._postsService.changeDate(org.id, id, date, action); | |
| } | |
| ('/separate-posts') | |
| async separatePosts( | |
| () org: Organization, | |
| () body: { content: string; len: number } | |
| ) { | |
| return this._postsService.separatePosts(body.content, body.len); | |
| } | |
| } | |