improved attachments preview
This commit is contained in:
@@ -72,7 +72,7 @@ async function createTables() {
|
||||
conversation_id INT REFERENCES Conversations(conversation_id) ON DELETE CASCADE,
|
||||
user_id INT REFERENCES Accounts(user_id) ON DELETE CASCADE,
|
||||
content TEXT NOT NULL,
|
||||
attachment_url TEXT,
|
||||
attachment_urls TEXT[],
|
||||
sent_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_messages_conversation_id ON Messages (conversation_id);
|
||||
@@ -156,16 +156,16 @@ async function insertMessage(
|
||||
senderId,
|
||||
conversation_id,
|
||||
content,
|
||||
attachmentUrl,
|
||||
attachmentUrls,
|
||||
) {
|
||||
console.log(
|
||||
`senderId: ${senderId}, conversation_id: ${conversation_id}, content: ${content}, attachmentUrl: ${attachmentUrl}`,
|
||||
`senderId: ${senderId}, conversation_id: ${conversation_id}, content: ${content}, attachmentUrl: ${attachmentUrls}`,
|
||||
);
|
||||
|
||||
const query = `
|
||||
INSERT INTO Messages (conversation_id, user_id, content, attachment_url)
|
||||
INSERT INTO Messages (conversation_id, user_id, content, attachment_urls)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING message_id, content, sent_at, attachment_url, user_id AS sender_id, conversation_id;
|
||||
RETURNING message_id, content, sent_at, attachment_urls, user_id AS sender_id, conversation_id;
|
||||
`;
|
||||
|
||||
try {
|
||||
@@ -173,8 +173,9 @@ async function insertMessage(
|
||||
conversation_id,
|
||||
senderId,
|
||||
content,
|
||||
attachmentUrl,
|
||||
attachmentUrls,
|
||||
]);
|
||||
console.log("insertmessageresult: ", result.rows);
|
||||
return result.rows[0];
|
||||
} catch (e) {
|
||||
console.error("Failed to insert message ", e);
|
||||
@@ -279,7 +280,7 @@ async function getMessages(user_id, conversation_id, limit = 50, cursor = 0) {
|
||||
m.message_id,
|
||||
m.content AS message,
|
||||
m.sent_at,
|
||||
m.attachment_url,
|
||||
m.attachment_urls,
|
||||
a.username AS sender
|
||||
FROM Messages m
|
||||
JOIN Accounts a ON m.user_id = a.user_id
|
||||
@@ -295,7 +296,7 @@ async function getMessages(user_id, conversation_id, limit = 50, cursor = 0) {
|
||||
m.message_id,
|
||||
m.content AS message,
|
||||
m.sent_at,
|
||||
m.attachment_url,
|
||||
m.attachment_urls,
|
||||
a.username AS sender
|
||||
FROM Messages m
|
||||
JOIN Accounts a ON m.user_id = a.user_id
|
||||
@@ -710,14 +711,11 @@ async function deleteMessage(user_id, message_id) {
|
||||
const deleteResult = await client.query(deleteMessageQuery, [message_id]);
|
||||
if (deleteResult.rowCount > 0) {
|
||||
console.log("Message deleted successfully");
|
||||
return { message: "Message deleted successfully" };
|
||||
} else {
|
||||
console.log("Failed to delete message");
|
||||
return { message: "Failed to delete message." };
|
||||
}
|
||||
} catch (e) {
|
||||
console.error("Failed to delete message ", e);
|
||||
return { message: "Failed to delete message" };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -255,6 +255,9 @@ app.post("/api/chat/contact/:contact", authorizeUser, async (req, res) => {
|
||||
usernameContact,
|
||||
true,
|
||||
);
|
||||
if (result.conversation_id === null) {
|
||||
res.status(500).json({ message: "Failed to create conversation" });
|
||||
}
|
||||
return res.status(200).json(result);
|
||||
} catch (e) {
|
||||
console.error("Failed to insert contact: ", e);
|
||||
@@ -317,19 +320,17 @@ app.get("/api/chat/messages/:contact", authorizeUser, async (req, res) => {
|
||||
});
|
||||
|
||||
app.post(
|
||||
"/api/chat/attachment",
|
||||
upload.single("attachment"),
|
||||
"/api/chat/attachments",
|
||||
upload.any("attachments"),
|
||||
authorizeUser,
|
||||
async (req, res) => {
|
||||
if (!req.file) {
|
||||
if (req.files?.length < 1) {
|
||||
return res.status(400).json({ message: "No file specified" });
|
||||
}
|
||||
const { finalName } = req.file;
|
||||
const url = `${process.env.ORIGIN}/attachments/${finalName}`;
|
||||
res.json({
|
||||
message: "File uploaded successfully",
|
||||
attachment_url: url,
|
||||
const attachment_urls = req.files.map((file) => {
|
||||
return `${process.env.ORIGIN}/attachments/${file.finalName}`;
|
||||
});
|
||||
res.json(attachment_urls);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -376,8 +377,8 @@ app.delete(
|
||||
}
|
||||
|
||||
const result = await deleteMessage(req.user.user_id, message_id);
|
||||
if (result.message) {
|
||||
return res.status(200).json({ message: result.message });
|
||||
if (result?.message) {
|
||||
return res.status(401).json({ message: result.message });
|
||||
} else {
|
||||
return res.status(200).json({ message: "Successfully deleted message" });
|
||||
}
|
||||
|
||||
@@ -64,13 +64,20 @@ function initializeSocket(io) {
|
||||
|
||||
try {
|
||||
const conversations = await getConversationsForUser(socket.user_id);
|
||||
conversations.push(socket.user_id);
|
||||
console.log("join conversations: ", conversations);
|
||||
socket.join(conversations);
|
||||
} catch (e) {
|
||||
console.error("(socket) Failed to get user conversations");
|
||||
}
|
||||
|
||||
socket.on("join socket", (msg) => {
|
||||
socket.join(msg.conversation_id);
|
||||
console.log("joinsocket: ", msg.conversation_id);
|
||||
});
|
||||
|
||||
socket.on("chat message", async (msg, callback) => {
|
||||
const { message, recipient, attachment_url } = msg;
|
||||
const { message, recipient, recipient_id, attachment_url } = msg;
|
||||
const sender = socket.username;
|
||||
|
||||
if (!message && !attachment_url) {
|
||||
@@ -116,7 +123,7 @@ function initializeSocket(io) {
|
||||
} = insertedMessage;
|
||||
console.log("(socket) received from chat message", msg);
|
||||
|
||||
io.to(username).to(recipient).emit("chat message", {
|
||||
io.to(username).to(recipient).to(recipient_id).emit("chat message", {
|
||||
sender,
|
||||
message: content,
|
||||
attachment_url,
|
||||
|
||||
Reference in New Issue
Block a user