Skip to content

Commit

Permalink
4 spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
gka committed Oct 5, 2017
1 parent e4db228 commit 3682fb9
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 145 deletions.
2 changes: 1 addition & 1 deletion build/embed.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions config.tpl.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"admins": [],
"date_format": "MMMM DD, YYYY"
"admins": [],
"date_format": "MMMM DD, YYYY"
}
128 changes: 64 additions & 64 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,94 +4,94 @@ const moment = require('moment');
const sqlite3 = require('sqlite3').verbose();

let db = new sqlite3.Database('./comments.db', (err) => {
if (err) console.error(err.message);
console.log('connected to db.');
init(run);
if (err) console.error(err.message);
console.log('connected to db.');
init(run);
});

const embedJS = fs.readFileSync('./build/embed.js', 'utf-8');

const config = JSON.parse(fs.readFileSync('./config.json'));

const queries = {
select:
`SELECT user_id, user.name, comment.created_at, comment
FROM comment INNER JOIN user ON (user_id=user.id)
WHERE NOT user.blocked AND NOT comment.rejected
AND (comment.approved OR user.trusted) AND slug = ?
ORDER BY comment.created_at DESC`,
insert:
`INSERT INTO comment
(user_id, slug, comment, created_at)
VALUES (?,?,?,datetime())`,
select:
`SELECT user_id, user.name, comment.created_at, comment
FROM comment INNER JOIN user ON (user_id=user.id)
WHERE NOT user.blocked AND NOT comment.rejected
AND (comment.approved OR user.trusted) AND slug = ?
ORDER BY comment.created_at DESC`,
insert:
`INSERT INTO comment
(user_id, slug, comment, created_at)
VALUES (?,?,?,datetime())`,
};

function init(next) {
db.all("SELECT name FROM sqlite_master WHERE type = 'table'", (err, rows) => {
if (err) console.error(err.message);
if (!rows.length) db.exec(fs.readFileSync('./src/schema.sql', 'utf-8'), next);
else next();
});
db.all("SELECT name FROM sqlite_master WHERE type = 'table'", (err, rows) => {
if (err) console.error(err.message);
if (!rows.length) db.exec(fs.readFileSync('./src/schema.sql', 'utf-8'), next);
else next();
});
}

function authenticate(callback) {
// todo
callback(null, { user_id: 1 });
// todo
callback(null, { user_id: 1 });
}

function error(err, request, reply) {
if (err) {
request.log.error(err.message);
reply.code(500).send({ error: err.message });
}
if (err) {
request.log.error(err.message);
reply.code(500).send({ error: err.message });
}
}

function run(err, res) {
if (err) console.error(err.message);
if (err) console.error(err.message);

// todo: limit cors to trusted domains
fastify.use(require('cors')());
// todo: limit cors to trusted domains
fastify.use(require('cors')());

fastify.get('/embed.js', (request, reply) => {
reply.type('application/javascript').send(embedJS);
});
fastify.get('/embed.js', (request, reply) => {
reply.type('application/javascript').send(embedJS);
});

fastify.get('/comments/:slug', (request, reply) => {
var slug = request.params.slug;
db.all(queries.select, [slug], (err, comments) => {
if (error(err, request, reply)) return;
comments.forEach((c) => c.created_at_s = moment(c.created_at).format(config.date_format || 'MMM DD, YYYY'))
reply.send({ slug, comments });
});
});
fastify.get('/comments/:slug', (request, reply) => {
var slug = request.params.slug;
db.all(queries.select, [slug], (err, comments) => {
if (error(err, request, reply)) return;
comments.forEach((c) => c.created_at_s = moment(c.created_at).format(config.date_format || 'MMM DD, YYYY'))
reply.send({ slug, comments });
});
});

fastify.post('/comments/:slug', (request, reply) => {
var slug = request.params.slug,
comment = request.body;
authenticate((err, res) => {
if (error(err, request, reply)) return;
db.run(queries.insert, [res.user_id, slug, comment], (err) => {
if (error(err, request, reply)) return;
reply.send({ status: 'ok' });
});
});
});
fastify.post('/comments/:slug', (request, reply) => {
var slug = request.params.slug,
comment = request.body;
authenticate((err, res) => {
if (error(err, request, reply)) return;
db.run(queries.insert, [res.user_id, slug, comment], (err) => {
if (error(err, request, reply)) return;
reply.send({ status: 'ok' });
});
});
});

fastify.get('/admin', (request, reply) => {
authenticate((err, res) => {
if (error(err, request, reply)) return;
if (config.admins.indexOf(res.user_id) > -1) {
// render admin
reply.send({ status: 'ok' });
} else {
reply.code(403).send({ error: 'access denied' });
}
});
});
fastify.get('/admin', (request, reply) => {
authenticate((err, res) => {
if (error(err, request, reply)) return;
if (config.admins.indexOf(res.user_id) > -1) {
// render admin
reply.send({ status: 'ok' });
} else {
reply.code(403).send({ error: 'access denied' });
}
});
});

fastify.listen(3000, (err) => {
if (err) throw err;
console.log(`server listening on ${fastify.server.address().port}`);
});
fastify.listen(3000, (err) => {
if (err) throw err;
console.log(`server listening on ${fastify.server.address().port}`);
});
}

24 changes: 12 additions & 12 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import string from 'rollup-plugin-string';
import uglify from 'rollup-plugin-uglify';

export default {
input: 'src/embed.js',
output: {
file: 'build/embed.js',
format: 'iife'
},
plugins: [
string({include: 'src/*.html'}),
commonjs(),
resolve(),
buble(),
uglify()
]
input: 'src/embed.js',
output: {
file: 'build/embed.js',
format: 'iife'
},
plugins: [
string({include: 'src/*.html'}),
commonjs(),
resolve(),
buble(),
uglify()
]
};
24 changes: 12 additions & 12 deletions src/comments.jst.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<form class="schnack-post">
<textarea></textarea><br>
<button>Send comment</button>
<textarea></textarea><br>
<button>Send comment</button>
</form>
<ul class="schnack-comments">
<% comments.forEach((comment) => { %>
<li class="schnack-comment">
<div class="schnack-author">
<%= comment.name %> <span class="schnack-date"><%= comment.created_at_s %></span>
</div>
<blockquote class="schnack-body">
<p><%= comment.comment %></p>
</blockquote>
</li>
<% }) %>
<% comments.forEach((comment) => { %>
<li class="schnack-comment">
<div class="schnack-author">
<%= comment.name %> <span class="schnack-date"><%= comment.created_at_s %></span>
</div>
<blockquote class="schnack-body">
<p><%= comment.comment %></p>
</blockquote>
</li>
<% }) %>
</ul>
20 changes: 10 additions & 10 deletions src/embed.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ var script = document.querySelectorAll('script[data-schnack-target]')[0];
if (!script) process.exit();

var opts = script.dataset,
host = opts.schnackHost,
target = opts.schnackTarget,
slug = opts.schnackSlug,
tpl = template(comments_tpl);
host = opts.schnackHost,
target = opts.schnackTarget,
slug = opts.schnackSlug,
tpl = template(comments_tpl);

function refresh() {
json(host+'/comments/'+slug, (err, data) => {
if (err) console.error(err);
console.log(data);
var html = tpl(data);
document.querySelectorAll(target)[0].innerHTML = html;
});
json(host+'/comments/'+slug, (err, data) => {
if (err) console.error(err);
console.log(data);
var html = tpl(data);
document.querySelectorAll(target)[0].innerHTML = html;
});
}

refresh();
56 changes: 28 additions & 28 deletions src/schema.sql
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
CREATE TABLE comment (
id INTEGER PRIMARY KEY NOT NULL,
user_id NOT NULL,
slug CHAR(128) NOT NULL,
created_at TEXT NOT NULL,
comment CHAR(4000) NOT NULL,
rejected BOOLEAN,
approved BOOLEAN
id INTEGER PRIMARY KEY NOT NULL,
user_id NOT NULL,
slug CHAR(128) NOT NULL,
created_at TEXT NOT NULL,
comment CHAR(4000) NOT NULL,
rejected BOOLEAN,
approved BOOLEAN
);

CREATE TABLE user (
id INTEGER PRIMARY KEY NOT NULL,
name CHAR(128),
twitter_id CHAR(20),
facebook_id CHAR(20),
created_at TIMESTAMP NOT NULL,
blocked BOOLEAN,
trusted BOOLEAN
id INTEGER PRIMARY KEY NOT NULL,
name CHAR(128),
twitter_id CHAR(20),
facebook_id CHAR(20),
created_at TIMESTAMP NOT NULL,
blocked BOOLEAN,
trusted BOOLEAN
);

-- test data
INSERT INTO user(name, created_at, trusted, blocked)
VALUES
("Normal User",datetime(), 0, 0),
("Trusted User",datetime(), 1, 0),
("Blocked User",datetime(), 0, 1);
VALUES
("Normal User",datetime(), 0, 0),
("Trusted User",datetime(), 1, 0),
("Blocked User",datetime(), 0, 1);

INSERT INTO comment(user_id, slug, created_at, comment, approved, rejected)
VALUES
(1,"foo", datetime(), "This comment was written by a normal user and was approved", 1, 0),
(1,"foo", datetime(), "This comment was written by a normal user and was rejected", 0, 1),
(1,"foo", datetime(), "This comment was written by a normal user and was neither approved nor rejected", 0, 0),
(2,"foo", datetime(), "This comment was written by a trusted user and was approved", 1, 0),
(2,"foo", datetime(), "This comment was written by a trusted user and was rejected", 0, 1),
(2,"foo", datetime(), "This comment was written by a trusted user and was neither approved nor rejected", 0, 0),
(3,"foo", datetime(), "This comment was written by a blocked user and was approved", 1, 0),
(3,"foo", datetime(), "This comment was written by a blocked user and was rejected", 0, 1),
(3,"foo", datetime(), "This comment was written by a blocked user and was neither approved nor rejected", 0, 0);
VALUES
(1,"foo", datetime(), "This comment was written by a normal user and was approved", 1, 0),
(1,"foo", datetime(), "This comment was written by a normal user and was rejected", 0, 1),
(1,"foo", datetime(), "This comment was written by a normal user and was neither approved nor rejected", 0, 0),
(2,"foo", datetime(), "This comment was written by a trusted user and was approved", 1, 0),
(2,"foo", datetime(), "This comment was written by a trusted user and was rejected", 0, 1),
(2,"foo", datetime(), "This comment was written by a trusted user and was neither approved nor rejected", 0, 0),
(3,"foo", datetime(), "This comment was written by a blocked user and was approved", 1, 0),
(3,"foo", datetime(), "This comment was written by a blocked user and was rejected", 0, 1),
(3,"foo", datetime(), "This comment was written by a blocked user and was neither approved nor rejected", 0, 0);
32 changes: 16 additions & 16 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
<style type="text/css">

ul.schnack-comments {
margin: 1em 0;
padding: 0;
margin: 1em 0;
padding: 0;
}
li.schnack-comment {
list-style: none;
margin: 0 0 1.5em;
padding: 0;
list-style: none;
margin: 0 0 1.5em;
padding: 0;
}
.schnack-body {
position: relative;
margin: 10px 0 0;
position: relative;
margin: 10px 0 0;
padding: 10px 10px;
border-left: 4px solid #cecece;
}
Expand All @@ -25,19 +25,19 @@
.schnack-date:after { content: ")"; }

.schnack-body p {
margin: 0;
margin: 0;
}

</style>
</head>
<body>
<script type="text/javascript"
src="http://localhost:3000/embed.js"
data-schnack-host="//localhost:3000"
data-schnack-slug="foo" data-schnack-target=".my-comments">
</script>
<div class="my-comments">
</div>
<script type="text/javascript"
src="http://localhost:3000/embed.js"
data-schnack-host="//localhost:3000"
data-schnack-slug="foo" data-schnack-target=".my-comments">
</script>
<div class="my-comments">
</div>
</body>
</html>

0 comments on commit 3682fb9

Please sign in to comment.