From a92c61ab4934777a688aad12b3410e20907513b6 Mon Sep 17 00:00:00 2001 From: Sam Lord Date: Tue, 16 Nov 2021 15:48:52 +0000 Subject: [PATCH] Add @tryghost/mw-update-user-last-seen no issue --- ghost/mw-update-user-last-seen/.eslintrc.js | 6 ++ ghost/mw-update-user-last-seen/LICENSE | 21 ++++++ ghost/mw-update-user-last-seen/README.md | 39 +++++++++++ ghost/mw-update-user-last-seen/index.js | 1 + .../lib/mw-update-user-last-seen.js | 15 +++++ ghost/mw-update-user-last-seen/package.json | 30 +++++++++ .../test/.eslintrc.js | 6 ++ .../test/mw-update-user-last-seen.test.js | 64 +++++++++++++++++++ .../test/utils/assertions.js | 11 ++++ .../test/utils/index.js | 11 ++++ .../test/utils/overrides.js | 10 +++ 11 files changed, 214 insertions(+) create mode 100644 ghost/mw-update-user-last-seen/.eslintrc.js create mode 100644 ghost/mw-update-user-last-seen/LICENSE create mode 100644 ghost/mw-update-user-last-seen/README.md create mode 100644 ghost/mw-update-user-last-seen/index.js create mode 100644 ghost/mw-update-user-last-seen/lib/mw-update-user-last-seen.js create mode 100644 ghost/mw-update-user-last-seen/package.json create mode 100644 ghost/mw-update-user-last-seen/test/.eslintrc.js create mode 100644 ghost/mw-update-user-last-seen/test/mw-update-user-last-seen.test.js create mode 100644 ghost/mw-update-user-last-seen/test/utils/assertions.js create mode 100644 ghost/mw-update-user-last-seen/test/utils/index.js create mode 100644 ghost/mw-update-user-last-seen/test/utils/overrides.js diff --git a/ghost/mw-update-user-last-seen/.eslintrc.js b/ghost/mw-update-user-last-seen/.eslintrc.js new file mode 100644 index 0000000000..c9c1bcb522 --- /dev/null +++ b/ghost/mw-update-user-last-seen/.eslintrc.js @@ -0,0 +1,6 @@ +module.exports = { + plugins: ['ghost'], + extends: [ + 'plugin:ghost/node' + ] +}; diff --git a/ghost/mw-update-user-last-seen/LICENSE b/ghost/mw-update-user-last-seen/LICENSE new file mode 100644 index 0000000000..366ae5f624 --- /dev/null +++ b/ghost/mw-update-user-last-seen/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2013-2021 Ghost Foundation + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/ghost/mw-update-user-last-seen/README.md b/ghost/mw-update-user-last-seen/README.md new file mode 100644 index 0000000000..603288780a --- /dev/null +++ b/ghost/mw-update-user-last-seen/README.md @@ -0,0 +1,39 @@ +# Mw Update User Last Seen + +## Install + +`npm install @tryghost/mw-update-user-last-seen --save` + +or + +`yarn add @tryghost/mw-update-user-last-seen` + + +## Usage + + +## Develop + +This is a mono repository, managed with [lerna](https://lernajs.io/). + +Follow the instructions for the top-level repo. +1. `git clone` this repo & `cd` into it as usual +2. Run `yarn` to install top-level dependencies. + + +## Run + +- `yarn dev` + + +## Test + +- `yarn lint` run just eslint +- `yarn test` run lint and tests + + + + +# Copyright & License + +Copyright (c) 2013-2021 Ghost Foundation - Released under the [MIT license](LICENSE). \ No newline at end of file diff --git a/ghost/mw-update-user-last-seen/index.js b/ghost/mw-update-user-last-seen/index.js new file mode 100644 index 0000000000..5a65cfe8a5 --- /dev/null +++ b/ghost/mw-update-user-last-seen/index.js @@ -0,0 +1 @@ +module.exports = require('./lib/mw-update-user-last-seen'); diff --git a/ghost/mw-update-user-last-seen/lib/mw-update-user-last-seen.js b/ghost/mw-update-user-last-seen/lib/mw-update-user-last-seen.js new file mode 100644 index 0000000000..4ba7fc916c --- /dev/null +++ b/ghost/mw-update-user-last-seen/lib/mw-update-user-last-seen.js @@ -0,0 +1,15 @@ +const constants = require('@tryghost/constants'); + +module.exports = function updateUserLastSeenMiddleware(req, _res, next) { + if (!req.user) { + return next(); + } + + if (Date.now() - req.user.get('last_seen') < constants.ONE_HOUR_MS) { + return next(); + } + + req.user.updateLastSeen().then(() => { + next(); + }, next); +}; \ No newline at end of file diff --git a/ghost/mw-update-user-last-seen/package.json b/ghost/mw-update-user-last-seen/package.json new file mode 100644 index 0000000000..f9fe7f4164 --- /dev/null +++ b/ghost/mw-update-user-last-seen/package.json @@ -0,0 +1,30 @@ +{ + "name": "@tryghost/mw-update-user-last-seen", + "version": "0.0.0", + "repository": "https://github.com/TryGhost/Utils/tree/main/packages/mw-update-user-last-seen", + "author": "Ghost Foundation", + "license": "MIT", + "main": "index.js", + "scripts": { + "dev": "echo \"Implement me!\"", + "test": "NODE_ENV=testing c8 --check-coverage mocha './test/**/*.test.js'", + "lint": "eslint . --ext .js --cache", + "posttest": "yarn lint" + }, + "files": [ + "index.js", + "lib" + ], + "publishConfig": { + "access": "public" + }, + "devDependencies": { + "c8": "7.10.0", + "mocha": "9.1.3", + "should": "13.2.3", + "sinon": "12.0.1" + }, + "dependencies": { + "@tryghost/constants": "1.0.0" + } +} diff --git a/ghost/mw-update-user-last-seen/test/.eslintrc.js b/ghost/mw-update-user-last-seen/test/.eslintrc.js new file mode 100644 index 0000000000..829b601eb0 --- /dev/null +++ b/ghost/mw-update-user-last-seen/test/.eslintrc.js @@ -0,0 +1,6 @@ +module.exports = { + plugins: ['ghost'], + extends: [ + 'plugin:ghost/test' + ] +}; diff --git a/ghost/mw-update-user-last-seen/test/mw-update-user-last-seen.test.js b/ghost/mw-update-user-last-seen/test/mw-update-user-last-seen.test.js new file mode 100644 index 0000000000..272019af6d --- /dev/null +++ b/ghost/mw-update-user-last-seen/test/mw-update-user-last-seen.test.js @@ -0,0 +1,64 @@ +// Switch these lines once there are useful utils +// const testUtils = require('./utils'); +require('./utils'); + +const updateUserLastSeenMiddleware = require('../lib/mw-update-user-last-seen'); + +class UserMock { + constructor(initialProps) { + this._props = initialProps; + } + + get(property) { + return this._props[property]; + } + + updateLastSeen() { + this._props.last_seen = Date.now(); + return Promise.resolve(); + } +} + +describe('Update User Last Seen Middleware', function () { + it('Does nothing when no user exists', function (done) { + updateUserLastSeenMiddleware({}, undefined, () => { + done(); + }); + }); + + it('Does nothing when user last seen in future', function (done) { + const lastSeen = Date.now() + 10 * 1000; + const user = new UserMock({ + last_seen: lastSeen + }); + + updateUserLastSeenMiddleware({user}, undefined, () => { + user.get('last_seen').should.equal(lastSeen); + done(); + }); + }); + + it('Does nothing when user last seen less than 1 hour ago', function (done) { + const lastSeen = Date.now() - 59 * 60 * 1000; + const user = new UserMock({ + last_seen: lastSeen + }); + + updateUserLastSeenMiddleware({user}, undefined, () => { + user.get('last_seen').should.equal(lastSeen); + done(); + }); + }); + + it('Sets the last seen when the user last seen over 1 hour ago', function (done) { + const lastSeen = Date.now() - 61 * 60 * 1000; + const user = new UserMock({ + last_seen: lastSeen + }); + + updateUserLastSeenMiddleware({user}, undefined, () => { + user.get('last_seen').should.not.equal(lastSeen); + done(); + }); + }); +}); diff --git a/ghost/mw-update-user-last-seen/test/utils/assertions.js b/ghost/mw-update-user-last-seen/test/utils/assertions.js new file mode 100644 index 0000000000..7364ee8aa1 --- /dev/null +++ b/ghost/mw-update-user-last-seen/test/utils/assertions.js @@ -0,0 +1,11 @@ +/** + * Custom Should Assertions + * + * Add any custom assertions to this file. + */ + +// Example Assertion +// should.Assertion.add('ExampleAssertion', function () { +// this.params = {operator: 'to be a valid Example Assertion'}; +// this.obj.should.be.an.Object; +// }); diff --git a/ghost/mw-update-user-last-seen/test/utils/index.js b/ghost/mw-update-user-last-seen/test/utils/index.js new file mode 100644 index 0000000000..0d67d86ff8 --- /dev/null +++ b/ghost/mw-update-user-last-seen/test/utils/index.js @@ -0,0 +1,11 @@ +/** + * Test Utilities + * + * Shared utils for writing tests + */ + +// Require overrides - these add globals for tests +require('./overrides'); + +// Require assertions - adds custom should assertions +require('./assertions'); diff --git a/ghost/mw-update-user-last-seen/test/utils/overrides.js b/ghost/mw-update-user-last-seen/test/utils/overrides.js new file mode 100644 index 0000000000..90203424ee --- /dev/null +++ b/ghost/mw-update-user-last-seen/test/utils/overrides.js @@ -0,0 +1,10 @@ +// This file is required before any test is run + +// Taken from the should wiki, this is how to make should global +// Should is a global in our eslint test config +global.should = require('should').noConflict(); +should.extend(); + +// Sinon is a simple case +// Sinon is a global in our eslint test config +global.sinon = require('sinon');