This repository has been archived by the owner on Oct 10, 2024. It is now read-only.
-
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.
- Loading branch information
1 parent
93ac403
commit 929466e
Showing
7 changed files
with
164 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import * as Logger from 'bunyan' | ||
import {difference, isEqual, uniq} from 'lodash' | ||
import {Command, CommandRunner, InquirerService} from 'nest-commander' | ||
import {InjectLogger} from 'nestjs-bunyan' | ||
import {CleanService} from './clean.service' | ||
|
||
@Command({ | ||
name: 'clean', | ||
arguments: '[tasks...]', | ||
}) | ||
export class CleanCommand implements CommandRunner { | ||
@InjectLogger() private readonly logger: Logger | ||
private readonly tasks = { | ||
user: '用户', | ||
userWallet: '用户钱包', | ||
category: '分类', | ||
thread: '主题', | ||
post: '回复', | ||
attachment: '附件', | ||
emoji: '表情', | ||
} | ||
|
||
constructor( | ||
private readonly inquirerService: InquirerService, | ||
private readonly cleanService: CleanService, | ||
) {} | ||
|
||
public async run(tasks: string[]): Promise<void> { | ||
tasks = uniq(tasks) | ||
let parts = Object.keys(this.tasks) | ||
if (tasks.length && !isEqual(tasks, ['all'])) { | ||
const left = difference(tasks, parts) | ||
if (left.length !== 0) throw new Error(`未知清理类型 ${left}`) | ||
parts = parts.filter((e) => tasks.includes(e)) | ||
} | ||
const taskNames = parts.map((e) => this.tasks[e]) | ||
const confirm = await this.inquirerService.ask('confirm', {taskNames, tasks, confirm: undefined}) | ||
if (!confirm.confirm) return | ||
this.logger.info('run task %s', parts) | ||
for (const part of parts) { | ||
this.logger.info(`开始清理${this.tasks[part]},请稍候`) | ||
const count = await this.cleanService[part]() | ||
this.logger.info(`删除${count}条数据`) | ||
} | ||
} | ||
} |
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,15 @@ | ||
import {Module} from '@nestjs/common' | ||
import {ModelsModule} from '../models/models.module' | ||
import {CleanCommand} from './clean.command' | ||
import {ConfirmQuestion} from './confirm.question' | ||
|
||
@Module({ | ||
imports: [ | ||
ModelsModule, | ||
], | ||
providers: [ | ||
CleanCommand, | ||
ConfirmQuestion, | ||
], | ||
}) | ||
export class CleanModule {} |
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,64 @@ | ||
import {Injectable} from '@nestjs/common' | ||
import {AttachmentModel} from '../models/q/attachment.model' | ||
import {CategoryModel} from '../models/q/category.model' | ||
import {EmojiModel} from '../models/q/emoji.model' | ||
import {GroupPermissionModel} from '../models/q/group-permission.model' | ||
import {PostModel} from '../models/q/post.model' | ||
import {ThreadModel} from '../models/q/thread.model' | ||
import {UserWalletModel} from '../models/q/user-wallet.model' | ||
import {UserModel} from '../models/q/user.model' | ||
import {ForumImagetypeModel} from '../models/x/forum-imagetype.model' | ||
|
||
@Injectable() | ||
export class CleanService { | ||
constructor( | ||
private readonly userModel: UserModel, | ||
private readonly userWalletModel: UserWalletModel, | ||
private readonly categoryModel: CategoryModel, | ||
private readonly groupPermissionModel: GroupPermissionModel, | ||
private readonly postModel: PostModel, | ||
private readonly threadModel: ThreadModel, | ||
private readonly attachmentModel: AttachmentModel, | ||
private readonly forumImagetypeModel: ForumImagetypeModel, | ||
private readonly emojiModel: EmojiModel, | ||
) {} | ||
|
||
public async user(): Promise<number> { | ||
return this.userModel.query.where('id', '>', 1).delete() | ||
} | ||
|
||
public async userWallet(): Promise<number> { | ||
return this.userWalletModel.query.where('user_id', '>', 1).delete() | ||
} | ||
|
||
public async category(): Promise<number> { | ||
const categories = await this.categoryModel.query.where('id', '>', 1).select() | ||
for (const category of categories) { | ||
const permissions = this.groupPermissionModel.getCategoryPermissions(category) | ||
for (const permission of permissions) { | ||
await this.groupPermissionModel.query.where('group_id', permission.group_id) | ||
.andWhere('permission', permission.permission) | ||
.delete() | ||
} | ||
} | ||
return this.categoryModel.query.where('id', '>', 1).delete() | ||
} | ||
|
||
public async thread(): Promise<number> { | ||
await this.postModel.query.where('is_first', 1).delete() | ||
return this.threadModel.query.where('id', '>', 0).delete() | ||
} | ||
|
||
public async post(): Promise<number> { | ||
return this.postModel.query.where('is_first', 0).delete() | ||
} | ||
|
||
public async attachment(): Promise<number> { | ||
return this.attachmentModel.query.where('id', '>', 0).delete() | ||
} | ||
|
||
public async emoji(): Promise<number> { | ||
const imageTypes = await this.forumImagetypeModel.query.select() | ||
return this.emojiModel.query.whereIn('category', imageTypes.map((e) => e.directory)) | ||
} | ||
} |
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,18 @@ | ||
import {MessageFor, Question, QuestionSet} from 'nest-commander' | ||
|
||
@QuestionSet({name: 'confirm'}) | ||
export class ConfirmQuestion { | ||
@Question({ | ||
name: 'confirm', | ||
}) | ||
public confirm(val: string): boolean { | ||
return val === 'yes i do' | ||
} | ||
|
||
@MessageFor({name: 'confirm'}) | ||
public confirmMessage({taskNames}: {taskNames: string[]}): string { | ||
return `请确认要清理所有转换相关的数据?将清理${taskNames.join('、')}转换数据 | ||
如确认,请输入 'yes i do' | ||
` | ||
} | ||
} |
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