File size: 5,031 Bytes
c13f601
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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"))