-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from TogetherCrew/microservices
Microservices
- Loading branch information
Showing
110 changed files
with
12,172 additions
and
5,842 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"jest.enable": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
(TGChat { id, title, type }) | ||
(TGUser { id }) | ||
(TGMessage { id, text }) | ||
|
||
(TGUser)-[:CREATED_MESSAGE { timestamp }]->(TGMessage) | ||
(TGMessage)-[:MESSAGE_EDITED { timestamp }]->(TGMessage) | ||
(TGMessage)-[:MENTIONED]->(User) | ||
(TGUser)-[:REACTED_TO { timestamp, old_reaction, new_reaction }]->(TGMessage) | ||
(TGMessage)-[:REPLIED]->(TGMessage) | ||
|
||
|
||
(TGUser)-[:JOINED { timestamp }]->(TGChat) | ||
(TGUser)-[:LEFT { timestamp }]->(TGChat) | ||
|
||
## Actions | ||
|
||
### Message | ||
|
||
1. Create or update chat | ||
2. Create or update user | ||
3. Create message |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { BotService } from './bot.service'; | ||
import { ConfigModule } from '@nestjs/config'; | ||
// import { BotUpdate } from './bot.update'; | ||
import { | ||
schemaConfig, | ||
telegramConfig, | ||
rmqConfig, | ||
RmqModule, | ||
} from '@app/common'; | ||
import { Services } from '@app/common'; | ||
|
||
@Module({ | ||
imports: [ | ||
ConfigModule.forRoot({ | ||
validationSchema: schemaConfig, | ||
load: [telegramConfig, rmqConfig], | ||
isGlobal: true, | ||
}), | ||
RmqModule.register(Services.EventStore), | ||
RmqModule.register(Services.GraphStore), | ||
], | ||
providers: [BotService], | ||
}) | ||
export class BotModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// bot.service.spec.ts | ||
|
||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { BotService } from './bot.service'; | ||
import { Services } from '@app/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
|
||
describe('BotService', () => { | ||
let service: BotService; | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
BotService, | ||
{ | ||
provide: Services.EventStore.name, | ||
useValue: { | ||
emit: jest.fn(), | ||
}, | ||
}, | ||
{ | ||
provide: Services.GraphStore.name, | ||
useValue: { | ||
emit: jest.fn(), | ||
}, | ||
}, | ||
{ | ||
provide: ConfigService, | ||
useValue: { | ||
get: jest.fn((key) => { | ||
if (key === 'telegram.token') return 'mocked value'; | ||
return null; | ||
}), | ||
}, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
service = module.get<BotService>(BotService); | ||
}); | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { IgnoreEvent, Services, UpdateEvent } from '@app/common'; | ||
import { Inject, Injectable, Logger, OnModuleInit } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { ClientProxy } from '@nestjs/microservices'; | ||
import { API_CONSTANTS, Bot } from 'grammy'; | ||
|
||
@Injectable() | ||
export class BotService implements OnModuleInit { | ||
private bot: Bot; | ||
private readonly logger = new Logger(BotService.name); | ||
|
||
constructor( | ||
@Inject(Services.EventStore.name) private readonly eventClient: ClientProxy, | ||
@Inject(Services.GraphStore.name) private readonly graphClient: ClientProxy, | ||
private readonly configService: ConfigService, | ||
) { | ||
this.bot = new Bot(configService.get<string>('telegram.token')); | ||
|
||
Object.values(IgnoreEvent).map((event) => { | ||
this.bot.on(event, () => { | ||
this.logger.warn(`Ignoring ${event} event.`); | ||
}); | ||
}); | ||
|
||
Object.values(UpdateEvent).map((event) => { | ||
this.bot.on(event, (ctx) => { | ||
this.logger.log(`Received ${event} from ${ctx.chat.id}`); | ||
this.eventClient.emit(event, ctx); | ||
this.graphClient.emit(event, ctx); | ||
}); | ||
}); | ||
} | ||
|
||
async onModuleInit() { | ||
await this.bot.start({ | ||
allowed_updates: API_CONSTANTS.ALL_UPDATE_TYPES, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { NestFactory } from '@nestjs/core'; | ||
import { BotModule } from './bot.module'; | ||
import { RmqService, Services } from '@app/common'; | ||
|
||
async function bootstrap() { | ||
// const app = await NestFactory.createMicroservice(BotModule); | ||
const app = await NestFactory.create(BotModule); | ||
const rmqService = app.get<RmqService>(RmqService); | ||
app.connectMicroservice(rmqService.getOptions(Services.TelegramBot.queue)); | ||
await app.startAllMicroservices(); | ||
app.listen(3000); | ||
} | ||
bootstrap(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"declaration": false, | ||
"outDir": "../../dist/apps/bot" | ||
}, | ||
"include": ["src/**/*"], | ||
"exclude": ["node_modules", "dist", "test", "**/*spec.ts"] | ||
} |
Oops, something went wrong.