- }) : undefined
-
- const videoImport = this.VideoImport ? {
- id: this.VideoImport.id,
- video: this.VideoImport.Video ? this.formatVideo(this.VideoImport.Video) : undefined,
- torrentName: this.VideoImport.torrentName,
- magnetUri: this.VideoImport.magnetUri,
- targetUrl: this.VideoImport.targetUrl
- } : undefined
-
- const comment = this.Comment ? {
- id: this.Comment.id,
- threadId: this.Comment.getThreadId(),
- account: {
- id: this.Comment.Account.id,
- displayName: this.Comment.Account.getDisplayName()
- },
- video: this.formatVideo(this.Comment.Video)
- } : undefined
-
- const videoAbuse = this.VideoAbuse ? {
- id: this.VideoAbuse.id,
- video: this.formatVideo(this.VideoAbuse.Video)
- } : undefined
-
- const videoBlacklist = this.VideoBlacklist ? {
- id: this.VideoBlacklist.id,
- video: this.formatVideo(this.VideoBlacklist.Video)
- } : undefined
-
- const account = this.Account ? {
- id: this.Account.id,
- displayName: this.Account.getDisplayName(),
- name: this.Account.Actor.preferredUsername
- } : undefined
-
- const actorFollow = this.ActorFollow ? {
- id: this.ActorFollow.id,
- follower: {
- displayName: this.ActorFollow.ActorFollower.Account.getDisplayName(),
- name: this.ActorFollow.ActorFollower.preferredUsername
- },
- following: {
- type: this.ActorFollow.ActorFollowing.VideoChannel ? 'channel' as 'channel' : 'account' as 'account',
- displayName: (this.ActorFollow.ActorFollowing.VideoChannel || this.ActorFollow.ActorFollowing.Account).getDisplayName(),
- name: this.ActorFollow.ActorFollowing.preferredUsername
+
+ return base +
+ ` WHERE "actor"."serverId" = ${id} ${whereSuffix}`
+ }
+
+ const queries = [
+ buildAccountWhereQuery(
+ `SELECT "userNotification"."id" FROM "userNotification" ` +
+ `INNER JOIN "account" ON "userNotification"."accountId" = "account"."id" ` +
+ `INNER JOIN actor ON "actor"."id" = "account"."actorId" `
+ ),
+
+ // Remove notifications from muted accounts that followed ours
+ buildAccountWhereQuery(
+ `SELECT "userNotification"."id" FROM "userNotification" ` +
+ `INNER JOIN "actorFollow" ON "actorFollow".id = "userNotification"."actorFollowId" ` +
+ `INNER JOIN actor ON actor.id = "actorFollow"."actorId" ` +
+ `INNER JOIN account ON account."actorId" = actor.id `
+ ),
+
+ // Remove notifications from muted accounts that commented something
+ buildAccountWhereQuery(
+ `SELECT "userNotification"."id" FROM "userNotification" ` +
+ `INNER JOIN "actorFollow" ON "actorFollow".id = "userNotification"."actorFollowId" ` +
+ `INNER JOIN actor ON actor.id = "actorFollow"."actorId" ` +
+ `INNER JOIN account ON account."actorId" = actor.id `
+ ),
+
+ buildAccountWhereQuery(
+ `SELECT "userNotification"."id" FROM "userNotification" ` +
+ `INNER JOIN "videoComment" ON "videoComment".id = "userNotification"."commentId" ` +
+ `INNER JOIN account ON account.id = "videoComment"."accountId" ` +
+ `INNER JOIN actor ON "actor"."id" = "account"."actorId" `
+ )
+ ]
+
+ const query = `DELETE FROM "userNotification" WHERE id IN (${queries.join(' UNION ')})`
+
+ return UserNotificationModel.sequelize.query(query)
+ }
+
+ toFormattedJSON (this: UserNotificationModelForApi): UserNotification {
+ const video = this.Video
+ ? Object.assign(this.formatVideo(this.Video), { channel: this.formatActor(this.Video.VideoChannel) })
+ : undefined
+
+ const videoImport = this.VideoImport
+ ? {
+ id: this.VideoImport.id,
+ video: this.VideoImport.Video ? this.formatVideo(this.VideoImport.Video) : undefined,
+ torrentName: this.VideoImport.torrentName,
+ magnetUri: this.VideoImport.magnetUri,
+ targetUrl: this.VideoImport.targetUrl
+ }
+ : undefined
+
+ const comment = this.Comment
+ ? {
+ id: this.Comment.id,
+ threadId: this.Comment.getThreadId(),
+ account: this.formatActor(this.Comment.Account),
+ video: this.formatVideo(this.Comment.Video)
+ }
+ : undefined
+
+ const abuse = this.Abuse ? this.formatAbuse(this.Abuse) : undefined
+
+ const videoBlacklist = this.VideoBlacklist
+ ? {
+ id: this.VideoBlacklist.id,
+ video: this.formatVideo(this.VideoBlacklist.Video)
+ }
+ : undefined
+
+ const account = this.Account ? this.formatActor(this.Account) : undefined
+
+ const actorFollowingType = {
+ Application: 'instance' as 'instance',
+ Group: 'channel' as 'channel',
+ Person: 'account' as 'account'
+ }
+ const actorFollow = this.ActorFollow
+ ? {
+ id: this.ActorFollow.id,
+ state: this.ActorFollow.state,
+ follower: {
+ id: this.ActorFollow.ActorFollower.Account.id,
+ displayName: this.ActorFollow.ActorFollower.Account.getDisplayName(),
+ name: this.ActorFollow.ActorFollower.preferredUsername,
+ avatar: this.ActorFollow.ActorFollower.Avatar ? { path: this.ActorFollow.ActorFollower.Avatar.getStaticPath() } : undefined,
+ host: this.ActorFollow.ActorFollower.getHost()
+ },
+ following: {
+ type: actorFollowingType[this.ActorFollow.ActorFollowing.type],
+ displayName: (this.ActorFollow.ActorFollowing.VideoChannel || this.ActorFollow.ActorFollowing.Account).getDisplayName(),
+ name: this.ActorFollow.ActorFollowing.preferredUsername,
+ host: this.ActorFollow.ActorFollowing.getHost()
+ }