Fix federation issue with some actor descriptions
authorChocobozzz <me@florianbigard.com>
Mon, 21 Oct 2019 07:48:21 +0000 (09:48 +0200)
committerChocobozzz <me@florianbigard.com>
Mon, 21 Oct 2019 07:52:51 +0000 (09:52 +0200)
server/helpers/core-utils.ts
server/helpers/custom-validators/activitypub/actor.ts
server/helpers/custom-validators/activitypub/videos.ts
server/helpers/youtube-dl.ts
server/models/video/video.ts

index 9ff67c43af4cb82cba3259a9bebb5dc7f95b780c..7e8252aa40f3db496d367303335ad9ab39b94f63 100644 (file)
@@ -179,18 +179,15 @@ function buildPath (path: string) {
 }
 
 // Consistent with .length, lodash truncate function is not
-function peertubeTruncate (str: string, maxLength: number) {
-  const options = {
-    length: maxLength
-  }
+function peertubeTruncate (str: string, options: { length: number, separator?: RegExp, omission?: string }) {
   const truncatedStr = truncate(str, options)
 
   // The truncated string is okay, we can return it
-  if (truncatedStr.length <= maxLength) return truncatedStr
+  if (truncatedStr.length <= options.length) return truncatedStr
 
   // 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 -= truncatedStr.length - maxLength
+  options.length -= truncatedStr.length - options.length
   return truncate(str, options)
 }
 
index 55bc8cc96c381b22b5349405f5340db9656a7729..4e9aabf0ed6f722599aad044ffa3fb538fb8acfa 100644 (file)
@@ -1,9 +1,9 @@
 import * as validator from 'validator'
 import { CONSTRAINTS_FIELDS } from '../../../initializers/constants'
 import { exists, isArray } from '../misc'
-import { truncate } from 'lodash'
 import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc'
 import { isHostValid } from '../servers'
+import { peertubeTruncate } from '@server/helpers/core-utils'
 
 function isActorEndpointsObjectValid (endpointObject: any) {
   return isActivityPubUrlValid(endpointObject.sharedInbox)
@@ -88,7 +88,7 @@ function normalizeActor (actor: any) {
   }
 
   if (actor.summary && typeof actor.summary === 'string') {
-    actor.summary = truncate(actor.summary, { length: CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max })
+    actor.summary = peertubeTruncate(actor.summary, { length: CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max })
 
     if (actor.summary.length < CONSTRAINTS_FIELDS.USERS.DESCRIPTION.min) {
       actor.summary = null
index 3ba6b07440cb07a3f943e77361f1d0d3b46245c0..02f91432699dda2c53e08eecad74c2e69594ff66 100644 (file)
@@ -155,7 +155,7 @@ function setValidRemoteVideoUrls (video: any) {
 
 function setRemoteVideoTruncatedContent (video: any) {
   if (video.content) {
-    video.content = peertubeTruncate(video.content, CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max)
+    video.content = peertubeTruncate(video.content, { length: CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max })
   }
 
   return true
index b3079370f0bd71ccebb7fd28172b708f4b881bce..87a0d0584e174ed73ee7c0da721893e60904f38e 100644 (file)
@@ -1,10 +1,9 @@
-import { truncate } from 'lodash'
 import { CONSTRAINTS_FIELDS, VIDEO_CATEGORIES } from '../initializers/constants'
 import { logger } from './logger'
 import { generateVideoImportTmpPath } from './utils'
 import { join } from 'path'
-import { root } from './core-utils'
-import { ensureDir, writeFile, remove } from 'fs-extra'
+import { peertubeTruncate, root } from './core-utils'
+import { ensureDir, remove, writeFile } from 'fs-extra'
 import * as request from 'request'
 import { createWriteStream } from 'fs'
 
@@ -212,20 +211,20 @@ function buildVideoInfo (obj: any) {
 }
 
 function titleTruncation (title: string) {
-  return truncate(title, {
-    'length': CONSTRAINTS_FIELDS.VIDEOS.NAME.max,
-    'separator': /,? +/,
-    'omission': ' […]'
+  return peertubeTruncate(title, {
+    length: CONSTRAINTS_FIELDS.VIDEOS.NAME.max,
+    separator: /,? +/,
+    omission: ' […]'
   })
 }
 
 function descriptionTruncation (description: string) {
   if (!description || description.length < CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.min) return undefined
 
-  return truncate(description, {
-    'length': CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max,
-    'separator': /,? +/,
-    'omission': ' […]'
+  return peertubeTruncate(description, {
+    length: CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max,
+    separator: /,? +/,
+    omission: ' […]'
   })
 }
 
index 6856dcd9f057baa5f7d0f7c895afee3d7df8e43f..0ee3feaf3dbc0bd90f96718dad8e02d7c0a38353 100644 (file)
@@ -1920,7 +1920,7 @@ export class VideoModel extends Model<VideoModel> {
     if (!this.description) return null
 
     const maxLength = CONSTRAINTS_FIELDS.VIDEOS.TRUNCATED_DESCRIPTION.max
-    return peertubeTruncate(this.description, maxLength)
+    return peertubeTruncate(this.description, { length: maxLength })
   }
 
   getOriginalFileResolution () {