From 5221ebbe9a63f50abb3d7f3565af64cd7060848d Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Mon, 12 Feb 2018 16:51:39 -0700 Subject: [PATCH] added docs, added new logger --- .eslintignore | 2 +- .gitignore | 47 ++++++++-------- app.js | 8 --- docs/_docs/04-03-additional-plugins.md | 33 +++++++++++- docs/_sass/talk.scss | 2 +- graph/context.js | 16 +++--- graph/index.js | 2 +- graph/resolvers/root_query.js | 2 +- graph/resolvers/user.js | 2 +- package.json | 3 +- .../.eslintrc.json | 0 .../index.js | 7 ++- .../translations.yml | 0 .../server/NotificationManager.js | 17 ++++-- routes/index.js | 7 +++ services/logging.js | 16 ++++++ yarn.lock | 53 +++++++++++++++++-- 17 files changed, 160 insertions(+), 57 deletions(-) rename plugins/{talk-plugin-notifications-reply => talk-plugin-notifications-category-reply}/.eslintrc.json (100%) rename plugins/{talk-plugin-notifications-reply => talk-plugin-notifications-category-reply}/index.js (91%) rename plugins/{talk-plugin-notifications-reply => talk-plugin-notifications-category-reply}/translations.yml (100%) create mode 100644 services/logging.js diff --git a/.eslintignore b/.eslintignore index 50c6db9f9..92cd969fb 100644 --- a/.eslintignore +++ b/.eslintignore @@ -20,7 +20,7 @@ public !plugins/talk-plugin-mod !plugins/talk-plugin-moderation-actions !plugins/talk-plugin-notifications -!plugins/talk-plugin-notifications-reply +!plugins/talk-plugin-notifications-category-reply !plugins/talk-plugin-offtopic !plugins/talk-plugin-permalink !plugins/talk-plugin-remember-sort diff --git a/.gitignore b/.gitignore index d6fe0880c..37560d6a5 100644 --- a/.gitignore +++ b/.gitignore @@ -23,37 +23,38 @@ browserstack.err plugins.json plugins/* + !plugins/talk-plugin-akismet -!plugins/talk-plugin-facebook-auth !plugins/talk-plugin-auth -!plugins/talk-plugin-respect -!plugins/talk-plugin-offtopic -!plugins/talk-plugin-like -!plugins/talk-plugin-mod -!plugins/talk-plugin-love -!plugins/talk-plugin-viewing-options +!plugins/talk-plugin-author-menu !plugins/talk-plugin-comment-content -!plugins/talk-plugin-permalink +!plugins/talk-plugin-deep-reply-count +!plugins/talk-plugin-facebook-auth !plugins/talk-plugin-featured-comments -!plugins/talk-plugin-toxic-comments -!plugins/talk-plugin-sort-newest -!plugins/talk-plugin-sort-oldest -!plugins/talk-plugin-sort-most-replied +!plugins/talk-plugin-flag-details +!plugins/talk-plugin-ignore-user +!plugins/talk-plugin-like +!plugins/talk-plugin-love +!plugins/talk-plugin-member-since +!plugins/talk-plugin-mod +!plugins/talk-plugin-moderation-actions +!plugins/talk-plugin-notifications +!plugins/talk-plugin-notifications-category-reply +!plugins/talk-plugin-offtopic +!plugins/talk-plugin-permalink +!plugins/talk-plugin-remember-sort +!plugins/talk-plugin-respect +!plugins/talk-plugin-slack-notifications !plugins/talk-plugin-sort-most-liked !plugins/talk-plugin-sort-most-loved +!plugins/talk-plugin-sort-most-replied !plugins/talk-plugin-sort-most-respected -!plugins/talk-plugin-author-menu -!plugins/talk-plugin-member-since -!plugins/talk-plugin-ignore-user -!plugins/talk-plugin-moderation-actions -!plugins/talk-plugin-toxic-comments -!plugins/talk-plugin-remember-sort -!plugins/talk-plugin-deep-reply-count +!plugins/talk-plugin-sort-newest +!plugins/talk-plugin-sort-oldest !plugins/talk-plugin-subscriber -!plugins/talk-plugin-flag-details -!plugins/talk-plugin-slack-notifications -!plugins/talk-plugin-notifications -!plugins/talk-plugin-notifications-reply +!plugins/talk-plugin-toxic-comments +!plugins/talk-plugin-toxic-comments +!plugins/talk-plugin-viewing-options **/node_modules/* yarn-error.log diff --git a/app.js b/app.js index 225b5b480..85e7a01fb 100644 --- a/app.js +++ b/app.js @@ -1,7 +1,6 @@ const express = require('express'); const morgan = require('morgan'); const path = require('path'); -const uuid = require('uuid'); const merge = require('lodash/merge'); const helmet = require('helmet'); const plugins = require('./services/plugins'); @@ -14,13 +13,6 @@ const { ENABLE_TRACING, APOLLO_ENGINE_KEY, PORT } = require('./config'); const app = express(); -// Request Identity Middleware -app.use((req, res, next) => { - req.id = uuid.v4(); - - next(); -}); - //============================================================================== // PLUGIN PRE APPLICATION MIDDLEWARE //============================================================================== diff --git a/docs/_docs/04-03-additional-plugins.md b/docs/_docs/04-03-additional-plugins.md index 3d342fad7..bbb4c5cff 100644 --- a/docs/_docs/04-03-additional-plugins.md +++ b/docs/_docs/04-03-additional-plugins.md @@ -117,4 +117,35 @@ point with the still spam-like comment, the comment will be reported as containi Configuration: - `TALK_AKISMET_API_KEY` (**required**) - The Akismet API key located on your account page -- `TALK_AKISMET_SITE` (**required**) - The URL for your site that the comment are appearing on (not the root url, the url for the articles) \ No newline at end of file +- `TALK_AKISMET_SITE` (**required**) - The URL for your site that the comment are appearing on (not the root url, the url for the articles) + +## talk-plugin-notifications + +Source: [plugins/talk-plugin-notifications](https://github.com/coralproject/talk/tree/master/plugins/talk-plugin-notifications){:target="_blank"} + +Enables the Notification system for sending out enabled email notifications to +users when they interact with Talk. By itself, this plugin will not send +anything. You need to enable one of the `talk-plugin-notifications-category-*` plugins. + +**Note that all `talk-plugin-notifications-*` plugins must be registered +*before* this plugin in order to work. For example:** + +```js +{ + "server": [ + // ... + "talk-plugin-notifications-category-reply", + "talk-plugin-notifications", + // ... + ] +} +``` +{:.no-copy} + +### talk-plugin-notifications-category-reply +{:.param} + +Source: [plugins/talk-plugin-notifications-category-reply](https://github.com/coralproject/talk/tree/master/plugins/talk-plugin-notifications-category-reply){:target="_blank"} + +Replies made to each user will trigger an email to be sent with the notification +details if enabled. \ No newline at end of file diff --git a/docs/_sass/talk.scss b/docs/_sass/talk.scss index dfacc549c..4b779962c 100644 --- a/docs/_sass/talk.scss +++ b/docs/_sass/talk.scss @@ -264,7 +264,7 @@ pre { .toc { a { @extend .coral-link; - border-bottom: none; + border-bottom: none !important; } } diff --git a/graph/context.js b/graph/context.js index 182ed60ad..df4794302 100644 --- a/graph/context.js +++ b/graph/context.js @@ -1,12 +1,12 @@ const loaders = require('./loaders'); const mutators = require('./mutators'); -const uuid = require('uuid'); -const merge = require('lodash/merge'); +const uuid = require('uuid/v4'); const connectors = require('./connectors'); - +const { get, merge } = require('lodash'); const plugins = require('../services/plugins'); const { getBroker } = require('./subscriptions/broker'); const debug = require('debug')('talk:graph:context'); +const { createLogger } = require('../services/logging'); /** * Contains the array of plugins that provide context to the server, these top @@ -50,10 +50,11 @@ class Context { // one. this.id = parent.id || uuid.v4(); + // Attach a logger or create one. + this.log = parent.log || createLogger('context', this.id); + // Load the current logged in user to `user`, otherwise this will be null. - if (parent.user) { - this.user = parent.user; - } + this.user = get(parent, 'user', null); // Attach the connectors. this.connectors = connectors; @@ -75,7 +76,8 @@ class Context { } /** - * + * forSystem returns a system context object that can be used for internal + * operations. */ static forSystem() { const { models: { User } } = connectors; diff --git a/graph/index.js b/graph/index.js index 8b9343c56..97974d30c 100644 --- a/graph/index.js +++ b/graph/index.js @@ -11,7 +11,7 @@ module.exports = { // Load in the new context here, this will create the loaders + mutators for // the lifespan of this request. - context: new Context(req), + context: new Context(req.context), // Tracing request options, needed for Apollo Engine. tracing: ENABLE_TRACING, diff --git a/graph/resolvers/root_query.js b/graph/resolvers/root_query.js index 51173f34f..4447646f1 100644 --- a/graph/resolvers/root_query.js +++ b/graph/resolvers/root_query.js @@ -1,4 +1,4 @@ -const { decorateWithPermissionCheck, checkSelfField } = require('./util'); +const { decorateWithPermissionCheck } = require('./util'); const { SEARCH_ASSETS, SEARCH_OTHERS_COMMENTS, diff --git a/graph/resolvers/user.js b/graph/resolvers/user.js index b86b85fea..c18c3ce97 100644 --- a/graph/resolvers/user.js +++ b/graph/resolvers/user.js @@ -19,7 +19,7 @@ const User = { action_summaries({ id }, _, { loaders: { Actions } }) { return Actions.getSummariesByItemID.load(id); }, - actions({ id }, _, { user, loaders: { Actions } }) { + actions({ id }, _, { loaders: { Actions } }) { return Actions.getByID.load(id); }, comments({ id }, { query }, { loaders: { Comments } }) { diff --git a/package.json b/package.json index 1c801db93..af45c04a5 100644 --- a/package.json +++ b/package.json @@ -74,6 +74,7 @@ "bcryptjs": "^2.4.3", "bowser": "^1.7.2", "brotli-webpack-plugin": "^0.5.0", + "bunyan": "^1.8.12", "cli-table": "^0.3.1", "clipboard": "^1.7.1", "colors": "^1.1.2", @@ -132,7 +133,7 @@ "minimist": "^1.2.0", "moment": "^2.18.1", "mongoose": "^4.12.3", - "morgan": "1.9.0", + "morgan": "^1.9.0", "ms": "^2.0.0", "murmurhash-js": "^1.0.0", "node-emoji": "^1.8.1", diff --git a/plugins/talk-plugin-notifications-reply/.eslintrc.json b/plugins/talk-plugin-notifications-category-reply/.eslintrc.json similarity index 100% rename from plugins/talk-plugin-notifications-reply/.eslintrc.json rename to plugins/talk-plugin-notifications-category-reply/.eslintrc.json diff --git a/plugins/talk-plugin-notifications-reply/index.js b/plugins/talk-plugin-notifications-category-reply/index.js similarity index 91% rename from plugins/talk-plugin-notifications-reply/index.js rename to plugins/talk-plugin-notifications-category-reply/index.js index 6d9f20fd7..cbbb9171f 100644 --- a/plugins/talk-plugin-notifications-reply/index.js +++ b/plugins/talk-plugin-notifications-category-reply/index.js @@ -1,4 +1,3 @@ -const debug = require('debug')('talk-plugin-notifications-reply'); const { graphql } = require('graphql'); const { get } = require('lodash'); const path = require('path'); @@ -9,7 +8,7 @@ const handle = async (ctx, comment) => { // Check to see if this is a reply to an existing comment. const parentID = get(comment, 'parent_id', null); if (parentID === null) { - debug('could not get parent comment id'); + ctx.log.debug('could not get parent comment id'); return; } @@ -51,14 +50,14 @@ const handle = async (ctx, comment) => { const userID = get(reply, 'data.comment.user.id', null); if (!userID) { - debug('could not get parent comment user id'); + ctx.log.debug('could not get parent comment user id'); return; } // Check to see if this is yourself replying to yourself, if that's the case // don't send a notification. if (userID === get(comment, 'author_id')) { - debug('user id of parent comment is the same as the new comment'); + ctx.log.debug('user id of parent comment is the same as the new comment'); return; } diff --git a/plugins/talk-plugin-notifications-reply/translations.yml b/plugins/talk-plugin-notifications-category-reply/translations.yml similarity index 100% rename from plugins/talk-plugin-notifications-reply/translations.yml rename to plugins/talk-plugin-notifications-category-reply/translations.yml diff --git a/plugins/talk-plugin-notifications/server/NotificationManager.js b/plugins/talk-plugin-notifications/server/NotificationManager.js index 556a97d85..4e3c3e87d 100644 --- a/plugins/talk-plugin-notifications/server/NotificationManager.js +++ b/plugins/talk-plugin-notifications/server/NotificationManager.js @@ -65,7 +65,7 @@ class NotificationManager { // Send the notification. return this.send(ctx, userID, date, handler, context); } catch (err) { - // TODO: handle error. + ctx.log.error({ err }, 'could not handle the event'); return; } }) @@ -114,14 +114,18 @@ class NotificationManager { 'organizationName' ); if (organizationName === null) { - // TODO: handle error + ctx.log.debug( + 'could not send the notification, organization name not in settings' + ); return; } // Get the User's email. const to = await this.getEmail(ctx, userID); if (!to) { - // TODO: handle error + ctx.log.debug( + 'could not send the notification, destination email address not available' + ); return; } @@ -142,9 +146,12 @@ class NotificationManager { to, }); - debug(`Sent the notification for Job.ID[${task.id}]`); + ctx.log.debug(`Sent the notification for Job.ID[${task.id}]`); } catch (err) { - // TODO: print out the error. + ctx.log.error( + { err }, + 'could not send the notification, an error occurred' + ); return; } } diff --git a/routes/index.js b/routes/index.js index 0eb5b6217..03ababb31 100644 --- a/routes/index.js +++ b/routes/index.js @@ -12,6 +12,7 @@ const staticTemplate = require('../middleware/staticTemplate'); const staticMiddleware = require('express-static-gzip'); const { DISABLE_STATIC_SERVER } = require('../config'); const { createGraphOptions } = require('../graph'); +const Context = require('../graph/context'); const { passport } = require('../services/passport'); const { MOUNT_PATH } = require('../url'); @@ -67,6 +68,12 @@ if (!DISABLE_STATIC_SERVER) { // Add the i18n middleware to all routes. router.use(i18n); +// Bind a new context to the request. +router.use((req, res, next) => { + req.context = new Context(req); + next(); +}); + //============================================================================== // STATIC ROUTES //============================================================================== diff --git a/services/logging.js b/services/logging.js new file mode 100644 index 000000000..48367f834 --- /dev/null +++ b/services/logging.js @@ -0,0 +1,16 @@ +const { version } = require('../package.json'); +const Logger = require('bunyan'); +const uuid = require('uuid/v1'); + +// Create the logging instance that all logger's are branched from. +function createLogger(name, id = uuid()) { + return new Logger({ + src: true, + name, + id, + version, + serializers: { req: Logger.stdSerializers.req }, + }); +} + +module.exports = { createLogger }; diff --git a/yarn.lock b/yarn.lock index d13397fee..164064e2b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1480,6 +1480,15 @@ builtin-status-codes@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/builtin-status-codes/-/builtin-status-codes-3.0.0.tgz#85982878e21b98e1c66425e03d0174788f569ee8" +bunyan@^1.8.12: + version "1.8.12" + resolved "https://registry.yarnpkg.com/bunyan/-/bunyan-1.8.12.tgz#f150f0f6748abdd72aeae84f04403be2ef113797" + optionalDependencies: + dtrace-provider "~0.8" + moment "^2.10.6" + mv "~2" + safe-json-stringify "~1" + bytes@3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/bytes/-/bytes-3.0.0.tgz#d32815404d689699f85a4ea4fa8755dd13a96048" @@ -2590,6 +2599,12 @@ double-ended-queue@^2.1.0-0: version "2.1.0-0" resolved "https://registry.yarnpkg.com/double-ended-queue/-/double-ended-queue-2.1.0-0.tgz#103d3527fd31528f40188130c841efdd78264e5c" +dtrace-provider@~0.8: + version "0.8.6" + resolved "https://registry.yarnpkg.com/dtrace-provider/-/dtrace-provider-0.8.6.tgz#428a223afe03425d2cd6d6347fdf40c66903563d" + dependencies: + nan "^2.3.3" + duplexer3@^0.1.4: version "0.1.4" resolved "https://registry.yarnpkg.com/duplexer3/-/duplexer3-0.1.4.tgz#ee01dd1cac0ed3cbc7fdbea37dc0a8f1ce002ce2" @@ -3643,6 +3658,16 @@ glob@^5.0.3: once "^1.3.0" path-is-absolute "^1.0.0" +glob@^6.0.1: + version "6.0.4" + resolved "https://registry.yarnpkg.com/glob/-/glob-6.0.4.tgz#0f08860f6a155127b2fadd4f9ce24b1aab6e4d22" + dependencies: + inflight "^1.0.4" + inherits "2" + minimatch "2 || 3" + once "^1.3.0" + path-is-absolute "^1.0.0" + glob@^7.0.0, glob@^7.0.3, glob@^7.0.5, glob@^7.1.1, glob@^7.1.2: version "7.1.2" resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15" @@ -5890,7 +5915,7 @@ moment@^2.10.3: version "2.19.1" resolved "https://registry.yarnpkg.com/moment/-/moment-2.19.1.tgz#56da1a2d1cbf01d38b7e1afc31c10bcfa1929167" -moment@^2.18.1: +moment@^2.10.6, moment@^2.18.1: version "2.20.1" resolved "https://registry.yarnpkg.com/moment/-/moment-2.20.1.tgz#d6eb1a46cbcc14a2b2f9434112c1ff8907f313fd" @@ -5931,7 +5956,7 @@ moo-server@*, moo-server@1.3.x: version "1.3.0" resolved "https://registry.yarnpkg.com/moo-server/-/moo-server-1.3.0.tgz#5dc79569565a10d6efed5439491e69d2392e58f1" -morgan@1.9.0: +morgan@^1.9.0: version "1.9.0" resolved "https://registry.yarnpkg.com/morgan/-/morgan-1.9.0.tgz#d01fa6c65859b76fcf31b3cb53a3821a311d8051" dependencies: @@ -5993,7 +6018,15 @@ mute-stream@0.0.7: version "0.0.7" resolved "https://registry.yarnpkg.com/mute-stream/-/mute-stream-0.0.7.tgz#3075ce93bc21b8fab43e1bc4da7e8115ed1e7bab" -nan@^2.3.0, nan@^2.6.2: +mv@~2: + version "2.1.1" + resolved "https://registry.yarnpkg.com/mv/-/mv-2.1.1.tgz#ae6ce0d6f6d5e0a4f7d893798d03c1ea9559b6a2" + dependencies: + mkdirp "~0.5.1" + ncp "~2.0.0" + rimraf "~2.4.0" + +nan@^2.3.0, nan@^2.3.3, nan@^2.6.2: version "2.8.0" resolved "https://registry.yarnpkg.com/nan/-/nan-2.8.0.tgz#ed715f3fe9de02b57a5e6252d90a96675e1f085a" @@ -6022,6 +6055,10 @@ nconf@^0.8.4: secure-keys "^1.0.0" yargs "^3.19.0" +ncp@~2.0.0: + version "2.0.0" + resolved "https://registry.yarnpkg.com/ncp/-/ncp-2.0.0.tgz#195a21d6c46e361d2fb1281ba38b91e9df7bdbb3" + nearley@^2.7.10: version "2.11.0" resolved "https://registry.yarnpkg.com/nearley/-/nearley-2.11.0.tgz#5e626c79a6cd2f6ab9e7e5d5805e7668967757ae" @@ -8195,6 +8232,12 @@ rimraf@^2.2.8, rimraf@~2.5.2: dependencies: glob "^7.0.5" +rimraf@~2.4.0: + version "2.4.5" + resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.4.5.tgz#ee710ce5d93a8fdb856fb5ea8ff0e2d75934b2da" + dependencies: + glob "^6.0.1" + ripemd160@^2.0.0, ripemd160@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/ripemd160/-/ripemd160-2.0.1.tgz#0f4584295c53a3628af7e6d79aca21ce57d1c6e7" @@ -8235,6 +8278,10 @@ safe-buffer@5.1.1, safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.1, s version "5.1.1" resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.1.tgz#893312af69b2123def71f57889001671eeb2c853" +safe-json-stringify@~1: + version "1.0.4" + resolved "https://registry.yarnpkg.com/safe-json-stringify/-/safe-json-stringify-1.0.4.tgz#81a098f447e4bbc3ff3312a243521bc060ef5911" + samsam@1.1.2, samsam@~1.1: version "1.1.2" resolved "https://registry.yarnpkg.com/samsam/-/samsam-1.1.2.tgz#bec11fdc83a9fda063401210e40176c3024d1567"