Message.attachments -> Message.attachmentIds

This commit is contained in:
ADAMJR 2021-11-03 15:19:07 +00:00
parent 5ad38e0749
commit 1202120d84
7 changed files with 21 additions and 18 deletions

View File

@ -11,16 +11,16 @@ export default class Messages extends DBWrapper<string, MessageDocument> {
return message;
}
public async create(authorId: string, channelId: string, { attachments, content }: Partial<Entity.Message>) {
public async create(authorId: string, channelId: string, { attachmentIds, content }: Partial<Entity.Message>) {
if (!content)
throw new TypeError('Content must be provided');
// TODO: TESTME
if (!content && !attachments?.length)
if (!content && !attachmentIds?.length)
throw new TypeError('Empty messages are not valid');
return await Message.create({
_id: generateSnowflake(),
attachments,
attachmentIds,
authorId,
channelId,
content,

View File

@ -14,9 +14,7 @@ export const Message = model<MessageDocument>('message', new Schema({
type: String,
default: generateSnowflake,
},
attachments: {
type: [Object],
},
attachmentIds: [String],
authorId: {
type: String,
required: [true, 'Author ID is required'],

View File

@ -1,12 +1,13 @@
import { Socket } from 'socket.io';
import { WebSocket } from '../websocket';
import { WSEvent, } from './ws-event';
import { WSGuard } from '../modules/ws-guard';
import Messages from '../../data/messages';
import Users from '../../data/users';
import { Channel } from '../../data/models/channel';
import { WS } from '../../types/ws';
import { promisify } from 'util';
import { stat } from 'fs';
import { resolve } from 'path';
const statAsync = promisify(stat);
export default class implements WSEvent<'MESSAGE_CREATE'> {
on = 'MESSAGE_CREATE' as const;
@ -17,12 +18,12 @@ export default class implements WSEvent<'MESSAGE_CREATE'> {
private users = deps.users,
) {}
public async invoke(ws: WebSocket, client: Socket, { attachments, channelId, content, embed }: WS.Params.MessageCreate) {
public async invoke(ws: WebSocket, client: Socket, { attachmentIds, channelId, content, embed }: WS.Params.MessageCreate) {
const authorId = ws.sessions.userId(client);
const [_, message, author] = await Promise.all([
this.guard.validateCanInChannel(client, channelId, 'SEND_MESSAGES'),
this.messages.create(authorId, channelId, { attachments, content, embed }),
this.messages.create(authorId, channelId, { attachmentIds, content, embed }),
this.users.getSelf(authorId),
]);

View File

@ -91,7 +91,7 @@ const MessageBox: React.FunctionComponent<MessageBoxProps> = (props) => {
const onChange: any = (e: Event) => {
const input = e.target as HTMLInputElement;
console.log(input.files);
dispatch(uploadFileAsMessage(input.files![0]));
dispatch(uploadFileAsMessage(channel.id, { content }, input.files![0]));
}
return (!props.editingMessageId) ? (

View File

@ -48,15 +48,17 @@ export const fetchMessages = (channelId: string) => (dispatch, getState: () => S
}));
}
export const createMessage = (channelId: string, payload: Partial<Entity.Message>) => (dispatch) => {
export const createMessage = (channelId: string, payload: Partial<Entity.Message>, file?: File) => (dispatch) => {
dispatch(api.wsCallBegan({
event: 'MESSAGE_CREATE',
data: { ...payload, channelId },
data: { ...payload, channelId, attachmentIds: [
] } as WS.Params.MessageCreate,
}));
}
// each file is uploaded individually as a separate API call
export const uploadFileAsMessage = (file: File) => (dispatch) => {
export const uploadFileAsMessage = (channelId: string, payload: Partial<Entity.Message>, file: File) => (dispatch) => {
const formData = new FormData();
formData.append('file', file, file.name);
@ -66,6 +68,8 @@ export const uploadFileAsMessage = (file: File) => (dispatch) => {
data: formData,
headers: { 'Content-Type': 'multipart/form-data' },
}));
dispatch(createMessage(channelId, payload, file));
}
export const updateMessage = (id: string, payload: Partial<Entity.Message>) => (dispatch) => {

2
types/entity.d.ts vendored
View File

@ -43,7 +43,7 @@ declare namespace Entity {
}
export interface Message {
id: string;
attachments?: MessageTypes.Attachment[];
attachmentIds?: string[];
authorId: string;
channelId: string;
content?: string;

2
types/ws.d.ts vendored
View File

@ -194,7 +194,7 @@ declare namespace WS {
export interface MessageCreate {
channelId: string;
content?: string;
attachments?: MessageTypes.Attachment[];
attachmentIds?: string[];
embed?: MessageTypes.Embed;
}
export interface MessageDelete {