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

Commit

Permalink
[#980] Add forum link to entry detail pages.
Browse files Browse the repository at this point in the history
Reuses much of the code used for the submission discussion link on the
Survey entry form page.
  • Loading branch information
brew committed Mar 24, 2017
1 parent f80e093 commit c155303
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 33 deletions.
39 changes: 9 additions & 30 deletions census/controllers/census.js
Expand Up @@ -4,8 +4,6 @@ const _ = require('lodash');
const marked = require('marked');
const config = require('../config');
const uuid = require('uuid');
const url = require('url');
const querystring = require('querystring');
const utils = require('./utils');
const util = require('util');
const modelUtils = require('../models').utils;
Expand Down Expand Up @@ -234,37 +232,18 @@ let submit = function(req, res) {
});
};

let _getDiscussionURL = function(req, dataset, place) {
let getDiscussionUrlForRequest = function(req, dataset, place) {
/*
If `submission_discussion_url` is defined in settings and it is in the
format: https://discuss.okfn.org/c/<topic>/<subtopic>, return a new topic
url with a prepopulated topic for place and dataset. Otherwise, return the
original `submission_discussion_url` without modification. If
`submission_discussion_url` is undefined return an empty string.
Returns a discussion url for the submission using site settings from the
`req`. Most of the work is done in `buildDiscussionUrl`.
*/
let submissionDiscussionURL =
_.get(req.params.site.settings, 'submission_discussion_url', '');
let parsedURL = url.parse(submissionDiscussionURL);
// URL is a discourse link
if (parsedURL.hostname === config.get('submission_discourse_hostname', '')) {
let splitPathName = _.trimLeft(parsedURL.pathname, '/').split('/');
// URL is a category link
if (splitPathName[0] === 'c') {
// Create a new topic link
let newTopicURL = url.parse('');
newTopicURL.protocol = parsedURL.protocol;
newTopicURL.host = parsedURL.host;
newTopicURL.pathname = 'new-topic';
newTopicURL.search = querystring.stringify({
title: util.format(req.gettext('Entry for %s / %s'), dataset, place),
body: util.format(req.gettext('This is a discussion about the submission for [%s / %s](%s).'),
dataset, place, req.res.locals.current_url),
category: _.rest(splitPathName).join('/').replace(/-/g, ' ')
});
submissionDiscussionURL = url.format(newTopicURL);
}
}
return submissionDiscussionURL;
return utils.buildDiscussionUrl(submissionDiscussionURL,
req.gettext,
req.res.locals.current_url,
dataset,
place);
};

let pending = function(req, res) {
Expand Down Expand Up @@ -307,7 +286,7 @@ let pending = function(req, res) {
updateEvery: dataset.updateevery
});
}
let submissionDiscussionURL = _getDiscussionURL(req, dataset.name, place.name);
let submissionDiscussionURL = getDiscussionUrlForRequest(req, dataset.name, place.name);
Promise.join(qsSchemaPromise, questionsPromise, (qsSchema, questions) => {
if (qsSchema === undefined) qsSchema = [];
let match = {place: place.id, dataset: dataset.id};
Expand Down
37 changes: 36 additions & 1 deletion census/controllers/utils.js
Expand Up @@ -7,6 +7,9 @@ const ANONYMOUS_USER_ID = process.env.ANONYMOUS_USER_ID ||
const marked = require('marked');
const Promise = require('bluebird');
const config = require('../config');
const url = require('url');
const querystring = require('querystring');
const util = require('util');

var makeChoiceValidator = function(param) {
return function(req) {
Expand Down Expand Up @@ -296,6 +299,37 @@ var canReview = function(reviewers, user) {
return false;
};

let buildDiscussionUrl = function(submissionDiscussionURL,
gettext, pageUrl, dataset, place) {
/*
If `submissionDiscussionURL` is in the format:
https://discuss.okfn.org/c/<topic>/<subtopic>, return a new topic url with
a prepopulated topic for place and dataset. Otherwise, return the original
`submissionDiscussionURL` without modification.
*/
let parsedURL = url.parse(submissionDiscussionURL);
// URL is a discourse link
if (parsedURL.hostname === config.get('submission_discourse_hostname', '')) {
let splitPathName = _.trimLeft(parsedURL.pathname, '/').split('/');
// URL is a category link
if (splitPathName[0] === 'c') {
// Create a new topic link
let newTopicURL = url.parse('');
newTopicURL.protocol = parsedURL.protocol;
newTopicURL.host = parsedURL.host;
newTopicURL.pathname = 'new-topic';
newTopicURL.search = querystring.stringify({
title: util.format(gettext('Entry for %s / %s'), dataset, place),
body: util.format(gettext('This is a discussion about the submission for [%s / %s](%s).'),
dataset, place, pageUrl),
category: _.rest(splitPathName).join('/').replace(/-/g, ' ')
});
submissionDiscussionURL = url.format(newTopicURL);
}
}
return submissionDiscussionURL;
};

module.exports = {
validateData: validateData,
placeMapper: placeMapper,
Expand All @@ -306,5 +340,6 @@ module.exports = {
canReview: canReview,
FIELD_SPLITTER: FIELD_SPLITTER,
ANONYMOUS_USER_ID: ANONYMOUS_USER_ID,
commonFieldArray: commonFieldArray
commonFieldArray: commonFieldArray,
buildDiscussionUrl: buildDiscussionUrl
};
14 changes: 14 additions & 0 deletions census/views/entry.html
Expand Up @@ -191,6 +191,20 @@ <h4>{{ gettext("Submitters") }}</h4>
</div>
</div>
</section>

{% if discussionUrl %}
<section>
<div class="container">
<div class="row">
<div class="col-md-12">
<h3>{{ gettext("Discuss this entry") }}</h3>
<p>Do you want to discuss this entry? Start a <a target="_blank" href="{{ discussionUrl }}">new topic</a> on the forum!</p>
</div>
</div>
</div>
</section>
{% endif %}

{% endif %}

{% if not is_index %}
Expand Down
4 changes: 2 additions & 2 deletions index/generate.js
Expand Up @@ -188,11 +188,11 @@ Metalsmith(__dirname)
.clean(options.clean)
.use(godiGetData({domain: domain, year: year})) // Populate metadata with data from Survey
.use(jsonToFiles({use_metadata: true}))
.use(godiDataFiles()) // Add file metadata to each entry file populated by json-to-files
.use(paths({property: 'paths', directoryIndex: 'index.html'}))
.use(godiIndexSettings({domain: domain})) // Add data from Index settings.
.use(godiDataFiles()) // Add file metadata to each entry file populated by json-to-files
.use(markdown())
.use(permalinks())
.use(paths({property: 'paths', directoryIndex: 'index.html'}))
.use(layouts({
engine: 'nunjucks',
rename: true,
Expand Down
6 changes: 6 additions & 0 deletions index/metalsmith-godi-indexsettings.js
Expand Up @@ -55,6 +55,12 @@ function plugin(options) {
if (indexSettings.title) {
metadata.site_title = indexSettings.title;
}

// Add entry_discussion_url to metadata
if (indexSettings.entry_discussion_url) {
metadata.discussionUrl = indexSettings.entry_discussion_url;
}

done();
})
.catch(err => done(err));
Expand Down
12 changes: 12 additions & 0 deletions index/metalsmith-godi-updatedatafiles.js
Expand Up @@ -4,6 +4,9 @@ const _ = require('lodash');

const debug = require('debug')('metalsmith-godi-updatedatafiles');

const buildDiscussionUrl =
require('../census/controllers/utils.js').buildDiscussionUrl;

module.exports = plugin;

/**
Expand Down Expand Up @@ -32,6 +35,15 @@ function plugin(options) {
file.entry = file.data;
file.place = _.find(metadata.places, {id: file.entry.place});
file.dataset = _.find(metadata.datasets, {id: file.entry.dataset});
// Add discussion_url to entry data
if (metadata.discussionUrl) {
file.discussionUrl = buildDiscussionUrl(metadata.discussionUrl,
metadata.gettext,
`${metadata.site_url}${file.paths.href}`,
file.dataset.id,
file.place.id);
}

delete file.data;
}

Expand Down

0 comments on commit c155303

Please sign in to comment.