sepia-search-motore-di-rice.../server/lib/elastic-search/shared/elastic-search-avatar.ts

78 lines
1.7 KiB
TypeScript
Raw Normal View History

2022-06-03 10:54:30 +02:00
import { MappingProperty, PropertyName } from '@elastic/elasticsearch/lib/api/types'
2021-06-24 15:18:54 +02:00
import { ActorImage } from '../../../../PeerTube/shared/models'
import { buildUrl } from '../../../helpers/utils'
2020-02-19 15:39:35 +01:00
2022-02-26 12:52:51 +01:00
function formatActorImageForAPI (image?: ActorImage) {
if (!image) return null
2020-02-19 15:39:35 +01:00
return {
2022-02-26 12:52:51 +01:00
url: image.url,
path: image.path,
width: image.width,
createdAt: image.createdAt,
updatedAt: image.updatedAt
2020-02-19 15:39:35 +01:00
}
}
2022-02-26 12:52:51 +01:00
function formatActorImagesForAPI (images?: ActorImage[], image?: ActorImage) {
// Does not exist in PeerTube < 4.2
if (!images) {
if (!image) return []
return [ image ]
}
return images.map(a => formatActorImageForAPI(a))
}
// ---------------------------------------------------------------------------
function formatActorImageForDB (image: ActorImage, host: string) {
if (!image) return null
2020-02-19 15:39:35 +01:00
return {
2022-02-26 12:52:51 +01:00
url: buildUrl(host, image.path),
path: image.path,
width: image.width,
createdAt: image.createdAt,
updatedAt: image.updatedAt
2020-02-19 15:39:35 +01:00
}
}
2022-02-26 12:52:51 +01:00
function formatActorImagesForDB (images: ActorImage[], host: string) {
if (!images) return null
return images.map(image => formatActorImageForDB(image, host))
}
// ---------------------------------------------------------------------------
function buildActorImageMapping () {
2020-02-19 15:39:35 +01:00
return {
path: {
type: 'keyword'
},
2022-02-26 12:52:51 +01:00
width: {
type: 'long'
},
2020-02-19 15:39:35 +01:00
createdAt: {
type: 'date',
format: 'date_optional_time'
},
updatedAt: {
type: 'date',
format: 'date_optional_time'
}
2022-06-03 10:54:30 +02:00
} as Record<PropertyName, MappingProperty>
2020-02-19 15:39:35 +01:00
}
export {
2022-02-26 12:52:51 +01:00
formatActorImageForAPI,
formatActorImagesForAPI,
formatActorImageForDB,
formatActorImagesForDB,
buildActorImageMapping
2020-02-19 15:39:35 +01:00
}