mirror of
https://github.com/hexastack/hexabot
synced 2025-01-22 10:35:37 +00:00
fix: minor
This commit is contained in:
parent
bdf1763503
commit
0b4a1085ec
@ -143,7 +143,7 @@ export class AttachmentController extends BaseController<Attachment> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const attachments = [];
|
const attachments: Attachment[] = [];
|
||||||
for (const file of files.file) {
|
for (const file of files.file) {
|
||||||
const attachment = await this.attachmentService.store(file, {
|
const attachment = await this.attachmentService.store(file, {
|
||||||
name: file.originalname,
|
name: file.originalname,
|
||||||
@ -153,8 +153,11 @@ export class AttachmentController extends BaseController<Attachment> {
|
|||||||
createdBy: userId,
|
createdBy: userId,
|
||||||
createdByRef: 'User',
|
createdByRef: 'User',
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (attachment) {
|
||||||
attachments.push(attachment);
|
attachments.push(attachment);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return attachments;
|
return attachments;
|
||||||
}
|
}
|
||||||
|
@ -132,5 +132,6 @@ export class AttachmentContextParamDto {
|
|||||||
})
|
})
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsIn(Object.values(AttachmentContext))
|
@IsIn(Object.values(AttachmentContext))
|
||||||
context?: TAttachmentContext;
|
@IsNotEmpty()
|
||||||
|
context: TAttachmentContext;
|
||||||
}
|
}
|
||||||
|
@ -181,10 +181,10 @@ export class AttachmentService extends BaseService<Attachment> {
|
|||||||
async store(
|
async store(
|
||||||
file: Buffer | Stream | Readable | Express.Multer.File,
|
file: Buffer | Stream | Readable | Express.Multer.File,
|
||||||
metadata: AttachmentMetadataDto,
|
metadata: AttachmentMetadataDto,
|
||||||
): Promise<Attachment | undefined> {
|
): Promise<Attachment | null> {
|
||||||
if (this.getStoragePlugin()) {
|
if (this.getStoragePlugin()) {
|
||||||
const storedDto = await this.getStoragePlugin()?.store?.(file, metadata);
|
const storedDto = await this.getStoragePlugin()?.store?.(file, metadata);
|
||||||
return storedDto ? await this.create(storedDto) : undefined;
|
return storedDto ? await this.create(storedDto) : null;
|
||||||
} else {
|
} else {
|
||||||
const rootDir = this.getRootDirByContext(metadata.context);
|
const rootDir = this.getRootDirByContext(metadata.context);
|
||||||
const uniqueFilename = generateUniqueFilename(metadata.name);
|
const uniqueFilename = generateUniqueFilename(metadata.name);
|
||||||
|
@ -8,7 +8,6 @@
|
|||||||
|
|
||||||
import { Attachment } from '@/attachment/schemas/attachment.schema';
|
import { Attachment } from '@/attachment/schemas/attachment.schema';
|
||||||
import { Subscriber } from '@/chat/schemas/subscriber.schema';
|
import { Subscriber } from '@/chat/schemas/subscriber.schema';
|
||||||
import { AttachmentPayload } from '@/chat/schemas/types/attachment';
|
|
||||||
import { SubscriberChannelData } from '@/chat/schemas/types/channel';
|
import { SubscriberChannelData } from '@/chat/schemas/types/channel';
|
||||||
import {
|
import {
|
||||||
IncomingMessageType,
|
IncomingMessageType,
|
||||||
@ -78,7 +77,6 @@ export default abstract class EventWrapper<
|
|||||||
messageType: this.getMessageType(),
|
messageType: this.getMessageType(),
|
||||||
payload: this.getPayload(),
|
payload: this.getPayload(),
|
||||||
message: this.getMessage(),
|
message: this.getMessage(),
|
||||||
attachments: this.getAttachments(),
|
|
||||||
deliveredMessages: this.getDeliveredMessages(),
|
deliveredMessages: this.getDeliveredMessages(),
|
||||||
watermark: this.getWatermark(),
|
watermark: this.getWatermark(),
|
||||||
},
|
},
|
||||||
@ -205,7 +203,15 @@ export default abstract class EventWrapper<
|
|||||||
this._adapter.eventType === StdEventType.message &&
|
this._adapter.eventType === StdEventType.message &&
|
||||||
this._adapter.messageType === IncomingMessageType.attachments
|
this._adapter.messageType === IncomingMessageType.attachments
|
||||||
) {
|
) {
|
||||||
await this._handler.persistMessageAttachments(this);
|
await this._handler.persistMessageAttachments(
|
||||||
|
this as EventWrapper<
|
||||||
|
any,
|
||||||
|
any,
|
||||||
|
ChannelName,
|
||||||
|
ChannelHandler<ChannelName>,
|
||||||
|
Record<string, any>
|
||||||
|
>,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -263,13 +269,6 @@ export default abstract class EventWrapper<
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the list of received attachments
|
|
||||||
*
|
|
||||||
* @returns Received attachments message
|
|
||||||
*/
|
|
||||||
abstract getAttachments(): AttachmentPayload[];
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the list of delivered messages
|
* Returns the list of delivered messages
|
||||||
*
|
*
|
||||||
@ -393,14 +392,6 @@ export class GenericEventWrapper extends EventWrapper<
|
|||||||
throw new Error('Unknown incoming message type');
|
throw new Error('Unknown incoming message type');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @returns A list of received attachments
|
|
||||||
* @deprecated - This method is deprecated
|
|
||||||
*/
|
|
||||||
getAttachments(): AttachmentPayload[] {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the delivered messages ids
|
* Returns the delivered messages ids
|
||||||
*
|
*
|
||||||
|
@ -256,6 +256,10 @@ export class ChatService {
|
|||||||
this.eventEmitter.emit('hook:stats:entry', 'new_users', 'New users');
|
this.eventEmitter.emit('hook:stats:entry', 'new_users', 'New users');
|
||||||
subscriberData.channel = event.getChannelData();
|
subscriberData.channel = event.getChannelData();
|
||||||
subscriber = await this.subscriberService.create(subscriberData);
|
subscriber = await this.subscriberService.create(subscriberData);
|
||||||
|
|
||||||
|
if (!subscriber) {
|
||||||
|
throw new Error('Unable to create a new subscriber');
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Already existing user profile
|
// Already existing user profile
|
||||||
// Exec lastvisit hook
|
// Exec lastvisit hook
|
||||||
@ -288,18 +292,22 @@ export class ChatService {
|
|||||||
avatar: avatar.id,
|
avatar: avatar.id,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (!subscriber) {
|
||||||
|
throw new Error('Unable to update the subscriber avatar');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.logger.error(
|
this.logger.error(
|
||||||
`Unable to retrieve avatar for subscriber ${subscriber.id}`,
|
`Unable to retrieve avatar for subscriber ${event.getSenderForeignId()}`,
|
||||||
err,
|
err,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the subscriber object
|
// Set the subscriber object
|
||||||
event.setSender(subscriber);
|
event.setSender(subscriber!);
|
||||||
|
|
||||||
// Preprocess the event (persist attachments, ...)
|
// Preprocess the event (persist attachments, ...)
|
||||||
if (event.preprocess) {
|
if (event.preprocess) {
|
||||||
@ -309,7 +317,7 @@ export class ChatService {
|
|||||||
// Trigger message received event
|
// Trigger message received event
|
||||||
this.eventEmitter.emit('hook:chatbot:received', event);
|
this.eventEmitter.emit('hook:chatbot:received', event);
|
||||||
|
|
||||||
if (subscriber.assignedTo) {
|
if (subscriber?.assignedTo) {
|
||||||
this.logger.debug('Conversation taken over', subscriber.assignedTo);
|
this.logger.debug('Conversation taken over', subscriber.assignedTo);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright © 2024 Hexastack. All rights reserved.
|
* Copyright © 2025 Hexastack. All rights reserved.
|
||||||
*
|
*
|
||||||
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
|
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
|
||||||
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
|
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
|
||||||
@ -149,17 +149,6 @@ export class ConversationService extends BaseService<
|
|||||||
lat: parseFloat(coordinates.lat.toString()),
|
lat: parseFloat(coordinates.lat.toString()),
|
||||||
lon: parseFloat(coordinates.lon.toString()),
|
lon: parseFloat(coordinates.lon.toString()),
|
||||||
};
|
};
|
||||||
} else if (msgType === 'attachments') {
|
|
||||||
// @TODO : deprecated in favor of geolocation msgType
|
|
||||||
const attachments = event.getAttachments();
|
|
||||||
// @ts-expect-error deprecated
|
|
||||||
if (attachments.length === 1 && attachments[0].type === 'location') {
|
|
||||||
// @ts-expect-error deprecated
|
|
||||||
const coord = attachments[0].payload.coordinates;
|
|
||||||
convo.context.user_location = { lat: 0, lon: 0 };
|
|
||||||
convo.context.user_location.lat = parseFloat(coord.lat);
|
|
||||||
convo.context.user_location.lon = parseFloat(coord.long);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deal with load more in the case of a list display
|
// Deal with load more in the case of a list display
|
||||||
|
@ -604,6 +604,11 @@ export default abstract class BaseWebChannelHandler<
|
|||||||
try {
|
try {
|
||||||
const { type, data } = req.body as Web.IncomingMessage;
|
const { type, data } = req.body as Web.IncomingMessage;
|
||||||
|
|
||||||
|
if (!req.session?.web?.profile?.id) {
|
||||||
|
this.logger.debug('Web Channel Handler : No session');
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
// Check if any file is provided
|
// Check if any file is provided
|
||||||
if (type !== 'file' || !('file' in data) || !data.file) {
|
if (type !== 'file' || !('file' in data) || !data.file) {
|
||||||
this.logger.debug('Web Channel Handler : No files provided');
|
this.logger.debug('Web Channel Handler : No files provided');
|
||||||
@ -622,7 +627,7 @@ export default abstract class BaseWebChannelHandler<
|
|||||||
type: data.type,
|
type: data.type,
|
||||||
context: 'message_attachment',
|
context: 'message_attachment',
|
||||||
createdByRef: 'Subscriber',
|
createdByRef: 'Subscriber',
|
||||||
createdBy: req.session.web.profile?.id,
|
createdBy: req.session?.web?.profile?.id,
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
this.logger.error(
|
this.logger.error(
|
||||||
@ -677,7 +682,7 @@ export default abstract class BaseWebChannelHandler<
|
|||||||
const file = await multerUpload;
|
const file = await multerUpload;
|
||||||
|
|
||||||
// Check if any file is provided
|
// Check if any file is provided
|
||||||
if (!req.file) {
|
if (!file) {
|
||||||
this.logger.debug('Web Channel Handler : No files provided');
|
this.logger.debug('Web Channel Handler : No files provided');
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,6 @@
|
|||||||
import { Attachment } from '@/attachment/schemas/attachment.schema';
|
import { Attachment } from '@/attachment/schemas/attachment.schema';
|
||||||
import EventWrapper from '@/channel/lib/EventWrapper';
|
import EventWrapper from '@/channel/lib/EventWrapper';
|
||||||
import { ChannelName } from '@/channel/types';
|
import { ChannelName } from '@/channel/types';
|
||||||
import { AttachmentPayload } from '@/chat/schemas/types/attachment';
|
|
||||||
import {
|
import {
|
||||||
IncomingMessageType,
|
IncomingMessageType,
|
||||||
PayloadType,
|
PayloadType,
|
||||||
@ -296,17 +295,6 @@ export default class WebEventWrapper<
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Return the list of recieved attachments
|
|
||||||
*
|
|
||||||
* @deprecated
|
|
||||||
* @returns Received attachments message
|
|
||||||
*/
|
|
||||||
getAttachments(): AttachmentPayload[] {
|
|
||||||
const message = this.getMessage() as any;
|
|
||||||
return 'attachment' in message ? [].concat(message.attachment) : [];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the delivered messages ids
|
* Return the delivered messages ids
|
||||||
*
|
*
|
||||||
|
@ -38,11 +38,11 @@ const getAdminUser = async () => {
|
|||||||
const UserModel = mongoose.model<User>(User.name, userSchema);
|
const UserModel = mongoose.model<User>(User.name, userSchema);
|
||||||
|
|
||||||
const adminRole = await RoleModel.findOne({ name: 'admin' });
|
const adminRole = await RoleModel.findOne({ name: 'admin' });
|
||||||
const user = await UserModel.findOne({ roles: [adminRole._id] }).sort({
|
const user = await UserModel.findOne({ roles: [adminRole!._id] }).sort({
|
||||||
createdAt: 'asc',
|
createdAt: 'asc',
|
||||||
});
|
});
|
||||||
|
|
||||||
return user;
|
return user!;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -638,7 +638,7 @@ const migrateAndPopulateAttachmentMessages = async ({
|
|||||||
if ('attachment_id' in msg.message.attachment.payload) {
|
if ('attachment_id' in msg.message.attachment.payload) {
|
||||||
// Add extra attrs
|
// Add extra attrs
|
||||||
await attachmentService.updateOne(
|
await attachmentService.updateOne(
|
||||||
msg.message.attachment.payload.attachment_id,
|
msg.message.attachment.payload.attachment_id as string,
|
||||||
{
|
{
|
||||||
createdByRef: msg.sender ? 'Subscriber' : 'User',
|
createdByRef: msg.sender ? 'Subscriber' : 'User',
|
||||||
createdBy: msg.sender ? msg.sender : adminUser.id,
|
createdBy: msg.sender ? msg.sender : adminUser.id,
|
||||||
@ -679,6 +679,8 @@ const migrateAndPopulateAttachmentMessages = async ({
|
|||||||
|
|
||||||
if (attachment) {
|
if (attachment) {
|
||||||
await updateAttachmentId(msg._id, attachment.id);
|
await updateAttachmentId(msg._id, attachment.id);
|
||||||
|
} else {
|
||||||
|
logger.warn(`Unable to store attachment for message ${msg._id}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
4
api/src/utils/test/fixtures/attachment.ts
vendored
4
api/src/utils/test/fixtures/attachment.ts
vendored
@ -24,7 +24,7 @@ export const attachmentFixtures: AttachmentCreateDto[] = [
|
|||||||
},
|
},
|
||||||
context: 'content_attachment',
|
context: 'content_attachment',
|
||||||
createdByRef: 'User',
|
createdByRef: 'User',
|
||||||
createdBy: null,
|
createdBy: '9'.repeat(24),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: 'store2.jpg',
|
name: 'store2.jpg',
|
||||||
@ -38,7 +38,7 @@ export const attachmentFixtures: AttachmentCreateDto[] = [
|
|||||||
},
|
},
|
||||||
context: 'content_attachment',
|
context: 'content_attachment',
|
||||||
createdByRef: 'User',
|
createdByRef: 'User',
|
||||||
createdBy: null,
|
createdBy: '9'.repeat(24),
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user