Skip to content
This repository was archived by the owner on Mar 10, 2024. It is now read-only.

Commit f022562

Browse files
add new features + limits fields
1 parent 39f8710 commit f022562

File tree

3 files changed

+69
-39
lines changed

3 files changed

+69
-39
lines changed

src/config/config dist.json

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
{
22
"DEBUG" : true,
3-
"HOST": "localhost",
4-
"PORT": 5000,
5-
"DB_TYPE": "mysql",
63
"PRODUCTION": false,
74
"CLIENT_VERBOSE": 2,
5+
"HOST": "localhost",
6+
"PORT": 5000,
7+
"DB_URL": "mongodb://host:port/?retryWrites=true&w=majority",
88
"DISCORD_WEBHOOK": null,
99
"SSL": false,
1010
"KEY_FILE_SSL": null,
1111
"CERT_FILE_SSL" : null,
12-
"DB_CONFIG": {
13-
"DB_HOST": "db",
14-
"DB_USERNAME": "root",
15-
"DB_PASSWORD": "root",
16-
"DB_DATABASE": "codewe",
17-
"DB_PORT": "3306"
18-
},
1912
"METRICS": false,
2013
"METRICS_PORT": 8000
2114
}

src/db/MongoDB.js

Lines changed: 64 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ const baseCode = [
1212

1313

1414
class 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

147189
function 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
}

src/socket/socket.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ module.exports = function (wss) {
5757
}));
5858
}else sock.send(JSON.stringify(data));
5959
});
60+
db.updateLastViewedDate(data.room);
6061
db.applyRequests(data.room, data.data);
6162
} catch (err) {
6263
throw new Error(err);
@@ -79,7 +80,7 @@ module.exports = function (wss) {
7980
break;
8081
case 'report': // Send issue to hook
8182
if (hook) {
82-
hook.warn('Report', data.data.content);
83+
hook.warn('Report', data.data.content.slice(0, 5000));
8384
}
8485

8586
}

0 commit comments

Comments
 (0)