refactor(backend): ロジックをサービスに切り出す
This commit is contained in:
parent
dc43fc68ef
commit
3bc6205150
@ -3,6 +3,7 @@ import { DataSource } from 'typeorm';
|
|||||||
import Redis from 'ioredis';
|
import Redis from 'ioredis';
|
||||||
import { DI } from '@/di-symbols.js';
|
import { DI } from '@/di-symbols.js';
|
||||||
import { Meta } from '@/models/entities/Meta.js';
|
import { Meta } from '@/models/entities/Meta.js';
|
||||||
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
||||||
import type { OnApplicationShutdown } from '@nestjs/common';
|
import type { OnApplicationShutdown } from '@nestjs/common';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@ -16,6 +17,8 @@ export class MetaService implements OnApplicationShutdown {
|
|||||||
|
|
||||||
@Inject(DI.db)
|
@Inject(DI.db)
|
||||||
private db: DataSource,
|
private db: DataSource,
|
||||||
|
|
||||||
|
private globalEventService: GlobalEventService,
|
||||||
) {
|
) {
|
||||||
this.onMessage = this.onMessage.bind(this);
|
this.onMessage = this.onMessage.bind(this);
|
||||||
|
|
||||||
@ -31,6 +34,22 @@ export class MetaService implements OnApplicationShutdown {
|
|||||||
this.redisSubscriber.on('message', this.onMessage);
|
this.redisSubscriber.on('message', this.onMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async onMessage(_, data): Promise<void> {
|
||||||
|
const obj = JSON.parse(data);
|
||||||
|
|
||||||
|
if (obj.channel === 'internal') {
|
||||||
|
const { type, body } = obj.message;
|
||||||
|
switch (type) {
|
||||||
|
case 'metaUpdated': {
|
||||||
|
this.cache = body;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public async fetch(noCache = false): Promise<Meta> {
|
public async fetch(noCache = false): Promise<Meta> {
|
||||||
if (!noCache && this.cache) return this.cache;
|
if (!noCache && this.cache) return this.cache;
|
||||||
|
|
||||||
@ -65,20 +84,34 @@ export class MetaService implements OnApplicationShutdown {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private async onMessage(_, data) {
|
public async update(data: Partial<Meta>): Promise<Meta> {
|
||||||
const obj = JSON.parse(data);
|
const updated = await this.db.transaction(async transactionalEntityManager => {
|
||||||
|
const metas = await transactionalEntityManager.find(Meta, {
|
||||||
|
order: {
|
||||||
|
id: 'DESC',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
if (obj.channel === 'internal') {
|
const meta = metas[0];
|
||||||
const { type, body } = obj.message;
|
|
||||||
switch (type) {
|
if (meta) {
|
||||||
case 'metaUpdated': {
|
await transactionalEntityManager.update(Meta, meta.id, data);
|
||||||
this.cache = body;
|
|
||||||
break;
|
const metas = await transactionalEntityManager.find(Meta, {
|
||||||
}
|
order: {
|
||||||
default:
|
id: 'DESC',
|
||||||
break;
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return metas[0];
|
||||||
|
} else {
|
||||||
|
return await transactionalEntityManager.save(Meta, data);
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
|
this.globalEventService.publishInternalEvent('metaUpdated', updated);
|
||||||
|
|
||||||
|
return updated;
|
||||||
}
|
}
|
||||||
|
|
||||||
public onApplicationShutdown(signal?: string | undefined) {
|
public onApplicationShutdown(signal?: string | undefined) {
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
import { Inject, Injectable } from '@nestjs/common';
|
import { Inject, Injectable } from '@nestjs/common';
|
||||||
import { DataSource } from 'typeorm';
|
import { DataSource } from 'typeorm';
|
||||||
import { Meta } from '@/models/entities/Meta.js';
|
import type { Meta } from '@/models/entities/Meta.js';
|
||||||
import { ModerationLogService } from '@/core/ModerationLogService.js';
|
import { ModerationLogService } from '@/core/ModerationLogService.js';
|
||||||
import { DB_MAX_NOTE_TEXT_LENGTH } from '@/misc/hard-limits.js';
|
import { DB_MAX_NOTE_TEXT_LENGTH } from '@/misc/hard-limits.js';
|
||||||
import { Endpoint } from '@/server/api/endpoint-base.js';
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
||||||
import { DI } from '@/di-symbols.js';
|
import { DI } from '@/di-symbols.js';
|
||||||
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
||||||
|
import { MetaService } from '@/core/MetaService.js';
|
||||||
|
|
||||||
export const meta = {
|
export const meta = {
|
||||||
tags: ['admin'],
|
tags: ['admin'],
|
||||||
@ -116,7 +117,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|||||||
@Inject(DI.db)
|
@Inject(DI.db)
|
||||||
private db: DataSource,
|
private db: DataSource,
|
||||||
|
|
||||||
private globalEventService: GlobalEventService,
|
private metaService: MetaService,
|
||||||
private moderationLogService: ModerationLogService,
|
private moderationLogService: ModerationLogService,
|
||||||
) {
|
) {
|
||||||
super(meta, paramDef, async (ps, me) => {
|
super(meta, paramDef, async (ps, me) => {
|
||||||
@ -438,32 +439,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|||||||
set.enableActiveEmailValidation = ps.enableActiveEmailValidation;
|
set.enableActiveEmailValidation = ps.enableActiveEmailValidation;
|
||||||
}
|
}
|
||||||
|
|
||||||
const updated = await this.db.transaction(async transactionalEntityManager => {
|
await this.metaService.update(set);
|
||||||
const metas = await transactionalEntityManager.find(Meta, {
|
|
||||||
order: {
|
|
||||||
id: 'DESC',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const meta = metas[0];
|
|
||||||
|
|
||||||
if (meta) {
|
|
||||||
await transactionalEntityManager.update(Meta, meta.id, set);
|
|
||||||
|
|
||||||
const metas = await transactionalEntityManager.find(Meta, {
|
|
||||||
order: {
|
|
||||||
id: 'DESC',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return metas[0];
|
|
||||||
} else {
|
|
||||||
return await transactionalEntityManager.save(Meta, set);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
this.globalEventService.publishInternalEvent('metaUpdated', updated);
|
|
||||||
|
|
||||||
this.moderationLogService.insertModerationLog(me, 'updateMeta');
|
this.moderationLogService.insertModerationLog(me, 'updateMeta');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user