11const { MongoClient, ObjectID } = require ( "mongodb" ) ;
2+ var crypto = require ( 'crypto' ) ;
23const configs = require ( '../config/config' ) ;
34const utils = require ( '../utils' ) ;
45
@@ -12,16 +13,19 @@ const baseCode = [
1213
1314
1415class MongoDB {
15- constructor ( username , password , host , database , port ) {
16- let url = `mongodb://${ username } :${ password } @${ host } :${ port } /?retryWrites=true&w=majority` ;
17- this . client = new MongoClient ( url ) ;
16+ constructor ( url ) {
17+ this . client = new MongoClient ( url , {
18+ useNewUrlParser : true ,
19+ useUnifiedTopology : true
20+ } ) ;
1821 }
1922
2023 async connect ( ) {
2124 try {
2225 this . db = await this . client . connect ( ) ;
2326 this . codeWe = await this . db . db ( 'codewe' ) ;
2427 this . documentsCollection = await this . codeWe . collection ( 'codewe' ) ;
28+ this . usersCollection = await this . codeWe . collection ( 'users' ) ;
2529 } catch ( err ) {
2630 if ( configs . DEBUG ) {
2731 console . error ( 'Error with db connection' ) ;
@@ -30,29 +34,29 @@ class MongoDB {
3034 }
3135 }
3236
33- async createDocument ( ) {
37+ async createDocument ( language ) {
3438 let doc = {
3539 content : baseCode ,
3640 creationDate : Date . now ( ) ,
3741 lastViewedDate : Date . now ( ) ,
3842 customDocumentName : '' ,
3943 documentOwner : '' ,
4044 editors : [ ] ,
41- linkEdit : '' ,
45+ documentLink : '' ,
4246 linkView : '' ,
43- language : '' ,
47+ language : language ,
4448 tab : 4
4549 } ;
4650 try {
4751 let results = ( await this . documentsCollection . insertOne ( doc ) ) ;
4852 const documentLink = utils . uuid ( results . insertedId . toString ( ) ) ;
49- this . documentsCollection . updateOne ( { _id : results . insertedId } , { $set : { documentLink : documentLink } } )
53+ const linkView = utils . uuid ( documentLink ) ;
54+ this . documentsCollection . updateOne ( { _id : results . insertedId } , { $set : { documentLink : documentLink , linkView : linkView } } ) ;
5055 return documentLink ;
5156 } catch ( err ) {
5257 if ( configs . DEBUG ) {
5358 console . error ( 'Error when creating a new document' ) ;
5459 }
55- throw new Error ( err ) ;
5660 }
5761
5862 }
@@ -64,94 +68,171 @@ class MongoDB {
6468 if ( configs . DEBUG ) {
6569 console . error ( 'Error when fetching document' ) ;
6670 }
67- throw new Error ( err ) ;
71+ }
72+ }
73+
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' ;
6897 }
6998 }
7099
71100 async setLine ( documentLink , uuid , content ) {
72101 try {
73- await this . documentsCollection . updateOne ( { documentLink : documentLink , 'content.uuid' : uuid } , { $set : { 'content.$.content' : content } } ) ;
102+ 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' ) ;
77107 }
78- throw new Error ( err ) ;
79108 }
80109 }
81110
82111 async newLine ( documentLink , previousUuid , uuid , content ) {
83112 // Insert a line at the right place
84- //TODO is it possible in one operation ?
113+ //TODO is it possible in one operation ?
85114 // TODO is it possible to implement with bulk?
86115 try {
87116 let doc = await this . documentsCollection . findOne ( { documentLink : documentLink } ) ;
88117 let index = doc . content . findIndex ( line => {
89118 return line . uuid == previousUuid ;
90119 } ) ;
91- this . documentsCollection . updateOne ( { documentLink : documentLink } , {
92- $push : {
93- content : {
94- $each : [ { uuid : uuid , content : content } ] ,
95- $position : index + 1
120+ if ( index ) {
121+ this . documentsCollection . updateOne ( { documentLink : documentLink } , {
122+ $push : {
123+ content : {
124+ $each : [ { uuid : uuid , content : content . slice ( 0 , 5000 ) } ] ,
125+ $position : index + 1
126+ }
96127 }
97- }
98- } ) ;
128+ } ) ;
129+ }
130+ return 'Succes' ;
99131 } catch ( err ) {
100132 if ( configs . DEBUG ) {
101133 console . error ( 'Error when adding a new line to document' ) ;
102134 }
103- throw new Error ( err ) ;
104135 }
105136 }
106137
107138 async deleteLine ( documentLink , uuid ) {
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' ) ;
114146 }
115- throw new Error ( err ) ;
116147 }
117148
118149 }
119150
151+ async changeParam ( documentLink , param , newValue ) {
152+ try {
153+ const update = { } ;
154+ update [ param ] = newValue ;
155+ await this . documentsCollection . updateOne ( { documentLink : documentLink } , { $set : update } ) ;
156+ return 'Succes' ;
157+ } catch ( err ) {
158+ if ( configs . DEBUG ) {
159+ console . error ( err ) ;
160+ }
161+ }
162+ }
163+
164+ async changeCustomName ( documentLink , newName ) {
165+ return this . changeParam ( documentLink , 'customDocumentName' , newName ) ;
166+ }
167+
168+ async changeTabSize ( documentLink , newTabSize ) {
169+ if ( Number . isInteger ( newTabSize ) ) {
170+ return this . changeParam ( documentLink , 'tab' , newTabSize ) ;
171+ }
172+ }
173+
174+ async changeLanguage ( documentLink , newLanguage ) {
175+ if ( [ "python" ] . includes ( newLanguage ) ) {
176+ return this . changeParam ( documentLink , 'language' , newLanguage ) ;
177+ }
178+ }
179+
180+ async addNewEditors ( documentLink , newEditorsId ) {
181+ try {
182+ await this . documentsCollection . updateOne ( { documentLink : documentLink } , { $addToSet : { editors : newEditorsId } } ) ;
183+ return 'Success' ;
184+ } catch ( err ) {
185+ if ( configs . DEBUG ) {
186+ console . error ( error ) ;
187+ }
188+ }
189+ }
190+
191+ async updateLastViewedDate ( documentLink ) {
192+ return this . changeParam ( documentLink , 'lastViewedDate' , Date . now ( ) ) ;
193+ }
194+
195+ async deleteOldDocuments ( days ) {
196+ const oldTimestamp = Date . now ( ) - 1000 * 60 * 60 * 24 * days ;
197+ return this . documentsCollection . deleteMany ( { 'lastViewedDate' : { $lt : oldTimestamp } } ) ;
198+ }
199+
120200 async applyRequests ( documentLink , requests ) {
121201 // TODO look to use bulk write
202+ let success = true ;
122203 try {
204+ // Avoid too many requests
205+ requests = requests . slice ( 0 , 50 ) ;
123206 for ( let request of requests ) {
124207 let requestType = request . type ;
125208 let data = request . data ;
209+ let results = ""
126210 switch ( requestType ) {
127211 case 'set-line' :
128- await this . setLine ( documentLink , data . id , data . content ) ;
212+ results = await this . setLine ( documentLink , data . id , data . content ) ;
213+ if ( ! results ) success = false ;
129214 break ;
130215 case 'new-line' :
131- await this . newLine ( documentLink , data . previous , data . id , data . content ) ;
216+ results = await this . newLine ( documentLink , data . previous , data . id , data . content ) ;
217+ if ( ! results ) success = false ;
132218 break ;
133219 case 'delete-line' :
134- await this . deleteLine ( documentLink , data . id ) ;
220+ results = await this . deleteLine ( documentLink , data . id ) ;
221+ if ( ! results ) success = false ;
135222 break ;
136223 }
137224 }
225+ return success ;
138226 } catch ( err ) {
139227 if ( configs . DEBUG ) {
140228 console . error ( 'Error when applying requests' ) ;
141229 }
142- throw new Error ( err ) ;
143230 }
144231 }
145232}
146233
147234function getDB ( ) {
148- db = new MongoDB (
149- configs . DB_CONFIG . DB_USERNAME ,
150- configs . DB_CONFIG . DB_PASSWORD ,
151- configs . DB_CONFIG . DB_HOST ,
152- configs . DB_CONFIG . DB_DATABASE ,
153- configs . DB_CONFIG . DB_PORT
154- ) ;
235+ db = new MongoDB ( configs . DB_URL ) ;
155236 db . connect ( ) ;
156237 return db ;
157238}
0 commit comments