@@ -12,9 +12,11 @@ const baseCode = [
1212
1313
1414class 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 ) ;
15+ constructor ( url ) {
16+ this . client = new MongoClient ( url , {
17+ useNewUrlParser : true ,
18+ useUnifiedTopology : true
19+ } ) ;
1820 }
1921
2022 async connect ( ) {
@@ -26,7 +28,6 @@ class MongoDB {
2628 if ( configs . DEBUG ) {
2729 console . error ( 'Error with db connection' ) ;
2830 }
29- throw new Error ( err ) ;
3031 }
3132 }
3233
@@ -38,21 +39,21 @@ class MongoDB {
3839 customDocumentName : '' ,
3940 documentOwner : '' ,
4041 editors : [ ] ,
41- linkEdit : '' ,
42+ documentLink : '' ,
4243 linkView : '' ,
43- language : '' ,
44+ language : 'python ' ,
4445 tab : 4
4546 } ;
4647 try {
4748 let results = ( await this . documentsCollection . insertOne ( doc ) ) ;
4849 const documentLink = utils . uuid ( results . insertedId . toString ( ) ) ;
49- this . documentsCollection . updateOne ( { _id : results . insertedId } , { $set : { documentLink : documentLink } } )
50+ const linkView = utils . uuid ( documentLink ) ;
51+ this . documentsCollection . updateOne ( { _id : results . insertedId } , { $set : { documentLink : documentLink , linkView : linkView } } ) ;
5052 return documentLink ;
5153 } catch ( err ) {
5254 if ( configs . DEBUG ) {
5355 console . error ( 'Error when creating a new document' ) ;
5456 }
55- throw new Error ( err ) ;
5657 }
5758
5859 }
@@ -64,18 +65,16 @@ class MongoDB {
6465 if ( configs . DEBUG ) {
6566 console . error ( 'Error when fetching document' ) ;
6667 }
67- throw new Error ( err ) ;
6868 }
6969 }
7070
7171 async setLine ( documentLink , uuid , content ) {
7272 try {
73- await this . documentsCollection . updateOne ( { documentLink : documentLink , 'content.uuid' : uuid } , { $set : { 'content.$.content' : content } } ) ;
73+ await this . documentsCollection . updateOne ( { documentLink : documentLink , 'content.uuid' : uuid } , { $set : { 'content.$.content' : content . slice ( 0 , 5000 ) } } ) ;
7474 } catch ( err ) {
7575 if ( configs . DEBUG ) {
7676 console . error ( 'Error when changing line content' ) ;
7777 }
78- throw new Error ( err ) ;
7978 }
8079 }
8180
@@ -88,19 +87,20 @@ class MongoDB {
8887 let index = doc . content . findIndex ( line => {
8988 return line . uuid == previousUuid ;
9089 } ) ;
91- this . documentsCollection . updateOne ( { documentLink : documentLink } , {
92- $push : {
93- content : {
94- $each : [ { uuid : uuid , content : content } ] ,
95- $position : index + 1
90+ if ( index ) {
91+ this . documentsCollection . updateOne ( { documentLink : documentLink } , {
92+ $push : {
93+ content : {
94+ $each : [ { uuid : uuid , content : content . slice ( 0 , 5000 ) } ] ,
95+ $position : index + 1
96+ }
9697 }
97- }
98- } ) ;
98+ } ) ;
99+ }
99100 } catch ( err ) {
100101 if ( configs . DEBUG ) {
101102 console . error ( 'Error when adding a new line to document' ) ;
102103 }
103- throw new Error ( err ) ;
104104 }
105105 }
106106
@@ -112,14 +112,57 @@ class MongoDB {
112112 if ( configs . DEBUG ) {
113113 console . error ( 'Error when deleting a line in document' ) ;
114114 }
115- throw new Error ( err ) ;
116115 }
117116
118117 }
119118
119+ async changeParam ( documentLink , param , newValue ) {
120+ try {
121+ const update = { } ;
122+ update [ param ] = newValue ;
123+ await this . documentsCollection . updateOne ( { documentLink : documentLink } , { $set : update } ) ;
124+ } catch ( err ) {
125+ if ( configs . DEBUG ) {
126+ console . error ( err ) ;
127+ }
128+ }
129+ }
130+
131+ async changeCustomName ( documentLink , newName ) {
132+ this . changeParam ( documentLink , 'customDocumentName' , newName ) ;
133+ }
134+
135+ async changeTabSize ( documentLink , newTabSize ) {
136+ if ( Number . isInteger ( newTabSize ) ) {
137+ this . changeParam ( documentLink , 'tab' , newTabSize ) ;
138+ }
139+ }
140+
141+ async changeLanguage ( documentLink , newLanguage ) {
142+ if ( [ "python" ] . includes ( newLanguage ) ) {
143+ this . changeParam ( documentLink , 'language' , newLanguage ) ;
144+ }
145+ }
146+
147+ async addNewEditors ( documentLink , newEditorsId ) {
148+ try {
149+ await this . documentsCollection . updateOne ( { documentLink : documentLink } , { $addToSet : { editors : newEditorsId } } ) ;
150+ } catch ( err ) {
151+ if ( configs . DEBUG ) {
152+ console . error ( error ) ;
153+ }
154+ }
155+ }
156+
157+ async updateLastViewedDate ( documentLink ) {
158+ this . changeParam ( documentLink , 'lastViewedDate' , Date . now ( ) ) ;
159+ }
160+
120161 async applyRequests ( documentLink , requests ) {
121162 // TODO look to use bulk write
122163 try {
164+ // Avoid too many requests
165+ requests = requests . slice ( 0 , 50 ) ;
123166 for ( let request of requests ) {
124167 let requestType = request . type ;
125168 let data = request . data ;
@@ -139,19 +182,12 @@ class MongoDB {
139182 if ( configs . DEBUG ) {
140183 console . error ( 'Error when applying requests' ) ;
141184 }
142- throw new Error ( err ) ;
143185 }
144186 }
145187}
146188
147189function 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- ) ;
190+ db = new MongoDB ( configs . DB_URL ) ;
155191 db . connect ( ) ;
156192 return db ;
157193}
0 commit comments