From a15871560f80e07386c1dabb8370cd2664ecfd1f Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 31 Jan 2020 16:56:52 +0100 Subject: Move to eslint --- server/lib/activitypub/actor.ts | 22 +++++++------ server/lib/activitypub/crawl.ts | 6 ++-- server/lib/activitypub/follow.ts | 1 - server/lib/activitypub/send/send-accept.ts | 2 +- server/lib/activitypub/send/send-create.ts | 8 ++--- server/lib/activitypub/send/send-dislike.ts | 2 +- server/lib/activitypub/send/send-flag.ts | 2 +- server/lib/activitypub/send/send-like.ts | 2 +- server/lib/activitypub/send/send-reject.ts | 2 +- server/lib/activitypub/send/send-undo.ts | 10 +++--- server/lib/activitypub/send/send-update.ts | 3 +- server/lib/activitypub/send/utils.ts | 4 +-- server/lib/activitypub/video-comments.ts | 14 ++++---- server/lib/activitypub/video-rates.ts | 2 -- server/lib/activitypub/videos.ts | 50 ++++++++++++++++------------- 15 files changed, 67 insertions(+), 63 deletions(-) (limited to 'server/lib/activitypub') diff --git a/server/lib/activitypub/actor.ts b/server/lib/activitypub/actor.ts index f802658cf..3f6edc070 100644 --- a/server/lib/activitypub/actor.ts +++ b/server/lib/activitypub/actor.ts @@ -1,6 +1,6 @@ import * as Bluebird from 'bluebird' import { Transaction } from 'sequelize' -import * as url from 'url' +import { URL } from 'url' import * as uuidv4 from 'uuid/v4' import { ActivityPubActor, ActivityPubActorType } from '../../../shared/models/activitypub' import { ActivityPubAttributedTo } from '../../../shared/models/activitypub/objects' @@ -33,8 +33,7 @@ import { MActorFull, MActorFullActor, MActorId, - MChannel, - MChannelAccountDefault + MChannel } from '../../typings/models' // Set account keys, this could be long so process after the account creation and do not block the client @@ -121,13 +120,13 @@ async function getOrCreateActorAndServerAndModel ( if ((created === true || refreshed === true) && updateCollections === true) { const payload = { uri: actor.outboxUrl, type: 'activity' as 'activity' } - await JobQueue.Instance.createJob({ type: 'activitypub-http-fetcher', payload }) + await JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload }) } // We created a new account: fetch the playlists if (created === true && actor.Account && accountPlaylistsUrl) { const payload = { uri: accountPlaylistsUrl, accountId: actor.Account.id, type: 'account-playlists' as 'account-playlists' } - await JobQueue.Instance.createJob({ type: 'activitypub-http-fetcher', payload }) + await JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload }) } return actorRefreshed @@ -215,7 +214,7 @@ async function fetchActorTotalItems (url: string) { } } -async function getAvatarInfoIfExists (actorJSON: ActivityPubActor) { +function getAvatarInfoIfExists (actorJSON: ActivityPubActor) { if ( actorJSON.icon && actorJSON.icon.type === 'Image' && MIMETYPES.IMAGE.MIMETYPE_EXT[actorJSON.icon.mediaType] !== undefined && isActivityPubUrlValid(actorJSON.icon.url) @@ -271,7 +270,10 @@ async function refreshActorIfNeeded | Promise { - let actor = result.actor + const actor = result.actor if (t !== undefined) return save(t) return sequelizeTypescript.transaction(t => save(t)) async function save (t: Transaction) { - const actorHost = url.parse(actor.url).host + const actorHost = new URL(actor.url).host const serverOptions = { where: { @@ -402,7 +404,7 @@ type FetchRemoteActorResult = { support?: string playlists?: string avatar?: { - name: string, + name: string fileUrl: string } attributedTo: ActivityPubAttributedTo[] diff --git a/server/lib/activitypub/crawl.ts b/server/lib/activitypub/crawl.ts index 9e469e3e6..eeafdf4ba 100644 --- a/server/lib/activitypub/crawl.ts +++ b/server/lib/activitypub/crawl.ts @@ -3,7 +3,7 @@ import { doRequest } from '../../helpers/requests' import { logger } from '../../helpers/logger' import * as Bluebird from 'bluebird' import { ActivityPubOrderedCollection } from '../../../shared/models/activitypub' -import { parse } from 'url' +import { URL } from 'url' type HandlerFunction = (items: T[]) => (Promise | Bluebird) type CleanerFunction = (startedDate: Date) => (Promise | Bluebird) @@ -24,7 +24,7 @@ async function crawlCollectionPage (uri: string, handler: HandlerFunction const response = await doRequest>(options) const firstBody = response.body - let limit = ACTIVITY_PUB.FETCH_PAGE_LIMIT + const limit = ACTIVITY_PUB.FETCH_PAGE_LIMIT let i = 0 let nextLink = firstBody.first while (nextLink && i < limit) { @@ -32,7 +32,7 @@ async function crawlCollectionPage (uri: string, handler: HandlerFunction if (typeof nextLink === 'string') { // Don't crawl ourselves - const remoteHost = parse(nextLink).host + const remoteHost = new URL(nextLink).host if (remoteHost === WEBSERVER.HOST) continue options.uri = nextLink diff --git a/server/lib/activitypub/follow.ts b/server/lib/activitypub/follow.ts index 1abf43cd4..a1c95504e 100644 --- a/server/lib/activitypub/follow.ts +++ b/server/lib/activitypub/follow.ts @@ -27,7 +27,6 @@ async function autoFollowBackIfNeeded (actorFollow: MActorFollowActors) { } JobQueue.Instance.createJob({ type: 'activitypub-follow', payload }) - .catch(err => logger.error('Cannot create auto follow back job for %s.', host, err)) } } diff --git a/server/lib/activitypub/send/send-accept.ts b/server/lib/activitypub/send/send-accept.ts index 9f0225b64..c4c6b849b 100644 --- a/server/lib/activitypub/send/send-accept.ts +++ b/server/lib/activitypub/send/send-accept.ts @@ -5,7 +5,7 @@ import { buildFollowActivity } from './send-follow' import { logger } from '../../../helpers/logger' import { MActor, MActorFollowActors } from '../../../typings/models' -async function sendAccept (actorFollow: MActorFollowActors) { +function sendAccept (actorFollow: MActorFollowActors) { const follower = actorFollow.ActorFollower const me = actorFollow.ActorFollowing diff --git a/server/lib/activitypub/send/send-create.ts b/server/lib/activitypub/send/send-create.ts index 1709d8348..3585d704a 100644 --- a/server/lib/activitypub/send/send-create.ts +++ b/server/lib/activitypub/send/send-create.ts @@ -130,10 +130,10 @@ export { // --------------------------------------------------------------------------- async function sendVideoRelatedCreateActivity (options: { - byActor: MActorLight, - video: MVideoAccountLight, - url: string, - object: any, + byActor: MActorLight + video: MVideoAccountLight + url: string + object: any transaction?: Transaction }) { const activityBuilder = (audience: ActivityAudience) => { diff --git a/server/lib/activitypub/send/send-dislike.ts b/server/lib/activitypub/send/send-dislike.ts index 6e41f241f..600469c71 100644 --- a/server/lib/activitypub/send/send-dislike.ts +++ b/server/lib/activitypub/send/send-dislike.ts @@ -6,7 +6,7 @@ import { sendVideoRelatedActivity } from './utils' import { audiencify, getAudience } from '../audience' import { MActor, MActorAudience, MVideoAccountLight, MVideoUrl } from '../../../typings/models' -async function sendDislike (byActor: MActor, video: MVideoAccountLight, t: Transaction) { +function sendDislike (byActor: MActor, video: MVideoAccountLight, t: Transaction) { logger.info('Creating job to dislike %s.', video.url) const activityBuilder = (audience: ActivityAudience) => { diff --git a/server/lib/activitypub/send/send-flag.ts b/server/lib/activitypub/send/send-flag.ts index da7638a7b..e4e523631 100644 --- a/server/lib/activitypub/send/send-flag.ts +++ b/server/lib/activitypub/send/send-flag.ts @@ -7,7 +7,7 @@ import { Transaction } from 'sequelize' import { MActor, MVideoFullLight } from '../../../typings/models' import { MVideoAbuseVideo } from '../../../typings/models/video' -async function sendVideoAbuse (byActor: MActor, videoAbuse: MVideoAbuseVideo, video: MVideoFullLight, t: Transaction) { +function sendVideoAbuse (byActor: MActor, videoAbuse: MVideoAbuseVideo, video: MVideoFullLight, t: Transaction) { if (!video.VideoChannel.Account.Actor.serverId) return // Local user const url = getVideoAbuseActivityPubUrl(videoAbuse) diff --git a/server/lib/activitypub/send/send-like.ts b/server/lib/activitypub/send/send-like.ts index e84a6f98b..5db252325 100644 --- a/server/lib/activitypub/send/send-like.ts +++ b/server/lib/activitypub/send/send-like.ts @@ -6,7 +6,7 @@ import { audiencify, getAudience } from '../audience' import { logger } from '../../../helpers/logger' import { MActor, MActorAudience, MVideoAccountLight, MVideoUrl } from '../../../typings/models' -async function sendLike (byActor: MActor, video: MVideoAccountLight, t: Transaction) { +function sendLike (byActor: MActor, video: MVideoAccountLight, t: Transaction) { logger.info('Creating job to like %s.', video.url) const activityBuilder = (audience: ActivityAudience) => { diff --git a/server/lib/activitypub/send/send-reject.ts b/server/lib/activitypub/send/send-reject.ts index 4258a3c36..643c468a9 100644 --- a/server/lib/activitypub/send/send-reject.ts +++ b/server/lib/activitypub/send/send-reject.ts @@ -5,7 +5,7 @@ import { buildFollowActivity } from './send-follow' import { logger } from '../../../helpers/logger' import { MActor } from '../../../typings/models' -async function sendReject (follower: MActor, following: MActor) { +function sendReject (follower: MActor, following: MActor) { if (!follower.serverId) { // This should never happen logger.warn('Do not sending reject to local follower.') return diff --git a/server/lib/activitypub/send/send-undo.ts b/server/lib/activitypub/send/send-undo.ts index e9ab5b3c5..33f1d4921 100644 --- a/server/lib/activitypub/send/send-undo.ts +++ b/server/lib/activitypub/send/send-undo.ts @@ -28,7 +28,7 @@ import { MVideoShare } from '../../../typings/models' -async function sendUndoFollow (actorFollow: MActorFollowActors, t: Transaction) { +function sendUndoFollow (actorFollow: MActorFollowActors, t: Transaction) { const me = actorFollow.ActorFollower const following = actorFollow.ActorFollowing @@ -118,10 +118,10 @@ function undoActivityData ( } async function sendUndoVideoRelatedActivity (options: { - byActor: MActor, - video: MVideoAccountLight, - url: string, - activity: ActivityFollow | ActivityLike | ActivityDislike | ActivityCreate | ActivityAnnounce, + byActor: MActor + video: MVideoAccountLight + url: string + activity: ActivityFollow | ActivityLike | ActivityDislike | ActivityCreate | ActivityAnnounce transaction: Transaction }) { const activityBuilder = (audience: ActivityAudience) => { diff --git a/server/lib/activitypub/send/send-update.ts b/server/lib/activitypub/send/send-update.ts index 9c76671b5..cb500bd34 100644 --- a/server/lib/activitypub/send/send-update.ts +++ b/server/lib/activitypub/send/send-update.ts @@ -8,7 +8,6 @@ import { getUpdateActivityPubUrl } from '../url' import { broadcastToFollowers, sendVideoRelatedActivity } from './utils' import { audiencify, getActorsInvolvedInVideo, getAudience } from '../audience' import { logger } from '../../../helpers/logger' -import { VideoCaptionModel } from '../../../models/video/video-caption' import { VideoPlaylistPrivacy } from '../../../../shared/models/videos/playlist/video-playlist-privacy.model' import { getServerActor } from '../../../helpers/utils' import { @@ -29,7 +28,7 @@ async function sendUpdateVideo (videoArg: MVideoAPWithoutCaption, t: Transaction logger.info('Creating job to update video %s.', video.url) - const byActor = overrodeByActor ? overrodeByActor : video.VideoChannel.Account.Actor + const byActor = overrodeByActor || video.VideoChannel.Account.Actor const url = getUpdateActivityPubUrl(video.url, video.updatedAt.toISOString()) diff --git a/server/lib/activitypub/send/utils.ts b/server/lib/activitypub/send/utils.ts index 77b723479..6fb4efd60 100644 --- a/server/lib/activitypub/send/utils.ts +++ b/server/lib/activitypub/send/utils.ts @@ -10,8 +10,8 @@ import { afterCommitIfTransaction } from '../../../helpers/database-utils' import { MActorWithInboxes, MActor, MActorId, MActorLight, MVideo, MVideoAccountLight } from '../../../typings/models' async function sendVideoRelatedActivity (activityBuilder: (audience: ActivityAudience) => Activity, options: { - byActor: MActorLight, - video: MVideoAccountLight, + byActor: MActorLight + video: MVideoAccountLight transaction?: Transaction }) { const { byActor, video, transaction } = options diff --git a/server/lib/activitypub/video-comments.ts b/server/lib/activitypub/video-comments.ts index d5c078a29..8642d2432 100644 --- a/server/lib/activitypub/video-comments.ts +++ b/server/lib/activitypub/video-comments.ts @@ -10,9 +10,9 @@ import { checkUrlsSameHost } from '../../helpers/activitypub' import { MCommentOwner, MCommentOwnerVideo, MVideoAccountLightBlacklistAllFiles } from '../../typings/models/video' type ResolveThreadParams = { - url: string, - comments?: MCommentOwner[], - isVideo?: boolean, + url: string + comments?: MCommentOwner[] + isVideo?: boolean commentCreated?: boolean } type ResolveThreadResult = Promise<{ video: MVideoAccountLightBlacklistAllFiles, comment: MCommentOwnerVideo, commentCreated: boolean }> @@ -28,7 +28,7 @@ async function resolveThread (params: ResolveThreadParams): ResolveThreadResult if (params.commentCreated === undefined) params.commentCreated = false if (params.comments === undefined) params.comments = [] - // Already have this comment? + // Already have this comment? if (isVideo !== true) { const result = await resolveCommentFromDB(params) if (result) return result @@ -87,7 +87,7 @@ async function tryResolveThreadFromVideo (params: ResolveThreadParams) { let resultComment: MCommentOwnerVideo if (comments.length !== 0) { - const firstReply = comments[ comments.length - 1 ] as MCommentOwnerVideo + const firstReply = comments[comments.length - 1] as MCommentOwnerVideo firstReply.inReplyToCommentId = null firstReply.originCommentId = null firstReply.videoId = video.id @@ -97,9 +97,9 @@ async function tryResolveThreadFromVideo (params: ResolveThreadParams) { comments[comments.length - 1] = await firstReply.save() for (let i = comments.length - 2; i >= 0; i--) { - const comment = comments[ i ] as MCommentOwnerVideo + const comment = comments[i] as MCommentOwnerVideo comment.originCommentId = firstReply.id - comment.inReplyToCommentId = comments[ i + 1 ].id + comment.inReplyToCommentId = comments[i + 1].id comment.videoId = video.id comment.changed('updatedAt', true) comment.Video = video diff --git a/server/lib/activitypub/video-rates.ts b/server/lib/activitypub/video-rates.ts index 6bd46bb58..79ccfbc7e 100644 --- a/server/lib/activitypub/video-rates.ts +++ b/server/lib/activitypub/video-rates.ts @@ -58,8 +58,6 @@ async function createRates (ratesUrl: string[], video: MVideo, rate: VideoRateTy const field = rate === 'like' ? 'likes' : 'dislikes' await video.increment(field, { by: rateCounts }) } - - return } async function sendVideoRateChange ( diff --git a/server/lib/activitypub/videos.ts b/server/lib/activitypub/videos.ts index 6bc2258cc..9e43caa20 100644 --- a/server/lib/activitypub/videos.ts +++ b/server/lib/activitypub/videos.ts @@ -197,25 +197,25 @@ async function syncVideoExternalAttributes (video: MVideo, fetchedVideo: VideoTo jobPayloads.push({ uri: fetchedVideo.comments, videoId: video.id, type: 'video-comments' as 'video-comments' }) } - await Bluebird.map(jobPayloads, payload => JobQueue.Instance.createJob({ type: 'activitypub-http-fetcher', payload })) + await Bluebird.map(jobPayloads, payload => JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload })) } function getOrCreateVideoAndAccountAndChannel (options: { - videoObject: { id: string } | string, - syncParam?: SyncParam, - fetchType?: 'all', + videoObject: { id: string } | string + syncParam?: SyncParam + fetchType?: 'all' allowRefresh?: boolean }): Promise<{ video: MVideoAccountLightBlacklistAllFiles, created: boolean, autoBlacklisted?: boolean }> function getOrCreateVideoAndAccountAndChannel (options: { - videoObject: { id: string } | string, - syncParam?: SyncParam, - fetchType?: VideoFetchByUrlType, + videoObject: { id: string } | string + syncParam?: SyncParam + fetchType?: VideoFetchByUrlType allowRefresh?: boolean }): Promise<{ video: MVideoAccountLightBlacklistAllFiles | MVideoThumbnail, created: boolean, autoBlacklisted?: boolean }> async function getOrCreateVideoAndAccountAndChannel (options: { - videoObject: { id: string } | string, - syncParam?: SyncParam, - fetchType?: VideoFetchByUrlType, + videoObject: { id: string } | string + syncParam?: SyncParam + fetchType?: VideoFetchByUrlType allowRefresh?: boolean // true by default }): Promise<{ video: MVideoAccountLightBlacklistAllFiles | MVideoThumbnail, created: boolean, autoBlacklisted?: boolean }> { // Default params @@ -235,8 +235,14 @@ async function getOrCreateVideoAndAccountAndChannel (options: { syncParam } - if (syncParam.refreshVideo === true) videoFromDatabase = await refreshVideoIfNeeded(refreshOptions) - else await JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'video', url: videoFromDatabase.url } }) + if (syncParam.refreshVideo === true) { + videoFromDatabase = await refreshVideoIfNeeded(refreshOptions) + } else { + await JobQueue.Instance.createJobWithPromise({ + type: 'activitypub-refresher', + payload: { type: 'video', url: videoFromDatabase.url } + }) + } } return { video: videoFromDatabase, created: false } @@ -255,10 +261,10 @@ async function getOrCreateVideoAndAccountAndChannel (options: { } async function updateVideoFromAP (options: { - video: MVideoAccountLightBlacklistAllFiles, - videoObject: VideoTorrentObject, - account: MAccountIdActor, - channel: MChannelDefault, + video: MVideoAccountLightBlacklistAllFiles + videoObject: VideoTorrentObject + account: MAccountIdActor + channel: MChannelDefault overrideTo?: string[] }) { const { video, videoObject, account, channel, overrideTo } = options @@ -289,7 +295,7 @@ async function updateVideoFromAP (options: { throw new Error('Account ' + account.Actor.url + ' does not own video channel ' + videoChannel.Actor.url) } - const to = overrideTo ? overrideTo : videoObject.to + const to = overrideTo || videoObject.to const videoData = await videoActivityObjectToDBAttributes(channel, videoObject, to) video.name = videoData.name video.uuid = videoData.uuid @@ -412,8 +418,8 @@ async function updateVideoFromAP (options: { } async function refreshVideoIfNeeded (options: { - video: MVideoThumbnail, - fetchedType: VideoFetchByUrlType, + video: MVideoThumbnail + fetchedType: VideoFetchByUrlType syncParam: SyncParam }): Promise { if (!options.video.isOutdated()) return options.video @@ -582,13 +588,13 @@ async function createVideo (videoObject: VideoTorrentObject, channel: MChannelAc thumbnailModel = videoCreated.id return thumbnailModel.save() - }) + }).catch(err => logger.error('Cannot create miniature from url.', { err })) } return { autoBlacklisted, videoCreated } } -async function videoActivityObjectToDBAttributes (videoChannel: MChannelId, videoObject: VideoTorrentObject, to: string[] = []) { +function videoActivityObjectToDBAttributes (videoChannel: MChannelId, videoObject: VideoTorrentObject, to: string[] = []) { const privacy = to.indexOf(ACTIVITY_PUB.PUBLIC) !== -1 ? VideoPrivacy.PUBLIC : VideoPrivacy.UNLISTED const duration = videoObject.duration.replace(/[^\d]+/, '') @@ -661,7 +667,7 @@ function videoFileActivityUrlToDBAttributes ( const mediaType = fileUrl.mediaType const attribute = { - extname: MIMETYPES.VIDEO.MIMETYPE_EXT[ mediaType ], + extname: MIMETYPES.VIDEO.MIMETYPE_EXT[mediaType], infoHash: parsed.infoHash, resolution: fileUrl.height, size: fileUrl.size, -- cgit v1.2.3