File

src/modules/disqus/disqus.controller.ts

Prefix

disqus

Index

Methods

Methods

closeWindowScript
closeWindowScript(response: FastifyReply)
Decorators :
@Get('close-window.js')
Parameters :
Name Type Optional
response FastifyReply No
Returns : void
createComment
createComment(undefined: IRequestContext, token: AccessToken | null, comment: CommentBase)
Decorators :
@Post('comment')
@Throttle({default: undefined})
@SuccessResponse('Create universal comment succeeded')
Parameters :
Name Type Optional
IRequestContext No
token AccessToken | null No
comment CommentBase No
Returns : any
deleteComment
deleteComment(payload: CommentIdDTO, token: AccessToken | null)
Decorators :
@Delete('comment')
@SuccessResponse('Delete universal comment succeeded')
Parameters :
Name Type Optional
payload CommentIdDTO No
token AccessToken | null No
Returns : any
exportXML
exportXML()
Decorators :
@Get('export-xml')
@UseGuards(AdminOnlyGuard)
@Header('content-type', 'application/xml')
Returns : any
getConfig
getConfig()
Decorators :
@Get('config')
@SuccessResponse('Get Disqus config succeeded')
Returns : { forum: any; admin_username: any; public_key: any; authorize_url: any; }
getPosts
getPosts(query: GeneralDisqusParams)
Decorators :
@Get('posts')
@UseGuards(AdminOnlyGuard)
@SuccessResponse('Get Disqus posts succeeded')
Parameters :
Name Type Optional
query GeneralDisqusParams No
Returns : any
getThread
getThread(query: ThreadPostIdDTO)
Decorators :
@Get('thread')
@SuccessResponse('Get Disqus thread info succeeded')
Parameters :
Name Type Optional
query ThreadPostIdDTO No
Returns : any
getThreads
getThreads(query: GeneralDisqusParams)
Decorators :
@Get('threads')
@UseGuards(AdminOnlyGuard)
@SuccessResponse('Get Disqus threads succeeded')
Parameters :
Name Type Optional
query GeneralDisqusParams No
Returns : any
getUserInfo
getUserInfo(token: AccessToken | null)
Decorators :
@Get('user-info')
@SuccessResponse('Get Disqus user info succeeded')
Parameters :
Name Type Optional
token AccessToken | null No
Returns : any
importXML
importXML(file: IUploadedFile)
Decorators :
@Post('import-xml')
@UseGuards(AdminOnlyGuard)
@SuccessResponse('Import XML from Disqus succeeded')
Parameters :
Name Type Optional
file IUploadedFile No
Returns : any
Async oauthCallback
oauthCallback(query: CallbackCodeDTO, response: FastifyReply)
Decorators :
@Get('oauth-callback')
Parameters :
Name Type Optional
query CallbackCodeDTO No
response FastifyReply No
Returns : any
oauthLogout
oauthLogout(token: AccessToken | null, response: FastifyReply)
Decorators :
@Post('oauth-logout')
Parameters :
Name Type Optional
token AccessToken | null No
response FastifyReply No
Returns : void
updatePost
updatePost(body)
Decorators :
@Post('post')
@UseGuards(AdminOnlyGuard)
@SuccessResponse('Update Disqus post succeeded')
Parameters :
Name Optional
body No
Returns : any
updateThread
updateThread(body)
Decorators :
@Post('thread')
@UseGuards(AdminOnlyGuard)
@SuccessResponse('Update Disqus thread succeeded')
Parameters :
Name Optional
body No
Returns : any
/**
 * @file Disqus controller
 * @module module/disqus/controller
 * @author Surmon <https://github.com/surmon-china>
 */

import type { FastifyReply } from 'fastify'
import { Controller, UseGuards, UnauthorizedException, BadRequestException } from '@nestjs/common'
import { Get, Post, Delete, Query, Body, Header, Response } from '@nestjs/common'
import { Throttle, seconds } from '@nestjs/throttler'
import { AdminOnlyGuard } from '@app/guards/admin-only.guard'
import { SuccessResponse } from '@app/decorators/success-response.decorator'
import { UploadedFile, IUploadedFile } from '@app/decorators/uploaded-file.decorator'
import { RequestContext, IRequestContext } from '@app/decorators/request-context.decorator'
import { CommentBase } from '@app/modules/comment/comment.model'
import { DISQUS } from '@app/app.config'
import { AccessToken } from '@app/utils/disqus'
import { DisqusPublicService } from './disqus.service.public'
import { DisqusPrivateService } from './disqus.service.private'
import { DisqusToken, TOKEN_COOKIE_KEY, encodeToken } from './disqus.token'
import { CallbackCodeDTO, ThreadPostIdDTO, CommentIdDTO, GeneralDisqusParams } from './disqus.dto'

@Controller('disqus')
export class DisqusController {
  constructor(
    private readonly disqusPublicService: DisqusPublicService,
    private readonly disqusPrivateService: DisqusPrivateService
  ) {}

  // --------------------------------
  // for client Disqus user

  @Get('config')
  @SuccessResponse('Get Disqus config succeeded')
  getConfig() {
    return {
      forum: DISQUS.forum,
      admin_username: DISQUS.adminUsername,
      public_key: DISQUS.publicKey,
      authorize_url: this.disqusPublicService.getAuthorizeURL()
    }
  }

  @Get('close-window.js')
  closeWindowScript(@Response() response: FastifyReply) {
    response.type('application/javascript').send('window.close();')
  }

  @Get('oauth-callback')
  async oauthCallback(@Query() query: CallbackCodeDTO, @Response() response: FastifyReply) {
    const accessToken = await this.disqusPublicService.getAccessToken(query.code)
    // Cache user info
    this.disqusPublicService.setUserInfoCache(
      accessToken.user_id,
      await this.disqusPublicService.getUserInfo(accessToken.access_token),
      accessToken.expires_in
    )
    // https://github.com/fastify/fastify-cookie?tab=readme-ov-file#options
    response.setCookie(TOKEN_COOKIE_KEY, encodeToken(accessToken), {
      maxAge: accessToken.expires_in * 1000,
      httpOnly: true,
      secure: 'auto'
    })
    // Close the popup window
    response.header('content-type', 'text/html')
    response.send(`<!DOCTYPE html><html><script src="/disqus/close-window.js"></script></html>`)
    // To maintain a secure `content-security-policy`, inline JavaScript is not used here.
    // response.send(`<script>window.close();</script>`)
  }

  @Post('oauth-logout')
  oauthLogout(@DisqusToken() token: AccessToken | null, @Response() response: FastifyReply) {
    if (token) this.disqusPublicService.deleteUserInfoCache(token.user_id)
    response.clearCookie(TOKEN_COOKIE_KEY)
    response.header('content-type', 'text/plain')
    response.send('Disqus OAuth logout succeeded')
  }

  @Get('user-info')
  @SuccessResponse('Get Disqus user info succeeded')
  getUserInfo(@DisqusToken() token: AccessToken | null) {
    if (!token) throw new UnauthorizedException('You are not logged in')
    return this.disqusPublicService.getUserInfoCache(token.user_id).then((cached) => {
      return cached ?? this.disqusPublicService.getUserInfo(token.access_token)
    })
  }

  @Get('thread')
  @SuccessResponse('Get Disqus thread info succeeded')
  getThread(@Query() query: ThreadPostIdDTO) {
    return this.disqusPublicService.ensureThreadDetailCache(Number(query.post_id))
  }

  @Post('comment')
  @Throttle({ default: { ttl: seconds(30), limit: 6 } })
  @SuccessResponse('Create universal comment succeeded')
  createComment(
    @RequestContext() { visitor }: IRequestContext,
    @DisqusToken() token: AccessToken | null,
    @Body() comment: CommentBase
  ) {
    return this.disqusPublicService.createUniversalComment(comment, visitor, token?.access_token)
  }

  @Delete('comment')
  @SuccessResponse('Delete universal comment succeeded')
  deleteComment(@Body() payload: CommentIdDTO, @DisqusToken() token: AccessToken | null) {
    if (!token) throw new UnauthorizedException('You are not logged in')
    return this.disqusPublicService.deleteUniversalComment(payload.comment_id, token.access_token)
  }

  // --------------------------------
  // for nodepress admin

  @Get('threads')
  @UseGuards(AdminOnlyGuard)
  @SuccessResponse('Get Disqus threads succeeded')
  getThreads(@Query() query: GeneralDisqusParams) {
    return this.disqusPrivateService.getThreads(query)
  }

  @Get('posts')
  @UseGuards(AdminOnlyGuard)
  @SuccessResponse('Get Disqus posts succeeded')
  getPosts(@Query() query: GeneralDisqusParams) {
    return this.disqusPrivateService.getPosts(query)
  }

  @Post('post')
  @UseGuards(AdminOnlyGuard)
  @SuccessResponse('Update Disqus post succeeded')
  updatePost(@Body() body) {
    return this.disqusPrivateService.updatePost(body)
  }

  @Post('thread')
  @UseGuards(AdminOnlyGuard)
  @SuccessResponse('Update Disqus thread succeeded')
  updateThread(@Body() body) {
    return this.disqusPrivateService.updateThread(body)
  }

  @Get('export-xml')
  @UseGuards(AdminOnlyGuard)
  @Header('content-type', 'application/xml')
  exportXML() {
    return this.disqusPrivateService.exportXMLFromNodepress()
  }

  @Post('import-xml')
  @UseGuards(AdminOnlyGuard)
  @SuccessResponse('Import XML from Disqus succeeded')
  importXML(@UploadedFile() file: IUploadedFile) {
    if (!['application/xml', 'text/xml'].includes(file.mimetype)) {
      throw new BadRequestException('Only XML files are allowed for import')
    }
    return this.disqusPrivateService.importXMLToNodepress(file.buffer)
  }
}

results matching ""

    No results matching ""