@@ -8,16 +8,38 @@ const Joi = require("joi");
8
8
*/
9
9
const chatSchema = Joi . object ( {
10
10
chatId : Joi . number ( ) . required ( ) ,
11
- username : Joi . string ( ) . default ( null ) . optional ( ) ,
12
- date : Joi . number ( ) . default ( null ) . optional ( ) ,
13
- title : Joi . string ( ) . default ( null ) . optional ( ) ,
14
- description : Joi . string ( ) . default ( null ) . optional ( ) ,
15
- member_count : Joi . number ( ) . min ( 0 ) . default ( 0 ) . optional ( ) ,
16
- is_channel : Joi . number ( ) . min ( 0 ) . max ( 1 ) . default ( 0 ) . optional ( ) ,
17
- is_verified : Joi . number ( ) . min ( 0 ) . max ( 1 ) . default ( 0 ) . optional ( ) ,
18
- is_scam : Joi . number ( ) . min ( 0 ) . max ( 1 ) . default ( 0 ) . optional ( ) ,
19
- last_updated : Joi . number ( ) . default ( - 1 ) . optional ( ) , // -1 = current time
20
- monitor : Joi . number ( ) . min ( 0 ) . max ( 1 ) . default ( 0 ) . optional ( ) ,
11
+ username : Joi . string ( ) . default ( null ) . allow ( "" ) . allow ( null ) . optional ( ) ,
12
+ date : Joi . number ( ) . default ( 0 ) . allow ( "" ) . allow ( null ) . optional ( ) ,
13
+ title : Joi . string ( ) . default ( null ) . allow ( "" ) . allow ( null ) . optional ( ) ,
14
+ description : Joi . string ( ) . default ( null ) . allow ( "" ) . allow ( null ) . optional ( ) ,
15
+ member_count : Joi . number ( ) . min ( 0 ) . default ( 0 ) . allow ( "" ) . allow ( null ) . optional ( ) ,
16
+ type : Joi . string ( )
17
+ . default ( "unknown" )
18
+ . allow ( "" )
19
+ . allow ( null )
20
+ . optional ( ) ,
21
+ is_verified : Joi . number ( )
22
+ . min ( 0 )
23
+ . max ( 1 )
24
+ . default ( 0 )
25
+ . allow ( "" )
26
+ . allow ( null )
27
+ . optional ( ) ,
28
+ is_scam : Joi . number ( )
29
+ . min ( 0 )
30
+ . max ( 1 )
31
+ . default ( 0 )
32
+ . allow ( "" )
33
+ . allow ( null )
34
+ . optional ( ) ,
35
+ last_updated : Joi . number ( ) . default ( - 1 ) . allow ( "" ) . allow ( null ) . optional ( ) , // -1 = current time
36
+ monitor : Joi . number ( )
37
+ . min ( 0 )
38
+ . max ( 1 )
39
+ . default ( 0 )
40
+ . allow ( "" )
41
+ . allow ( null )
42
+ . optional ( ) ,
21
43
} ) ;
22
44
23
45
/*
@@ -30,19 +52,146 @@ module.exports.getChats = async (limit = 200, offset = 0) => {
30
52
31
53
// POST /chats
32
54
module . exports . addChat = async ( chat ) => {
33
- return dbController . add ( "chats" , chatSchema , chat , [
34
- "chatId" ,
35
- "username" ,
36
- "date" ,
37
- "title" ,
38
- "description" ,
39
- "member_count" ,
40
- "is_channel" ,
41
- "is_verified" ,
42
- "is_scam" ,
43
- "last_updated" ,
44
- "monitor" ,
45
- ] ) ;
55
+ // validate user schema
56
+ const { error, value } = chatSchema . validate ( chat ) ;
57
+ if ( error ) {
58
+ return {
59
+ error : true ,
60
+ message : error . details [ 0 ] . message ,
61
+ } ;
62
+ }
63
+
64
+ // get chat id and check
65
+ const chatId = parseInt ( value . chatId ) ;
66
+
67
+ // current date in milliseconds
68
+ const date = Date . now ( ) ;
69
+
70
+ // get a connection from the database
71
+ const connection = await dbController . pool . getConnection ( ) ;
72
+
73
+ try {
74
+ // get existing chats if any exists
75
+ const rows = await connection . query (
76
+ "SELECT * FROM chats WHERE chatId = ? LIMIT 1;" ,
77
+ [ chatId ]
78
+ ) ;
79
+
80
+ // there is an chat existing with the same chatId
81
+ if ( rows . length >= 1 ) {
82
+ const oldChat = rows [ 0 ] ;
83
+
84
+ // check if we monitor this channel, if not, return with an error
85
+ if ( oldChat . monitor == 0 ) {
86
+ return {
87
+ error : true ,
88
+ message : "Not monitoring" ,
89
+ } ;
90
+ }
91
+
92
+ // we'll compare the following mysql & object keys
93
+ const fields = [
94
+ "username" ,
95
+ "date" ,
96
+ "title" ,
97
+ "description" ,
98
+ "member_count" ,
99
+ "type" ,
100
+ "is_verified" ,
101
+ "is_scam" ,
102
+ "last_updated" ,
103
+ ] ;
104
+
105
+ let update = {
106
+ query : "" ,
107
+ params : [ ] ,
108
+ } ;
109
+
110
+ for ( let i = 0 ; i < fields . length ; i ++ ) {
111
+ const field = fields [ i ] ;
112
+
113
+ if ( ! ( field in chat ) ) {
114
+ continue ;
115
+ }
116
+
117
+ const _old = oldChat [ field ] ;
118
+ const _new = value [ field ] ;
119
+
120
+ if ( _old != _new ) {
121
+ console . log (
122
+ "[Chat Update | " +
123
+ chatId +
124
+ "] Updating field " +
125
+ field +
126
+ " from " +
127
+ _old +
128
+ " to " +
129
+ _new
130
+ ) ;
131
+
132
+ update . query +=
133
+ ( update . query . length == 0 ? "" : ", " ) + field + " = ?" ;
134
+ update . params . push ( _new ) ;
135
+
136
+ // add to updates
137
+ await connection . query (
138
+ "INSERT INTO chats_updates (`chatId`, `key`, `old_value`, `new_value`, `date`) VALUES (?, ?, ?, ?, ?);" ,
139
+ [ chatId , field , _old , _new , date ]
140
+ ) ;
141
+ }
142
+ }
143
+
144
+ // update ?!
145
+ if ( update . query . length > 0 ) {
146
+ update . query += ", last_updated = ?" ;
147
+ update . params . push ( date ) ;
148
+
149
+ update . params . push ( chatId ) ; // this is used for the where clause and should be here.
150
+
151
+ console . log ( "[Chat Update | " + chatId + "] Updated chat" ) ;
152
+
153
+ // update chat
154
+ return await connection . query (
155
+ "UPDATE chats SET " + update . query + " WHERE chatId = ?;" ,
156
+ update . params
157
+ ) ;
158
+ } else {
159
+ console . log ( "[Chat Update | " + chatId + "] No need to update" ) ;
160
+ }
161
+ } else {
162
+ // insert new chat
163
+ return await connection . query (
164
+ "INSERT INTO chats VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" ,
165
+ [
166
+ value . chatId ,
167
+ value . username ,
168
+ value . date ,
169
+ value . title ,
170
+ value . description ,
171
+ value . member_count ,
172
+ value . type ,
173
+ value . is_verified ,
174
+ value . is_scam ,
175
+ value . last_updated ,
176
+ 1 ,
177
+ ]
178
+ ) ;
179
+ }
180
+
181
+ return {
182
+ error : false ,
183
+ message : "Nothing updated." ,
184
+ } ;
185
+ } catch ( exception ) {
186
+ return {
187
+ error : true ,
188
+ message : exception . code ,
189
+ } ;
190
+ } finally {
191
+ if ( connection ) {
192
+ connection . end ( ) ;
193
+ }
194
+ }
46
195
} ;
47
196
48
197
// GET /chats/:id
@@ -63,4 +212,29 @@ module.exports.updateChat = async (id, chat) => {
63
212
"is_scam" ,
64
213
"monitor" ,
65
214
] ) ;
66
- } ;
215
+ } ;
216
+
217
+ module . exports . updateMemberCount = async ( id , count ) => {
218
+ // get a connection from the database
219
+ const connection = await dbController . pool . getConnection ( ) ;
220
+
221
+ try {
222
+ const chatId = parseInt ( id ) ;
223
+ const memberCount = parseInt ( count ) ;
224
+ const date = Date . now ( ) ;
225
+
226
+ return await connection . query (
227
+ "INSERT INTO chats_online_member_count VALUES (?, ?, ?);" ,
228
+ [ chatId , date , memberCount ]
229
+ ) ;
230
+ } catch ( exception ) {
231
+ return {
232
+ error : true ,
233
+ message : exception . code ,
234
+ } ;
235
+ } finally {
236
+ if ( connection ) {
237
+ connection . end ( ) ;
238
+ }
239
+ }
240
+ } ;
0 commit comments