mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
Merge pull request #998 from Hexastack/fix/message-attachment-remove
feat: undo message attachment reference removal
This commit is contained in:
commit
75d96650aa
@ -11,9 +11,6 @@ import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { MongooseModule } from '@nestjs/mongoose';
|
||||
|
||||
import { AttachmentModule } from '@/attachment/attachment.module';
|
||||
import { AttachmentRepository } from '@/attachment/repositories/attachment.repository';
|
||||
import { AttachmentModel } from '@/attachment/schemas/attachment.schema';
|
||||
import { AttachmentService } from '@/attachment/services/attachment.service';
|
||||
import { ChannelModule } from '@/channel/channel.module';
|
||||
import { CmsModule } from '@/cms/cms.module';
|
||||
import { UserModule } from '@/user/user.module';
|
||||
@ -61,7 +58,6 @@ import { SubscriberService } from './services/subscriber.service';
|
||||
SubscriberModel,
|
||||
ConversationModel,
|
||||
SubscriberModel,
|
||||
AttachmentModel,
|
||||
]),
|
||||
forwardRef(() => ChannelModule),
|
||||
CmsModule,
|
||||
@ -96,8 +92,6 @@ import { SubscriberService } from './services/subscriber.service';
|
||||
ConversationService,
|
||||
ChatService,
|
||||
BotService,
|
||||
AttachmentService,
|
||||
AttachmentRepository,
|
||||
],
|
||||
exports: [
|
||||
SubscriberService,
|
||||
|
@ -11,14 +11,8 @@ import {
|
||||
InternalServerErrorException,
|
||||
Optional,
|
||||
} from '@nestjs/common';
|
||||
import { OnEvent } from '@nestjs/event-emitter';
|
||||
import { Document, Query } from 'mongoose';
|
||||
|
||||
import { Attachment } from '@/attachment/schemas/attachment.schema';
|
||||
import { AttachmentService } from '@/attachment/services/attachment.service';
|
||||
import { DeleteResult } from '@/utils/generics/base-repository';
|
||||
import { BaseService } from '@/utils/generics/base-service';
|
||||
import { TFilterQuery } from '@/utils/types/filter.types';
|
||||
import {
|
||||
SocketGet,
|
||||
SocketPost,
|
||||
@ -45,7 +39,6 @@ export class MessageService extends BaseService<
|
||||
|
||||
constructor(
|
||||
private readonly messageRepository: MessageRepository,
|
||||
private attachmentService: AttachmentService,
|
||||
@Optional() gateway?: WebsocketGateway,
|
||||
) {
|
||||
super(messageRepository);
|
||||
@ -134,40 +127,4 @@ export class MessageService extends BaseService<
|
||||
|
||||
return lastMessages.reverse();
|
||||
}
|
||||
|
||||
@OnEvent('hook:attachment:preDelete')
|
||||
async handleDeleteImage(
|
||||
_query: Query<
|
||||
DeleteResult,
|
||||
Document<Attachment, any, any>,
|
||||
unknown,
|
||||
Attachment,
|
||||
'deleteOne' | 'deleteMany'
|
||||
>,
|
||||
criteria: TFilterQuery<Attachment>,
|
||||
) {
|
||||
try {
|
||||
this.logger.log(
|
||||
'deleting attachment messages containing deleted images',
|
||||
criteria,
|
||||
);
|
||||
const foundAttachments = await this.attachmentService.find(criteria);
|
||||
|
||||
for (const attachment of foundAttachments) {
|
||||
await this.updateMany(
|
||||
{
|
||||
'message.attachment.payload.id': attachment.id,
|
||||
},
|
||||
{
|
||||
['message.attachment.payload.id' as any]: null,
|
||||
},
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
this.logger.error(
|
||||
'Unable to cleanup old messages with attachment ids',
|
||||
error,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user