Initial move

This commit is contained in:
2024-09-03 14:50:33 +02:00
parent adb6be0006
commit 9fa656ed5e
138 changed files with 13117 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
import type { Request, Response } from "express";
import * as ChannelService from "../services/channel-service";
import { logger } from "../globals";
export const createChannel = async (req: Request, res: Response) => {
const { name } = req.body;
if (!name) {
return res.status(400).json({ error: 'Name is required' });
}
const chan = await ChannelService.createChannel(name);
logger.info(`Channel ${name} created`);
res.json(chan);
}
export const deleteChannel = async (req: Request, res: Response) => {
const { channelId } = req.params;
if (!channelId) {
return res.status(400).json({ error: 'Channel ID is required' });
}
const result = await ChannelService.deleteChannel(channelId);
if (result.changes === 0) {
logger.warn(`Channel ${channelId} not found while deleting`);
return res.status(404).json({ error: 'Channel not found' });
}
logger.info(`Channel ${channelId} deleted`);
res.json({ message: 'Channel deleted successfully' });
}
export const getChannels = async (req: Request, res: Response) => {
const channels = await ChannelService.getChannels();
res.json({ channels });
}
export const mergeChannel = async (req: Request, res: Response) => {
const { channelId } = req.params;
const { targetChannelId } = req.body;
if (!channelId || !targetChannelId) {
return res.status(400).json({ error: 'Channel ID and targetChannelId are required' });
}
const result = await ChannelService.mergeChannel(channelId, targetChannelId);
logger.info(`Channel ${targetChannelId} merged into ${channelId}`);
res.json({ message: 'Channels merged successfully' });
}
export const updateChannel = async (req: Request, res: Response) => {
const { channelId } = req.params;
const { name } = req.body;
if (!channelId || !name) {
return res.status(400).json({ error: 'Channel ID and name are required' });
}
const result = await ChannelService.updateChannel(channelId, name);
if (result.changes === 0) {
return res.status(404).json({ error: 'Channel not found' });
}
logger.info(`Channel ${channelId} updated as ${name}`);
res.json({ message: 'Channel updated successfully' });
}

View File

@@ -0,0 +1,33 @@
import type { Request, Response } from "express";
import * as FileService from "../services/file-service";
import { logger } from "../globals";
export const uploadFile = async (req: Request, res: Response) => {
const { channelId, messageId } = req.params;
const filePath = (req.file as Express.Multer.File).path;
const fileType = req.file?.mimetype;
const fileSize = req.file?.size;
const originalName = req.file?.originalname;
if (!channelId || !messageId) {
return res.status(400).json({ error: 'Channel ID and message ID are required' });
}
if (!filePath || !fileType || !fileSize || !originalName) {
return res.status(400).json({ error: 'File is required' });
}
const result = await FileService.uploadFile(channelId, messageId, filePath, fileType!, fileSize!, originalName!);
logger.info(`File ${originalName} uploaded to message ${messageId} as ${filePath}`);
res.json({ id: result.lastInsertRowid, channelId, messageId, filePath, fileType });
}
export const getFiles = async (req: Request, res: Response) => {
const { messageId } = req.params;
if (!messageId) {
return res.status(400).json({ error: 'Message ID is required' });
}
const files = await FileService.getFiles(messageId);
res.json({ files });
}

View File

@@ -0,0 +1,54 @@
import type { Request, Response } from "express";
import * as MessageService from "../services/message-service";
import { logger } from "../globals";
export const createMessage = async (req: Request, res: Response) => {
const { content } = req.body;
const { channelId } = req.params;
if (!content || !channelId) {
return res.status(400).json({ error: 'Content and channel ID are required' });
}
const messageId = await MessageService.createMessage(channelId, content);
logger.info(`Message ${messageId} created in channel ${channelId}`);
res.json({ id: messageId, channelId, content, createdAt: new Date().toISOString() });
};
export const updateMessage = async (req: Request, res: Response) => {
const { content } = req.body;
const { messageId } = req.params;
if (!content || !messageId) {
return res.status(400).json({ error: 'Content and message ID are required ' });
}
const result = await MessageService.updateMessage(messageId, content);
if (result.changes === 0) {
return res.status(404).json({ error: 'Message not found' });
}
logger.info(`Message ${messageId} updated`);
res.json({ id: messageId, content });
}
export const deleteMessage = async (req: Request, res: Response) => {
const { messageId } = req.params;
if (!messageId) {
return res.status(400).json({ error: 'Message ID is required' });
}
const result = await MessageService.deleteMessage(messageId);
if (result.changes === 0) {
return res.status(404).json({ error: 'Message not found' });
}
logger.info(`Message ${messageId} deleted`);
res.json({ message: 'Message deleted successfully' });
}
export const getMessages = async (req: Request, res: Response) => {
const { channelId } = req.params;
if (!channelId) {
return res.status(400).json({ error: 'Channel ID is required' });
}
const messages = await MessageService.getMessages(channelId);
res.json({ messages });
}

View File

@@ -0,0 +1,13 @@
import type { Request, Response } from "express";
import * as SearchService from "../services/search-service";
import { logger } from "../globals";
export const search = async (req: Request, res: Response) => {
const { query, channelId } = req.query;
if (!query) {
return res.status(400).json({ error: 'Query is required' });
}
const results = await SearchService.search(query as string, channelId as string);
logger.info(`Searched for ${query}`);
res.json({ results });
}

View File

@@ -0,0 +1,29 @@
import { events } from "../globals";
import { WebSocket } from "ws";
export const attachEvents = (ws: WebSocket) => {
events.on('file-uploaded', (id, channelId, messageId, filePath, fileType, fileSize, originalName) => {
ws.send(JSON.stringify({ type: 'file-uploaded', data: {id, channelId, messageId, filePath, fileType, fileSize, originalName }}));
});
events.on('message-created', (id, channelId, content) => {
ws.send(JSON.stringify({ type: 'message-created', data: {id, channelId, content }}));
});
events.on('message-updated', (id, content) => {
ws.send(JSON.stringify({ type: 'message-updated', data: {id, content }}));
});
events.on('message-deleted', (id) => {
ws.send(JSON.stringify({ type: 'message-deleted', data: {id }}));
});
events.on('channel-created', (channel) => {
ws.send(JSON.stringify({ type: 'channel-created', data: {channel }}));
});
events.on('channel-deleted', (id) => {
ws.send(JSON.stringify({ type: 'channel-deleted', data: {id} }));
});
events.on('channel-merged', (channelId, targetChannelId) => {
ws.send(JSON.stringify({ type: 'channel-merged', data: {channelId, targetChannelId }}));
});
events.on('channel-updated', (id, name) => {
ws.send(JSON.stringify({ type: 'channel-updated', data: {id, name }}));
});
}