Fix federation issue with some actor descriptions

This commit is contained in:
Chocobozzz 2019-10-21 09:48:21 +02:00
parent 4386e66e55
commit 687c6180bc
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
5 changed files with 17 additions and 21 deletions

View 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)
}

View 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

View 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

View 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: ' […]'
})
}

View 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 () {