Skip to content
This repository has been archived by the owner on Dec 23, 2021. It is now read-only.

Admins #603

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ RUN npm run build-js-prod && npm run build-css && npm run minify && npm run vers

# do docker exec: npm run database

CMD npm run database && DEBUG=dat-registry npm run server
CMD npm run database config/config.default.js && DEBUG=dat-registry npm run server
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ A web registry for the dat network. Hosted at [http://datproject.org](http://dat

## Setup

0. Clone this repository, then copy the configuration file:
Copy the configuration file and modify it to your liking.

```
cp config/default.js config/config.development.js
```

1. Install the dependencies:
Install the dependencies

```
npm install
```

Create the database

Watch assets:
```
npm run database
npm run watch-css
npm run watch-js
```

Start the server
Expand Down
1 change: 1 addition & 0 deletions client/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ app.route('/view', require('./pages/archive'))
app.route('/download/:archiveKey', require('./pages/download'))
app.route('/profile/edit', require('./pages/auth/edit-profile'))
app.route('/profile/delete', require('./pages/auth/delete-account'))
app.route('/admin', require('./pages/admin'))
app.route('/dat://:archiveKey', require('./pages/archive'))
app.route('/dat://:archiveKey/contents', require('./pages/archive'))
app.route('/dat://:archiveKey/contents/*', require('./pages/archive'))
Expand Down
1 change: 1 addition & 0 deletions client/js/components/auth/user-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ module.exports = function (state, emit) {
</div>
</div>
<ul>
${state.township.profile.role === 'Admin' ? html`<li><a href="/admin">Admin</a></li>` : html``}
<li><a href="/${state.township.username}" data-no-routing>View Profile</a></li>
<li><a href="/profile/edit" data-no-routing>Edit Profile</a></li>
<li><a href="http://github.com/datproject/datproject.org/issues" target="_blank" class="color-neutral-50 hover-color-neutral-70">Report Bug</a></li>
Expand Down
14 changes: 14 additions & 0 deletions client/js/components/delete-button.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const html = require('choo/html')
const button = require('../elements/button')

module.exports = function (state, emit) {
var meta = state.archive.metadata
var owner = (meta && state.township) && meta.username === state.township.username
if (!state.township.profile.admin && !owner) return html``

return button({
text: 'Unpublish',
click: () => emit('archive:delete', {id: state.archive.id}),
klass: 'btn btn--red'
})
}
2 changes: 0 additions & 2 deletions client/js/components/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,11 @@ module.exports = function (state, emit) {
}

function onerror (err) {
console.log('got err', err)
var update = {}
console.error(err)
var message = 'Unsupported filetype'
update.isLoading = false // Allow downloads for unsupported files
update.error = {message: message}
console.log('sending', update)
return emit('preview:update', update)
}
})
Expand Down
4 changes: 2 additions & 2 deletions client/js/elements/404/index.js → client/js/elements/404.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const html = require('choo/html')
const datIcon = require('../icon')
const loaderIcon = require('../loader-icon')
const datIcon = require('./icon')
const loaderIcon = require('./loader-icon')

module.exports = function (props) {
if (!props) props = {}
Expand Down
10 changes: 0 additions & 10 deletions client/js/elements/delete-button/index.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const html = require('choo/html')
const loaderIcon = require('../loader-icon')
const loaderIcon = require('./loader-icon')

module.exports = function () {
return html`<div class="loading">
Expand Down
6 changes: 5 additions & 1 deletion client/js/models/archive.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ module.exports = function (state, emitter) {
emitter.emit('archive:update', json)
})
})

emitter.on('archive:delete', function (data) {
api.dats.delete({id: data.id}, function (err, resp, json) {
if (err) return emitter.emit('archive:update', {error: {message: err.message}})
emitter.emit('pushState', '/profile')
if (json.deleted === 1) {
emitter.emit('message:success', 'Archive deleted.')
window.location.href = window.location.href
}
})
})
emitter.on('archive:view', function (link) {
Expand Down
4 changes: 4 additions & 0 deletions client/js/models/roles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

module.exports = {
'0': 'Unverified', '1': 'Verified', '2': 'Admin', '86': 'Suspended'
}
2 changes: 2 additions & 0 deletions client/js/models/township.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Api = require('../api')
const xtend = require('xtend')
const defaults = require('./defaults')
const roles = require('./roles')

module.exports = function (state, emitter) {
emitter.on('DOMContentLoaded', function () {
Expand Down Expand Up @@ -30,6 +31,7 @@ module.exports = function (state, emitter) {
if (!results.length) return done()
var newState = user
newState.profile = results[0]
newState.profile.role = roles[newState.profile.role]
api.dats.get({user_id: user.id}, function (err, resp, results) {
if (err && err.message === 'jwt expired') {
emitter.emit('township:logout', data)
Expand Down
18 changes: 18 additions & 0 deletions client/js/pages/admin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const html = require('choo/html')
const fourohfour = require('../../pages/fourohfour')
const header = require('../../components/header')

module.exports = function (state, emit) {
if (state.township.profile.role !== 'Admin') return fourohfour(state, emit)
return html`
<div>
${header(state, emit)}
<div class="container">
<h1>Hey ${state.township.username}. You're an admin. Cool!</h1>
<h2>You can delete, edit, and add any user or dat on the site. With great power comes great responsibility!</h2>
<p>This page is mostly here as a placeholder. It'd be cool if there were some ways to administrate the site here,
including aggregate site statistics, uis for the api and archiver, and other fun stuff. Want to open a PR?</p>
</div>
</div>
`
}
10 changes: 2 additions & 8 deletions client/js/pages/archive/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const copyButton = require('../../components/copy-button')
const header = require('../../components/header')
const preview = require('../../components/preview')
const fourohfour = require('../../elements/404')
const deleteButton = require('../../components/delete-button')
const error = require('../../elements/error')
const hyperdriveStats = require('../../elements/hyperdrive-stats')
const css = require('sheetify')
Expand Down Expand Up @@ -36,7 +37,6 @@ const archivePage = (state, emit) => {
}
}
}
// var owner = (meta && state.township) && meta.username === state.township.username
var meta = state.archive.metadata
var title = meta && meta.title || meta.shortname || state.archive.key
var description = meta && meta.description
Expand Down Expand Up @@ -91,19 +91,13 @@ const archivePage = (state, emit) => {
}
`

// TODO: add delete button with confirm modal.
// const deleteButton = require('../../elements/delete-button')
// function remove () {
// emit('archive:delete', meta.id)
// }
// ${owner ? deleteButton(remove) : html``}

return html`
<div class="${styles}">
${header(state, emit)}
<div id="dat-info" class="dat-header">
<div class="container">
<div class="dat-header-actions-wrapper">
${deleteButton(state, emit)}
${copyButton('dat://' + state.archive.key, emit)}
<a href="/download/${state.archive.key}" target="_blank" class="dat-header-action">
<div class="btn__icon-wrapper">
Expand Down
24 changes: 23 additions & 1 deletion client/js/pages/auth/profile.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
const html = require('choo/html')
const fourohfour = require('../../elements/404')
const css = require('sheetify')
const fourohfour = require('../../elements/404')
const gravatar = require('./../../elements/gravatar')
const header = require('./../../components/header')
const list = require('./../../components/list')
const button = require('../../elements/button')

const unsuspendButton = function (state, emit) {
return button({
text: 'Unsuspend',
click: () => emit('profile:edit', {id: state.profile.id, role: '0'}),
klass: 'btn btn--red'
})
}
const suspendButton = function (state, emit) {
return button({
text: 'Suspend',
click: () => emit('profile:edit', {id: state.profile.id, role: '86'}),
klass: 'btn btn--red'
})
}

var profileStyles = css`
:host {
Expand Down Expand Up @@ -52,17 +68,23 @@ module.exports = (state, emit) => {
var pic = gravatar({email}, {}, avatarStyles)
var owner = state.township.profile.username === state.profile.username
var showPlaceholder = numDats === 0 && owner
var currentUser = state.township.profile

state.profile.dats.map(function (dat) {
dat.shortname = `${state.profile.username}/${dat.name}`
return dat
})

return html`
<div>
${header(state, emit)}
<div class="flex flex-column flex-row-m flex-row-l ${profileStyles} bg-splash-02">
<div class="bg-neutral-04 pa4 tc tl-m tl-l">
<div class="name">
${currentUser.admin
? state.profile.role !== '86'
? suspendButton(state, emit) : unsuspendButton(state, emit)
: ''}
<h1 class="f4 mb1">${name}</h1>
<h2 class="f5 color-neutral-80">${username}</h2>
${pic}
Expand Down
6 changes: 5 additions & 1 deletion config/config.production.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,9 @@ module.exports = {
},
useNullAsDefault: true
},
whitelist: false
whitelist: false,
archiver: {
dir: 'archiver',
verifyConnection: true
}
}
9 changes: 8 additions & 1 deletion config/config.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ var path = require('path')

module.exports = {
data: path.join(__dirname, '..', 'tests'),
admins: [
'admin'
],
db: {
dialect: 'sqlite3',
connection: {
Expand All @@ -10,5 +13,9 @@ module.exports = {
useNullAsDefault: true
},
whitelist: false,
port: process.env.PORT || 8888
port: process.env.PORT || 8888,
archiver: {
dir: 'archiver',
verifyConnection: false
}
}
7 changes: 6 additions & 1 deletion config/default.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
data: 'data',
admins: ['admins'],
mixpanel: process.env.MIXPANEL || 'notakey',
township: {
secret: process.env.TOWNSHIP_SECRET || 'very very not secret',
Expand All @@ -17,5 +18,9 @@ module.exports = {
useNullAsDefault: true
},
whitelist: false,
archiver: 'archiver'
archiver: {
dir: 'archiver',
verifyConnection: false,
timeout: 3000
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"dat-design": "^5.0.0",
"dat-icons": "^2.5.0",
"dat-registry": "^3.0.2",
"dat-registry-api": "^6.0.5",
"dat-registry-api": "^7.0.0",
"dat-swarm-defaults": "^1.0.0",
"debug": "^2.6.8",
"discovery-swarm": "^4.2.0",
Expand Down
1 change: 0 additions & 1 deletion scripts/add-dats-for-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ function createDat (dat) {
dat.url = 'dat://' + archive.key.toString('hex')
api.dats.create(dat, function (err, resp, json) {
if (err) console.error(err.toString())
console.log('json')
})
})
}
Expand Down
8 changes: 5 additions & 3 deletions server/cli.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
#!/usr/bin/env node
const path = require('path')
const bole = require('bole')
const db = require('dat-registry-api/database/init')
const Server = require('./')
const Config = require('./config')

const env = process.env.NODE_ENV || 'development'
const config = require(path.join(__dirname, '..', 'config', 'config.' + env + '.js'))

bole.output({
level: 'info',
stream: process.stdout
})

const config = Config()
const PORT = process.env.PORT || process.env.DATLAND_PORT || 8080
config.log = bole(__filename)
const server = Server(config)
db(config.db, function (err, db) {
db(config, function (err, db) {
if (err) throw err
db.knex.destroy(function () {
server.listen(PORT, function () {
Expand Down
24 changes: 0 additions & 24 deletions server/config.js

This file was deleted.

1 change: 1 addition & 0 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = function (config) {
var server = http.createServer(function (req, res) {
router(req, res)
})
server.router = router

server.on('close', function () {
router.api.close(function () {
Expand Down
Loading