From 0ff54d8a7b2092528354b78d178efe88868fa218 Mon Sep 17 00:00:00 2001 From: wgroeneveld Date: Tue, 9 Mar 2021 21:24:22 +0100 Subject: [PATCH] add serve-my-jams getwebmention part --- .pnp.js | 11 +++++++ README.md | 8 +++++ package.json | 3 +- src/config.js | 14 +++++++++ src/index.js | 4 +++ src/webmention/get.js | 22 +++++++++++++ test/__mocks__/get-sample.json | 1 + test/__mocks__/got.js | 13 ++++++-- .../__mocks__/expected-goodreads-content.js | 0 test/{ => goodreads}/__mocks__/https.js | 2 +- .../__mocks__/howlongtobeat.js | 0 test/mastodon/feed-parser.test.js | 21 +++++++------ test/mocks.test.js | 8 ----- test/webmention/get.test.js | 31 +++++++++++++++++++ yarn.lock | 8 +++++ 15 files changed, 124 insertions(+), 22 deletions(-) create mode 100644 src/config.js create mode 100644 src/webmention/get.js create mode 100644 test/__mocks__/get-sample.json rename test/{ => goodreads}/__mocks__/expected-goodreads-content.js (100%) rename test/{ => goodreads}/__mocks__/https.js (70%) rename test/{ => howlongtobeat}/__mocks__/howlongtobeat.js (100%) delete mode 100644 test/mocks.test.js create mode 100644 test/webmention/get.test.js diff --git a/.pnp.js b/.pnp.js index cad9e48..9604206 100755 --- a/.pnp.js +++ b/.pnp.js @@ -44,6 +44,7 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { ["howlongtobeat", "npm:1.3.1"], ["jest", "npm:26.6.3"], ["lunr", "npm:2.3.9"], + ["mockdate", "npm:3.0.2"], ["parser-front-matter", "npm:1.6.4"] ], "linkType": "SOFT", @@ -3534,6 +3535,7 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { ["howlongtobeat", "npm:1.3.1"], ["jest", "npm:26.6.3"], ["lunr", "npm:2.3.9"], + ["mockdate", "npm:3.0.2"], ["parser-front-matter", "npm:1.6.4"] ], "linkType": "SOFT", @@ -4516,6 +4518,15 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { "linkType": "HARD", }] ]], + ["mockdate", [ + ["npm:3.0.2", { + "packageLocation": "./.yarn/cache/mockdate-npm-3.0.2-14482dcf0f-27a242abaa.zip/node_modules/mockdate/", + "packageDependencies": [ + ["mockdate", "npm:3.0.2"] + ], + "linkType": "HARD", + }] + ]], ["ms", [ ["npm:2.0.0", { "packageLocation": "./.yarn/cache/ms-npm-2.0.0-9e1101a471-1a230340cc.zip/node_modules/ms/", diff --git a/README.md b/README.md index ddcd306..d68502d 100644 --- a/README.md +++ b/README.md @@ -142,3 +142,11 @@ Usage example: It will print out games and metadata it found. Uses the cool [howlongtobeat npm package](https://www.npmjs.com/package/howlongtobeat) to do its dirty work. Working example: https://jefklakscodex.com/articles/reviews/diablo-3/ (on the left side). Check out the Hugo template to use the properties at https://github.com/wgroeneveld/jefklakscodex . + +### 5. Webmentions + +In cooperation with https://github.com/wgroeneveld/serve-my-jams + +#### 5.1 `getWebmentions` + +Calls the get webmention endpoint, sorts by date, adds metadata such as relative date (`x days ago`, property `publishedFromNow`), and returns data. Could be written in a `data` folder for Hugo to parse, for example. diff --git a/package.json b/package.json index 5f942c0..4e5ca9c 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "parser-front-matter": "^1.6.4" }, "devDependencies": { - "jest": "^26.6.3" + "jest": "^26.6.3", + "mockdate": "^3.0.2" } } diff --git a/src/config.js b/src/config.js new file mode 100644 index 0000000..ed317c3 --- /dev/null +++ b/src/config.js @@ -0,0 +1,14 @@ + +const serveMyJamDomain = "brainbaking.com" +const serveMyJamToken = "miauwkes" + +const serveMyJamEndpoint = "https://jam.brainbaking.com" + +// see https://github.com/wgroeneveld/serve-my-jams +// don't care if token is visible, just an extra security-by-obscurity step, everything is public anyway +module.exports = { + serveMyJamEndpoint, + + serveMyJamDomain, + serveMyJamToken +} diff --git a/src/index.js b/src/index.js index 5c65ce9..95c436c 100644 --- a/src/index.js +++ b/src/index.js @@ -2,6 +2,7 @@ const { parseMastoFeed } = require('./mastodon/feed-parser') const { widgetify } = require('./goodreads/widgetify.js') const { buildIndex } = require('./lunr/index-builder.js') const { howlong } = require('./howlongtobeat/howlong.js') +const { getWebmentions } = require('./webmention/get.js') module.exports = { mastodon: { @@ -15,5 +16,8 @@ module.exports = { }, howlongtobeat: { howlong: howlong + }, + webmention: { + getWebmentions: getWebmentions } }; diff --git a/src/webmention/get.js b/src/webmention/get.js new file mode 100644 index 0000000..0743ab7 --- /dev/null +++ b/src/webmention/get.js @@ -0,0 +1,22 @@ + +const got = require('got') +const config = require('../config') + +const dayjs = require('dayjs') +const relativeTime = require('dayjs/plugin/relativeTime') +dayjs.extend(relativeTime) + +async function getWebmentions() { + const url = `${config.serveMyJamEndpoint}/webmention/${config.serveMyJamDomain}/${config.serveMyJamToken}` + const result = await got(url) + + result.body.json.forEach(mention => { + mention.publishedFromNow = dayjs(mention.published).fromNow() + }) + + return result.body.json.sort((a, b) => dayjs(b.published).toDate() - dayjs(a.published).toDate()) +} + +module.exports = { + getWebmentions +} diff --git a/test/__mocks__/get-sample.json b/test/__mocks__/get-sample.json new file mode 100644 index 0000000..58b21bb --- /dev/null +++ b/test/__mocks__/get-sample.json @@ -0,0 +1 @@ +{"status":"success","json":[{"author":{"name":"Stampeding Longhorn"},"content":"undefined...","published":"2021-03-08T18:35:24","source":"https://brid.gy/like/mastodon/@wouter@chat.brainbaking.com/A4nx1rFwKUJYSe4TqK/A4nwg4LYyh4WgrJOXg","target":"https://brainbaking.com/post/2021/02/my-retro-desktop-setup/"},{"author":{"name":"Wouter Groeneveld"},"content":"@StampedingLonghorn I tried to chase him away, but you know how that turned out... 😼 There's even cat hair inside the cases... (to be clear: also unintentional)...","published":"2021-03-02T16:18:46.000Z","source":"https://brid.gy/comment/mastodon/@wouter@chat.brainbaking.com/A4nx1rFwKUJYSe4TqK/A4nxVFgAbVtebcM9gW","target":"https://brainbaking.com/post/2021/02/my-retro-desktop-setup/"},{"author":{"name":"https://jefklakscodex.com/about"},"content":"About the Codex | Jefklaks Codex","published":"2021-03-08T17:14:25","source":"https://jefklakscodex.com/about","target":"https://brainbaking.com/about/"},{"author":{"name":"Stampeding Longhorn"},"content":"@wouter The cat pictures are awesome....","published":"2021-03-02T16:17:18.000Z","source":"https://brid.gy/comment/mastodon/@wouter@chat.brainbaking.com/A4nx1rFwKUJYSe4TqK/A4nxN8ytch0BW2FVXU","target":"https://brainbaking.com/post/2021/02/my-retro-desktop-setup/"}]} \ No newline at end of file diff --git a/test/__mocks__/got.js b/test/__mocks__/got.js index 1ab33cb..70be0e3 100644 --- a/test/__mocks__/got.js +++ b/test/__mocks__/got.js @@ -1,7 +1,16 @@ const fs = require('fs').promises -async function got() { - return fs.readFile('./test/__mocks__/masto-feed-sample.xml', 'utf8'); +async function got(url) { + console.log(`through got mock, url ${url}`) + if(url.indexOf('/webmention') >= 0) { + const result = await fs.readFile(`./test/__mocks__/get-sample.json`, 'utf8'); + return { + body: JSON.parse(result) + } + } + + const result = await fs.readFile(`./test/__mocks__/masto-feed-sample.xml`, 'utf8'); + return result } module.exports = got diff --git a/test/__mocks__/expected-goodreads-content.js b/test/goodreads/__mocks__/expected-goodreads-content.js similarity index 100% rename from test/__mocks__/expected-goodreads-content.js rename to test/goodreads/__mocks__/expected-goodreads-content.js diff --git a/test/__mocks__/https.js b/test/goodreads/__mocks__/https.js similarity index 70% rename from test/__mocks__/https.js rename to test/goodreads/__mocks__/https.js index c272bf6..a88162a 100644 --- a/test/__mocks__/https.js +++ b/test/goodreads/__mocks__/https.js @@ -1,6 +1,6 @@ const fs = require('fs') -const fakedata = fs.readFileSync('./test/__mocks__/expected-goodreads-content.js') +const fakedata = fs.readFileSync('./test/goodreads/__mocks__/expected-goodreads-content.js') function getmock(url, callback) { callback({ diff --git a/test/__mocks__/howlongtobeat.js b/test/howlongtobeat/__mocks__/howlongtobeat.js similarity index 100% rename from test/__mocks__/howlongtobeat.js rename to test/howlongtobeat/__mocks__/howlongtobeat.js diff --git a/test/mastodon/feed-parser.test.js b/test/mastodon/feed-parser.test.js index 6be9349..e62b5e4 100644 --- a/test/mastodon/feed-parser.test.js +++ b/test/mastodon/feed-parser.test.js @@ -1,15 +1,16 @@ -const fs = require('fs'); -const fsp = require('fs').promises; -const { rmdir } = require('./../utils') - -const frontMatterParser = require('parser-front-matter'); - -jest.mock('got'); - -const { parseMastoFeed } = require('../../src/mastodon/feed-parser') -const dumpdir = `${__dirname}/dump` describe("mastodon feed parser tests", () => { + const fs = require('fs'); + const fsp = require('fs').promises; + const { rmdir } = require('./../utils') + + const frontMatterParser = require('parser-front-matter'); + + jest.mock('got'); + + const { parseMastoFeed } = require('../../src/mastodon/feed-parser') + const dumpdir = `${__dirname}/dump` + beforeEach(() => { if(fs.existsSync(dumpdir)) { rmdir(dumpdir) diff --git a/test/mocks.test.js b/test/mocks.test.js deleted file mode 100644 index 6c87914..0000000 --- a/test/mocks.test.js +++ /dev/null @@ -1,8 +0,0 @@ - -jest.mock('got'); -const gotmock = require("got"); - -test("got is mocked", async () => { - const data = await gotmock("bla"); - expect(data).toMatch(/brainbaking/) -}) diff --git a/test/webmention/get.test.js b/test/webmention/get.test.js new file mode 100644 index 0000000..2a1b399 --- /dev/null +++ b/test/webmention/get.test.js @@ -0,0 +1,31 @@ + +const MockDate = require('mockdate') +const dayjs = require('dayjs') + +describe("webmention receive serve-my-jam tests", () => { + + const { getWebmentions } = require('./../../src/webmention/get') + MockDate.set(dayjs('2021-03-11T19:00:00').toDate()) + + test("getWebmentions fetches from serve-my-jam depending on config", async () => { + const result = await getWebmentions() + expect(result.length).toBe(4) + }) + + test("getWebmentions enriches data with fromNow data", async () => { + const result = await getWebmentions() + const mention = result[0] + + expect(mention.published).toEqual("2021-03-08T18:35:24") + expect(mention.publishedFromNow).toEqual("3 days ago") + }) + + test("getWebmentions are sorted by published date descending", async() => { + const result = await getWebmentions() + + expect(result[0].published).toEqual("2021-03-08T18:35:24") + expect(result[1].published).toEqual("2021-03-08T17:14:25") + expect(result[2].published).toEqual("2021-03-02T16:18:46.000Z") + }) + +}) diff --git a/yarn.lock b/yarn.lock index 12c6c1b..6a2c01a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2954,6 +2954,7 @@ fsevents@^2.1.2: howlongtobeat: ^1.3.1 jest: ^26.6.3 lunr: ^2.3.9 + mockdate: ^3.0.2 parser-front-matter: ^1.6.4 languageName: unknown linkType: soft @@ -3834,6 +3835,13 @@ fsevents@^2.1.2: languageName: node linkType: hard +"mockdate@npm:^3.0.2": + version: 3.0.2 + resolution: "mockdate@npm:3.0.2" + checksum: 27a242abaa48f170f6174988b3cd597806715c11d8dc644efa466ca4f1b899e66e6d0618808df60f8a7807c89e6e316b832138f2028039d1caf23341e3d01f90 + languageName: node + linkType: hard + "ms@npm:2.0.0": version: 2.0.0 resolution: "ms@npm:2.0.0"