diff options
Diffstat (limited to 'server/models/account')
-rw-r--r-- | server/models/account/user-notification.ts | 158 |
1 files changed, 120 insertions, 38 deletions
diff --git a/server/models/account/user-notification.ts b/server/models/account/user-notification.ts index 9e4f982a3..1094eec78 100644 --- a/server/models/account/user-notification.ts +++ b/server/models/account/user-notification.ts | |||
@@ -27,11 +27,27 @@ import { VideoBlacklistModel } from '../video/video-blacklist' | |||
27 | import { VideoImportModel } from '../video/video-import' | 27 | import { VideoImportModel } from '../video/video-import' |
28 | import { ActorModel } from '../activitypub/actor' | 28 | import { ActorModel } from '../activitypub/actor' |
29 | import { ActorFollowModel } from '../activitypub/actor-follow' | 29 | import { ActorFollowModel } from '../activitypub/actor-follow' |
30 | import { AvatarModel } from '../avatar/avatar' | ||
30 | 31 | ||
31 | enum ScopeNames { | 32 | enum ScopeNames { |
32 | WITH_ALL = 'WITH_ALL' | 33 | WITH_ALL = 'WITH_ALL' |
33 | } | 34 | } |
34 | 35 | ||
36 | function buildActorWithAvatarInclude () { | ||
37 | return { | ||
38 | attributes: [ 'preferredUsername' ], | ||
39 | model: () => ActorModel.unscoped(), | ||
40 | required: true, | ||
41 | include: [ | ||
42 | { | ||
43 | attributes: [ 'filename' ], | ||
44 | model: () => AvatarModel.unscoped(), | ||
45 | required: false | ||
46 | } | ||
47 | ] | ||
48 | } | ||
49 | } | ||
50 | |||
35 | function buildVideoInclude (required: boolean) { | 51 | function buildVideoInclude (required: boolean) { |
36 | return { | 52 | return { |
37 | attributes: [ 'id', 'uuid', 'name' ], | 53 | attributes: [ 'id', 'uuid', 'name' ], |
@@ -40,19 +56,21 @@ function buildVideoInclude (required: boolean) { | |||
40 | } | 56 | } |
41 | } | 57 | } |
42 | 58 | ||
43 | function buildChannelInclude (required: boolean) { | 59 | function buildChannelInclude (required: boolean, withActor = false) { |
44 | return { | 60 | return { |
45 | required, | 61 | required, |
46 | attributes: [ 'id', 'name' ], | 62 | attributes: [ 'id', 'name' ], |
47 | model: () => VideoChannelModel.unscoped() | 63 | model: () => VideoChannelModel.unscoped(), |
64 | include: withActor === true ? [ buildActorWithAvatarInclude() ] : [] | ||
48 | } | 65 | } |
49 | } | 66 | } |
50 | 67 | ||
51 | function buildAccountInclude (required: boolean) { | 68 | function buildAccountInclude (required: boolean, withActor = false) { |
52 | return { | 69 | return { |
53 | required, | 70 | required, |
54 | attributes: [ 'id', 'name' ], | 71 | attributes: [ 'id', 'name' ], |
55 | model: () => AccountModel.unscoped() | 72 | model: () => AccountModel.unscoped(), |
73 | include: withActor === true ? [ buildActorWithAvatarInclude() ] : [] | ||
56 | } | 74 | } |
57 | } | 75 | } |
58 | 76 | ||
@@ -60,47 +78,40 @@ function buildAccountInclude (required: boolean) { | |||
60 | [ScopeNames.WITH_ALL]: { | 78 | [ScopeNames.WITH_ALL]: { |
61 | include: [ | 79 | include: [ |
62 | Object.assign(buildVideoInclude(false), { | 80 | Object.assign(buildVideoInclude(false), { |
63 | include: [ buildChannelInclude(true) ] | 81 | include: [ buildChannelInclude(true, true) ] |
64 | }), | 82 | }), |
83 | |||
65 | { | 84 | { |
66 | attributes: [ 'id', 'originCommentId' ], | 85 | attributes: [ 'id', 'originCommentId' ], |
67 | model: () => VideoCommentModel.unscoped(), | 86 | model: () => VideoCommentModel.unscoped(), |
68 | required: false, | 87 | required: false, |
69 | include: [ | 88 | include: [ |
70 | buildAccountInclude(true), | 89 | buildAccountInclude(true, true), |
71 | buildVideoInclude(true) | 90 | buildVideoInclude(true) |
72 | ] | 91 | ] |
73 | }, | 92 | }, |
93 | |||
74 | { | 94 | { |
75 | attributes: [ 'id' ], | 95 | attributes: [ 'id' ], |
76 | model: () => VideoAbuseModel.unscoped(), | 96 | model: () => VideoAbuseModel.unscoped(), |
77 | required: false, | 97 | required: false, |
78 | include: [ buildVideoInclude(true) ] | 98 | include: [ buildVideoInclude(true) ] |
79 | }, | 99 | }, |
100 | |||
80 | { | 101 | { |
81 | attributes: [ 'id' ], | 102 | attributes: [ 'id' ], |
82 | model: () => VideoBlacklistModel.unscoped(), | 103 | model: () => VideoBlacklistModel.unscoped(), |
83 | required: false, | 104 | required: false, |
84 | include: [ buildVideoInclude(true) ] | 105 | include: [ buildVideoInclude(true) ] |
85 | }, | 106 | }, |
107 | |||
86 | { | 108 | { |
87 | attributes: [ 'id', 'magnetUri', 'targetUrl', 'torrentName' ], | 109 | attributes: [ 'id', 'magnetUri', 'targetUrl', 'torrentName' ], |
88 | model: () => VideoImportModel.unscoped(), | 110 | model: () => VideoImportModel.unscoped(), |
89 | required: false, | 111 | required: false, |
90 | include: [ buildVideoInclude(false) ] | 112 | include: [ buildVideoInclude(false) ] |
91 | }, | 113 | }, |
92 | { | 114 | |
93 | attributes: [ 'id', 'name' ], | ||
94 | model: () => AccountModel.unscoped(), | ||
95 | required: false, | ||
96 | include: [ | ||
97 | { | ||
98 | attributes: [ 'id', 'preferredUsername' ], | ||
99 | model: () => ActorModel.unscoped(), | ||
100 | required: true | ||
101 | } | ||
102 | ] | ||
103 | }, | ||
104 | { | 115 | { |
105 | attributes: [ 'id' ], | 116 | attributes: [ 'id' ], |
106 | model: () => ActorFollowModel.unscoped(), | 117 | model: () => ActorFollowModel.unscoped(), |
@@ -111,7 +122,18 @@ function buildAccountInclude (required: boolean) { | |||
111 | model: () => ActorModel.unscoped(), | 122 | model: () => ActorModel.unscoped(), |
112 | required: true, | 123 | required: true, |
113 | as: 'ActorFollower', | 124 | as: 'ActorFollower', |
114 | include: [ buildAccountInclude(true) ] | 125 | include: [ |
126 | { | ||
127 | attributes: [ 'id', 'name' ], | ||
128 | model: () => AccountModel.unscoped(), | ||
129 | required: true | ||
130 | }, | ||
131 | { | ||
132 | attributes: [ 'filename' ], | ||
133 | model: () => AvatarModel.unscoped(), | ||
134 | required: false | ||
135 | } | ||
136 | ] | ||
115 | }, | 137 | }, |
116 | { | 138 | { |
117 | attributes: [ 'preferredUsername' ], | 139 | attributes: [ 'preferredUsername' ], |
@@ -124,7 +146,9 @@ function buildAccountInclude (required: boolean) { | |||
124 | ] | 146 | ] |
125 | } | 147 | } |
126 | ] | 148 | ] |
127 | } | 149 | }, |
150 | |||
151 | buildAccountInclude(false, true) | ||
128 | ] | 152 | ] |
129 | } | 153 | } |
130 | }) | 154 | }) |
@@ -132,10 +156,63 @@ function buildAccountInclude (required: boolean) { | |||
132 | tableName: 'userNotification', | 156 | tableName: 'userNotification', |
133 | indexes: [ | 157 | indexes: [ |
134 | { | 158 | { |
135 | fields: [ 'videoId' ] | 159 | fields: [ 'userId' ] |
136 | }, | 160 | }, |
137 | { | 161 | { |
138 | fields: [ 'commentId' ] | 162 | fields: [ 'videoId' ], |
163 | where: { | ||
164 | videoId: { | ||
165 | [Op.ne]: null | ||
166 | } | ||
167 | } | ||
168 | }, | ||
169 | { | ||
170 | fields: [ 'commentId' ], | ||
171 | where: { | ||
172 | commentId: { | ||
173 | [Op.ne]: null | ||
174 | } | ||
175 | } | ||
176 | }, | ||
177 | { | ||
178 | fields: [ 'videoAbuseId' ], | ||
179 | where: { | ||
180 | videoAbuseId: { | ||
181 | [Op.ne]: null | ||
182 | } | ||
183 | } | ||
184 | }, | ||
185 | { | ||
186 | fields: [ 'videoBlacklistId' ], | ||
187 | where: { | ||
188 | videoBlacklistId: { | ||
189 | [Op.ne]: null | ||
190 | } | ||
191 | } | ||
192 | }, | ||
193 | { | ||
194 | fields: [ 'videoImportId' ], | ||
195 | where: { | ||
196 | videoImportId: { | ||
197 | [Op.ne]: null | ||
198 | } | ||
199 | } | ||
200 | }, | ||
201 | { | ||
202 | fields: [ 'accountId' ], | ||
203 | where: { | ||
204 | accountId: { | ||
205 | [Op.ne]: null | ||
206 | } | ||
207 | } | ||
208 | }, | ||
209 | { | ||
210 | fields: [ 'actorFollowId' ], | ||
211 | where: { | ||
212 | actorFollowId: { | ||
213 | [Op.ne]: null | ||
214 | } | ||
215 | } | ||
139 | } | 216 | } |
140 | ] | 217 | ] |
141 | }) | 218 | }) |
@@ -297,12 +374,9 @@ export class UserNotificationModel extends Model<UserNotificationModel> { | |||
297 | } | 374 | } |
298 | 375 | ||
299 | toFormattedJSON (): UserNotification { | 376 | toFormattedJSON (): UserNotification { |
300 | const video = this.Video ? Object.assign(this.formatVideo(this.Video), { | 377 | const video = this.Video |
301 | channel: { | 378 | ? Object.assign(this.formatVideo(this.Video),{ channel: this.formatActor(this.Video.VideoChannel) }) |
302 | id: this.Video.VideoChannel.id, | 379 | : undefined |
303 | displayName: this.Video.VideoChannel.getDisplayName() | ||
304 | } | ||
305 | }) : undefined | ||
306 | 380 | ||
307 | const videoImport = this.VideoImport ? { | 381 | const videoImport = this.VideoImport ? { |
308 | id: this.VideoImport.id, | 382 | id: this.VideoImport.id, |
@@ -315,10 +389,7 @@ export class UserNotificationModel extends Model<UserNotificationModel> { | |||
315 | const comment = this.Comment ? { | 389 | const comment = this.Comment ? { |
316 | id: this.Comment.id, | 390 | id: this.Comment.id, |
317 | threadId: this.Comment.getThreadId(), | 391 | threadId: this.Comment.getThreadId(), |
318 | account: { | 392 | account: this.formatActor(this.Comment.Account), |
319 | id: this.Comment.Account.id, | ||
320 | displayName: this.Comment.Account.getDisplayName() | ||
321 | }, | ||
322 | video: this.formatVideo(this.Comment.Video) | 393 | video: this.formatVideo(this.Comment.Video) |
323 | } : undefined | 394 | } : undefined |
324 | 395 | ||
@@ -332,17 +403,15 @@ export class UserNotificationModel extends Model<UserNotificationModel> { | |||
332 | video: this.formatVideo(this.VideoBlacklist.Video) | 403 | video: this.formatVideo(this.VideoBlacklist.Video) |
333 | } : undefined | 404 | } : undefined |
334 | 405 | ||
335 | const account = this.Account ? { | 406 | const account = this.Account ? this.formatActor(this.Account) : undefined |
336 | id: this.Account.id, | ||
337 | displayName: this.Account.getDisplayName(), | ||
338 | name: this.Account.Actor.preferredUsername | ||
339 | } : undefined | ||
340 | 407 | ||
341 | const actorFollow = this.ActorFollow ? { | 408 | const actorFollow = this.ActorFollow ? { |
342 | id: this.ActorFollow.id, | 409 | id: this.ActorFollow.id, |
343 | follower: { | 410 | follower: { |
411 | id: this.ActorFollow.ActorFollower.Account.id, | ||
344 | displayName: this.ActorFollow.ActorFollower.Account.getDisplayName(), | 412 | displayName: this.ActorFollow.ActorFollower.Account.getDisplayName(), |
345 | name: this.ActorFollow.ActorFollower.preferredUsername | 413 | name: this.ActorFollow.ActorFollower.preferredUsername, |
414 | avatar: this.ActorFollow.ActorFollower.Avatar ? { path: this.ActorFollow.ActorFollower.Avatar.getWebserverPath() } : undefined | ||
346 | }, | 415 | }, |
347 | following: { | 416 | following: { |
348 | type: this.ActorFollow.ActorFollowing.VideoChannel ? 'channel' as 'channel' : 'account' as 'account', | 417 | type: this.ActorFollow.ActorFollowing.VideoChannel ? 'channel' as 'channel' : 'account' as 'account', |
@@ -374,4 +443,17 @@ export class UserNotificationModel extends Model<UserNotificationModel> { | |||
374 | name: video.name | 443 | name: video.name |
375 | } | 444 | } |
376 | } | 445 | } |
446 | |||
447 | private formatActor (accountOrChannel: AccountModel | VideoChannelModel) { | ||
448 | const avatar = accountOrChannel.Actor.Avatar | ||
449 | ? { path: accountOrChannel.Actor.Avatar.getWebserverPath() } | ||
450 | : undefined | ||
451 | |||
452 | return { | ||
453 | id: accountOrChannel.id, | ||
454 | displayName: accountOrChannel.getDisplayName(), | ||
455 | name: accountOrChannel.Actor.preferredUsername, | ||
456 | avatar | ||
457 | } | ||
458 | } | ||
377 | } | 459 | } |