From ac48f0e76b22c7557b0a27f7ef99d3c7b9273750 Mon Sep 17 00:00:00 2001 From: Jeff Sikes Date: Sun, 8 Oct 2023 22:23:51 -0500 Subject: [PATCH 1/2] Empty title fixes; formatting AP content updates --- server.js | 5 ++++- src/activitypub.js | 9 +++++---- src/pages/bookmark.hbs | 4 ++-- src/pages/bookmarks-xml.hbs | 6 ++++-- src/pages/partials/edit_bookmark.hbs | 4 +++- src/pages/partials/show_bookmark.hbs | 2 +- src/routes/bookmark.js | 6 +++--- src/util.js | 7 +++++++ 8 files changed, 29 insertions(+), 14 deletions(-) diff --git a/server.js b/server.js index f8f256f..31df5ca 100644 --- a/server.js +++ b/server.js @@ -3,7 +3,7 @@ import express from 'express'; import cors from 'cors'; import { create } from 'express-handlebars'; -import { domain, account, simpleLogger, actorInfo } from './src/util.js'; +import { domain, account, simpleLogger, actorInfo, replaceEmptyText } from './src/util.js'; import session, { isAuthenticated } from './src/session-auth.js'; import * as bookmarksDb from './src/bookmarks-db.js'; import * as apDb from './src/activity-pub-db.js'; @@ -97,6 +97,9 @@ const hbs = create({ eq(a, b, options) { return a === b ? options.fn(this) : options.inverse(this); }, + setTitle(item) { + return replaceEmptyText(item.title, item.url); + }, }, partialsDir: './src/pages/partials', extname: '.hbs', diff --git a/src/activitypub.js b/src/activitypub.js index 9896aa1..775ce03 100644 --- a/src/activitypub.js +++ b/src/activitypub.js @@ -2,7 +2,7 @@ import fetch from 'node-fetch'; import crypto from 'crypto'; import { signedGetJSON, signedPostJSON } from './signature.js'; -import { actorInfo, actorMatchesUsername } from './util.js'; +import { actorInfo, actorMatchesUsername, replaceEmptyText } from './util.js'; function getGuidFromPermalink(urlString) { return urlString.match(/m\/([a-zA-Z0-9+/]+)/)[1]; @@ -34,9 +34,10 @@ export function createNoteObject(bookmark, account, domain) { type: 'Note', published: d.toISOString(), attributedTo: `https://${domain}/u/${account}`, - content: ` - ${bookmark.title}
- ${bookmark.description?.replace('\n', '
') || ''}`, + content: `${replaceEmptyText( + bookmark.title, + bookmark.url, + )}
${bookmark.description?.trim().replace('\n', '
') || ''}`, to: [`https://${domain}/u/${account}/followers/`, 'https://www.w3.org/ns/activitystreams#Public'], tag: [], }; diff --git a/src/pages/bookmark.hbs b/src/pages/bookmark.hbs index d77d1f2..b4bf040 100644 --- a/src/pages/bookmark.hbs +++ b/src/pages/bookmark.hbs @@ -1,7 +1,7 @@

- {{bookmark.title}} - + {{setTitle bookmark}} +

{{> show_bookmark bookmark=bookmark permalink=true}} diff --git a/src/pages/bookmarks-xml.hbs b/src/pages/bookmarks-xml.hbs index 644f1bd..dfe78e1 100644 --- a/src/pages/bookmarks-xml.hbs +++ b/src/pages/bookmarks-xml.hbs @@ -9,12 +9,14 @@ {{#each bookmarks}} - {{title}} + {{setTitle this}} {{projectUrl}}/bookmark/{{id}} {{created_at}} - {{#if description}} + {{#if description }} {{description}} + {{ else }} + {{/if}} {{#each tag_array}} diff --git a/src/pages/partials/edit_bookmark.hbs b/src/pages/partials/edit_bookmark.hbs index e2c8fd0..1e65261 100644 --- a/src/pages/partials/edit_bookmark.hbs +++ b/src/pages/partials/edit_bookmark.hbs @@ -9,9 +9,11 @@
diff --git a/src/pages/partials/show_bookmark.hbs b/src/pages/partials/show_bookmark.hbs index 54f7d5f..972dc5d 100644 --- a/src/pages/partials/show_bookmark.hbs +++ b/src/pages/partials/show_bookmark.hbs @@ -2,7 +2,7 @@ {{^if permalink}} {{/if}} diff --git a/src/routes/bookmark.js b/src/routes/bookmark.js index ffa9224..9fe7c6d 100644 --- a/src/routes/bookmark.js +++ b/src/routes/bookmark.js @@ -171,8 +171,8 @@ router.post('/multiadd', isAuthenticated, async (req, res) => { await bookmarksDb.createBookmark({ url: link, - title: meta.result.ogTitle, - description: meta.result.ogDescription || ' ', // add *something*, even if ogDesc is empty (keeps Atom feed validation happy) + title: meta.result?.ogTitle, + description: (meta.result && meta.result.ogDescription) || ' ', // add *something*, even if ogDesc is empty (keeps Atom feed validation happy) }); }); @@ -248,7 +248,7 @@ router.post('/:id?', isAuthenticated, async (req, res) => { bookmark = await bookmarksDb.createBookmark({ // STRONG PARAMETERS url: mergedObject.url.trim(), - title: mergedObject.title?.trim() || 'Untitled', + title: mergedObject.title?.trim(), description: mergedObject.description?.trim() || '', tags, }); diff --git a/src/util.js b/src/util.js index 71c8088..87c15a7 100644 --- a/src/util.js +++ b/src/util.js @@ -116,6 +116,13 @@ export function actorMatchesUsername(actor, username) { return actorAccount === actorResult[3] && actorDomain === actorResult[1]; } +export function replaceEmptyText(currentValue, defaultValue) { + if (!currentValue || currentValue?.trim().replace(/\n/g, '') == '') { + return defaultValue; + } + return currentValue; +} + export function simpleLogger(req, res, next) { // middleware function const currentDatetime = new Date(); From fcd80cf687271ade851b03d273c595d02880923c Mon Sep 17 00:00:00 2001 From: Casey Kolderup Date: Sun, 8 Oct 2023 21:54:19 -0700 Subject: [PATCH 2/2] satisfy eslint --- src/util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util.js b/src/util.js index 87c15a7..0a41a4d 100644 --- a/src/util.js +++ b/src/util.js @@ -117,7 +117,7 @@ export function actorMatchesUsername(actor, username) { } export function replaceEmptyText(currentValue, defaultValue) { - if (!currentValue || currentValue?.trim().replace(/\n/g, '') == '') { + if (!currentValue || currentValue?.trim().replace(/\n/g, '') === '') { return defaultValue; } return currentValue;