Spaces:
Runtime error
Runtime error
| import { lucia } from "./auth.js"; | |
| import { generateId, Lucia } from "lucia"; | |
| import bcrypt from "bcrypt"; | |
| import { Database } from "better-sqlite3"; | |
| import { getDB } from "./db.js"; | |
| var db: Database; | |
| db = getDB(); | |
| type MessageType = { | |
| id: string, | |
| content: string, | |
| type: string, | |
| timestamp: number, | |
| sender_id: string, | |
| receiver_id: string, | |
| status: "sent" | "received" | "seen", | |
| }; | |
| type ContactType = { | |
| id: string, | |
| name: string, | |
| chat_data: { | |
| last_message: MessageType, | |
| unseen_count: number, | |
| } | |
| }; | |
| export function searchContacts(user_id: string, search_string: string) { | |
| var contacts = db.prepare(`SELECT id,name FROM users WHERE id LIKE ? AND id!=?`) | |
| .all("%" + search_string + "%",user_id); | |
| // console.log(contacts); | |
| contacts = contacts.map((contact: any) => { | |
| var last_msg: any = getLastMessage(user_id, contact.id); | |
| // console.log(user_id, contact.id, last_msg); | |
| var chat_data: any = {}; | |
| if (last_msg.id) { | |
| chat_data.last_message = last_msg; | |
| chat_data.unseen_count = getUnseenCount(user_id, contact.id); | |
| } | |
| return { ...contact, chat_data } | |
| }) | |
| return contacts as ContactType[]; | |
| } | |
| export function getContact(user_id: string, other_contact_id: string) { | |
| var contact: any = {}; | |
| var user: any = db.prepare(`SELECT id,name from users | |
| WHERE id=? | |
| `).get(other_contact_id); | |
| contact.id = user.id; | |
| contact.name = user.name; | |
| contact.chat_data = { | |
| last_message: getLastMessage(user_id, other_contact_id), | |
| unseen_count: getUnseenCount(user_id, other_contact_id), | |
| } | |
| return contact as ContactType; | |
| } | |
| export const getMyChats = (user_id: string) => { | |
| // var contacts = db.prepare("SELECT send_to,send_from FROM messages WHERE send_to=? or send_from=?").all(user_id,user_id); | |
| var chat_ids: any = db.prepare(` | |
| SELECT Distinct(sender_id) as id FROM messages WHERE receiver_id=? | |
| UNION | |
| SELECT Distinct(receiver_id) as id FROM messages WHERE sender_id=? | |
| `).all(user_id, user_id); | |
| // get data of all | |
| var contacts: any = []; | |
| for (var i = 0; i < chat_ids.length; i++) { | |
| contacts.push(getContact(user_id, chat_ids[i].id)); | |
| } | |
| return contacts as ContactType[]; | |
| }; | |
| export function getLastMessage(user_id: string, other_contact_id: string) { | |
| const msg_stmt = db.prepare(`SELECT id,content,type,MAX(timestamp) as timestamp,sender_id,receiver_id,status | |
| FROM messages | |
| WHERE sender_id in (?,?) AND receiver_id in (?,?)`) | |
| const last_message = msg_stmt.get(user_id, other_contact_id, user_id, other_contact_id); | |
| return last_message as MessageType; | |
| } | |
| export function getUnseenCount(user_id: string, other_contact_id: string) { | |
| const stmt = db.prepare(`SELECT COUNT(id) as unseen_count FROM messages | |
| WHERE sender_id=? AND receiver_id=? AND status!='seen' | |
| `) | |
| const unseen_count: any = stmt.get(other_contact_id, user_id); | |
| return unseen_count.unseen_count; | |
| } | |
| export function addMessage({ | |
| content, | |
| type = "text", | |
| sender_id, | |
| receiver_id, | |
| }: { | |
| content: string, | |
| type: string, | |
| sender_id: string, | |
| receiver_id: string, | |
| }) { | |
| type = type.toLowerCase(); | |
| const id = generateId(10); | |
| const stmt = db.prepare(`INSERT INTO messages(id,content,type,sender_id,receiver_id) VALUES(?,?,?,?,?)`); | |
| stmt.run( | |
| id, | |
| content, | |
| type, | |
| sender_id, | |
| receiver_id | |
| ); | |
| return db.prepare(`SELECT * FROM messages WHERE id=?`).get(id) as MessageType; | |
| } | |
| export function markMessageSeen(user_id: string, msg_id: string) { | |
| const stmt = db.prepare(`UPDATE messages SET status='seen' WHERE sender_id!=? AND id=?`); | |
| stmt.run(user_id, msg_id); | |
| } | |
| export function getAllMessages(user_id: string, other_contact_id: string) { | |
| const stmt = db.prepare(`SELECT * from messages | |
| WHERE sender_id in (?,?) AND receiver_id in (?,?) | |
| ORDER BY messages.timestamp DESC | |
| `); | |
| const msgs = stmt.all(user_id, other_contact_id, user_id, other_contact_id); | |
| return msgs as MessageType[]; | |
| } | |
| export function deleteChat(user_id: string, other_contact_id: string) { | |
| db.prepare(`DELETE FROM message WHERE sender_id in (?,?) AND receiver_id in (?,?) `) | |
| .run(user_id, other_contact_id, user_id, other_contact_id); | |
| return true; | |
| } | |
| // console.log(searchContacts("user1","u")) | |
| // console.log(getMyChats("user1")) | |
| // console.log(getMyChats("user2")) | |
| // console.log(getMyChats("user3")) | |
| // console.log(getLastMessage("user1","user1")) | |
| // console.log(getUnseenCount("user1", "user2")) | |
| // // console.log(addMessage({ | |
| // // message: "new_msg", | |
| // // type: "text", | |
| // // send_from: "user3", | |
| // // send_to: "wx6uv", | |
| // // })) | |
| // console.log(getUnseenCount("user1", "wx6uv")) | |
| // // markMessageSeen("user1", "33w87"); | |
| // console.log(getAllMessages("user1", "wx6uv")) | |
| // console.log(deleteChat('user2',"sblua")) |