File size: 6,345 Bytes
e758fa7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
module.exports = function ({ api }) {
    const Users = require("./users")({ api });
    const logger =require("../../utils/log.js");
    const { writeFileSync, readFileSync } = require("fs-extra");
    var path = __dirname + "/data/threadsData.json";

    try {
        var threadsData = require(path)
    } catch {
        writeFileSync(path, "{}", { flag: 'a+' });
    }

    async function getInfo(threadID) {
        try {
            const result = await api.getThreadInfo(threadID);
            return result;
        }
        catch (error) { 
            throw new Error(error);
            return false
        };
    }

    async function getData(threadID, callback) {
        try {
            if (!threadID) throw new Error("threadID cannot be empty");
            if (isNaN(threadID)) throw new Error("Invalid threadID");
            if (!threadsData.hasOwnProperty(threadID)) await createData(threadID);
            const data = threadsData[threadID];
            if (callback && typeof callback == "function") callback(null, data);
            return data;
        } catch (error) {
            if (callback && typeof callback == "function") callback(error, null);
            return false
        }
    }

    async function saveData(data) {
        try {
            if (!data) throw new Error('Data cannot be left blank');
            writeFileSync(path, JSON.stringify(data, null, 4))
        } catch (error) {
            return false
        }
    }
    async function getAll(keys, callback) {
        try {
            if (!keys) {
                if (Object.keys(threadsData).length == 0) return [];
                else if (Object.keys(threadsData).length > 0) {
                    var db = [];
                    for (var i of Object.keys(threadsData)) db.push(threadsData[i]);
                    return db;
                }
            }
            if (!Array.isArray(keys)) throw new Error("The input parameter must be an array");
            const data = [];
            for (var ID in threadsData) {
                const database = {
                    ID: ID
                };
                const threadData = threadsData[ID];
                for (var i of keys) database[i] = threadData[i];
                data.push(database);
            }
            if (callback && typeof callback == "function") callback(null, data);
            return data;
        } catch (error) {
            if (callback && typeof callback == "function") callback(error, null);
            return false
        }
    }
    async function setData(threadID, options, callback) {
        try {
            if (!threadID) throw new Error("threadID cannot be empty");
            if (isNaN(threadID)) throw new Error("Invalid threadID");
            if (!threadsData.hasOwnProperty(threadID)) throw new Error(`Threads with ID: ${threadID} does not exist in Database`);
            if (typeof options != 'object') throw new Error("The options parameter passed must be an object");
            threadsData[threadID] = {
                ...threadsData[threadID],
                ...options
            }
            await saveData(threadsData);
            if (callback && typeof callback == "function") callback(null, threadsData[threadID]);
            return threadsData[threadID];
        }
        catch(error) {
            if (callback && typeof callback == "function") callback(error, null);
            return false
        }
    }

    async function delData(threadID, callback) {
        try {
            if (!threadID) throw new Error("threadID cannot be empty");
            if (isNaN(threadID)) throw new Error("Invalid threadID");
            if (!threadsData.hasOwnProperty(threadID)) throw new Error(`Threads with ID: ${threadID} does not exist in Database`);
            delete threadsData[threadID];
            await saveData(threadsData);
            if (callback && typeof callback == "function") callback(null, "REMOVE THREAD"+ threadID + "SUCCESS");
            return true;
        } catch(error) {
            if (callback && typeof callback == "function") callback(error, null);
            return false
        }
    }

    async function createData(threadID, callback) {
        try {
            if (!threadID) throw new Error("threadID cannot be empty");
            if (isNaN(threadID)) throw new Error("Invalid threadID");
            if (threadsData.hasOwnProperty(threadID)) throw new Error(`Threads with ID: ${threadID} already exists in Database`);
            var threadInfo = await api.getThreadInfo(threadID);
            var data = {
                [threadID]: {
                    threadInfo: {
                        threadID: threadID,
                        threadName: threadInfo.threadName,
                        emoji: threadInfo.emoji,
                        adminIDs: threadInfo.adminIDs,
                        participantIDs: threadInfo.participantIDs,
                        isGroup: threadInfo.isGroup,
                    },
                    createTime: {
                        timestamp: Date.now()
                    },
                    data: {
                        timestamp: Date.now()
                    }
                }
            }
            Object.assign(threadsData, data);
            const dataUser = global.data.allUserID
                for (singleData of threadInfo.userInfo) {
                    if(singleData.gender != undefined) {
                        try {
                            if(dataUser.includes(singleData.id) || Users.hasOwnProperty(singleData.id)) continue
                            dataUser.push(singleData.id)
                            await Users.createData(singleData.id)
                            logger.log(global.getText('handleCreateDatabase', 'newUser', singleData.id), 'DATABASE');
                        } catch(e) { console.log(e) };
                    }
                }
            await saveData(threadsData)
            if (callback && typeof callback == "function") callback(null, data);
            return data;
        } catch (error) {
            if (callback && typeof callback == "function") callback(error, null);
            return false
        }
    }

    return {
        getInfo,
        getAll,
        getData,
        setData,
        delData,
        createData
    };
};