-
Notifications
You must be signed in to change notification settings - Fork 0
/
http-server.js
85 lines (77 loc) · 2.57 KB
/
http-server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const express = require('express')
const path = require('path')
const bodyParser = require('body-parser')
const validate = require('./utils/validate')
const db = require('./db')
const sendLink = require('./ssb-actions').sendLink
const removeLink = require('./ssb-actions').removeLink
const app = express()
const jsonParser = bodyParser.json()
app.use('/', express.static('html'))
app.post('/api/link', jsonParser, (req, res) => {
Promise.all([
validate.jsonLink(req.body),
validate.hashExist(req.body.username, db)
]).then(result => {
if (!result[1]) {
if (result[0].status === 'ok') {
sendLink(req.body.ssbId, req.body.username, db)
res.json({status:'ok'})
} else {
res.json(result[0])
}
} else {
res.json({status: 'error', message: 'Username already taken'})
}
}).catch(err => {
res.json(err)
})
})
app.post('/api/unlink',jsonParser, (req, res) => {
Promise.all([
validate.jsonLink(req.body),
validate.hashExist(req.body.username, db)
]).then(result => {
if (result[1]) {
if (result[0].status === 'ok') {
removeLink(req.body.ssbId, req.body.username, db)
res.json({status:'ok'})
} else {
res.json(result[0])
}
} else {
res.json({status: 'error', message: 'Username not found'})
}
}).catch(err => {
res.json(err)
})
})
app.use('/action/link/:hash', (req, res) => {
validate.hashExist(req.params.hash,db)
.then(exist => {
if (exist === true) {
db.get(req.params.hash, (err, value) => {
value = JSON.parse(value);
db.put(value.username, JSON.stringify(Object.assign(value, {confirm: true})))
res.redirect('/#linked')
})
} else {
res.json({error: 'Hash not found'})
}
})
})
app.use('/action/unlink/:hash', (req, res) => {
validate.hashExist(req.params.hash,db)
.then(exist => {
if (exist === true) {
db.get(req.params.hash, (err, value) => {
value = JSON.parse(value);
db.put(value.username, JSON.stringify(Object.assign(value, {confirm: true, deleted: true})))
res.redirect('/#unlinked')
})
} else {
res.json({error: 'Hash not found'})
}
})
})
module.exports = app