mirror of
https://github.com/zoriya/flood.git
synced 2026-06-04 11:35:11 +00:00
dependencies: bump (major)
This commit is contained in:
+32
-36
@@ -80,26 +80,24 @@ class HistoryEra {
|
||||
|
||||
const minTimestamp = Date.now() - this.opts.nextEraUpdateInterval;
|
||||
|
||||
return this.db
|
||||
.find<TransferSnapshot>({timestamp: {$gte: minTimestamp}})
|
||||
.then((snapshots) => {
|
||||
if (this.opts.nextEra == null) {
|
||||
return;
|
||||
}
|
||||
return this.db.find<TransferSnapshot>({timestamp: {$gte: minTimestamp}}).then((snapshots) => {
|
||||
if (this.opts.nextEra == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
let downTotal = 0;
|
||||
let upTotal = 0;
|
||||
let downTotal = 0;
|
||||
let upTotal = 0;
|
||||
|
||||
snapshots.forEach((snapshot) => {
|
||||
downTotal += Number(snapshot.download);
|
||||
upTotal += Number(snapshot.upload);
|
||||
});
|
||||
|
||||
this.opts.nextEra.addData({
|
||||
download: Number(Number(downTotal / snapshots.length).toFixed(1)),
|
||||
upload: Number(Number(upTotal / snapshots.length).toFixed(1)),
|
||||
});
|
||||
snapshots.forEach((snapshot) => {
|
||||
downTotal += Number(snapshot.download);
|
||||
upTotal += Number(snapshot.upload);
|
||||
});
|
||||
|
||||
this.opts.nextEra.addData({
|
||||
download: Number(Number(downTotal / snapshots.length).toFixed(1)),
|
||||
upload: Number(Number(upTotal / snapshots.length).toFixed(1)),
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
async addData(data: TransferData): Promise<void> {
|
||||
@@ -116,27 +114,25 @@ class HistoryEra {
|
||||
})
|
||||
.catch(() => undefined);
|
||||
} else {
|
||||
await this.db
|
||||
.find<TransferSnapshot>({timestamp: this.lastUpdate})
|
||||
.then(
|
||||
async (snapshots) => {
|
||||
if (snapshots.length !== 0) {
|
||||
const snapshot = snapshots[0];
|
||||
const numUpdates = snapshot.numUpdates || 1;
|
||||
await this.db.find<TransferSnapshot>({timestamp: this.lastUpdate}).then(
|
||||
async (snapshots) => {
|
||||
if (snapshots.length !== 0) {
|
||||
const snapshot = snapshots[0];
|
||||
const numUpdates = snapshot.numUpdates || 1;
|
||||
|
||||
// calculate average and update
|
||||
const updatedSnapshot: TransferSnapshot = {
|
||||
timestamp: this.lastUpdate,
|
||||
upload: Number(((snapshot.upload * numUpdates + data.upload) / (numUpdates + 1)).toFixed(1)),
|
||||
download: Number(((snapshot.download * numUpdates + data.download) / (numUpdates + 1)).toFixed(1)),
|
||||
numUpdates: numUpdates + 1,
|
||||
};
|
||||
// calculate average and update
|
||||
const updatedSnapshot: TransferSnapshot = {
|
||||
timestamp: this.lastUpdate,
|
||||
upload: Number(((snapshot.upload * numUpdates + data.upload) / (numUpdates + 1)).toFixed(1)),
|
||||
download: Number(((snapshot.download * numUpdates + data.download) / (numUpdates + 1)).toFixed(1)),
|
||||
numUpdates: numUpdates + 1,
|
||||
};
|
||||
|
||||
await this.db.update({timestamp: this.lastUpdate}, updatedSnapshot).catch(() => undefined);
|
||||
}
|
||||
},
|
||||
() => undefined,
|
||||
);
|
||||
await this.db.update({timestamp: this.lastUpdate}, updatedSnapshot).catch(() => undefined);
|
||||
}
|
||||
},
|
||||
() => undefined,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+24
-28
@@ -61,30 +61,28 @@ class Users {
|
||||
* @return {Promise<AccessLevel>} - Returns access level of the user if matched or rejects with error.
|
||||
*/
|
||||
async comparePassword(credentials: Pick<Credentials, 'username' | 'password'>): Promise<AccessLevel> {
|
||||
return this.db
|
||||
.findOne<Credentials>({username: credentials.username})
|
||||
.then((user) => {
|
||||
// Wrong data provided
|
||||
if (credentials?.password == null) {
|
||||
return this.db.findOne<Credentials>({username: credentials.username}).then((user) => {
|
||||
// Wrong data provided
|
||||
if (credentials?.password == null) {
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
// Username not found.
|
||||
if (user == null) {
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
return argon2Verify({
|
||||
password: credentials.password,
|
||||
hash: user.password,
|
||||
}).then((isMatch) => {
|
||||
if (isMatch) {
|
||||
return user.level;
|
||||
} else {
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
// Username not found.
|
||||
if (user == null) {
|
||||
throw new Error();
|
||||
}
|
||||
|
||||
return argon2Verify({
|
||||
password: credentials.password,
|
||||
hash: user.password,
|
||||
}).then((isMatch) => {
|
||||
if (isMatch) {
|
||||
return user.level;
|
||||
} else {
|
||||
throw new Error();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -124,13 +122,11 @@ class Users {
|
||||
* @return {Promise<string>} - Returns ID of removed user or rejects with error.
|
||||
*/
|
||||
async removeUser(username: string): Promise<string> {
|
||||
return this.db
|
||||
.findOne<Credentials>({username})
|
||||
.then(async (user) => {
|
||||
await this.db.remove({username}, {});
|
||||
fs.rmdirSync(path.join(config.dbPath, user._id), {recursive: true});
|
||||
return user._id;
|
||||
});
|
||||
return this.db.findOne<Credentials>({username}).then(async (user) => {
|
||||
await this.db.remove({username}, {});
|
||||
fs.rmdirSync(path.join(config.dbPath, user._id), {recursive: true});
|
||||
return user._id;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user