From 0283eaac2a8e73006c66df3cf5bb9012e37450e5 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 20 Aug 2019 13:52:49 +0200 Subject: Cleanup model types --- server/lib/activitypub/actor.ts | 13 +++++++------ server/lib/activitypub/process/process-announce.ts | 4 ++-- server/lib/activitypub/process/process-create.ts | 4 ++-- server/lib/activitypub/video-comments.ts | 4 ++-- server/lib/activitypub/videos.ts | 12 ++++++------ server/lib/avatar.ts | 4 ++-- server/lib/emailer.ts | 6 ++---- .../lib/job-queue/handlers/activitypub-follow.ts | 17 +++++++++++++---- server/lib/job-queue/handlers/video-import.ts | 22 +++++++++++----------- server/lib/notifier.ts | 6 +++--- .../lib/schedulers/videos-redundancy-scheduler.ts | 4 +++- 11 files changed, 53 insertions(+), 43 deletions(-) (limited to 'server/lib') diff --git a/server/lib/activitypub/actor.ts b/server/lib/activitypub/actor.ts index 7862b0f00..5201bdeef 100644 --- a/server/lib/activitypub/actor.ts +++ b/server/lib/activitypub/actor.ts @@ -24,15 +24,17 @@ import { ActorFetchByUrlType, fetchActorByUrl } from '../../helpers/actor' import { sequelizeTypescript } from '../../initializers/database' import { MAccount, + MAccountDefault, MActor, MActorAccountChannelId, + MActorAccountChannelIdActor, MActorAccountId, MActorDefault, MActorFull, + MActorFullActor, MActorId, - MActorAccountChannelIdActor, MChannel, - MActorFullActor, MAccountActorDefault, MChannelActorDefault, MChannelActorAccountDefault + MChannelAccountDefault } from '../../typings/models' // Set account keys, this could be long so process after the account creation and do not block the client @@ -374,12 +376,11 @@ function saveActorAndServerAndModelIfNotExist ( }) if (actorCreated.type === 'Person' || actorCreated.type === 'Application') { - actorCreated.Account = await saveAccount(actorCreated, result, t) as MAccountActorDefault + actorCreated.Account = await saveAccount(actorCreated, result, t) as MAccountDefault actorCreated.Account.Actor = actorCreated } else if (actorCreated.type === 'Group') { // Video channel - actorCreated.VideoChannel = await saveVideoChannel(actorCreated, result, ownerActor, t) as MChannelActorAccountDefault - actorCreated.VideoChannel.Actor = actorCreated - actorCreated.VideoChannel.Account = ownerActor.Account + const channel = await saveVideoChannel(actorCreated, result, ownerActor, t) + actorCreated.VideoChannel = Object.assign(channel, { Actor: actorCreated, Account: ownerActor.Account }) } actorCreated.Server = server diff --git a/server/lib/activitypub/process/process-announce.ts b/server/lib/activitypub/process/process-announce.ts index 91a9ad72c..7e22125d5 100644 --- a/server/lib/activitypub/process/process-announce.ts +++ b/server/lib/activitypub/process/process-announce.ts @@ -7,7 +7,7 @@ import { getOrCreateVideoAndAccountAndChannel } from '../videos' import { Notifier } from '../../notifier' import { logger } from '../../../helpers/logger' import { APProcessorOptions } from '../../../typings/activitypub-processor.model' -import { MActorSignature, MVideoAccountAllFiles } from '../../../typings/models' +import { MActorSignature, MVideoAccountLightBlacklistAllFiles } from '../../../typings/models' async function processAnnounceActivity (options: APProcessorOptions) { const { activity, byActor: actorAnnouncer } = options @@ -28,7 +28,7 @@ export { async function processVideoShare (actorAnnouncer: MActorSignature, activity: ActivityAnnounce, notify: boolean) { const objectUri = typeof activity.object === 'string' ? activity.object : activity.object.id - let video: MVideoAccountAllFiles + let video: MVideoAccountLightBlacklistAllFiles let videoCreated: boolean try { diff --git a/server/lib/activitypub/process/process-create.ts b/server/lib/activitypub/process/process-create.ts index c45f09f52..bee853721 100644 --- a/server/lib/activitypub/process/process-create.ts +++ b/server/lib/activitypub/process/process-create.ts @@ -11,7 +11,7 @@ import { Notifier } from '../../notifier' import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object' import { createOrUpdateVideoPlaylist } from '../playlist' import { APProcessorOptions } from '../../../typings/activitypub-processor.model' -import { MActorSignature, MCommentOwnerVideo, MVideoAccountAllFiles } from '../../../typings/models' +import { MActorSignature, MCommentOwnerVideo, MVideoAccountLightBlacklistAllFiles } from '../../../typings/models' async function processCreateActivity (options: APProcessorOptions) { const { activity, byActor } = options @@ -81,7 +81,7 @@ async function processCreateVideoComment (activity: ActivityCreate, byActor: MAc if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url) - let video: MVideoAccountAllFiles + let video: MVideoAccountLightBlacklistAllFiles let created: boolean let comment: MCommentOwnerVideo try { diff --git a/server/lib/activitypub/video-comments.ts b/server/lib/activitypub/video-comments.ts index 375ac0aad..3e8306fa4 100644 --- a/server/lib/activitypub/video-comments.ts +++ b/server/lib/activitypub/video-comments.ts @@ -7,7 +7,7 @@ import { getOrCreateActorAndServerAndModel } from './actor' import { getOrCreateVideoAndAccountAndChannel } from './videos' import * as Bluebird from 'bluebird' import { checkUrlsSameHost } from '../../helpers/activitypub' -import { MCommentOwner, MCommentOwnerVideo, MVideoAccountAllFiles } from '../../typings/models/video' +import { MCommentOwner, MCommentOwnerVideo, MVideoAccountLightBlacklistAllFiles } from '../../typings/models/video' type ResolveThreadParams = { url: string, @@ -15,7 +15,7 @@ type ResolveThreadParams = { isVideo?: boolean, commentCreated?: boolean } -type ResolveThreadResult = Promise<{ video: MVideoAccountAllFiles, comment: MCommentOwnerVideo, commentCreated: boolean }> +type ResolveThreadResult = Promise<{ video: MVideoAccountLightBlacklistAllFiles, comment: MCommentOwnerVideo, commentCreated: boolean }> async function addVideoComments (commentUrls: string[]) { return Bluebird.map(commentUrls, commentUrl => { diff --git a/server/lib/activitypub/videos.ts b/server/lib/activitypub/videos.ts index 035994da8..970a953fc 100644 --- a/server/lib/activitypub/videos.ts +++ b/server/lib/activitypub/videos.ts @@ -58,7 +58,7 @@ import { MChannelDefault, MChannelId, MVideo, - MVideoAccountAllFiles, + MVideoAccountLightBlacklistAllFiles, MVideoAccountLight, MVideoAP, MVideoAPWithoutCaption, @@ -213,19 +213,19 @@ function getOrCreateVideoAndAccountAndChannel (options: { syncParam?: SyncParam, fetchType?: 'all', allowRefresh?: boolean -}): Promise<{ video: MVideoAccountAllFiles, created: boolean, autoBlacklisted?: boolean }> +}): Promise<{ video: MVideoAccountLightBlacklistAllFiles, created: boolean, autoBlacklisted?: boolean }> function getOrCreateVideoAndAccountAndChannel (options: { videoObject: { id: string } | string, syncParam?: SyncParam, fetchType?: VideoFetchByUrlType, allowRefresh?: boolean -}): Promise<{ video: MVideoAccountAllFiles | MVideoThumbnail, created: boolean, autoBlacklisted?: boolean }> +}): Promise<{ video: MVideoAccountLightBlacklistAllFiles | MVideoThumbnail, created: boolean, autoBlacklisted?: boolean }> async function getOrCreateVideoAndAccountAndChannel (options: { videoObject: { id: string } | string, syncParam?: SyncParam, fetchType?: VideoFetchByUrlType, allowRefresh?: boolean // true by default -}): Promise<{ video: MVideoAccountAllFiles | MVideoThumbnail, created: boolean, autoBlacklisted?: boolean }> { +}): Promise<{ video: MVideoAccountLightBlacklistAllFiles | MVideoThumbnail, created: boolean, autoBlacklisted?: boolean }> { // Default params const syncParam = options.syncParam || { likes: true, dislikes: true, shares: true, comments: true, thumbnail: true, refreshVideo: false } const fetchType = options.fetchType || 'all' @@ -263,7 +263,7 @@ async function getOrCreateVideoAndAccountAndChannel (options: { } async function updateVideoFromAP (options: { - video: MVideoAccountAllFiles, + video: MVideoAccountLightBlacklistAllFiles, videoObject: VideoTorrentObject, account: MAccountActor, channel: MChannelDefault, @@ -420,7 +420,7 @@ async function refreshVideoIfNeeded (options: { // We need more attributes if the argument video was fetched with not enough joints const video = options.fetchedType === 'all' - ? options.video as MVideoAccountAllFiles + ? options.video as MVideoAccountLightBlacklistAllFiles : await VideoModel.loadByUrlAndPopulateAccount(options.video.url) try { diff --git a/server/lib/avatar.ts b/server/lib/avatar.ts index 9005b3e22..ad4cdd3ab 100644 --- a/server/lib/avatar.ts +++ b/server/lib/avatar.ts @@ -11,11 +11,11 @@ import { sequelizeTypescript } from '../initializers/database' import * as LRUCache from 'lru-cache' import { queue } from 'async' import { downloadImage } from '../helpers/requests' -import { MAccountActorDefault, MChannelActorDefault } from '../typings/models' +import { MAccountDefault, MChannelDefault } from '../typings/models' async function updateActorAvatarFile ( avatarPhysicalFile: Express.Multer.File, - accountOrChannel: MAccountActorDefault | MChannelActorDefault + accountOrChannel: MAccountDefault | MChannelDefault ) { const extension = extname(avatarPhysicalFile.filename) const avatarName = uuidv4() + extension diff --git a/server/lib/emailer.ts b/server/lib/emailer.ts index fe57a3e4c..a888b7a72 100644 --- a/server/lib/emailer.ts +++ b/server/lib/emailer.ts @@ -2,14 +2,12 @@ import { createTransport, Transporter } from 'nodemailer' import { isTestInstance } from '../helpers/core-utils' import { bunyanLogger, logger } from '../helpers/logger' import { CONFIG } from '../initializers/config' -import { UserModel } from '../models/account/user' import { JobQueue } from './job-queue' import { EmailPayload } from './job-queue/handlers/email' import { readFileSync } from 'fs-extra' -import { VideoBlacklistModel } from '../models/video/video-blacklist' import { WEBSERVER } from '../initializers/constants' import { MCommentOwnerVideo, MVideo, MVideoAbuseVideo, MVideoAccountLight, MVideoBlacklistVideo } from '../typings/models/video' -import { MActorFollowActors, MActorFollowFull, MUser } from '../typings/models' +import { MActorFollowActors, MActorFollowFollowingFullFollowerAccount, MUser } from '../typings/models' import { MVideoImport, MVideoImportVideo } from '@server/typings/models/video/video-import' type SendEmailOptions = { @@ -109,7 +107,7 @@ class Emailer { return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) } - addNewFollowNotification (to: string[], actorFollow: MActorFollowFull, followType: 'account' | 'channel') { + addNewFollowNotification (to: string[], actorFollow: MActorFollowFollowingFullFollowerAccount, followType: 'account' | 'channel') { const followerName = actorFollow.ActorFollower.Account.getDisplayName() const followingName = (actorFollow.ActorFollowing.VideoChannel || actorFollow.ActorFollowing.Account).getDisplayName() diff --git a/server/lib/job-queue/handlers/activitypub-follow.ts b/server/lib/job-queue/handlers/activitypub-follow.ts index 741b1ffde..5cb55cad6 100644 --- a/server/lib/job-queue/handlers/activitypub-follow.ts +++ b/server/lib/job-queue/handlers/activitypub-follow.ts @@ -10,7 +10,7 @@ import { ActorFollowModel } from '../../../models/activitypub/actor-follow' import { ActorModel } from '../../../models/activitypub/actor' import { Notifier } from '../../notifier' import { sequelizeTypescript } from '../../../initializers/database' -import { MActorFollowFull, MActorFull } from '../../../typings/models' +import { MAccount, MActor, MActorFollowActors, MActorFollowFull, MActorFull } from '../../../typings/models' export type ActivitypubFollowPayload = { followerActorId: number @@ -45,7 +45,7 @@ export { // --------------------------------------------------------------------------- -async function follow (fromActor: MActorFull, targetActor: MActorFull) { +async function follow (fromActor: MActor, targetActor: MActorFull) { if (fromActor.id === targetActor.id) { throw new Error('Follower is the same than target actor.') } @@ -54,7 +54,7 @@ async function follow (fromActor: MActorFull, targetActor: MActorFull) { const state = !fromActor.serverId && !targetActor.serverId ? 'accepted' : 'pending' const actorFollow = await sequelizeTypescript.transaction(async t => { - const [ actorFollow ] = await ActorFollowModel.findOrCreate({ + const [ actorFollow ] = await ActorFollowModel.findOrCreate({ where: { actorId: fromActor.id, targetActorId: targetActor.id @@ -75,5 +75,14 @@ async function follow (fromActor: MActorFull, targetActor: MActorFull) { return actorFollow }) - if (actorFollow.state === 'accepted') Notifier.Instance.notifyOfNewUserFollow(actorFollow) + if (actorFollow.state === 'accepted') { + const followerFull = Object.assign(fromActor, { Account: await actorFollow.ActorFollower.$get('Account') as MAccount }) + + const actorFollowFull = Object.assign(actorFollow, { + ActorFollowing: targetActor, + ActorFollower: followerFull + }) + + Notifier.Instance.notifyOfNewUserFollow(actorFollowFull) + } } diff --git a/server/lib/job-queue/handlers/video-import.ts b/server/lib/job-queue/handlers/video-import.ts index f9dda79f8..ff8c93328 100644 --- a/server/lib/job-queue/handlers/video-import.ts +++ b/server/lib/job-queue/handlers/video-import.ts @@ -111,13 +111,11 @@ type ProcessFileOptions = { generateThumbnail: boolean generatePreview: boolean } -async function processFile (downloader: () => Promise, videoImportArg: MVideoImportDefault, options: ProcessFileOptions) { +async function processFile (downloader: () => Promise, videoImport: MVideoImportDefault, options: ProcessFileOptions) { let tempVideoPath: string let videoDestFile: string let videoFile: VideoFileModel - const videoImport = videoImportArg as MVideoImportDefaultFiles - try { // Download video from youtubeDL tempVideoPath = await downloader() @@ -142,35 +140,37 @@ async function processFile (downloader: () => Promise, videoImportArg: M videoId: videoImport.videoId } videoFile = new VideoFileModel(videoFileData) + + const videoWithFiles = Object.assign(videoImport.Video, { VideoFiles: [ videoFile ] }) // To clean files if the import fails - videoImport.Video.VideoFiles = [ videoFile ] + const videoImportWithFiles: MVideoImportDefaultFiles = Object.assign(videoImport, { Video: videoWithFiles }) // Move file - videoDestFile = join(CONFIG.STORAGE.VIDEOS_DIR, videoImport.Video.getVideoFilename(videoFile)) + videoDestFile = join(CONFIG.STORAGE.VIDEOS_DIR, videoImportWithFiles.Video.getVideoFilename(videoFile)) await move(tempVideoPath, videoDestFile) tempVideoPath = null // This path is not used anymore // Process thumbnail let thumbnailModel: MThumbnail if (options.downloadThumbnail && options.thumbnailUrl) { - thumbnailModel = await createVideoMiniatureFromUrl(options.thumbnailUrl, videoImport.Video, ThumbnailType.MINIATURE) + thumbnailModel = await createVideoMiniatureFromUrl(options.thumbnailUrl, videoImportWithFiles.Video, ThumbnailType.MINIATURE) } else if (options.generateThumbnail || options.downloadThumbnail) { - thumbnailModel = await generateVideoMiniature(videoImport.Video, videoFile, ThumbnailType.MINIATURE) + thumbnailModel = await generateVideoMiniature(videoImportWithFiles.Video, videoFile, ThumbnailType.MINIATURE) } // Process preview let previewModel: MThumbnail if (options.downloadPreview && options.thumbnailUrl) { - previewModel = await createVideoMiniatureFromUrl(options.thumbnailUrl, videoImport.Video, ThumbnailType.PREVIEW) + previewModel = await createVideoMiniatureFromUrl(options.thumbnailUrl, videoImportWithFiles.Video, ThumbnailType.PREVIEW) } else if (options.generatePreview || options.downloadPreview) { - previewModel = await generateVideoMiniature(videoImport.Video, videoFile, ThumbnailType.PREVIEW) + previewModel = await generateVideoMiniature(videoImportWithFiles.Video, videoFile, ThumbnailType.PREVIEW) } // Create torrent - await videoImport.Video.createTorrentAndSetInfoHash(videoFile) + await videoImportWithFiles.Video.createTorrentAndSetInfoHash(videoFile) const { videoImportUpdated, video } = await sequelizeTypescript.transaction(async t => { - const videoImportToUpdate = videoImport as MVideoImportVideo + const videoImportToUpdate = videoImportWithFiles as MVideoImportVideo // Refresh video const video = await VideoModel.load(videoImportToUpdate.videoId, t) diff --git a/server/lib/notifier.ts b/server/lib/notifier.ts index f01101b8e..23f76a21a 100644 --- a/server/lib/notifier.ts +++ b/server/lib/notifier.ts @@ -21,7 +21,7 @@ import { MVideoFullLight } from '../typings/models/video' import { MUser, MUserAccount, MUserWithNotificationSetting, UserNotificationModelForApi } from '@server/typings/models/user' -import { MActorFollowActors, MActorFollowFull } from '../typings/models' +import { MActorFollowActors, MActorFollowFull, MActorFollowFollowingFullFollowerAccount } from '../typings/models' import { ActorFollowModel } from '../models/activitypub/actor-follow' import { MVideoImportVideo } from '@server/typings/models/video/video-import' import { AccountModel } from '@server/models/account/account' @@ -102,7 +102,7 @@ class Notifier { .catch(err => logger.error('Cannot notify moderators of new user registration (%s).', user.username, { err })) } - notifyOfNewUserFollow (actorFollow: MActorFollowFull): void { + notifyOfNewUserFollow (actorFollow: MActorFollowFollowingFullFollowerAccount): void { this.notifyUserOfNewActorFollow(actorFollow) .catch(err => { logger.error( @@ -231,7 +231,7 @@ class Notifier { return this.notify({ users, settingGetter, notificationCreator, emailSender }) } - private async notifyUserOfNewActorFollow (actorFollow: MActorFollowFull) { + private async notifyUserOfNewActorFollow (actorFollow: MActorFollowFollowingFullFollowerAccount) { if (actorFollow.ActorFollowing.isOwned() === false) return // Account follows one of our account? diff --git a/server/lib/schedulers/videos-redundancy-scheduler.ts b/server/lib/schedulers/videos-redundancy-scheduler.ts index de8fc075b..d9018e606 100644 --- a/server/lib/schedulers/videos-redundancy-scheduler.ts +++ b/server/lib/schedulers/videos-redundancy-scheduler.ts @@ -32,7 +32,9 @@ type CandidateToDuplicate = { streamingPlaylists: MStreamingPlaylist[] } -function isMVideoRedundancyFileVideo (o: MVideoRedundancyVideo): o is MVideoRedundancyFileVideo { +function isMVideoRedundancyFileVideo ( + o: MVideoRedundancyFileVideo | MVideoRedundancyStreamingPlaylistVideo +): o is MVideoRedundancyFileVideo { return !!(o as MVideoRedundancyFileVideo).VideoFile } -- cgit v1.2.3