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

210 lines
5.0 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 { elasticSearch } from '../../helpers/elastic-search'
import { logger } from '../../helpers/logger'
import { CONFIG, ELASTIC_SEARCH_QUERY } from '../../initializers/constants'
import { DBChannel, EnhancedVideoChannel, IndexableChannel } from '../../types/channel.model'
import { ChannelsSearchQuery } from '../../types/search-query/channel-search.model'
2022-06-03 10:54:30 +02:00
import { buildSort, extractSearchQueryResult } from './elastic-search-queries'
2021-11-24 13:46:03 +01:00
import { buildChannelOrAccountCommonMapping, buildMultiMatchBool } from './shared'
2022-02-26 12:52:51 +01:00
import {
formatActorImageForAPI,
formatActorImageForDB,
formatActorImagesForAPI,
formatActorImagesForDB
} from './shared/elastic-search-avatar'
2021-06-24 15:18:54 +02:00
async function queryChannels (search: ChannelsSearchQuery) {
const bool: any = {}
const mustNot: any[] = []
2021-07-28 11:22:42 +02:00
const filter: any[] = []
2021-06-24 15:18:54 +02:00
if (search.search) {
2021-11-24 13:46:03 +01:00
Object.assign(bool, buildMultiMatchBool(search.search, ELASTIC_SEARCH_QUERY.CHANNELS_MULTI_MATCH_FIELDS))
2021-06-24 15:18:54 +02:00
}
if (search.blockedAccounts) {
mustNot.push({
terms: {
'ownerAccount.handle': search.blockedAccounts
}
})
}
if (search.blockedHosts) {
mustNot.push({
terms: {
host: search.blockedHosts
}
})
}
2022-12-20 15:35:09 +01:00
mustNot.push({
term: {
videosCount: 0
}
})
2021-07-28 11:22:42 +02:00
if (search.host) {
filter.push({
term: {
host: search.host
}
})
}
2021-07-29 10:57:33 +02:00
if (search.handles) {
filter.push({
terms: {
handle: search.handles
}
})
}
2021-07-28 11:22:42 +02:00
if (filter.length !== 0) {
Object.assign(bool, { filter })
}
2021-06-24 15:18:54 +02:00
if (mustNot.length !== 0) {
Object.assign(bool, { must_not: mustNot })
}
const body = {
from: search.start,
size: search.count,
sort: buildSort(search.sort),
query: { bool }
}
logger.debug({ body }, 'Will query Elastic Search for channels.')
const res = await elasticSearch.search({
index: CONFIG.ELASTIC_SEARCH.INDEXES.CHANNELS,
body
})
2022-06-03 10:54:30 +02:00
return extractSearchQueryResult(res)
2021-06-24 15:18:54 +02:00
}
function formatChannelForAPI (c: DBChannel, fromHost?: string): EnhancedVideoChannel {
return {
id: c.id,
score: c.score,
url: c.url,
name: c.name,
host: c.host,
followingCount: c.followingCount,
followersCount: c.followersCount,
createdAt: c.createdAt,
updatedAt: c.updatedAt,
2022-02-26 12:52:51 +01:00
avatar: formatActorImageForAPI(c.avatar),
avatars: formatActorImagesForAPI(c.avatars, c.avatar),
banner: formatActorImageForAPI(c.banner),
banners: formatActorImagesForAPI(c.banners, c.banner),
2021-06-24 15:18:54 +02:00
displayName: c.displayName,
description: c.description,
support: c.support,
isLocal: fromHost === c.host,
2022-12-20 15:35:09 +01:00
videosCount: c.videosCount || 0,
2021-06-24 15:18:54 +02:00
ownerAccount: {
id: c.ownerAccount.id,
url: c.ownerAccount.url,
displayName: c.ownerAccount.displayName,
description: c.ownerAccount.description,
name: c.ownerAccount.name,
host: c.ownerAccount.host,
followingCount: c.ownerAccount.followingCount,
followersCount: c.ownerAccount.followersCount,
createdAt: c.ownerAccount.createdAt,
updatedAt: c.ownerAccount.updatedAt,
2022-02-26 12:52:51 +01:00
avatar: formatActorImageForAPI(c.ownerAccount.avatar),
avatars: formatActorImagesForAPI(c.ownerAccount.avatars, c.ownerAccount.avatar)
2021-06-24 15:18:54 +02:00
}
}
}
function formatChannelForDB (c: IndexableChannel): DBChannel {
return {
id: c.id,
name: c.name,
host: c.host,
url: c.url,
2022-02-26 12:52:51 +01:00
avatar: formatActorImageForDB(c.avatar, c.host),
avatars: formatActorImagesForDB(c.avatars, c.host),
banner: formatActorImageForDB(c.banner, c.host),
banners: formatActorImagesForDB(c.banners, c.host),
2021-06-24 15:18:54 +02:00
displayName: c.displayName,
indexedAt: new Date(),
followingCount: c.followingCount,
followersCount: c.followersCount,
createdAt: c.createdAt,
updatedAt: c.updatedAt,
description: c.description,
support: c.support,
2021-11-24 11:36:42 +01:00
videosCount: c.videosCount,
2021-06-24 15:18:54 +02:00
handle: `${c.name}@${c.host}`,
ownerAccount: {
id: c.ownerAccount.id,
url: c.ownerAccount.url,
displayName: c.ownerAccount.displayName,
description: c.ownerAccount.description,
name: c.ownerAccount.name,
host: c.ownerAccount.host,
followingCount: c.ownerAccount.followingCount,
followersCount: c.ownerAccount.followersCount,
createdAt: c.ownerAccount.createdAt,
updatedAt: c.ownerAccount.updatedAt,
handle: `${c.ownerAccount.name}@${c.ownerAccount.host}`,
2022-02-26 12:52:51 +01:00
avatar: formatActorImageForDB(c.ownerAccount.avatar, c.ownerAccount.host),
avatars: formatActorImagesForDB(c.ownerAccount.avatars, c.ownerAccount.host)
2021-06-24 15:18:54 +02:00
}
}
}
function buildChannelsMapping () {
const base = buildChannelOrAccountCommonMapping()
Object.assign(base, {
2021-11-24 11:36:42 +01:00
videosCount: {
type: 'long'
},
2021-06-24 15:18:54 +02:00
support: {
type: 'keyword'
},
ownerAccount: {
properties: buildChannelOrAccountCommonMapping()
}
2022-06-03 10:54:30 +02:00
} as Record<PropertyName, MappingProperty>)
2021-06-24 15:18:54 +02:00
return base
}
export {
buildChannelsMapping,
formatChannelForDB,
formatChannelForAPI,
queryChannels
}