diff --git a/src/app.module.ts b/src/app.module.ts index 7c7d319b971b2f2e5fecdd32391873e545085dfd..b076a4b56c1fa6f47d39aca42b831c2b948af2fa 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,9 +1,9 @@ -import { Module } from '@nestjs/common'; +import { ForbiddenException, Module, NotFoundException } from '@nestjs/common'; import { PrismaModule } from './prisma/prisma.module'; import { TelegramModule } from './telegram/telegram.module'; import { GraphQLModule } from '@nestjs/graphql'; import { ResolverModule } from './resolvers/resolver.module'; -import { GroupAuthGuard } from './auth/graphql-auth.guard'; +import { GroupAuthGuard } from './auth/group-auth.guard'; import { ImageController } from './images.controller'; import { WebAuthGuard } from './auth/web-auth.guard'; @@ -16,8 +16,15 @@ import { WebAuthGuard } from './auth/web-auth.guard'; autoSchemaFile: true, playground: true, installSubscriptionHandlers: true, - context: ({ request }) => ({ - req: request, + subscriptions: { + 'subscriptions-transport-ws': { + onConnect: (headers) => ({ + token: headers['Authorization'], + }), + }, + }, + context: (ctx) => ({ + req: ctx.request || ctx.req, }), }), ], diff --git a/src/auth/agent-auth.guard.ts b/src/auth/agent-auth.guard.ts index ebabd4b88339a8eba01b0d59c87066bc0a982a77..87b1299b9d0901eb069afbb97532e9cdae0a2ee1 100644 --- a/src/auth/agent-auth.guard.ts +++ b/src/auth/agent-auth.guard.ts @@ -20,8 +20,6 @@ export class AgentAuthGuard implements CanActivate { where: { id: token }, }); - if (!req.agent) return false; - - return true; + return !!req.agent; } } diff --git a/src/auth/graphql-auth.guard.ts b/src/auth/group-auth.guard.ts similarity index 92% rename from src/auth/graphql-auth.guard.ts rename to src/auth/group-auth.guard.ts index 95ae5a8b9316f93f0460cef2175d5d0e99a924c0..3ec093a8fde7047fbc84308e23da42465ac8e173 100644 --- a/src/auth/graphql-auth.guard.ts +++ b/src/auth/group-auth.guard.ts @@ -20,8 +20,6 @@ export class GroupAuthGuard implements CanActivate { where: { code: token }, }); - if (!req.group) return false; - - return true; + return !!req.group; } } diff --git a/src/main.ts b/src/main.ts index 66614b21973648193d5b36b5b5fdb89b20eab717..bc9cbd47640f2e193b92e4ee4b8a7f1b430bb09c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,5 +1,5 @@ import { NestFactory } from '@nestjs/core'; -import { Group, Agent, TokenCode } from '@prisma/client'; +import { Group, Agent } from '@prisma/client'; declare module 'express' { interface Request { diff --git a/src/resolvers/agent.resolver.ts b/src/resolvers/agent.resolver.ts index 843b0fe17025559f4fe9699c1823a610c672ddf7..ee01185ea519190ce4990b52f54344dae5e4d707 100644 --- a/src/resolvers/agent.resolver.ts +++ b/src/resolvers/agent.resolver.ts @@ -10,7 +10,7 @@ import { Agent } from './models/agent.model'; import { PrismaService } from '../prisma/prisma.service'; import { GraphQLString } from 'graphql'; import { NotFoundException, UseGuards } from '@nestjs/common'; -import { GroupAuthGuard } from '../auth/graphql-auth.guard'; +import { GroupAuthGuard } from '../auth/group-auth.guard'; import { Request } from 'express'; @Resolver(() => Agent) diff --git a/src/resolvers/entry.resolver.ts b/src/resolvers/entry.resolver.ts index 22af1ec29636dc7edea400fd8ad75e66007d2351..a2ff9e80df8784274cd190c81294c061b4cb9ed7 100644 --- a/src/resolvers/entry.resolver.ts +++ b/src/resolvers/entry.resolver.ts @@ -16,7 +16,7 @@ import { UseGuards, } from '@nestjs/common'; import { Request } from 'express'; -import { GroupAuthGuard } from '../auth/graphql-auth.guard'; +import { GroupAuthGuard } from '../auth/group-auth.guard'; import { pubSub } from '../pubSub.instance'; @Resolver(() => Entry) diff --git a/src/resolvers/group.resolver.ts b/src/resolvers/group.resolver.ts index 2ebf54d328b4b73b6223d0114feb6f6e5354b55a..af7bc5db855852f23d6e333783866e3ebf417edd 100644 --- a/src/resolvers/group.resolver.ts +++ b/src/resolvers/group.resolver.ts @@ -13,7 +13,7 @@ import { PrismaService } from '../prisma/prisma.service'; import { GraphQLString } from 'graphql'; import * as seedWords from 'mnemonic-words'; import { NotFoundException, UseGuards } from '@nestjs/common'; -import { GroupAuthGuard } from '../auth/graphql-auth.guard'; +import { GroupAuthGuard } from '../auth/group-auth.guard'; import { Request } from 'express'; import { pubSub } from '../pubSub.instance'; import { AgentAuthGuard } from 'src/auth/agent-auth.guard'; @@ -23,8 +23,8 @@ export class GroupResolver { constructor(private prismaService: PrismaService) {} @Subscription(() => Group, { - filter: (payload, variables) => - payload.updateToken.code === variables.code, + filter: (payload, variables, state) => + payload.updateToken.code === state.token, }) // eslint-disable-next-line @typescript-eslint/no-unused-vars updateToken(@Args('code') code: string) { diff --git a/src/resolvers/resolver.module.ts b/src/resolvers/resolver.module.ts index dba4f8249c412cf07d2b49f6dd30da0bd2a2bdb0..5df537d520eae98cd5e6b32ec3c5cb158b02ac55 100644 --- a/src/resolvers/resolver.module.ts +++ b/src/resolvers/resolver.module.ts @@ -2,7 +2,7 @@ import { Module } from '@nestjs/common'; import { GroupResolver } from './group.resolver'; import { EntryResolver } from './entry.resolver'; import { AgentResolver } from './agent.resolver'; -import { TokenCodeResolver } from './tokenCode.resolver'; +import { TokenCodeResolver } from './token-code.resolver'; @Module({ providers: [GroupResolver, EntryResolver, AgentResolver, TokenCodeResolver], diff --git a/src/resolvers/tokenCode.resolver.ts b/src/resolvers/token-code.resolver.ts similarity index 70% rename from src/resolvers/tokenCode.resolver.ts rename to src/resolvers/token-code.resolver.ts index 73bfd955b4d6bff66c29a507c25149ae2b3c100a..684c17c4aa5e587f45940ad997aaea8d4e8f7286 100644 --- a/src/resolvers/tokenCode.resolver.ts +++ b/src/resolvers/token-code.resolver.ts @@ -1,23 +1,30 @@ -import { UseGuards, NotFoundException } from "@nestjs/common"; -import { Args, Context, Mutation, Parent, ResolveField, Resolver, Subscription } from "@nestjs/graphql"; +import { UseGuards, NotFoundException } from '@nestjs/common'; +import { + Args, + Context, + Mutation, + Parent, + ResolveField, + Resolver, + Subscription, +} from '@nestjs/graphql'; import { GraphQLString, GraphQLInt } from 'graphql'; -import { Request } from "express"; -import { GroupAuthGuard } from "../auth/graphql-auth.guard"; -import { PrismaService } from "../prisma/prisma.service"; -import { TokenCode } from "./models/tokenCode.model"; -import { AgentAuthGuard } from "../auth/agent-auth.guard"; -import { pubSub } from "../pubSub.instance"; +import { Request } from 'express'; +import { GroupAuthGuard } from '../auth/group-auth.guard'; +import { PrismaService } from '../prisma/prisma.service'; +import { TokenCode } from './models/tokenCode.model'; +import { AgentAuthGuard } from '../auth/agent-auth.guard'; +import { pubSub } from '../pubSub.instance'; @Resolver(() => TokenCode) export class TokenCodeResolver { constructor(private prismaService: PrismaService) {} - // TODO: protect subscription @Subscription(() => TokenCode, { - filter: (payload, variables) => - payload.newTokenCode.agent.id === variables.id, + filter: (payload, variables, state) => + payload.newTokenCode.agent.id === state.token, }) - newTokenCode(@Args('id') id: string){ + newTokenCode() { return pubSub.asyncIterator('newTokenCode'); } @@ -39,9 +46,9 @@ export class TokenCodeResolver { }, }, }); - - pubSub.publish('newTokenCode', { - newTokenCode: tokenCode + + await pubSub.publish('newTokenCode', { + newTokenCode: tokenCode, }); } @@ -58,13 +65,11 @@ export class TokenCodeResolver { where: { id: code }, include: { agent: true, - } + }, }); - if (!tokenCode) - throw new NotFoundException(); + if (!tokenCode) throw new NotFoundException(); - const newTokenCode = await this.prismaService.tokenCode.create({ data: { value: 1, @@ -74,10 +79,10 @@ export class TokenCodeResolver { }, include: { agent: true, - } + }, }); - pubSub.publish('newTokenCode', { + await pubSub.publish('newTokenCode', { newTokenCode: newTokenCode, }); @@ -94,7 +99,7 @@ export class TokenCodeResolver { }, }); - pubSub.publish('updateToken', { + await pubSub.publish('updateToken', { updateToken: req.group, }); @@ -109,5 +114,4 @@ export class TokenCodeResolver { }, }); } - -} \ No newline at end of file +} diff --git a/src/telegram/telegram.service.ts b/src/telegram/telegram.service.ts index d149f9008cda6d2130f5b569d0b250aa4af326fa..a4dc1017cdec41758b451f4fc4da833d6f0c6c15 100644 --- a/src/telegram/telegram.service.ts +++ b/src/telegram/telegram.service.ts @@ -19,7 +19,10 @@ export class TelegramService { polling: true, }); - this.telegram.onText(/^\/register ([\w|-]+) (.+)/, this.register.bind(this)); + this.telegram.onText( + /^\/register ([\w|-]+) (.+)/, + this.register.bind(this), + ); this.telegram.onText(/^\/unregister/, this.unregister.bind(this)); this.telegram.onText(/^\/profile/, this.profile.bind(this)); this.telegram.onText(/^\/start/, this.start.bind(this)); @@ -115,7 +118,7 @@ export class TelegramService { } async register(msg: Message, match: RegExpMatchArray) { - if (!match[1]&&!match[2]) return; + if (!match[1] && !match[2]) return; if (await this.isRegistered(msg.from.id)) return void (await this.telegram.sendMessage( @@ -123,17 +126,19 @@ export class TelegramService { 'Already registered', )); - if((await this.prismaService.agent.count({ - where: { id: match[1], uid: "" }, - })) == 0) + const existsCheck = await this.prismaService.agent.count({ + where: { id: match[1], uid: '' }, + }); + + if (existsCheck == 0) return void (await this.telegram.sendMessage( msg.chat.id, 'AgentId not found or already taken!', )); const agent = await this.prismaService.agent.update({ - where:{ - id: match[1] + where: { + id: match[1], }, data: { uid: String(msg.from.id),