Skip to content

Commit

Permalink
message attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
targoninc-alex committed Jun 29, 2024
1 parent 79c145b commit 8149071
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 39 deletions.
1 change: 1 addition & 0 deletions .idea/venel-api.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions src/features/database/mariaDbDatabase.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import mariadb from 'mariadb';
import {CLI} from "../../tooling/CLI";
import {
Attachment,
BridgeInstance,
Channel,
ChannelMember,
Expand Down Expand Up @@ -177,6 +178,10 @@ WHERE ur.userId = ?`, [userId]);
return await this.query("SELECT * FROM venel.messages m WHERE channelId = ? ORDER BY createdAt DESC LIMIT 100 OFFSET ?", [channelId, offset]);
}

async getAttachmentsForMessage(messageId: Id): Promise<Attachment[]> {
return await this.query("SELECT * FROM venel.attachments WHERE messageId = ?", [messageId]) ?? [];
}

async deleteUser(id: Id) {
await this.query("DELETE FROM venel.users WHERE id = ?", [id]);
}
Expand Down Expand Up @@ -325,4 +330,8 @@ WHERE ur.userId = ?`, [userId]);
async getUserSettings(id: Id): Promise<UserSetting[] | null> {
return await this.query("SELECT * FROM venel.userSettings WHERE userId = ?", [id]);
}

async createAttachment(id: Id, type: string, data: Buffer | null) {
await this.query("INSERT INTO venel.attachments (messageId, type, data) VALUES (?, ?, ?)", [id, type, data]);
}
}
23 changes: 9 additions & 14 deletions src/features/database/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
*/
export type Id = number;

export interface AudioAttachment {
'binaryContent': Buffer | null;
export interface Attachment {
'data': Buffer | null;
'id': Id;
'messageId': Id | null;
'type': string;
}

export interface BridgedUser {
Expand Down Expand Up @@ -39,16 +40,10 @@ export interface Channel {
'updatedAt': Date;
}

export interface ImageAttachment {
'binaryContent': Buffer | null;
'id': Id;
'messageId': Id | null;
}

export interface MessageReaction {
'messageId': Id;
'reactionId': Id;
'userId': Id;
'userId': Id;
}

export interface Message {
Expand Down Expand Up @@ -113,10 +108,10 @@ export interface User {
}

export interface UserSetting {
'createdAt': Date;
'settingKey': string;
'updatedAt': Date;
'userId': Id;
'value': string;
'createdAt': Date;
'settingKey': string;
'updatedAt': Date;
'userId': Id;
'value': string;
}

42 changes: 37 additions & 5 deletions src/features/liveFeature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {MariaDbDatabase} from "./database/mariaDbDatabase";
import {ServerOptions, WebSocketServer} from "ws";
import {MessagingEndpoints} from "./messaging/endpoints";
import {CLI} from "../tooling/CLI";
import {User} from "./database/models";
import {Attachment, User} from "./database/models";
import {Application} from "express";
import {createServer} from "http";
import {UserWebSocket} from "./live/UserWebSocket";
Expand Down Expand Up @@ -47,9 +47,6 @@ export class LiveFeature {

ws.on("message", async (message: any) => {
const data = JSON.parse(message.toString());
if (data.type !== "ping") {
CLI.debug("Received message: " + JSON.stringify(data, null, 2));
}
switch (data.type) {
case "message":
await LiveFeature.sendMessage(data, ws.user, clients, ws, db);
Expand Down Expand Up @@ -78,6 +75,12 @@ export class LiveFeature {
case "createChannelDm":
await LiveFeature.createChannelDm(data, ws.user, clients, ws, db);
break;
case "ping":
CLI.debug("Received ping");
break;
default:
CLI.debug("Received unknown message: " + JSON.stringify(data, null, 2));
break;
}
});
});
Expand Down Expand Up @@ -144,13 +147,42 @@ export class LiveFeature {
client.send(JSON.stringify({error: "Message not found"}));
return;
}

let attachments: Attachment[] = [];
if (data.attachments) {
for (const attachment of data.attachments) {
if (!attachment.type) {
client.send(JSON.stringify({error: "Attachment type is required"}));
return;
}
if (!attachment.data) {
client.send(JSON.stringify({error: "Attachment data is required"}));
return;
}
attachments.push({
messageId: message.id,
id: -1,
type: attachment.type,
data: Buffer.from(attachment.data, "base64")
});
}

CLI.debug(`Creating ${attachments.length} attachments`);
for (const attachment of attachments) {
CLI.debug(`Creating attachment with length ${attachment.data?.length} of type ${attachment.type}`);
await db.createAttachment(message.id, attachment.type, attachment.data);
}
}

const sender = await db.getUserById(user.id);
if (!sender) {
client.send(JSON.stringify({error: "Sender not found"}));
return;
}

message.sender = safeUser(sender);
message.reactions = await db.getReactionsForMessage(message.id);
message.reactions = [];
message.attachments = attachments;

const payload = JSON.stringify({
type: "message",
Expand Down
1 change: 1 addition & 0 deletions src/features/messaging/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ export class MessagingEndpoints {
const sender = users.get(message.senderId);
message.sender = safeUser(sender as User);
message.reactions = await db.getReactionsForMessage(message.id);
message.attachments = await db.getAttachmentsForMessage(message.id);
}
res.json(messages);
}
Expand Down
3 changes: 2 additions & 1 deletion src/models/receivableMessage.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {Message, Reaction} from "../features/database/models";
import {Attachment, Message, Reaction} from "../features/database/models";

import {SafeUser} from "./safeUser";

export interface ReceivableMessage extends Message {
sender: SafeUser;
reactions: Reaction[];
attachments: Attachment[];
}
25 changes: 6 additions & 19 deletions updateDb.sql
Original file line number Diff line number Diff line change
Expand Up @@ -154,33 +154,20 @@ create table if not exists venel.messages
on delete cascade
);

create table if not exists venel.audioAttachments
create table if not exists venel.attachments
(
id bigint auto_increment
primary key,
messageId bigint null,
binaryContent mediumblob null,
constraint audioAttachments_ibfk_1
messageId bigint null,
type varchar(16) default 'file' not null,
data mediumblob null,
constraint attachments_ibfk_1
foreign key (messageId) references venel.messages (id)
on delete cascade
);

create index if not exists messageId
on venel.audioAttachments (messageId);

create table if not exists venel.imageAttachments
(
id bigint auto_increment
primary key,
messageId bigint null,
binaryContent mediumblob null,
constraint imageAttachments_ibfk_1
foreign key (messageId) references venel.messages (id)
on delete cascade
);

create index if not exists messageId
on venel.imageAttachments (messageId);
on venel.attachments (messageId);

create table if not exists venel.messageReactions
(
Expand Down

0 comments on commit 8149071

Please sign in to comment.