1
0
Fork 0

fetch -> load

avoid confusion with AP fetch functions
This commit is contained in:
Chocobozzz 2021-06-03 18:10:56 +02:00
parent 10363c74c1
commit 868fce62f8
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
9 changed files with 47 additions and 45 deletions

View File

@ -3,7 +3,7 @@ import { checkUrlsSameHost, getAPId } from '@server/helpers/activitypub'
import { retryTransactionWrapper } from '@server/helpers/database-utils' import { retryTransactionWrapper } from '@server/helpers/database-utils'
import { logger } from '@server/helpers/logger' import { logger } from '@server/helpers/logger'
import { JobQueue } from '@server/lib/job-queue' import { JobQueue } from '@server/lib/job-queue'
import { ActorFetchByUrlType, fetchActorByUrl } from '@server/lib/model-loaders' import { ActorLoadByUrlType, loadActorByUrl } from '@server/lib/model-loaders'
import { MActor, MActorAccountChannelId, MActorAccountChannelIdActor, MActorAccountId, MActorFullActor } from '@server/types/models' import { MActor, MActorAccountChannelId, MActorAccountChannelIdActor, MActorAccountId, MActorFullActor } from '@server/types/models'
import { ActivityPubActor } from '@shared/models' import { ActivityPubActor } from '@shared/models'
import { refreshActorIfNeeded } from './refresh' import { refreshActorIfNeeded } from './refresh'
@ -25,7 +25,7 @@ function getOrCreateAPActor (
async function getOrCreateAPActor ( async function getOrCreateAPActor (
activityActor: string | ActivityPubActor, activityActor: string | ActivityPubActor,
fetchType: ActorFetchByUrlType = 'association-ids', fetchType: ActorLoadByUrlType = 'association-ids',
recurseIfNeeded = true, recurseIfNeeded = true,
updateCollections = false updateCollections = false
): Promise<MActorFullActor | MActorAccountChannelId> { ): Promise<MActorFullActor | MActorAccountChannelId> {
@ -73,8 +73,8 @@ export {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
async function loadActorFromDB (actorUrl: string, fetchType: ActorFetchByUrlType) { async function loadActorFromDB (actorUrl: string, fetchType: ActorLoadByUrlType) {
let actor = await fetchActorByUrl(actorUrl, fetchType) let actor = await loadActorByUrl(actorUrl, fetchType)
// Orphan actor (not associated to an account of channel) so recreate it // Orphan actor (not associated to an account of channel) so recreate it
if (actor && (!actor.Account && !actor.VideoChannel)) { if (actor && (!actor.Account && !actor.VideoChannel)) {

View File

@ -1,6 +1,6 @@
import { logger } from '@server/helpers/logger' import { logger } from '@server/helpers/logger'
import { PeerTubeRequestError } from '@server/helpers/requests' import { PeerTubeRequestError } from '@server/helpers/requests'
import { ActorFetchByUrlType } from '@server/lib/model-loaders' import { ActorLoadByUrlType } from '@server/lib/model-loaders'
import { ActorModel } from '@server/models/actor/actor' import { ActorModel } from '@server/models/actor/actor'
import { MActorAccountChannelId, MActorFull } from '@server/types/models' import { MActorAccountChannelId, MActorFull } from '@server/types/models'
import { HttpStatusCode } from '@shared/core-utils' import { HttpStatusCode } from '@shared/core-utils'
@ -10,7 +10,7 @@ import { getUrlFromWebfinger } from './webfinger'
async function refreshActorIfNeeded <T extends MActorFull | MActorAccountChannelId> ( async function refreshActorIfNeeded <T extends MActorFull | MActorAccountChannelId> (
actorArg: T, actorArg: T,
fetchedType: ActorFetchByUrlType fetchedType: ActorLoadByUrlType
): Promise<{ actor: T | MActorFull, refreshed: boolean }> { ): Promise<{ actor: T | MActorFull, refreshed: boolean }> {
if (!actorArg.isOutdated()) return { actor: actorArg, refreshed: false } if (!actorArg.isOutdated()) return { actor: actorArg, refreshed: false }

View File

@ -1,7 +1,7 @@
import { getAPId } from '@server/helpers/activitypub' import { getAPId } from '@server/helpers/activitypub'
import { retryTransactionWrapper } from '@server/helpers/database-utils' import { retryTransactionWrapper } from '@server/helpers/database-utils'
import { JobQueue } from '@server/lib/job-queue' import { JobQueue } from '@server/lib/job-queue'
import { fetchVideoByUrl, VideoFetchByUrlType } from '@server/lib/model-loaders' import { loadVideoByUrl, VideoLoadByUrlType } from '@server/lib/model-loaders'
import { MVideoAccountLightBlacklistAllFiles, MVideoImmutable, MVideoThumbnail } from '@server/types/models' import { MVideoAccountLightBlacklistAllFiles, MVideoImmutable, MVideoThumbnail } from '@server/types/models'
import { refreshVideoIfNeeded } from './refresh' import { refreshVideoIfNeeded } from './refresh'
import { APVideoCreator, fetchRemoteVideo, SyncParam, syncVideoExternalAttributes } from './shared' import { APVideoCreator, fetchRemoteVideo, SyncParam, syncVideoExternalAttributes } from './shared'
@ -47,7 +47,7 @@ async function getOrCreateAPVideo (
// Get video url // Get video url
const videoUrl = getAPId(options.videoObject) const videoUrl = getAPId(options.videoObject)
let videoFromDatabase = await fetchVideoByUrl(videoUrl, fetchType) let videoFromDatabase = await loadVideoByUrl(videoUrl, fetchType)
if (videoFromDatabase) { if (videoFromDatabase) {
if (allowRefresh === true) { if (allowRefresh === true) {
@ -71,7 +71,7 @@ async function getOrCreateAPVideo (
} catch (err) { } catch (err) {
// Maybe a concurrent getOrCreateAPVideo call created this video // Maybe a concurrent getOrCreateAPVideo call created this video
if (err.name === 'SequelizeUniqueConstraintError') { if (err.name === 'SequelizeUniqueConstraintError') {
const alreadyCreatedVideo = await fetchVideoByUrl(videoUrl, fetchType) const alreadyCreatedVideo = await loadVideoByUrl(videoUrl, fetchType)
if (alreadyCreatedVideo) return { video: alreadyCreatedVideo, created: false } if (alreadyCreatedVideo) return { video: alreadyCreatedVideo, created: false }
} }
@ -87,7 +87,7 @@ export {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
async function scheduleRefresh (video: MVideoThumbnail, fetchType: VideoFetchByUrlType, syncParam: SyncParam) { async function scheduleRefresh (video: MVideoThumbnail, fetchType: VideoLoadByUrlType, syncParam: SyncParam) {
if (!video.isOutdated()) return video if (!video.isOutdated()) return video
const refreshOptions = { const refreshOptions = {

View File

@ -1,7 +1,7 @@
import { logger, loggerTagsFactory } from '@server/helpers/logger' import { logger, loggerTagsFactory } from '@server/helpers/logger'
import { PeerTubeRequestError } from '@server/helpers/requests' import { PeerTubeRequestError } from '@server/helpers/requests'
import { ActorFollowScoreCache } from '@server/lib/files-cache' import { ActorFollowScoreCache } from '@server/lib/files-cache'
import { VideoFetchByUrlType } from '@server/lib/model-loaders' import { VideoLoadByUrlType } from '@server/lib/model-loaders'
import { VideoModel } from '@server/models/video/video' import { VideoModel } from '@server/models/video/video'
import { MVideoAccountLightBlacklistAllFiles, MVideoThumbnail } from '@server/types/models' import { MVideoAccountLightBlacklistAllFiles, MVideoThumbnail } from '@server/types/models'
import { HttpStatusCode } from '@shared/core-utils' import { HttpStatusCode } from '@shared/core-utils'
@ -10,7 +10,7 @@ import { APVideoUpdater } from './updater'
async function refreshVideoIfNeeded (options: { async function refreshVideoIfNeeded (options: {
video: MVideoThumbnail video: MVideoThumbnail
fetchedType: VideoFetchByUrlType fetchedType: VideoLoadByUrlType
syncParam: SyncParam syncParam: SyncParam
}): Promise<MVideoThumbnail> { }): Promise<MVideoThumbnail> {
if (!options.video.isOutdated()) return options.video if (!options.video.isOutdated()) return options.video

View File

@ -1,7 +1,7 @@
import * as Bull from 'bull' import * as Bull from 'bull'
import { refreshVideoPlaylistIfNeeded } from '@server/lib/activitypub/playlists' import { refreshVideoPlaylistIfNeeded } from '@server/lib/activitypub/playlists'
import { refreshVideoIfNeeded } from '@server/lib/activitypub/videos' import { refreshVideoIfNeeded } from '@server/lib/activitypub/videos'
import { fetchVideoByUrl } from '@server/lib/model-loaders' import { loadVideoByUrl } from '@server/lib/model-loaders'
import { RefreshPayload } from '@shared/models' import { RefreshPayload } from '@shared/models'
import { logger } from '../../../helpers/logger' import { logger } from '../../../helpers/logger'
import { ActorModel } from '../../../models/actor/actor' import { ActorModel } from '../../../models/actor/actor'
@ -30,7 +30,7 @@ async function refreshVideo (videoUrl: string) {
const fetchType = 'all' as 'all' const fetchType = 'all' as 'all'
const syncParam = { likes: true, dislikes: true, shares: true, comments: true, thumbnail: true } const syncParam = { likes: true, dislikes: true, shares: true, comments: true, thumbnail: true }
const videoFromDatabase = await fetchVideoByUrl(videoUrl, fetchType) const videoFromDatabase = await loadVideoByUrl(videoUrl, fetchType)
if (videoFromDatabase) { if (videoFromDatabase) {
const refreshOptions = { const refreshOptions = {
video: videoFromDatabase, video: videoFromDatabase,

View File

@ -2,15 +2,16 @@
import { ActorModel } from '../../models/actor/actor' import { ActorModel } from '../../models/actor/actor'
import { MActorAccountChannelId, MActorFull } from '../../types/models' import { MActorAccountChannelId, MActorFull } from '../../types/models'
type ActorFetchByUrlType = 'all' | 'association-ids' type ActorLoadByUrlType = 'all' | 'association-ids'
function fetchActorByUrl (url: string, fetchType: ActorFetchByUrlType): Promise<MActorFull | MActorAccountChannelId> { function loadActorByUrl (url: string, fetchType: ActorLoadByUrlType): Promise<MActorFull | MActorAccountChannelId> {
if (fetchType === 'all') return ActorModel.loadByUrlAndPopulateAccountAndChannel(url) if (fetchType === 'all') return ActorModel.loadByUrlAndPopulateAccountAndChannel(url)
if (fetchType === 'association-ids') return ActorModel.loadByUrl(url) if (fetchType === 'association-ids') return ActorModel.loadByUrl(url)
} }
export { export {
ActorFetchByUrlType, ActorLoadByUrlType,
fetchActorByUrl
loadActorByUrl
} }

View File

@ -8,21 +8,21 @@ import {
MVideoWithRights MVideoWithRights
} from '@server/types/models' } from '@server/types/models'
type VideoFetchType = 'all' | 'only-video' | 'only-video-with-rights' | 'id' | 'none' | 'only-immutable-attributes' type VideoLoadType = 'all' | 'only-video' | 'only-video-with-rights' | 'id' | 'none' | 'only-immutable-attributes'
function fetchVideo (id: number | string, fetchType: 'all', userId?: number): Promise<MVideoFullLight> function loadVideo (id: number | string, fetchType: 'all', userId?: number): Promise<MVideoFullLight>
function fetchVideo (id: number | string, fetchType: 'only-immutable-attributes'): Promise<MVideoImmutable> function loadVideo (id: number | string, fetchType: 'only-immutable-attributes'): Promise<MVideoImmutable>
function fetchVideo (id: number | string, fetchType: 'only-video', userId?: number): Promise<MVideoThumbnail> function loadVideo (id: number | string, fetchType: 'only-video', userId?: number): Promise<MVideoThumbnail>
function fetchVideo (id: number | string, fetchType: 'only-video-with-rights', userId?: number): Promise<MVideoWithRights> function loadVideo (id: number | string, fetchType: 'only-video-with-rights', userId?: number): Promise<MVideoWithRights>
function fetchVideo (id: number | string, fetchType: 'id' | 'none', userId?: number): Promise<MVideoIdThumbnail> function loadVideo (id: number | string, fetchType: 'id' | 'none', userId?: number): Promise<MVideoIdThumbnail>
function fetchVideo ( function loadVideo (
id: number | string, id: number | string,
fetchType: VideoFetchType, fetchType: VideoLoadType,
userId?: number userId?: number
): Promise<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail | MVideoImmutable> ): Promise<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail | MVideoImmutable>
function fetchVideo ( function loadVideo (
id: number | string, id: number | string,
fetchType: VideoFetchType, fetchType: VideoLoadType,
userId?: number userId?: number
): Promise<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail | MVideoImmutable> { ): Promise<MVideoFullLight | MVideoThumbnail | MVideoWithRights | MVideoIdThumbnail | MVideoImmutable> {
if (fetchType === 'all') return VideoModel.loadAndPopulateAccountAndServerAndTags(id, undefined, userId) if (fetchType === 'all') return VideoModel.loadAndPopulateAccountAndServerAndTags(id, undefined, userId)
@ -36,18 +36,18 @@ function fetchVideo (
if (fetchType === 'id' || fetchType === 'none') return VideoModel.loadOnlyId(id) if (fetchType === 'id' || fetchType === 'none') return VideoModel.loadOnlyId(id)
} }
type VideoFetchByUrlType = 'all' | 'only-video' | 'only-immutable-attributes' type VideoLoadByUrlType = 'all' | 'only-video' | 'only-immutable-attributes'
function fetchVideoByUrl (url: string, fetchType: 'all'): Promise<MVideoAccountLightBlacklistAllFiles> function loadVideoByUrl (url: string, fetchType: 'all'): Promise<MVideoAccountLightBlacklistAllFiles>
function fetchVideoByUrl (url: string, fetchType: 'only-immutable-attributes'): Promise<MVideoImmutable> function loadVideoByUrl (url: string, fetchType: 'only-immutable-attributes'): Promise<MVideoImmutable>
function fetchVideoByUrl (url: string, fetchType: 'only-video'): Promise<MVideoThumbnail> function loadVideoByUrl (url: string, fetchType: 'only-video'): Promise<MVideoThumbnail>
function fetchVideoByUrl ( function loadVideoByUrl (
url: string, url: string,
fetchType: VideoFetchByUrlType fetchType: VideoLoadByUrlType
): Promise<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail | MVideoImmutable> ): Promise<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail | MVideoImmutable>
function fetchVideoByUrl ( function loadVideoByUrl (
url: string, url: string,
fetchType: VideoFetchByUrlType fetchType: VideoLoadByUrlType
): Promise<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail | MVideoImmutable> { ): Promise<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail | MVideoImmutable> {
if (fetchType === 'all') return VideoModel.loadByUrlAndPopulateAccount(url) if (fetchType === 'all') return VideoModel.loadByUrlAndPopulateAccount(url)
@ -57,8 +57,9 @@ function fetchVideoByUrl (
} }
export { export {
VideoFetchType, VideoLoadType,
VideoFetchByUrlType, VideoLoadByUrlType,
fetchVideo,
fetchVideoByUrl loadVideo,
loadVideoByUrl
} }

View File

@ -1,7 +1,7 @@
import * as express from 'express' import * as express from 'express'
import { query } from 'express-validator' import { query } from 'express-validator'
import { join } from 'path' import { join } from 'path'
import { fetchVideo } from '@server/lib/model-loaders' import { loadVideo } from '@server/lib/model-loaders'
import { VideoPlaylistModel } from '@server/models/video/video-playlist' import { VideoPlaylistModel } from '@server/models/video/video-playlist'
import { VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models' import { VideoPlaylistPrivacy, VideoPrivacy } from '@shared/models'
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
@ -85,7 +85,7 @@ const oembedValidator = [
} }
if (isVideo) { if (isVideo) {
const video = await fetchVideo(elementId, 'all') const video = await loadVideo(elementId, 'all')
if (!video) { if (!video) {
return res.fail({ return res.fail({

View File

@ -1,5 +1,5 @@
import { Response } from 'express' import { Response } from 'express'
import { fetchVideo, VideoFetchType } from '@server/lib/model-loaders' import { loadVideo, VideoLoadType } from '@server/lib/model-loaders'
import { VideoChannelModel } from '@server/models/video/video-channel' import { VideoChannelModel } from '@server/models/video/video-channel'
import { VideoFileModel } from '@server/models/video/video-file' import { VideoFileModel } from '@server/models/video/video-file'
import { import {
@ -15,10 +15,10 @@ import {
import { HttpStatusCode } from '@shared/core-utils' import { HttpStatusCode } from '@shared/core-utils'
import { UserRight } from '@shared/models' import { UserRight } from '@shared/models'
async function doesVideoExist (id: number | string, res: Response, fetchType: VideoFetchType = 'all') { async function doesVideoExist (id: number | string, res: Response, fetchType: VideoLoadType = 'all') {
const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined
const video = await fetchVideo(id, fetchType, userId) const video = await loadVideo(id, fetchType, userId)
if (video === null) { if (video === null) {
res.fail({ res.fail({