Skip to content
This repository has been archived by the owner on Oct 14, 2022. It is now read-only.

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pfrazee committed Nov 30, 2018
1 parent 0872c82 commit b58a605
Show file tree
Hide file tree
Showing 17 changed files with 268 additions and 143 deletions.
6 changes: 3 additions & 3 deletions docs/flows/registration.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ User POSTS to `/v2/accounts/register` with body:

Server creates a new account for the user. A random 32-byte email-verification nonce is created. The user record indicates:

scopes|isEmailVerified|emailVerifyNonce
scopes|isEmailVerified|emailVerificationNonce
------|---------------|----------------
none|false|XXX

Server sends an email to the user with the `emailVerifyNonce`.
Server sends an email to the user with the `emailVerificationNonce`.

Server responds 200 with HTML/JSON indicating to check email.

Expand All @@ -35,7 +35,7 @@ User GETS or POSTS `/v2/accounts/verify` with query-params or body:

Server updates user record to indicate:

scopes|isEmailVerified|emailVerifyNonce
scopes|isEmailVerified|emailVerificationNonce
------|---------------|----------------
user|true|null

Expand Down
2 changes: 1 addition & 1 deletion docs/schemas/leveldb.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Schema:
createdAt: Number, the timestamp of creation time
isEmailVerified: Boolean
emailVerifyNonce: String, the random verification nonce (register flow)
emailVerificationNonce: String, the random verification nonce (register flow)
forgotPasswordNonce: String, the random verification nonce (forgot password flow)
Expand Down
2 changes: 1 addition & 1 deletion docs/webapis-v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ Response body:
email: String, the user's email address
username: String, the chosen username
isEmailVerified: Boolean
emailVerifyNonce: String, the random verification nonce
emailVerificationNonce: String, the random verification nonce
scopes: Array of strings, what is this user's perms?
diskUsage: Number, how many bytes the user is using
diskQuota: Number, how many bytes the user is allowed
Expand Down
2 changes: 1 addition & 1 deletion docs/webapis.md
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ Response body:
email: String, the user's email address
username: String, the chosen username
isEmailVerified: Boolean
emailVerifyNonce: String, the random verification nonce
emailVerificationNonce: String, the random verification nonce
scopes: Array of strings, what is this user's perms?
diskUsage: Number, how many bytes the user is using
diskQuota: Number, how many bytes the user is allowed
Expand Down
5 changes: 2 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ module.exports = async function (config) {
addConfigHelpers(config)
var cloud = new Hypercloud(config)
cloud.version = packageJson.version
await cloud.setupDatabase() // pause all loading during DB setup
await cloud.setup()
cloud.loadAllArchives()
cloud.setupAdminUser()

console.log(figures.pointerSmall, 'Instantiating server')
var app = express()
Expand Down Expand Up @@ -245,7 +244,7 @@ module.exports = async function (config) {
}

// general uncaught error
console.error('[ERROR]', err)
console.error('[ERROR]', req.method, req.url, err)
res.status(500)
var error = {
message: 'Internal server error',
Expand Down
10 changes: 7 additions & 3 deletions lib/apis/archives.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ module.exports = class ArchivesAPI {

// update the records
try {
await this.archivesDB.addHostingUser(key, userRecord.id)
await this.archivesDB.addHostingUser(key, userRecord.id, name)
} catch (e) {
if (e.alreadyHosted) {
return res.status(422).json({
Expand Down Expand Up @@ -160,14 +160,18 @@ module.exports = class ArchivesAPI {
key = DAT_KEY_REGEX.exec(url)[1]
}

var name
var release = await Promise.all([lock('users'), lock('archives')])
try {
// fetch the user
var userRecord = await this.usersDB.getByID(res.locals.session.id)

// find the archive name
var archiveRecord = await this.archivesDB.getExtraByKey(key)
var name = archiveRecord.name
if (!archiveRecord) {
return res.status(204).end()
}
name = archiveRecord.name

// update the records
if (userRecord.archives.find(a => a.key === key)) {
Expand Down Expand Up @@ -296,7 +300,7 @@ module.exports = class ArchivesAPI {

// update the records
archiveRecord.name = name
await this.archivesDB.addHostingUser(archiveRecord.key, userRecord.id)
await this.archivesDB.addHostingUser(archiveRecord.key, userRecord.id, name)
await this.usersDB.put(userRecord)
} finally {
release[0]()
Expand Down
3 changes: 2 additions & 1 deletion lib/apis/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ module.exports = class ServicesAPI {
recent: (await this.archivesDB.list({
sort: 'createdAt',
limit: 25,
cursor: req.query.start
cursor: req.query.start,
getExtra: true
})).map(mapArchiveObject)
})
}
Expand Down
7 changes: 4 additions & 3 deletions lib/archiver.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ module.exports = class Archiver extends EventEmitter {
archive.diskUsage = await du(path)

// cache to the db
let archiveRecord = await this.cloud.archivesDB.update(key, {diskUsage: archive.diskUsage})
await this.cloud.archivesDB.update(key, {diskUsage: archive.diskUsage})
let archiveRecord = await this.cloud.archivesDB.getByKey(key)

// if different, update the user record as well
if (!dontUpdateUser && oldUsage !== archive.diskUsage) {
Expand Down Expand Up @@ -257,11 +258,11 @@ module.exports = class Archiver extends EventEmitter {
var start = Date.now()
var users = await this.cloud.usersDB.list()
await Promise.all(users.map(this.computeUserDiskUsageAndSwarm.bind(this)))
console.log(figures.tick, `FINISH Compute user quota usage (${(Date.now() - start)}ms)`)
} catch (e) {
console.error(e)
console.log(figures.warning, `FAILED Compute user quota usage (${(Date.now() - start)}ms)`)
} finally {
console.log(figures.tick, `FINISH Compute user quota usage (${(Date.now() - start)}ms)`)
release()
}
}
Expand All @@ -283,11 +284,11 @@ module.exports = class Archiver extends EventEmitter {
// delete DB record
await this.cloud.archivesDB.del(archiveKey)
}))
console.log(figures.tick, `FINISH Delete dead archives (${(Date.now() - start)}ms)`)
} catch (e) {
console.error(e)
console.log(figures.warning, `FAILED Delete dead archives (${(Date.now() - start)}ms)`)
} finally {
console.log(figures.tick, `FINISH Delete dead archives (${(Date.now() - start)}ms)`)
release()
}
}
Expand Down
34 changes: 28 additions & 6 deletions lib/dbs/activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class ActivityDB {
this.sqlite = cloud.db
}

async setup () {
// noop
}

// basic ops
// =

Expand All @@ -30,8 +34,7 @@ class ActivityDB {
if (!opts.doNotModify) {
record.ts = Date.now()
}
var {ts, userid, username, action, params} = record
params = JSON.stringify(params)
var {ts, userid, username, action, params} = ActivityDB.serialize(record)
await this.sqlite.run(SQL`
INSERT INTO activity
(ts, userid, username, action, params)
Expand All @@ -49,7 +52,7 @@ class ActivityDB {
// getters
// =

listGlobalEvents ({limit, lt, gt, lte, gte, reverse} = {}) {
async listGlobalEvents ({limit, lt, gt, lte, gte, reverse} = {}) {
var query = SQL`SELECT * FROM activity`
if (lt) query.append(SQL` WHERE key < ${lt}`)
if (lte) query.append(SQL` WHERE key <= ${lte}`)
Expand All @@ -58,10 +61,11 @@ class ActivityDB {
if (!reverse) query.append(SQL` ORDER BY key`)
else query.append(SQL` ORDER BY key DESC`)
if (limit) query.append(SQL` LIMIT ${limit}`)
return this.sqlite.all(query)
var records = await this.sqlite.all(query)
return records.map(ActivityDB.deserialize)
}

listUserEvents (username, {limit, lt, gt, lte, gte, reverse} = {}) {
async listUserEvents (username, {limit, lt, gt, lte, gte, reverse} = {}) {
var query = SQL`SELECT * FROM activity WHERE username = ${username}`
if (lt) query.append(SQL` AND key < ${lt}`)
if (lte) query.append(SQL` AND key <= ${lte}`)
Expand All @@ -70,7 +74,25 @@ class ActivityDB {
if (!reverse) query.append(SQL` ORDER BY key`)
else query.append(SQL` ORDER BY key DESC`)
if (limit) query.append(SQL` LIMIT ${limit}`)
return this.sqlite.all(query)
var records = await this.sqlite.all(query)
return records.map(ActivityDB.deserialize)
}

// helpers
// =

static serialize (record) {
if (!record) return null
var r2 = Object.assign({}, record)
if ('params' in r2) r2.params = JSON.stringify(record.params)
return r2
}

static deserialize (record) {
if (!record) return null
var r2 = Object.assign({}, record)
if ('params' in r2) r2.params = JSON.parse(r2.params)
return r2
}
}
module.exports = ActivityDB
Loading

0 comments on commit b58a605

Please sign in to comment.