はじまりの大地

このコミットが含まれているのは:
2024-07-15 09:14:04 +09:00
コミット 6632905f32
3501個のファイルの変更1439465行の追加0行の削除
+32
ファイルの表示
@@ -0,0 +1,32 @@
import { QueryTypes, Sequelize, Transaction } from 'sequelize'
/**
*
* Abstract builder to run video SQL queries
*
*/
export class AbstractRunQuery {
protected query: string
protected replacements: any = {}
constructor (protected readonly sequelize: Sequelize) {
}
protected runQuery (options: { nest?: boolean, transaction?: Transaction, logging?: boolean } = {}) {
const queryOptions = {
transaction: options.transaction,
logging: options.logging,
replacements: this.replacements,
type: QueryTypes.SELECT as QueryTypes.SELECT,
nest: options.nest ?? false
}
return this.sequelize.query<any>(this.query, queryOptions)
}
protected buildSelect (entities: string[]) {
return `SELECT ${entities.join(', ')} `
}
}
+9
ファイルの表示
@@ -0,0 +1,9 @@
export * from './abstract-run-query.js'
export * from './model-builder.js'
export * from './model-cache.js'
export * from './query.js'
export * from './sequelize-helpers.js'
export * from './sequelize-type.js'
export * from './sort.js'
export * from './sql.js'
export * from './update.js'
+119
ファイルの表示
@@ -0,0 +1,119 @@
import { logger } from '@server/helpers/logger.js'
import isPlainObject from 'lodash-es/isPlainObject.js'
import { ModelStatic, Sequelize, Model as SequelizeModel } from 'sequelize'
/**
*
* Build Sequelize models from sequelize raw query (that must use { nest: true } options)
*
* In order to sequelize to correctly build the JSON this class will ingest,
* the columns selected in the raw query should be in the following form:
* * All tables must be Pascal Cased (for example "VideoChannel")
* * Root table must end with `Model` (for example "VideoCommentModel")
* * Joined tables must contain the origin table name + '->JoinedTable'. For example:
* * "Actor" is joined to "Account": "Actor" table must be renamed "Account->Actor"
* * "Account->Actor" is joined to "Server": "Server" table must be renamed to "Account->Actor->Server"
* * Selected columns must be renamed to contain the JSON path:
* * "videoComment"."id": "VideoCommentModel"."id"
* * "Account"."Actor"."Server"."id": "Account.Actor.Server.id"
* * All tables must contain the row id
*/
export class ModelBuilder <T extends SequelizeModel> {
private readonly modelRegistry = new Map<string, T>()
constructor (private readonly sequelize: Sequelize) {
}
createModels (jsonArray: any[], baseModelName: string): T[] {
const result: T[] = []
for (const json of jsonArray) {
const { created, model } = this.createModel(json, baseModelName, json.id + '.' + baseModelName)
if (created) result.push(model)
}
return result
}
private createModel (json: any, modelName: string, keyPath: string) {
if (!json.id) return { created: false, model: null }
const { created, model } = this.createOrFindModel(json, modelName, keyPath)
for (const key of Object.keys(json)) {
const value = json[key]
if (!value) continue
// Child model
if (isPlainObject(value)) {
const { created, model: subModel } = this.createModel(value, key, `${keyPath}.${json.id}.${key}`)
if (!created || !subModel) continue
const Model = this.findModelBuilder(modelName)
const association = Model.associations[key]
if (!association) {
logger.error('Cannot find association %s of model %s', key, modelName, { associations: Object.keys(Model.associations) })
continue
}
if (association.isMultiAssociation) {
if (!Array.isArray(model[key])) model[key] = []
model[key].push(subModel)
} else {
model[key] = subModel
}
}
}
return { created, model }
}
private createOrFindModel (json: any, modelName: string, keyPath: string) {
const registryKey = this.getModelRegistryKey(json, keyPath)
if (this.modelRegistry.has(registryKey)) {
return {
created: false,
model: this.modelRegistry.get(registryKey)
}
}
const Model = this.findModelBuilder(modelName)
if (!Model) {
logger.error(
'Cannot build model %s that does not exist', this.buildSequelizeModelName(modelName),
{ existing: this.sequelize.modelManager.all.map(m => m.name) }
)
return { created: false, model: null }
}
const model = Model.build(json, { raw: true, isNewRecord: false })
this.modelRegistry.set(registryKey, model)
return { created: true, model }
}
private findModelBuilder (modelName: string) {
return this.sequelize.modelManager.getModel(this.buildSequelizeModelName(modelName)) as ModelStatic<T>
}
private buildSequelizeModelName (modelName: string) {
if (modelName === 'Avatars') return 'ActorImageModel'
if (modelName === 'ActorFollowing') return 'ActorModel'
if (modelName === 'ActorFollower') return 'ActorModel'
if (modelName === 'FlaggedAccount') return 'AccountModel'
if (modelName === 'CommentAutomaticTags') return 'CommentAutomaticTagModel'
return modelName + 'Model'
}
private getModelRegistryKey (json: any, keyPath: string) {
return keyPath + json.id
}
}
+94
ファイルの表示
@@ -0,0 +1,94 @@
import { Model } from 'sequelize-typescript'
import { logger } from '@server/helpers/logger.js'
type ModelCacheType =
'server-account'
| 'local-actor-name'
| 'local-actor-url'
| 'load-video-immutable-id'
| 'load-video-immutable-url'
type DeleteKey =
'video'
class ModelCache {
private static instance: ModelCache
private readonly localCache: { [id in ModelCacheType]: Map<string, any> } = {
'server-account': new Map(),
'local-actor-name': new Map(),
'local-actor-url': new Map(),
'load-video-immutable-id': new Map(),
'load-video-immutable-url': new Map()
}
private readonly deleteIds: {
[deleteKey in DeleteKey]: Map<number, { cacheType: ModelCacheType, key: string }[]>
} = {
video: new Map()
}
private constructor () {
}
static get Instance () {
return this.instance || (this.instance = new this())
}
doCache<T extends Model> (options: {
cacheType: ModelCacheType
key: string
fun: () => Promise<T>
whitelist?: () => boolean
deleteKey?: DeleteKey
}) {
const { cacheType, key, fun, whitelist, deleteKey } = options
if (whitelist && whitelist() !== true) return fun()
const cache = this.localCache[cacheType]
if (cache.has(key)) {
logger.debug('Model cache hit for %s -> %s.', cacheType, key)
return Promise.resolve<T>(cache.get(key))
}
return fun().then(m => {
if (!m) return m
if (!whitelist || whitelist()) cache.set(key, m)
if (deleteKey) {
const map = this.deleteIds[deleteKey]
if (!map.has(m.id)) map.set(m.id, [])
const a = map.get(m.id)
a.push({ cacheType, key })
}
return m
})
}
invalidateCache (deleteKey: DeleteKey, modelId: number) {
const map = this.deleteIds[deleteKey]
if (!map.has(modelId)) return
for (const toDelete of map.get(modelId)) {
logger.debug('Removing %s -> %d of model cache %s -> %s.', deleteKey, modelId, toDelete.cacheType, toDelete.key)
this.localCache[toDelete.cacheType].delete(toDelete.key)
}
map.delete(modelId)
}
clearCache (cacheType: ModelCacheType) {
this.localCache[cacheType] = new Map()
}
}
export {
ModelCache
}
+88
ファイルの表示
@@ -0,0 +1,88 @@
import { forceNumber } from '@peertube/peertube-core-utils'
import { BindOrReplacements, Op, QueryOptionsWithType, QueryTypes, Sequelize, Transaction } from 'sequelize'
import { Fn } from 'sequelize/types/utils'
import validator from 'validator'
async function doesExist (options: {
sequelize: Sequelize
query: string
bind?: BindOrReplacements
transaction?: Transaction
}) {
const { sequelize, query, bind, transaction } = options
const queryOptions: QueryOptionsWithType<QueryTypes.SELECT> = {
type: QueryTypes.SELECT,
bind,
raw: true,
transaction
}
const results = await sequelize.query(query, queryOptions)
return results.length === 1
}
// FIXME: have to specify the result type to not break peertube typings generation
function createSimilarityAttribute (col: string, value: string): Fn {
return Sequelize.fn(
'similarity',
searchTrigramNormalizeCol(col),
searchTrigramNormalizeValue(value)
)
}
function buildWhereIdOrUUID (id: number | string) {
return validator.default.isInt('' + id) ? { id } : { uuid: id }
}
function parseAggregateResult (result: any) {
if (!result) return 0
const total = forceNumber(result)
if (isNaN(total)) return 0
return total
}
function parseRowCountResult (result: any) {
if (result.length !== 0) return result[0].total
return 0
}
function createSafeIn (sequelize: Sequelize, toEscape: (string | number)[], additionalUnescaped: string[] = []) {
return toEscape.map(t => {
return t === null
? null
: sequelize.escape('' + t)
}).concat(additionalUnescaped).join(', ')
}
function searchAttribute (sourceField?: string, targetField?: string) {
if (!sourceField) return {}
return {
[targetField]: {
// FIXME: ts error
[Op.iLike as any]: `%${sourceField}%`
}
}
}
export {
buildWhereIdOrUUID, createSafeIn, createSimilarityAttribute, doesExist, parseAggregateResult,
parseRowCountResult, searchAttribute
}
// ---------------------------------------------------------------------------
function searchTrigramNormalizeValue (value: string) {
return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', value))
}
function searchTrigramNormalizeCol (col: string) {
return Sequelize.fn('lower', Sequelize.fn('immutable_unaccent', Sequelize.col(col)))
}
+39
ファイルの表示
@@ -0,0 +1,39 @@
import { Sequelize } from 'sequelize'
function isOutdated (model: { createdAt: Date, updatedAt: Date }, refreshInterval: number) {
if (!model.createdAt || !model.updatedAt) {
throw new Error('Miss createdAt & updatedAt attributes to model')
}
const now = Date.now()
const createdAtTime = model.createdAt.getTime()
const updatedAtTime = model.updatedAt.getTime()
return (now - createdAtTime) > refreshInterval && (now - updatedAtTime) > refreshInterval
}
function throwIfNotValid (value: any, validator: (value: any) => boolean, fieldName = 'value', nullable = false) {
if (nullable && (value === null || value === undefined)) return
if (validator(value) === false) {
throw new Error(`"${value}" is not a valid ${fieldName}.`)
}
}
function buildTrigramSearchIndex (indexName: string, attribute: string) {
return {
name: indexName,
// FIXME: gin_trgm_ops is not taken into account in Sequelize 6, so adding it ourselves in the literal function
fields: [ Sequelize.literal('lower(immutable_unaccent(' + attribute + ')) gin_trgm_ops') as any ],
using: 'gin',
operator: 'gin_trgm_ops'
}
}
// ---------------------------------------------------------------------------
export {
throwIfNotValid,
buildTrigramSearchIndex,
isOutdated
}
+6
ファイルの表示
@@ -0,0 +1,6 @@
import { AttributesOnly } from '@peertube/peertube-typescript-utils'
import { Model } from 'sequelize-typescript'
export abstract class SequelizeModel <T> extends Model<Partial<AttributesOnly<T>>> {
id: number
}
+146
ファイルの表示
@@ -0,0 +1,146 @@
import { literal, OrderItem, Sequelize } from 'sequelize'
// Translate for example "-name" to [ [ 'name', 'DESC' ], [ 'id', 'ASC' ] ]
function getSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
const { direction, field } = buildSortDirectionAndField(value)
let finalField: string | ReturnType<typeof Sequelize.col>
if (field.toLowerCase() === 'match') { // Search
finalField = Sequelize.col('similarity')
} else {
finalField = field
}
return [ [ finalField, direction ], lastSort ]
}
function getAdminUsersSort (value: string): OrderItem[] {
const { direction, field } = buildSortDirectionAndField(value)
let finalField: string | ReturnType<typeof Sequelize.col>
if (field === 'videoQuotaUsed') { // Users list
finalField = Sequelize.col('videoQuotaUsed')
} else {
finalField = field
}
const nullPolicy = direction === 'ASC'
? 'NULLS FIRST'
: 'NULLS LAST'
// FIXME: typings
return [ [ finalField as any, direction, nullPolicy ], [ 'id', 'ASC' ] ]
}
function getPlaylistSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
const { direction, field } = buildSortDirectionAndField(value)
if (field.toLowerCase() === 'name') {
return [ [ 'displayName', direction ], lastSort ]
}
return getSort(value, lastSort)
}
function getVideoSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
const { direction, field } = buildSortDirectionAndField(value)
if (field.toLowerCase() === 'trending') { // Sort by aggregation
return [
[ Sequelize.fn('COALESCE', Sequelize.fn('SUM', Sequelize.col('VideoViews.views')), '0'), direction ],
[ Sequelize.col('VideoModel.views'), direction ],
lastSort
]
} else if (field === 'publishedAt') {
return [
[ 'ScheduleVideoUpdate', 'updateAt', direction + ' NULLS LAST' ],
[ Sequelize.col('VideoModel.publishedAt'), direction ],
lastSort
]
}
let finalField: string | ReturnType<typeof Sequelize.col>
// Alias
if (field.toLowerCase() === 'match') { // Search
finalField = Sequelize.col('similarity')
} else {
finalField = field
}
const firstSort: OrderItem = typeof finalField === 'string'
? finalField.split('.').concat([ direction ]) as OrderItem
: [ finalField, direction ]
return [ firstSort, lastSort ]
}
function getBlacklistSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
const { direction, field } = buildSortDirectionAndField(value)
const videoFields = new Set([ 'name', 'duration', 'views', 'likes', 'dislikes', 'uuid' ])
if (videoFields.has(field)) {
return [
[ literal(`"Video.${field}" ${direction}`) ],
lastSort
] as OrderItem[]
}
return getSort(value, lastSort)
}
function getInstanceFollowsSort (value: string, lastSort: OrderItem = [ 'id', 'ASC' ]): OrderItem[] {
const { direction, field } = buildSortDirectionAndField(value)
if (field === 'redundancyAllowed') {
return [
[ 'ActorFollowing.Server.redundancyAllowed', direction ],
lastSort
]
}
return getSort(value, lastSort)
}
function getChannelSyncSort (value: string): OrderItem[] {
const { direction, field } = buildSortDirectionAndField(value)
if (field.toLowerCase() === 'videochannel') {
return [
[ literal('"VideoChannel.name"'), direction ]
]
}
return [ [ field, direction ] ]
}
function buildSortDirectionAndField (value: string) {
let field: string
let direction: 'ASC' | 'DESC'
if (value.substring(0, 1) === '-') {
direction = 'DESC'
field = value.substring(1)
} else {
direction = 'ASC'
field = value
}
return { direction, field }
}
export {
buildSortDirectionAndField,
getPlaylistSort,
getSort,
getAdminUsersSort,
getVideoSort,
getBlacklistSort,
getChannelSyncSort,
getInstanceFollowsSort
}
+72
ファイルの表示
@@ -0,0 +1,72 @@
import { literal, Model, ModelStatic } from 'sequelize'
import { Literal } from 'sequelize/types/utils'
import { forceNumber } from '@peertube/peertube-core-utils'
import { AttributesOnly } from '@peertube/peertube-typescript-utils'
// FIXME: have to specify the result type to not break peertube typings generation
export function buildLocalAccountIdsIn (): Literal {
return literal(
'(SELECT "account"."id" FROM "account" INNER JOIN "actor" ON "actor"."id" = "account"."actorId" AND "actor"."serverId" IS NULL)'
)
}
// FIXME: have to specify the result type to not break peertube typings generation
export function buildLocalActorIdsIn (): Literal {
return literal(
'(SELECT "actor"."id" FROM "actor" WHERE "actor"."serverId" IS NULL)'
)
}
export function buildBlockedAccountSQL (blockerIds: number[]) {
const blockerIdsString = blockerIds.join(', ')
return 'SELECT "targetAccountId" AS "id" FROM "accountBlocklist" WHERE "accountId" IN (' + blockerIdsString + ')' +
' UNION ' +
'SELECT "account"."id" AS "id" FROM account INNER JOIN "actor" ON account."actorId" = actor.id ' +
'INNER JOIN "serverBlocklist" ON "actor"."serverId" = "serverBlocklist"."targetServerId" ' +
'WHERE "serverBlocklist"."accountId" IN (' + blockerIdsString + ')'
}
export function buildServerIdsFollowedBy (actorId: any) {
const actorIdNumber = forceNumber(actorId)
return '(' +
'SELECT "actor"."serverId" FROM "actorFollow" ' +
'INNER JOIN "actor" ON actor.id = "actorFollow"."targetActorId" ' +
'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
')'
}
export function buildSQLAttributes<M extends Model> (options: {
model: ModelStatic<M>
tableName: string
excludeAttributes?: Exclude<keyof AttributesOnly<M>, symbol>[]
aliasPrefix?: string
idBuilder?: string[]
}) {
const { model, tableName, aliasPrefix = '', excludeAttributes, idBuilder } = options
const attributes = Object.keys(model.getAttributes()) as Exclude<keyof AttributesOnly<M>, symbol>[]
const builtAttributes = attributes
.filter(a => {
if (!excludeAttributes) return true
if (excludeAttributes.includes(a)) return false
return true
})
.map(a => {
return `"${tableName}"."${a}" AS "${aliasPrefix}${a}"`
})
if (idBuilder) {
const idSelect = idBuilder.map(a => `"${tableName}"."${a}"`)
.join(` || '-' || `)
builtAttributes.push(`${idSelect} AS "${aliasPrefix}id"`)
}
return builtAttributes
}
+34
ファイルの表示
@@ -0,0 +1,34 @@
import { QueryTypes, Sequelize, Transaction } from 'sequelize'
const updating = new Set<string>()
// Sequelize always skip the update if we only update updatedAt field
async function setAsUpdated (options: {
sequelize: Sequelize
table: string
id: number
transaction?: Transaction
}) {
const { sequelize, table, id, transaction } = options
const key = table + '-' + id
if (updating.has(key)) return
updating.add(key)
try {
await sequelize.query(
`UPDATE "${table}" SET "updatedAt" = :updatedAt WHERE id = :id`,
{
replacements: { table, id, updatedAt: new Date() },
type: QueryTypes.UPDATE,
transaction
}
)
} finally {
updating.delete(key)
}
}
export {
setAsUpdated
}