Skip to content

Commit

Permalink
[examples]: chat-ts-run 添加 逻辑模块依赖流入示例 #19
Browse files Browse the repository at this point in the history
  • Loading branch information
whtiehack committed Sep 6, 2022
1 parent b2bb4fc commit ae947cd
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Inject, Injectable } from "@nestjs/common";


@Injectable()
export class MuteDao {
constructor(
@Inject("mydatabase")
private readonly dbConnection: any
) {

}

public async checkUidMuted(uid: string) {
console.log("check user has been muted:", uid);
this.dbConnection.hget()
if (uid == "abc") {
return true
}
return false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Module } from "@nestjs/common";
import { pinusAppProvider } from "../../util/nestutil";
import { databaseProvider } from "../provider/db.provider";
import { MuteDao } from "./mute.dao";
import { MuteService } from "./mute.service";


@Module({
imports: [],
providers: [pinusAppProvider, databaseProvider, MuteDao, MuteService,],
// 因为外部要用 需要需要导出
exports: [MuteService],
})
export class MuteModule { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Injectable } from "@nestjs/common";
import { MuteDao } from "./mute.dao";


@Injectable()
export class MuteService {
constructor(
private readonly muteDao: MuteDao,
) {

}

public async checkUidMuted(uid: string) {
return this.muteDao.checkUidMuted(uid)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

// more examples
// https://docs.nestjs.com/fundamentals/custom-providers


export const databaseProvider = {
provide: "mydatabase",
useFactory: async () => {
// create redis,mongodb,mysql etc.
return {
hget: () => { console.log("mydatabase hget") },
hset: () => { console.log("mydatabase hset") },
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Module } from '@nestjs/common';
import { MuteModule } from '../logic/mutemodule/mute.module';
import { pinusAppProvider } from '../util/nestutil';
import { CronTest } from './chat/cron/cronTest';
import { ChatHandler } from './chat/handler/chatHandler';
Expand All @@ -13,7 +14,7 @@ import { NotifyRemoter } from './chat/remote/notifyRemote';


@Module({
imports: [],
imports: [MuteModule],
controllers: [],
providers: [pinusAppProvider, ChatHandler, AnotherName, ChatRemote, NotifyRemoter, CronTest],
// 因为外部要用 需要需要导出
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Application, BackendSession } from 'pinus';
import { Injectable } from '@nestjs/common';
import { getNestClass } from '../../../util/nestutil';
import { MuteService } from '../../../logic/mutemodule/mute.service';


export default function (app: Application) {
Expand All @@ -9,7 +10,10 @@ export default function (app: Application) {

@Injectable()
export class ChatHandler {
constructor(private app: Application) {
constructor(
private app: Application,
private readonly muteService: MuteService,
) {
}

/**
Expand All @@ -22,6 +26,10 @@ export class ChatHandler {
async send(msg: { content: string, target: string }, session: BackendSession) {
let rid = session.get('rid');
let username = session.uid.split('*')[0];
if (this.muteService.checkUidMuted(username)) {
console.log("user has been muted", username, " chage msg");
msg.content = "##@ user has been muted## - " + msg.content
}
let channelService = this.app.get('channelService');
let param = {
msg: msg.content,
Expand Down

0 comments on commit ae947cd

Please sign in to comment.