From bffbebbe6b33ce306e7ec1b6051f0e51521c0440 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 21 Mar 2018 15:00:58 +0100 Subject: [PATCH] Fix issues with truncated description and utf characters --- server/helpers/database-utils.ts | 1 - server/lib/activitypub/process/process.ts | 3 +-- server/lib/activitypub/videos.ts | 2 +- server/models/video/video.ts | 15 ++++++++++++--- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/server/helpers/database-utils.ts b/server/helpers/database-utils.ts index 31808f9e4..d09b4b245 100644 --- a/server/helpers/database-utils.ts +++ b/server/helpers/database-utils.ts @@ -16,7 +16,6 @@ function retryTransactionWrapper ( .catch(err => callback(err)) }) .catch(err => { - console.error(err) logger.error(options.errorMessage, err) throw err }) diff --git a/server/lib/activitypub/process/process.ts b/server/lib/activitypub/process/process.ts index 7edf3bba0..094219489 100644 --- a/server/lib/activitypub/process/process.ts +++ b/server/lib/activitypub/process/process.ts @@ -40,8 +40,7 @@ async function processActivities (activities: Activity[], signatureActor?: Actor try { await activityProcessor(activity, inboxActor) } catch (err) { - logger.warn(err.stack) - logger.warn('Cannot process activity %s.', activity.type, err) + logger.warn('Cannot process activity %s.', activity.type, { error: err.stack }) } } } diff --git a/server/lib/activitypub/videos.ts b/server/lib/activitypub/videos.ts index e7b516129..907fe458d 100644 --- a/server/lib/activitypub/videos.ts +++ b/server/lib/activitypub/videos.ts @@ -187,7 +187,7 @@ async function getOrCreateAccountAndVideoAndChannel (videoObject: VideoTorrentOb } videoObject = await fetchRemoteVideo(videoObject) - if (!videoObject) throw new Error('Cannot fetch remote video') + if (!videoObject) throw new Error('Cannot fetch remote video (maybe invalid...)') } if (!actor) { diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 4e175c410..f43b73e49 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -1166,10 +1166,19 @@ export class VideoModel extends Model { getTruncatedDescription () { if (!this.description) return null - const options = { - length: CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max - } + const maxLength = CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max + const options = { + length: maxLength + } + const truncatedDescription = truncate(this.description, options) + + // The truncated string is okay, we can return it + if (truncatedDescription.length <= maxLength) return truncatedDescription + + // Lodash takes into account all UTF characters, whereas String.prototype.length does not: some characters have a length of 2 + // We always use the .length so we need to truncate more if needed + options.length -= maxLength - truncatedDescription.length return truncate(this.description, options) }