From 8424c4026afd7304880a4ce8138a04ffb3d8c938 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 30 Aug 2019 16:50:12 +0200 Subject: Add auto follow back support for instances --- server/lib/activitypub/follow.ts | 36 ++++ server/lib/activitypub/process/process-accept.ts | 2 +- server/lib/activitypub/process/process-follow.ts | 20 ++- server/lib/activitypub/send/send-follow.ts | 1 - server/lib/emailer.ts | 33 +++- .../lib/job-queue/handlers/activitypub-follow.ts | 24 +-- server/lib/job-queue/handlers/video-import.ts | 5 +- server/lib/notifier.ts | 184 ++++++++++++--------- server/lib/user.ts | 3 +- server/lib/video-blacklist.ts | 8 +- 10 files changed, 210 insertions(+), 106 deletions(-) create mode 100644 server/lib/activitypub/follow.ts (limited to 'server/lib') diff --git a/server/lib/activitypub/follow.ts b/server/lib/activitypub/follow.ts new file mode 100644 index 000000000..c57e43c91 --- /dev/null +++ b/server/lib/activitypub/follow.ts @@ -0,0 +1,36 @@ +import { MActorFollowActors } from '../../typings/models' +import { CONFIG } from '../../initializers/config' +import { SERVER_ACTOR_NAME } from '../../initializers/constants' +import { JobQueue } from '../job-queue' +import { logger } from '../../helpers/logger' +import { getServerActor } from '../../helpers/utils' +import { ServerModel } from '@server/models/server/server' + +async function autoFollowBackIfNeeded (actorFollow: MActorFollowActors) { + if (!CONFIG.FOLLOWINGS.INSTANCE.AUTO_FOLLOW_BACK.ENABLED) return + + const follower = actorFollow.ActorFollower + + if (follower.type === 'Application' && follower.preferredUsername === SERVER_ACTOR_NAME) { + logger.info('Auto follow back %s.', follower.url) + + const me = await getServerActor() + + const server = await ServerModel.load(follower.serverId) + const host = server.host + + const payload = { + host, + name: SERVER_ACTOR_NAME, + followerActorId: me.id, + isAutoFollow: true + } + + JobQueue.Instance.createJob({ type: 'activitypub-follow', payload }) + .catch(err => logger.error('Cannot create auto follow back job for %s.', host, err)) + } +} + +export { + autoFollowBackIfNeeded +} diff --git a/server/lib/activitypub/process/process-accept.ts b/server/lib/activitypub/process/process-accept.ts index 86f7c764d..dcfbb2c84 100644 --- a/server/lib/activitypub/process/process-accept.ts +++ b/server/lib/activitypub/process/process-accept.ts @@ -24,7 +24,7 @@ async function processAccept (actor: MActorDefault, targetActor: MActorSignature if (!follow) throw new Error('Cannot find associated follow.') if (follow.state !== 'accepted') { - follow.set('state', 'accepted') + follow.state = 'accepted' await follow.save() await addFetchOutboxJob(targetActor) diff --git a/server/lib/activitypub/process/process-follow.ts b/server/lib/activitypub/process/process-follow.ts index bc5660395..85f22d654 100644 --- a/server/lib/activitypub/process/process-follow.ts +++ b/server/lib/activitypub/process/process-follow.ts @@ -10,7 +10,8 @@ import { getAPId } from '../../../helpers/activitypub' import { getServerActor } from '../../../helpers/utils' import { CONFIG } from '../../../initializers/config' import { APProcessorOptions } from '../../../typings/activitypub-processor.model' -import { MAccount, MActorFollowActors, MActorFollowFull, MActorSignature } from '../../../typings/models' +import { MActorFollowActors, MActorSignature } from '../../../typings/models' +import { autoFollowBackIfNeeded } from '../follow' async function processFollowActivity (options: APProcessorOptions) { const { activity, byActor } = options @@ -28,7 +29,7 @@ export { // --------------------------------------------------------------------------- async function processFollow (byActor: MActorSignature, targetActorURL: string) { - const { actorFollow, created, isFollowingInstance } = await sequelizeTypescript.transaction(async t => { + const { actorFollow, created, isFollowingInstance, targetActor } = await sequelizeTypescript.transaction(async t => { const targetActor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(targetActorURL, t) if (!targetActor) throw new Error('Unknown actor') @@ -67,21 +68,24 @@ async function processFollow (byActor: MActorSignature, targetActorURL: string) actorFollow.ActorFollowing = targetActor // Target sends to actor he accepted the follow request - if (actorFollow.state === 'accepted') await sendAccept(actorFollow) + if (actorFollow.state === 'accepted') { + await sendAccept(actorFollow) + await autoFollowBackIfNeeded(actorFollow) + } - return { actorFollow, created, isFollowingInstance } + return { actorFollow, created, isFollowingInstance, targetActor } }) // Rejected if (!actorFollow) return if (created) { + const follower = await ActorModel.loadFull(byActor.id) + const actorFollowFull = Object.assign(actorFollow, { ActorFollowing: targetActor, ActorFollower: follower }) + if (isFollowingInstance) { - Notifier.Instance.notifyOfNewInstanceFollow(actorFollow) + Notifier.Instance.notifyOfNewInstanceFollow(actorFollowFull) } else { - const actorFollowFull = actorFollow as MActorFollowFull - actorFollowFull.ActorFollower.Account = await actorFollow.ActorFollower.$get('Account') as MAccount - Notifier.Instance.notifyOfNewUserFollow(actorFollowFull) } } diff --git a/server/lib/activitypub/send/send-follow.ts b/server/lib/activitypub/send/send-follow.ts index 6b17b25da..ce400d8ff 100644 --- a/server/lib/activitypub/send/send-follow.ts +++ b/server/lib/activitypub/send/send-follow.ts @@ -1,5 +1,4 @@ import { ActivityFollow } from '../../../../shared/models/activitypub' -import { ActorFollowModel } from '../../../models/activitypub/actor-follow' import { getActorFollowActivityPubUrl } from '../url' import { unicastTo } from './utils' import { logger } from '../../../helpers/logger' diff --git a/server/lib/emailer.ts b/server/lib/emailer.ts index 76349ef8f..bd3d4f252 100644 --- a/server/lib/emailer.ts +++ b/server/lib/emailer.ts @@ -6,8 +6,15 @@ import { JobQueue } from './job-queue' import { EmailPayload } from './job-queue/handlers/email' import { readFileSync } from 'fs-extra' import { WEBSERVER } from '../initializers/constants' -import { MCommentOwnerVideo, MVideo, MVideoAbuseVideo, MVideoAccountLight, MVideoBlacklistVideo } from '../typings/models/video' -import { MActorFollowActors, MActorFollowFollowingFullFollowerAccount, MUser } from '../typings/models' +import { + MCommentOwnerVideo, + MVideo, + MVideoAbuseVideo, + MVideoAccountLight, + MVideoBlacklistLightVideo, + MVideoBlacklistVideo +} from '../typings/models/video' +import { MActorFollowActors, MActorFollowFull, MUser } from '../typings/models' import { MVideoImport, MVideoImportVideo } from '@server/typings/models/video/video-import' type SendEmailOptions = { @@ -107,7 +114,7 @@ class Emailer { return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) } - addNewFollowNotification (to: string[], actorFollow: MActorFollowFollowingFullFollowerAccount, followType: 'account' | 'channel') { + addNewFollowNotification (to: string[], actorFollow: MActorFollowFull, followType: 'account' | 'channel') { const followerName = actorFollow.ActorFollower.Account.getDisplayName() const followingName = (actorFollow.ActorFollowing.VideoChannel || actorFollow.ActorFollowing.Account).getDisplayName() @@ -144,6 +151,22 @@ class Emailer { return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) } + addAutoInstanceFollowingNotification (to: string[], actorFollow: MActorFollowActors) { + const text = `Hi dear admin,\n\n` + + `Your instance automatically followed a new instance: ${actorFollow.ActorFollowing.url}` + + `\n\n` + + `Cheers,\n` + + `${CONFIG.EMAIL.BODY.SIGNATURE}` + + const emailPayload: EmailPayload = { + to, + subject: CONFIG.EMAIL.SUBJECT.PREFIX + 'Auto instance following', + text + } + + return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) + } + myVideoPublishedNotification (to: string[], video: MVideo) { const videoUrl = WEBSERVER.URL + video.getWatchStaticPath() @@ -265,9 +288,9 @@ class Emailer { return JobQueue.Instance.createJob({ type: 'email', payload: emailPayload }) } - addVideoAutoBlacklistModeratorsNotification (to: string[], video: MVideo) { + addVideoAutoBlacklistModeratorsNotification (to: string[], videoBlacklist: MVideoBlacklistLightVideo) { const VIDEO_AUTO_BLACKLIST_URL = WEBSERVER.URL + '/admin/moderation/video-auto-blacklist/list' - const videoUrl = WEBSERVER.URL + video.getWatchStaticPath() + const videoUrl = WEBSERVER.URL + videoBlacklist.Video.getWatchStaticPath() const text = `Hi,\n\n` + `A recently added video was auto-blacklisted and requires moderator review before publishing.` + diff --git a/server/lib/job-queue/handlers/activitypub-follow.ts b/server/lib/job-queue/handlers/activitypub-follow.ts index 5cb55cad6..af7c8a838 100644 --- a/server/lib/job-queue/handlers/activitypub-follow.ts +++ b/server/lib/job-queue/handlers/activitypub-follow.ts @@ -10,12 +10,13 @@ import { ActorFollowModel } from '../../../models/activitypub/actor-follow' import { ActorModel } from '../../../models/activitypub/actor' import { Notifier } from '../../notifier' import { sequelizeTypescript } from '../../../initializers/database' -import { MAccount, MActor, MActorFollowActors, MActorFollowFull, MActorFull } from '../../../typings/models' +import { MActor, MActorFollowActors, MActorFull } from '../../../typings/models' export type ActivitypubFollowPayload = { followerActorId: number name: string host: string + isAutoFollow?: boolean } async function processActivityPubFollow (job: Bull.Job) { @@ -35,7 +36,7 @@ async function processActivityPubFollow (job: Bull.Job) { const fromActor = await ActorModel.load(payload.followerActorId) - return retryTransactionWrapper(follow, fromActor, targetActor) + return retryTransactionWrapper(follow, fromActor, targetActor, payload.isAutoFollow) } // --------------------------------------------------------------------------- @@ -45,7 +46,7 @@ export { // --------------------------------------------------------------------------- -async function follow (fromActor: MActor, targetActor: MActorFull) { +async function follow (fromActor: MActor, targetActor: MActorFull, isAutoFollow = false) { if (fromActor.id === targetActor.id) { throw new Error('Follower is the same than target actor.') } @@ -75,14 +76,15 @@ async function follow (fromActor: MActor, targetActor: MActorFull) { return actorFollow }) - if (actorFollow.state === 'accepted') { - const followerFull = Object.assign(fromActor, { Account: await actorFollow.ActorFollower.$get('Account') as MAccount }) + const followerFull = await ActorModel.loadFull(fromActor.id) - const actorFollowFull = Object.assign(actorFollow, { - ActorFollowing: targetActor, - ActorFollower: followerFull - }) + const actorFollowFull = Object.assign(actorFollow, { + ActorFollowing: targetActor, + ActorFollower: followerFull + }) - Notifier.Instance.notifyOfNewUserFollow(actorFollowFull) - } + if (actorFollow.state === 'accepted') Notifier.Instance.notifyOfNewUserFollow(actorFollowFull) + if (isAutoFollow === true) Notifier.Instance.notifyOfAutoInstanceFollowing(actorFollowFull) + + return actorFollow } diff --git a/server/lib/job-queue/handlers/video-import.ts b/server/lib/job-queue/handlers/video-import.ts index ff8c93328..93a3e9d90 100644 --- a/server/lib/job-queue/handlers/video-import.ts +++ b/server/lib/job-queue/handlers/video-import.ts @@ -21,6 +21,7 @@ import { createVideoMiniatureFromUrl, generateVideoMiniature } from '../../thumb import { ThumbnailType } from '../../../../shared/models/videos/thumbnail.type' import { MThumbnail } from '../../../typings/models/video/thumbnail' import { MVideoImportDefault, MVideoImportDefaultFiles, MVideoImportVideo } from '@server/typings/models/video/video-import' +import { MVideoBlacklistVideo, MVideoBlacklist } from '@server/typings/models' type VideoImportYoutubeDLPayload = { type: 'youtube-dl' @@ -204,7 +205,9 @@ async function processFile (downloader: () => Promise, videoImport: MVid Notifier.Instance.notifyOnFinishedVideoImport(videoImportUpdated, true) if (video.isBlacklisted()) { - Notifier.Instance.notifyOnVideoAutoBlacklist(video) + const videoBlacklist = Object.assign(video.VideoBlacklist, { Video: video }) + + Notifier.Instance.notifyOnVideoAutoBlacklist(videoBlacklist) } else { Notifier.Instance.notifyOnNewVideoIfNeeded(video) } diff --git a/server/lib/notifier.ts b/server/lib/notifier.ts index 23f76a21a..b7cc2607d 100644 --- a/server/lib/notifier.ts +++ b/server/lib/notifier.ts @@ -1,30 +1,30 @@ import { UserNotificationSettingValue, UserNotificationType, UserRight } from '../../shared/models/users' import { logger } from '../helpers/logger' -import { VideoModel } from '../models/video/video' import { Emailer } from './emailer' import { UserNotificationModel } from '../models/account/user-notification' -import { VideoCommentModel } from '../models/video/video-comment' import { UserModel } from '../models/account/user' import { PeerTubeSocket } from './peertube-socket' import { CONFIG } from '../initializers/config' import { VideoPrivacy, VideoState } from '../../shared/models/videos' -import { VideoBlacklistModel } from '../models/video/video-blacklist' import * as Bluebird from 'bluebird' -import { VideoImportModel } from '../models/video/video-import' import { AccountBlocklistModel } from '../models/account/account-blocklist' import { MCommentOwnerVideo, - MVideo, MVideoAbuseVideo, MVideoAccountLight, + MVideoBlacklistLightVideo, MVideoBlacklistVideo, MVideoFullLight } from '../typings/models/video' -import { MUser, MUserAccount, MUserWithNotificationSetting, UserNotificationModelForApi } from '@server/typings/models/user' -import { MActorFollowActors, MActorFollowFull, MActorFollowFollowingFullFollowerAccount } from '../typings/models' -import { ActorFollowModel } from '../models/activitypub/actor-follow' +import { + MUser, + MUserDefault, + MUserNotifSettingAccount, + MUserWithNotificationSetting, + UserNotificationModelForApi +} from '@server/typings/models/user' +import { MActorFollowFull } from '../typings/models' import { MVideoImportVideo } from '@server/typings/models/video/video-import' -import { AccountModel } from '@server/models/account/account' class Notifier { @@ -77,9 +77,9 @@ class Notifier { .catch(err => logger.error('Cannot notify of new video abuse of video %s.', videoAbuse.Video.url, { err })) } - notifyOnVideoAutoBlacklist (video: MVideo): void { - this.notifyModeratorsOfVideoAutoBlacklist(video) - .catch(err => logger.error('Cannot notify of auto-blacklist of video %s.', video.url, { err })) + notifyOnVideoAutoBlacklist (videoBlacklist: MVideoBlacklistLightVideo): void { + this.notifyModeratorsOfVideoAutoBlacklist(videoBlacklist) + .catch(err => logger.error('Cannot notify of auto-blacklist of video %s.', videoBlacklist.Video.url, { err })) } notifyOnVideoBlacklist (videoBlacklist: MVideoBlacklistVideo): void { @@ -87,7 +87,7 @@ class Notifier { .catch(err => logger.error('Cannot notify video owner of new video blacklist of %s.', videoBlacklist.Video.url, { err })) } - notifyOnVideoUnblacklist (video: MVideo): void { + notifyOnVideoUnblacklist (video: MVideoFullLight): void { this.notifyVideoOwnerOfUnblacklist(video) .catch(err => logger.error('Cannot notify video owner of unblacklist of %s.', video.url, { err })) } @@ -97,12 +97,12 @@ class Notifier { .catch(err => logger.error('Cannot notify owner that its video import %s is finished.', videoImport.getTargetIdentifier(), { err })) } - notifyOnNewUserRegistration (user: MUserAccount): void { + notifyOnNewUserRegistration (user: MUserDefault): void { this.notifyModeratorsOfNewUserRegistration(user) .catch(err => logger.error('Cannot notify moderators of new user registration (%s).', user.username, { err })) } - notifyOfNewUserFollow (actorFollow: MActorFollowFollowingFullFollowerAccount): void { + notifyOfNewUserFollow (actorFollow: MActorFollowFull): void { this.notifyUserOfNewActorFollow(actorFollow) .catch(err => { logger.error( @@ -114,30 +114,37 @@ class Notifier { }) } - notifyOfNewInstanceFollow (actorFollow: MActorFollowActors): void { + notifyOfNewInstanceFollow (actorFollow: MActorFollowFull): void { this.notifyAdminsOfNewInstanceFollow(actorFollow) .catch(err => { logger.error('Cannot notify administrators of new follower %s.', actorFollow.ActorFollower.url, { err }) }) } + notifyOfAutoInstanceFollowing (actorFollow: MActorFollowFull): void { + this.notifyAdminsOfAutoInstanceFollowing(actorFollow) + .catch(err => { + logger.error('Cannot notify administrators of auto instance following %s.', actorFollow.ActorFollowing.url, { err }) + }) + } + private async notifySubscribersOfNewVideo (video: MVideoAccountLight) { // List all followers that are users const users = await UserModel.listUserSubscribersOf(video.VideoChannel.actorId) logger.info('Notifying %d users of new video %s.', users.length, video.url) - function settingGetter (user: UserModel) { + function settingGetter (user: MUserWithNotificationSetting) { return user.NotificationSetting.newVideoFromSubscription } - async function notificationCreator (user: UserModel) { - const notification = await UserNotificationModel.create({ + async function notificationCreator (user: MUserWithNotificationSetting) { + const notification = await UserNotificationModel.create({ type: UserNotificationType.NEW_VIDEO_FROM_SUBSCRIPTION, userId: user.id, videoId: video.id }) - notification.Video = video as VideoModel + notification.Video = video return notification } @@ -162,17 +169,17 @@ class Notifier { logger.info('Notifying user %s of new comment %s.', user.username, comment.url) - function settingGetter (user: UserModel) { + function settingGetter (user: MUserWithNotificationSetting) { return user.NotificationSetting.newCommentOnMyVideo } - async function notificationCreator (user: UserModel) { - const notification = await UserNotificationModel.create({ + async function notificationCreator (user: MUserWithNotificationSetting) { + const notification = await UserNotificationModel.create({ type: UserNotificationType.NEW_COMMENT_ON_MY_VIDEO, userId: user.id, commentId: comment.id }) - notification.Comment = comment as VideoCommentModel + notification.Comment = comment return notification } @@ -207,19 +214,19 @@ class Notifier { logger.info('Notifying %d users of new comment %s.', users.length, comment.url) - function settingGetter (user: UserModel) { + function settingGetter (user: MUserNotifSettingAccount) { if (accountMutedHash[user.Account.id] === true) return UserNotificationSettingValue.NONE return user.NotificationSetting.commentMention } - async function notificationCreator (user: UserModel) { - const notification = await UserNotificationModel.create({ + async function notificationCreator (user: MUserNotifSettingAccount) { + const notification = await UserNotificationModel.create({ type: UserNotificationType.COMMENT_MENTION, userId: user.id, commentId: comment.id }) - notification.Comment = comment as VideoCommentModel + notification.Comment = comment return notification } @@ -231,7 +238,7 @@ class Notifier { return this.notify({ users, settingGetter, notificationCreator, emailSender }) } - private async notifyUserOfNewActorFollow (actorFollow: MActorFollowFollowingFullFollowerAccount) { + private async notifyUserOfNewActorFollow (actorFollow: MActorFollowFull) { if (actorFollow.ActorFollowing.isOwned() === false) return // Account follows one of our account? @@ -253,17 +260,17 @@ class Notifier { logger.info('Notifying user %s of new follower: %s.', user.username, followerAccount.getDisplayName()) - function settingGetter (user: UserModel) { + function settingGetter (user: MUserWithNotificationSetting) { return user.NotificationSetting.newFollow } - async function notificationCreator (user: UserModel) { - const notification = await UserNotificationModel.create({ + async function notificationCreator (user: MUserWithNotificationSetting) { + const notification = await UserNotificationModel.create({ type: UserNotificationType.NEW_FOLLOW, userId: user.id, actorFollowId: actorFollow.id }) - notification.ActorFollow = actorFollow as ActorFollowModel + notification.ActorFollow = actorFollow return notification } @@ -275,22 +282,22 @@ class Notifier { return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) } - private async notifyAdminsOfNewInstanceFollow (actorFollow: MActorFollowActors) { + private async notifyAdminsOfNewInstanceFollow (actorFollow: MActorFollowFull) { const admins = await UserModel.listWithRight(UserRight.MANAGE_SERVER_FOLLOW) logger.info('Notifying %d administrators of new instance follower: %s.', admins.length, actorFollow.ActorFollower.url) - function settingGetter (user: UserModel) { + function settingGetter (user: MUserWithNotificationSetting) { return user.NotificationSetting.newInstanceFollower } - async function notificationCreator (user: UserModel) { - const notification = await UserNotificationModel.create({ + async function notificationCreator (user: MUserWithNotificationSetting) { + const notification = await UserNotificationModel.create({ type: UserNotificationType.NEW_INSTANCE_FOLLOWER, userId: user.id, actorFollowId: actorFollow.id }) - notification.ActorFollow = actorFollow as ActorFollowModel + notification.ActorFollow = actorFollow return notification } @@ -302,18 +309,45 @@ class Notifier { return this.notify({ users: admins, settingGetter, notificationCreator, emailSender }) } + private async notifyAdminsOfAutoInstanceFollowing (actorFollow: MActorFollowFull) { + const admins = await UserModel.listWithRight(UserRight.MANAGE_SERVER_FOLLOW) + + logger.info('Notifying %d administrators of auto instance following: %s.', admins.length, actorFollow.ActorFollowing.url) + + function settingGetter (user: MUserWithNotificationSetting) { + return user.NotificationSetting.autoInstanceFollowing + } + + async function notificationCreator (user: MUserWithNotificationSetting) { + const notification = await UserNotificationModel.create({ + type: UserNotificationType.AUTO_INSTANCE_FOLLOWING, + userId: user.id, + actorFollowId: actorFollow.id + }) + notification.ActorFollow = actorFollow + + return notification + } + + function emailSender (emails: string[]) { + return Emailer.Instance.addAutoInstanceFollowingNotification(emails, actorFollow) + } + + return this.notify({ users: admins, settingGetter, notificationCreator, emailSender }) + } + private async notifyModeratorsOfNewVideoAbuse (videoAbuse: MVideoAbuseVideo) { const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_ABUSES) if (moderators.length === 0) return logger.info('Notifying %s user/moderators of new video abuse %s.', moderators.length, videoAbuse.Video.url) - function settingGetter (user: UserModel) { + function settingGetter (user: MUserWithNotificationSetting) { return user.NotificationSetting.videoAbuseAsModerator } - async function notificationCreator (user: UserModel) { - const notification: UserNotificationModelForApi = await UserNotificationModel.create({ + async function notificationCreator (user: MUserWithNotificationSetting) { + const notification: UserNotificationModelForApi = await UserNotificationModel.create({ type: UserNotificationType.NEW_VIDEO_ABUSE_FOR_MODERATORS, userId: user.id, videoAbuseId: videoAbuse.id @@ -330,29 +364,29 @@ class Notifier { return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) } - private async notifyModeratorsOfVideoAutoBlacklist (video: MVideo) { + private async notifyModeratorsOfVideoAutoBlacklist (videoBlacklist: MVideoBlacklistLightVideo) { const moderators = await UserModel.listWithRight(UserRight.MANAGE_VIDEO_BLACKLIST) if (moderators.length === 0) return - logger.info('Notifying %s moderators of video auto-blacklist %s.', moderators.length, video.url) + logger.info('Notifying %s moderators of video auto-blacklist %s.', moderators.length, videoBlacklist.Video.url) - function settingGetter (user: UserModel) { + function settingGetter (user: MUserWithNotificationSetting) { return user.NotificationSetting.videoAutoBlacklistAsModerator } - async function notificationCreator (user: UserModel) { - const notification = await UserNotificationModel.create({ + async function notificationCreator (user: MUserWithNotificationSetting) { + const notification = await UserNotificationModel.create({ type: UserNotificationType.VIDEO_AUTO_BLACKLIST_FOR_MODERATORS, userId: user.id, - videoId: video.id + videoBlacklistId: videoBlacklist.id }) - notification.Video = video as VideoModel + notification.VideoBlacklist = videoBlacklist return notification } function emailSender (emails: string[]) { - return Emailer.Instance.addVideoAutoBlacklistModeratorsNotification(emails, video) + return Emailer.Instance.addVideoAutoBlacklistModeratorsNotification(emails, videoBlacklist) } return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) @@ -364,17 +398,17 @@ class Notifier { logger.info('Notifying user %s that its video %s has been blacklisted.', user.username, videoBlacklist.Video.url) - function settingGetter (user: UserModel) { + function settingGetter (user: MUserWithNotificationSetting) { return user.NotificationSetting.blacklistOnMyVideo } - async function notificationCreator (user: UserModel) { - const notification = await UserNotificationModel.create({ + async function notificationCreator (user: MUserWithNotificationSetting) { + const notification = await UserNotificationModel.create({ type: UserNotificationType.BLACKLIST_ON_MY_VIDEO, userId: user.id, videoBlacklistId: videoBlacklist.id }) - notification.VideoBlacklist = videoBlacklist as VideoBlacklistModel + notification.VideoBlacklist = videoBlacklist return notification } @@ -386,23 +420,23 @@ class Notifier { return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) } - private async notifyVideoOwnerOfUnblacklist (video: MVideo) { + private async notifyVideoOwnerOfUnblacklist (video: MVideoFullLight) { const user = await UserModel.loadByVideoId(video.id) if (!user) return logger.info('Notifying user %s that its video %s has been unblacklisted.', user.username, video.url) - function settingGetter (user: UserModel) { + function settingGetter (user: MUserWithNotificationSetting) { return user.NotificationSetting.blacklistOnMyVideo } - async function notificationCreator (user: UserModel) { - const notification = await UserNotificationModel.create({ + async function notificationCreator (user: MUserWithNotificationSetting) { + const notification = await UserNotificationModel.create({ type: UserNotificationType.UNBLACKLIST_ON_MY_VIDEO, userId: user.id, videoId: video.id }) - notification.Video = video as VideoModel + notification.Video = video return notification } @@ -420,17 +454,17 @@ class Notifier { logger.info('Notifying user %s of the publication of its video %s.', user.username, video.url) - function settingGetter (user: UserModel) { + function settingGetter (user: MUserWithNotificationSetting) { return user.NotificationSetting.myVideoPublished } - async function notificationCreator (user: UserModel) { - const notification = await UserNotificationModel.create({ + async function notificationCreator (user: MUserWithNotificationSetting) { + const notification = await UserNotificationModel.create({ type: UserNotificationType.MY_VIDEO_PUBLISHED, userId: user.id, videoId: video.id }) - notification.Video = video as VideoModel + notification.Video = video return notification } @@ -448,17 +482,17 @@ class Notifier { logger.info('Notifying user %s its video import %s is finished.', user.username, videoImport.getTargetIdentifier()) - function settingGetter (user: UserModel) { + function settingGetter (user: MUserWithNotificationSetting) { return user.NotificationSetting.myVideoImportFinished } - async function notificationCreator (user: UserModel) { - const notification = await UserNotificationModel.create({ + async function notificationCreator (user: MUserWithNotificationSetting) { + const notification = await UserNotificationModel.create({ type: success ? UserNotificationType.MY_VIDEO_IMPORT_SUCCESS : UserNotificationType.MY_VIDEO_IMPORT_ERROR, userId: user.id, videoImportId: videoImport.id }) - notification.VideoImport = videoImport as VideoImportModel + notification.VideoImport = videoImport return notification } @@ -472,7 +506,7 @@ class Notifier { return this.notify({ users: [ user ], settingGetter, notificationCreator, emailSender }) } - private async notifyModeratorsOfNewUserRegistration (registeredUser: MUserAccount) { + private async notifyModeratorsOfNewUserRegistration (registeredUser: MUserDefault) { const moderators = await UserModel.listWithRight(UserRight.MANAGE_USERS) if (moderators.length === 0) return @@ -481,17 +515,17 @@ class Notifier { moderators.length, registeredUser.username ) - function settingGetter (user: UserModel) { + function settingGetter (user: MUserWithNotificationSetting) { return user.NotificationSetting.newUserRegistration } - async function notificationCreator (user: UserModel) { - const notification = await UserNotificationModel.create({ + async function notificationCreator (user: MUserWithNotificationSetting) { + const notification = await UserNotificationModel.create({ type: UserNotificationType.NEW_USER_REGISTRATION, userId: user.id, accountId: registeredUser.Account.id }) - notification.Account = registeredUser.Account as AccountModel + notification.Account = registeredUser.Account return notification } @@ -503,11 +537,11 @@ class Notifier { return this.notify({ users: moderators, settingGetter, notificationCreator, emailSender }) } - private async notify (options: { - users: MUserWithNotificationSetting[], - notificationCreator: (user: MUserWithNotificationSetting) => Promise, + private async notify (options: { + users: T[], + notificationCreator: (user: T) => Promise, emailSender: (emails: string[]) => Promise | Bluebird, - settingGetter: (user: MUserWithNotificationSetting) => UserNotificationSettingValue + settingGetter: (user: T) => UserNotificationSettingValue }) { const emails: string[] = [] diff --git a/server/lib/user.ts b/server/lib/user.ts index d84aff464..c45438d95 100644 --- a/server/lib/user.ts +++ b/server/lib/user.ts @@ -138,7 +138,8 @@ function createDefaultUserNotificationSettings (user: MUserId, t: Transaction | newUserRegistration: UserNotificationSettingValue.WEB, commentMention: UserNotificationSettingValue.WEB, newFollow: UserNotificationSettingValue.WEB, - newInstanceFollower: UserNotificationSettingValue.WEB + newInstanceFollower: UserNotificationSettingValue.WEB, + autoInstanceFollowing: UserNotificationSettingValue.WEB } return UserNotificationSettingModel.create(values, { transaction: t }) diff --git a/server/lib/video-blacklist.ts b/server/lib/video-blacklist.ts index a0fc26e84..1dd45b76d 100644 --- a/server/lib/video-blacklist.ts +++ b/server/lib/video-blacklist.ts @@ -6,7 +6,7 @@ import { logger } from '../helpers/logger' import { UserAdminFlag } from '../../shared/models/users/user-flag.model' import { Hooks } from './plugins/hooks' import { Notifier } from './notifier' -import { MUser, MVideoBlacklist, MVideoWithBlacklistLight } from '@server/typings/models' +import { MUser, MVideoBlacklistVideo, MVideoWithBlacklistLight } from '@server/typings/models' async function autoBlacklistVideoIfNeeded (parameters: { video: MVideoWithBlacklistLight, @@ -31,7 +31,7 @@ async function autoBlacklistVideoIfNeeded (parameters: { reason: 'Auto-blacklisted. Moderator review required.', type: VideoBlacklistType.AUTO_BEFORE_PUBLISHED } - const [ videoBlacklist ] = await VideoBlacklistModel.findOrCreate({ + const [ videoBlacklist ] = await VideoBlacklistModel.findOrCreate({ where: { videoId: video.id }, @@ -40,7 +40,9 @@ async function autoBlacklistVideoIfNeeded (parameters: { }) video.VideoBlacklist = videoBlacklist - if (notify) Notifier.Instance.notifyOnVideoAutoBlacklist(video) + videoBlacklist.Video = video + + if (notify) Notifier.Instance.notifyOnVideoAutoBlacklist(videoBlacklist) logger.info('Video %s auto-blacklisted.', video.uuid) -- cgit v1.2.3