Skip to content

Commit 02295f7

Browse files
swangmaxbeatty
authored andcommitted
Use github login info to associate pages/comments with a user (#467)
1 parent e547eeb commit 02295f7

File tree

19 files changed

+95
-25
lines changed

19 files changed

+95
-25
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
up: function (genericQuery) {
3+
return genericQuery(`
4+
ALTER TABLE comments ADD COLUMN authorGitHub varchar(39) NOT NULL AFTER author, ADD INDEX gh_username (authorGitHub);
5+
`);
6+
},
7+
8+
down: function (genericQuery) {
9+
return genericQuery(`
10+
ALTER TABLE comments DROP COLUMN authorGithub, DROP INDEX gh_username;
11+
`);
12+
}
13+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = {
2+
up: function (genericQuery) {
3+
return genericQuery(`
4+
ALTER TABLE pages ADD COLUMN authorGitHub varchar(39) NOT NULL AFTER author, ADD INDEX gh_username (authorGitHub);
5+
`);
6+
},
7+
8+
down: function (genericQuery) {
9+
return genericQuery(`
10+
ALTER TABLE pages DROP COLUMN authorGithub, DROP INDEX gh_username;
11+
`);
12+
}
13+
};

server/lib/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ exports.testPageContext = {
3737
genError: null,
3838
slugPattern: regex.slug,
3939
author: '',
40+
authorGitHub: '',
4041
authorEmail: '',
4142
authorURL: '',
4243
title: '',

server/lib/regex.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,6 @@ module.exports = {
8585
// resource path
8686
'(?:/\\S*)?' + '$',
8787
slug: '[A-Za-z0-9](?:-?[A-Za-z0-9])*',
88-
script: '(<script[^>]*?>)([\\s\\S]*?)(</script>)'
88+
script: '(<script[^>]*?>)([\\s\\S]*?)(</script>)',
89+
github: '(?=^.{0,39}$)[A-Za-z0-9]+(-?[A-Za-z0-9])+$'
8990
};

server/lib/schema.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ const mediumText = Joi.string().allow('').max(defaults.mediumTextLength);
66
const author = Joi.string().min(1);
77
const authorEmail = Joi.string().email();
88
const authorURL = Joi.string().allow('').regex(new RegExp(regex.url, 'i'), 'url');
9+
const authorGitHub = Joi.string().allow('').max(39).regex(new RegExp(regex.github, 'i'));
910

1011
exports.mediumText = mediumText;
1112

1213
exports.testPage = Joi.object().keys({
1314
author: author.allow(''),
1415
authorEmail: authorEmail.allow(''),
1516
authorURL,
17+
authorGitHub,
1618
title: Joi.string().required().trim().min(1).max(255),
1719
slug: Joi.string().required().trim().min(1).max(55).regex(new RegExp(regex.slug), 'slug'),
1820
visible: Joi.string().default('n').valid('y', 'n'),
@@ -32,5 +34,6 @@ exports.comment = Joi.object().keys({
3234
author: author.required(),
3335
authorEmail: authorEmail.required(),
3436
authorURL: authorURL.required(),
37+
authorGitHub,
3538
message: Joi.string().min(1).max(defaults.mediumTextLength)
3639
});

server/repositories/pages.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,18 @@ exports.register = function (server, options, next) {
5454
// WHERE
5555
// author LIKE '%{{author}}%'
5656
// OR author LIKE '{{author}}'
57+
// OR authorGitHub = '{{author}}'
5758
// AND updated IN (SELECT MAX(updated) FROM pages WHERE visible = 'y' GROUP BY slug)
5859
// AND visible = 'y'
5960
// ORDER BY updated DESC
6061

6162
var wcAuthor = '%' + author + '%';
6263
var a = author.trim().replace('-', '%');
64+
var gh = author.trim();
6365

6466
return db.genericQuery(
65-
"SELECT id AS pID, slug AS url, revision, title, published, updated, author, (SELECT COUNT(*) FROM pages WHERE slug = url AND visible = 'y') AS revisionCount, (SELECT COUNT(*) FROM tests WHERE pageID = pID) AS testCount FROM pages WHERE author LIKE ? OR author LIKE ? AND updated IN (SELECT MAX(updated) FROM pages WHERE visible = 'y' GROUP BY slug) AND visible = 'y' ORDER BY updated DESC",
66-
[wcAuthor, a]
67+
"SELECT id AS pID, slug AS url, revision, title, published, updated, author, authorGitHub, (SELECT COUNT(*) FROM pages WHERE slug = url AND visible = 'y') AS revisionCount, (SELECT COUNT(*) FROM tests WHERE pageID = pID) AS testCount FROM pages WHERE (author LIKE ? OR author LIKE ? OR authorGitHub = ?) AND updated IN (SELECT MAX(updated) FROM pages WHERE visible = 'y' GROUP BY slug) AND visible = 'y' ORDER BY updated DESC",
68+
[wcAuthor, a, gh]
6769
);
6870
});
6971

@@ -119,7 +121,7 @@ exports.register = function (server, options, next) {
119121
server.log(['debug'], `${name}::findBySlug: ${JSON.stringify(arguments)}`);
120122

121123
return db.genericQuery(
122-
'SELECT published, updated, author, authorEmail, authorURL, revision, visible, title FROM pages WHERE slug = ? ORDER BY published ASC',
124+
'SELECT published, updated, author, authorGitHub, authorEmail, authorURL, revision, visible, title FROM pages WHERE slug = ? ORDER BY published ASC',
123125
[slug]
124126
);
125127
});
@@ -128,7 +130,7 @@ exports.register = function (server, options, next) {
128130
server.log(['debug'], `${name}::findBySlug: ${JSON.stringify(arguments)}`);
129131

130132
return db.genericQuery(
131-
'SELECT published, updated, author, authorEmail, revision, visible, title FROM pages WHERE slug = ? AND visible = ? ORDER BY published ASC',
133+
'SELECT published, updated, author, authorGitHub, authorEmail, revision, visible, title FROM pages WHERE slug = ? AND visible = ? ORDER BY published ASC',
132134
[slug, 'y']
133135
);
134136
});

server/services/comments.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ exports.register = function (server, options, next) {
1010
author: payload.author,
1111
authorEmail: payload.authorEmail,
1212
authorURL: payload.authorURL,
13+
authorGitHub: payload.authorGitHub,
1314
content: payload.message,
1415
ip,
1516
published: new Date()

server/web/auth/github.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ exports.register = function (server, options, next) {
1313
});
1414

1515
// attach credentials to view context to let people know they are logged in
16-
// attach authorSlug to view context to construct "My Tests" link in footer
16+
// attach authorGitHub to view context to construct "My Tests" link in footer
1717
server.ext('onPreResponse', function (request, reply) {
1818
const response = request.response;
1919
if (response.variety === 'view') {
2020
response.source.context = response.source.context || {};
2121
response.source.context.credentials = request.auth.isAuthenticated ? request.auth.credentials : null;
22-
response.source.context.authorSlug = request.yar.get('authorSlug');
22+
response.source.context.authorGitHub = request.yar.get('authorGitHub');
2323
}
2424
return reply.continue();
2525
});

server/web/browse/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,19 +90,19 @@ exports.register = function (server, options, next) {
9090

9191
server.route({
9292
method: 'GET',
93-
path: '/browse/{authorSlug}',
93+
path: '/browse/{authorGitHub}',
9494
handler: function (request, reply) {
95-
pagesRepo.getLatestVisibleForAuthor(request.params.authorSlug)
95+
pagesRepo.getLatestVisibleForAuthor(request.params.authorGitHub)
9696
.then(function (rows) {
9797
if (rows.length === 0) {
9898
reply(Boom.notFound('The author was not found'));
9999
} else {
100100
reply.view('browse/author', {
101-
headTitle: 'Test cases by ' + request.params.authorSlug,
101+
headTitle: 'Test cases by ' + request.params.authorGitHub,
102102
showAtom: {
103-
slug: 'browse/' + request.params.authorSlug
103+
slug: 'browse/' + request.params.authorGitHub
104104
},
105-
author: request.params.authorSlug,
105+
author: request.params.authorGitHub,
106106
pages: rows
107107
});
108108
}
@@ -115,14 +115,14 @@ exports.register = function (server, options, next) {
115115

116116
server.route({
117117
method: 'GET',
118-
path: '/browse/{authorSlug}.atom',
118+
path: '/browse/{authorGitHub}.atom',
119119
handler: function (request, reply) {
120-
pagesRepo.getLatestVisibleForAuthor(request.params.authorSlug)
120+
pagesRepo.getLatestVisibleForAuthor(request.params.authorGitHub)
121121
.then(function (rows) {
122122
var updated = getUpdatedDate(rows);
123123

124124
reply.view('browse/author-atom', {
125-
author: request.params.authorSlug,
125+
author: request.params.authorGitHub,
126126
update: updated.toISOString,
127127
pages: rows
128128
}, {

server/web/edit/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ exports.register = function (server, options, next) {
7878
}).code(400);
7979
};
8080

81+
request.payload.authorGitHub = request.auth.credentials.username;
82+
8183
Joi.validate(request.payload, schema.testPage, function (err, pageWithTests) {
8284
let errObj = {};
8385
if (err) {
@@ -127,8 +129,7 @@ exports.register = function (server, options, next) {
127129
return pagesService.edit(pageWithTests, update, prevPage.maxRev, prevPage.id);
128130
})
129131
.then(resultingRevision => {
130-
request.yar.set('authorSlug', pageWithTests.author.replace(' ', '-').replace(/[^a-zA-Z0-9 -]/, ''));
131-
132+
request.yar.set('authorGitHub', pageWithTests.authorGitHub.replace(/[^a-zA-Z0-9-]/, ''));
132133
const r = resultingRevision > 1 ? `/${resultingRevision}` : '';
133134

134135
reply.redirect(`/${request.params.testSlug}${r}`);

0 commit comments

Comments
 (0)