11const { MongoClient, ObjectID } = require ( "mongodb" ) ;
2+ var crypto = require ( 'crypto' ) ;
23const configs = require ( '../config/config' ) ;
34const utils = require ( '../utils' ) ;
45
@@ -24,14 +25,16 @@ class MongoDB {
2425 this . db = await this . client . connect ( ) ;
2526 this . codeWe = await this . db . db ( 'codewe' ) ;
2627 this . documentsCollection = await this . codeWe . collection ( 'codewe' ) ;
28+ this . usersCollection = await this . codeWe . collection ( 'users' ) ;
2729 } catch ( err ) {
2830 if ( configs . DEBUG ) {
2931 console . error ( 'Error with db connection' ) ;
3032 }
33+ throw new Error ( err ) ;
3134 }
3235 }
3336
34- async createDocument ( ) {
37+ async createDocument ( language ) {
3538 let doc = {
3639 content : baseCode ,
3740 creationDate : Date . now ( ) ,
@@ -41,7 +44,7 @@ class MongoDB {
4144 editors : [ ] ,
4245 documentLink : '' ,
4346 linkView : '' ,
44- language : 'python' ,
47+ language : language ,
4548 tab : 4
4649 } ;
4750 try {
@@ -68,9 +71,36 @@ class MongoDB {
6871 }
6972 }
7073
74+ async createUser ( userId , secretToken ) {
75+ try {
76+ await this . usersCollection . insertOne ( {
77+ userId : userId ,
78+ secretToken : crypto . createHash ( 'sha256' ) . update ( secretToken ) . digest ( 'base64' )
79+ } ) ;
80+ return 'Success' ;
81+ } catch ( err ) {
82+ if ( configs . DEBUG ) {
83+ console . error ( 'Error when creating user' ) ;
84+ }
85+ }
86+ }
87+
88+ async checkUserSecretToken ( userId , secretToken ) {
89+ try {
90+ const user = await this . usersCollection . findOne ( { userId : userId } ) ;
91+ return ( user . secretToken == crypto . createHash ( 'sha256' ) . update ( secretToken ) . digest ( 'base64' ) ) ;
92+ } catch ( err ) {
93+ if ( configs . DEBUG ) {
94+ console . error ( 'Error when checking user secret token' ) ;
95+ }
96+ return 'Error' ;
97+ }
98+ }
99+
71100 async setLine ( documentLink , uuid , content ) {
72101 try {
73102 await this . documentsCollection . updateOne ( { documentLink : documentLink , 'content.uuid' : uuid } , { $set : { 'content.$.content' : content . slice ( 0 , 5000 ) } } ) ;
103+ return 'Succes' ;
74104 } catch ( err ) {
75105 if ( configs . DEBUG ) {
76106 console . error ( 'Error when changing line content' ) ;
@@ -97,6 +127,7 @@ class MongoDB {
97127 }
98128 } ) ;
99129 }
130+ return 'Succes' ;
100131 } catch ( err ) {
101132 if ( configs . DEBUG ) {
102133 console . error ( 'Error when adding a new line to document' ) ;
@@ -108,6 +139,7 @@ class MongoDB {
108139 try {
109140 // Delete line at the right place
110141 await this . documentsCollection . updateOne ( { documentLink : documentLink } , { $pull : { content : { uuid : uuid } } } ) ;
142+ return 'Succes' ;
111143 } catch ( err ) {
112144 if ( configs . DEBUG ) {
113145 console . error ( 'Error when deleting a line in document' ) ;
@@ -121,6 +153,7 @@ class MongoDB {
121153 const update = { } ;
122154 update [ param ] = newValue ;
123155 await this . documentsCollection . updateOne ( { documentLink : documentLink } , { $set : update } ) ;
156+ return 'Succes' ;
124157 } catch ( err ) {
125158 if ( configs . DEBUG ) {
126159 console . error ( err ) ;
@@ -129,24 +162,25 @@ class MongoDB {
129162 }
130163
131164 async changeCustomName ( documentLink , newName ) {
132- this . changeParam ( documentLink , 'customDocumentName' , newName ) ;
165+ return this . changeParam ( documentLink , 'customDocumentName' , newName ) ;
133166 }
134167
135168 async changeTabSize ( documentLink , newTabSize ) {
136169 if ( Number . isInteger ( newTabSize ) ) {
137- this . changeParam ( documentLink , 'tab' , newTabSize ) ;
170+ return this . changeParam ( documentLink , 'tab' , newTabSize ) ;
138171 }
139172 }
140173
141174 async changeLanguage ( documentLink , newLanguage ) {
142175 if ( [ "python" ] . includes ( newLanguage ) ) {
143- this . changeParam ( documentLink , 'language' , newLanguage ) ;
176+ return this . changeParam ( documentLink , 'language' , newLanguage ) ;
144177 }
145178 }
146179
147180 async addNewEditors ( documentLink , newEditorsId ) {
148181 try {
149182 await this . documentsCollection . updateOne ( { documentLink : documentLink } , { $addToSet : { editors : newEditorsId } } ) ;
183+ return 'Success' ;
150184 } catch ( err ) {
151185 if ( configs . DEBUG ) {
152186 console . error ( error ) ;
@@ -155,16 +189,17 @@ class MongoDB {
155189 }
156190
157191 async updateLastViewedDate ( documentLink ) {
158- this . changeParam ( documentLink , 'lastViewedDate' , Date . now ( ) ) ;
192+ return this . changeParam ( documentLink , 'lastViewedDate' , Date . now ( ) ) ;
159193 }
160194
161195 async deleteOldDocuments ( days ) {
162196 const oldTimestamp = Date . now ( ) - 1000 * 60 * 60 * 24 * days ;
163- this . documentsCollection . deleteMany ( { 'lastViewedDate' : { $lt : oldTimestamp } } ) ;
197+ return this . documentsCollection . deleteMany ( { 'lastViewedDate' : { $lt : oldTimestamp } } ) ;
164198 }
165199
166200 async applyRequests ( documentLink , requests ) {
167201 // TODO look to use bulk write
202+ let success = true ;
168203 try {
169204 // Avoid too many requests
170205 requests = requests . slice ( 0 , 50 ) ;
@@ -173,16 +208,20 @@ class MongoDB {
173208 let data = request . data ;
174209 switch ( requestType ) {
175210 case 'set-line' :
176- await this . setLine ( documentLink , data . id , data . content ) ;
211+ let results = await this . setLine ( documentLink , data . id , data . content ) ;
212+ if ( ! results ) success = false ;
177213 break ;
178214 case 'new-line' :
179- await this . newLine ( documentLink , data . previous , data . id , data . content ) ;
215+ let results = await this . newLine ( documentLink , data . previous , data . id , data . content ) ;
216+ if ( ! results ) success = false ;
180217 break ;
181218 case 'delete-line' :
182- await this . deleteLine ( documentLink , data . id ) ;
219+ let results = await this . deleteLine ( documentLink , data . id ) ;
220+ if ( ! results ) success = false ;
183221 break ;
184222 }
185223 }
224+ return success ;
186225 } catch ( err ) {
187226 if ( configs . DEBUG ) {
188227 console . error ( 'Error when applying requests' ) ;
0 commit comments