From 2cf70675dc39505a14974fc5bed3a968527906c0 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Tue, 15 Jun 2021 12:25:55 +0100 Subject: [PATCH] All: Fixed user content URLs when sharing note via Joplin Server --- .eslintignore | 3 +++ .gitignore | 3 +++ packages/app-desktop/runForSharing.sh | 4 ++-- packages/lib/BaseApplication.ts | 8 ++++---- packages/lib/JoplinServerApi.ts | 11 +++-------- .../personalizedUserContentBaseUrl.ts | 18 ++++++++++++++++++ packages/server/src/models/BaseModel.ts | 7 ++++++- packages/server/src/models/ShareModel.ts | 6 +++--- packages/server/src/utils/joplinUtils.ts | 2 +- 9 files changed, 43 insertions(+), 19 deletions(-) create mode 100644 packages/lib/services/joplinServer/personalizedUserContentBaseUrl.ts diff --git a/.eslintignore b/.eslintignore index 2780b675c..0488807a2 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1169,6 +1169,9 @@ packages/lib/services/interop/InteropService_Importer_Raw.js.map packages/lib/services/interop/types.d.ts packages/lib/services/interop/types.js packages/lib/services/interop/types.js.map +packages/lib/services/joplinServer/personalizedUserContentBaseUrl.d.ts +packages/lib/services/joplinServer/personalizedUserContentBaseUrl.js +packages/lib/services/joplinServer/personalizedUserContentBaseUrl.js.map packages/lib/services/keychain/KeychainService.d.ts packages/lib/services/keychain/KeychainService.js packages/lib/services/keychain/KeychainService.js.map diff --git a/.gitignore b/.gitignore index 296939af1..b6e80a919 100644 --- a/.gitignore +++ b/.gitignore @@ -1155,6 +1155,9 @@ packages/lib/services/interop/InteropService_Importer_Raw.js.map packages/lib/services/interop/types.d.ts packages/lib/services/interop/types.js packages/lib/services/interop/types.js.map +packages/lib/services/joplinServer/personalizedUserContentBaseUrl.d.ts +packages/lib/services/joplinServer/personalizedUserContentBaseUrl.js +packages/lib/services/joplinServer/personalizedUserContentBaseUrl.js.map packages/lib/services/keychain/KeychainService.d.ts packages/lib/services/keychain/KeychainService.js packages/lib/services/keychain/KeychainService.js.map diff --git a/packages/app-desktop/runForSharing.sh b/packages/app-desktop/runForSharing.sh index 6ea7f7a5a..aae4de022 100755 --- a/packages/app-desktop/runForSharing.sh +++ b/packages/app-desktop/runForSharing.sh @@ -26,7 +26,7 @@ if [ "$RESET_ALL" == "1" ]; then echo "config keychain.supported 0" >> "$CMD_FILE" echo "config sync.target 10" >> "$CMD_FILE" - echo "config sync.10.path http://api.joplincloud.local:22300" >> "$CMD_FILE" + # echo "config sync.10.path http://api.joplincloud.local:22300" >> "$CMD_FILE" echo "config sync.10.username $USER_EMAIL" >> "$CMD_FILE" echo "config sync.10.password 123456" >> "$CMD_FILE" @@ -45,4 +45,4 @@ if [ "$RESET_ALL" == "1" ]; then fi cd "$ROOT_DIR/packages/app-desktop" -npm start -- --profile "$PROFILE_DIR" +npm start -- --env dev --profile "$PROFILE_DIR" diff --git a/packages/lib/BaseApplication.ts b/packages/lib/BaseApplication.ts index 4247413d5..477f39bc1 100644 --- a/packages/lib/BaseApplication.ts +++ b/packages/lib/BaseApplication.ts @@ -766,10 +766,10 @@ export default class BaseApplication { } if (Setting.value('env') === Env.Dev) { - Setting.setValue('sync.10.path', 'https://api.joplincloud.com'); - Setting.setValue('sync.10.userContentPath', 'https://joplinusercontent.com'); - // Setting.setValue('sync.10.path', 'http://api.joplincloud.local:22300'); - // Setting.setValue('sync.10.userContentPath', 'http://joplinusercontent.local:22300'); + // Setting.setValue('sync.10.path', 'https://api.joplincloud.com'); + // Setting.setValue('sync.10.userContentPath', 'https://joplinusercontent.com'); + Setting.setValue('sync.10.path', 'http://api.joplincloud.local:22300'); + Setting.setValue('sync.10.userContentPath', 'http://joplinusercontent.local:22300'); } // For now always disable fuzzy search due to performance issues: diff --git a/packages/lib/JoplinServerApi.ts b/packages/lib/JoplinServerApi.ts index 334b36e2c..e244b0171 100644 --- a/packages/lib/JoplinServerApi.ts +++ b/packages/lib/JoplinServerApi.ts @@ -4,6 +4,7 @@ const { rtrimSlashes } = require('./path-utils.js'); import JoplinError from './JoplinError'; import { Env } from './models/Setting'; import Logger from './Logger'; +import personalizedUserContentBaseUrl from './services/joplinServer/personalizedUserContentBaseUrl'; const { stringify } = require('query-string'); const logger = Logger.create('JoplinServerApi'); @@ -56,14 +57,8 @@ export default class JoplinServerApi { return rtrimSlashes(this.options_.baseUrl()); } - public userContentBaseUrl(userId: string) { - if (this.options_.userContentBaseUrl()) { - if (!userId) throw new Error('User ID must be specified'); - const url = new URL(this.options_.userContentBaseUrl()); - return `${url.protocol}//${userId.substr(0, 10).toLowerCase()}.${url.host}`; - } else { - return this.baseUrl(); - } + public personalizedUserContentBaseUrl(userId: string) { + return personalizedUserContentBaseUrl(userId, this.baseUrl(), this.options_.userContentBaseUrl()); } private async session() { diff --git a/packages/lib/services/joplinServer/personalizedUserContentBaseUrl.ts b/packages/lib/services/joplinServer/personalizedUserContentBaseUrl.ts new file mode 100644 index 000000000..c3b38b99e --- /dev/null +++ b/packages/lib/services/joplinServer/personalizedUserContentBaseUrl.ts @@ -0,0 +1,18 @@ +// For this: +// +// userId: d67VzcrHs6zGzROagnzwhOZJI0vKbezc +// baseUrl: http://example.com +// userContentBaseUrl: http://usercontent.com +// +// => Returns http://d67Vzcrhs6.usercontent.com +// +// If the userContentBaseUrl is an empty string, the baseUrl is returned instead. +export default function(userId: string, baseUrl: string, userContentBaseUrl: string) { + if (userContentBaseUrl) { + if (!userId) throw new Error('User ID must be specified'); + const url = new URL(userContentBaseUrl); + return `${url.protocol}//${userId.substr(0, 10).toLowerCase()}.${url.host}`; + } else { + return baseUrl; + } +} diff --git a/packages/server/src/models/BaseModel.ts b/packages/server/src/models/BaseModel.ts index 71c8c03e5..a62f799ae 100644 --- a/packages/server/src/models/BaseModel.ts +++ b/packages/server/src/models/BaseModel.ts @@ -5,6 +5,7 @@ import { ErrorUnprocessableEntity, ErrorBadRequest } from '../utils/errors'; import { Models } from './factory'; import * as EventEmitter from 'events'; import { Config } from '../utils/types'; +import personalizedUserContentBaseUrl from '@joplin/lib/services/joplinServer/personalizedUserContentBaseUrl'; export interface SaveOptions { isNew?: boolean; @@ -64,10 +65,14 @@ export default abstract class BaseModel { return this.config_.baseUrl; } - protected get userContentUrl(): string { + protected get userContentBaseUrl(): string { return this.config_.userContentBaseUrl; } + protected personalizedUserContentBaseUrl(userId: Uuid): string { + return personalizedUserContentBaseUrl(userId, this.baseUrl, this.userContentBaseUrl); + } + protected get appName(): string { return this.config_.appName; } diff --git a/packages/server/src/models/ShareModel.ts b/packages/server/src/models/ShareModel.ts index cb313f563..35421331a 100644 --- a/packages/server/src/models/ShareModel.ts +++ b/packages/server/src/models/ShareModel.ts @@ -35,7 +35,7 @@ export default class ShareModel extends BaseModel { } public checkShareUrl(share: Share, shareUrl: string) { - if (this.baseUrl === this.userContentUrl) return; // OK + if (this.baseUrl === this.userContentBaseUrl) return; // OK const userId = userIdFromUserContentUrl(shareUrl); const shareUserId = share.owner_id.toLowerCase(); @@ -93,8 +93,8 @@ export default class ShareModel extends BaseModel { return !!r; } - public shareUrl(id: Uuid, query: any = null): string { - return setQueryParameters(`${this.userContentUrl}/shares/${id}`, query); + public shareUrl(shareOwnerId: Uuid, id: Uuid, query: any = null): string { + return setQueryParameters(`${this.personalizedUserContentBaseUrl(shareOwnerId)}/shares/${id}`, query); } public async byItemId(itemId: Uuid): Promise { diff --git a/packages/server/src/utils/joplinUtils.ts b/packages/server/src/utils/joplinUtils.ts index 0d14d9a02..ad08d8616 100644 --- a/packages/server/src/utils/joplinUtils.ts +++ b/packages/server/src/utils/joplinUtils.ts @@ -177,7 +177,7 @@ async function renderNote(share: Share, note: NoteEntity, resourceInfos: Resourc if (item.type_ === ModelType.Note) { return '#'; } else if (item.type_ === ModelType.Resource) { - return `${models_.share().shareUrl(share.id)}?resource_id=${item.id}&t=${item.updated_time}`; + return `${models_.share().shareUrl(share.owner_id, share.id)}?resource_id=${item.id}&t=${item.updated_time}`; } else { throw new Error(`Unsupported item type: ${item.type_}`); }