]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/user/sql/user-notitication-list-query-builder.ts
Add ability to set start/end date to timeserie
[github/Chocobozzz/PeerTube.git] / server / models / user / sql / user-notitication-list-query-builder.ts
CommitLineData
156c44c8
C
1import { Sequelize } from 'sequelize'
2import { AbstractRunQuery, ModelBuilder } from '@server/models/shared'
d0800f76 3import { getSort } from '@server/models/utils'
4import { UserNotificationModelForApi } from '@server/types/models'
5import { ActorImageType } from '@shared/models'
6
7export interface ListNotificationsOptions {
8 userId: number
9 unread?: boolean
10 sort: string
11 offset: number
12 limit: number
d0800f76 13}
14
156c44c8 15export class UserNotificationListQueryBuilder extends AbstractRunQuery {
d0800f76 16 private innerQuery: string
d0800f76 17
156c44c8
C
18 constructor (
19 protected readonly sequelize: Sequelize,
20 private readonly options: ListNotificationsOptions
21 ) {
22 super(sequelize)
d0800f76 23 }
24
25 async listNotifications () {
26 this.buildQuery()
27
156c44c8
C
28 const results = await this.runQuery({ nest: true })
29 const modelBuilder = new ModelBuilder<UserNotificationModelForApi>(this.sequelize)
d0800f76 30
31 return modelBuilder.createModels(results, 'UserNotification')
32 }
33
34 private buildInnerQuery () {
35 this.innerQuery = `SELECT * FROM "userNotification" AS "UserNotificationModel" ` +
36 `${this.getWhere()} ` +
37 `${this.getOrder()} ` +
38 `LIMIT :limit OFFSET :offset `
39
40 this.replacements.limit = this.options.limit
41 this.replacements.offset = this.options.offset
42 }
43
44 private buildQuery () {
45 this.buildInnerQuery()
46
47 this.query = `
48 ${this.getSelect()}
49 FROM (${this.innerQuery}) "UserNotificationModel"
50 ${this.getJoins()}
51 ${this.getOrder()}`
52 }
53
54 private getWhere () {
55 let base = '"UserNotificationModel"."userId" = :userId '
56 this.replacements.userId = this.options.userId
57
58 if (this.options.unread === true) {
59 base += 'AND "UserNotificationModel"."read" IS FALSE '
60 } else if (this.options.unread === false) {
61 base += 'AND "UserNotificationModel"."read" IS TRUE '
62 }
63
64 return `WHERE ${base}`
65 }
66
67 private getOrder () {
68 const orders = getSort(this.options.sort)
69
70 return 'ORDER BY ' + orders.map(o => `"UserNotificationModel"."${o[0]}" ${o[1]}`).join(', ')
71 }
72
73 private getSelect () {
74 return `SELECT
75 "UserNotificationModel"."id",
76 "UserNotificationModel"."type",
77 "UserNotificationModel"."read",
78 "UserNotificationModel"."createdAt",
79 "UserNotificationModel"."updatedAt",
80 "Video"."id" AS "Video.id",
81 "Video"."uuid" AS "Video.uuid",
82 "Video"."name" AS "Video.name",
83 "Video->VideoChannel"."id" AS "Video.VideoChannel.id",
84 "Video->VideoChannel"."name" AS "Video.VideoChannel.name",
85 "Video->VideoChannel->Actor"."id" AS "Video.VideoChannel.Actor.id",
86 "Video->VideoChannel->Actor"."preferredUsername" AS "Video.VideoChannel.Actor.preferredUsername",
87 "Video->VideoChannel->Actor->Avatars"."id" AS "Video.VideoChannel.Actor.Avatars.id",
88 "Video->VideoChannel->Actor->Avatars"."width" AS "Video.VideoChannel.Actor.Avatars.width",
89 "Video->VideoChannel->Actor->Avatars"."filename" AS "Video.VideoChannel.Actor.Avatars.filename",
90 "Video->VideoChannel->Actor->Server"."id" AS "Video.VideoChannel.Actor.Server.id",
91 "Video->VideoChannel->Actor->Server"."host" AS "Video.VideoChannel.Actor.Server.host",
92 "VideoComment"."id" AS "VideoComment.id",
93 "VideoComment"."originCommentId" AS "VideoComment.originCommentId",
94 "VideoComment->Account"."id" AS "VideoComment.Account.id",
95 "VideoComment->Account"."name" AS "VideoComment.Account.name",
96 "VideoComment->Account->Actor"."id" AS "VideoComment.Account.Actor.id",
97 "VideoComment->Account->Actor"."preferredUsername" AS "VideoComment.Account.Actor.preferredUsername",
98 "VideoComment->Account->Actor->Avatars"."id" AS "VideoComment.Account.Actor.Avatars.id",
99 "VideoComment->Account->Actor->Avatars"."width" AS "VideoComment.Account.Actor.Avatars.width",
100 "VideoComment->Account->Actor->Avatars"."filename" AS "VideoComment.Account.Actor.Avatars.filename",
101 "VideoComment->Account->Actor->Server"."id" AS "VideoComment.Account.Actor.Server.id",
102 "VideoComment->Account->Actor->Server"."host" AS "VideoComment.Account.Actor.Server.host",
103 "VideoComment->Video"."id" AS "VideoComment.Video.id",
104 "VideoComment->Video"."uuid" AS "VideoComment.Video.uuid",
105 "VideoComment->Video"."name" AS "VideoComment.Video.name",
106 "Abuse"."id" AS "Abuse.id",
107 "Abuse"."state" AS "Abuse.state",
108 "Abuse->VideoAbuse"."id" AS "Abuse.VideoAbuse.id",
109 "Abuse->VideoAbuse->Video"."id" AS "Abuse.VideoAbuse.Video.id",
110 "Abuse->VideoAbuse->Video"."uuid" AS "Abuse.VideoAbuse.Video.uuid",
111 "Abuse->VideoAbuse->Video"."name" AS "Abuse.VideoAbuse.Video.name",
112 "Abuse->VideoCommentAbuse"."id" AS "Abuse.VideoCommentAbuse.id",
113 "Abuse->VideoCommentAbuse->VideoComment"."id" AS "Abuse.VideoCommentAbuse.VideoComment.id",
114 "Abuse->VideoCommentAbuse->VideoComment"."originCommentId" AS "Abuse.VideoCommentAbuse.VideoComment.originCommentId",
115 "Abuse->VideoCommentAbuse->VideoComment->Video"."id" AS "Abuse.VideoCommentAbuse.VideoComment.Video.id",
116 "Abuse->VideoCommentAbuse->VideoComment->Video"."name" AS "Abuse.VideoCommentAbuse.VideoComment.Video.name",
117 "Abuse->VideoCommentAbuse->VideoComment->Video"."uuid" AS "Abuse.VideoCommentAbuse.VideoComment.Video.uuid",
118 "Abuse->FlaggedAccount"."id" AS "Abuse.FlaggedAccount.id",
119 "Abuse->FlaggedAccount"."name" AS "Abuse.FlaggedAccount.name",
120 "Abuse->FlaggedAccount"."description" AS "Abuse.FlaggedAccount.description",
121 "Abuse->FlaggedAccount"."actorId" AS "Abuse.FlaggedAccount.actorId",
122 "Abuse->FlaggedAccount"."userId" AS "Abuse.FlaggedAccount.userId",
123 "Abuse->FlaggedAccount"."applicationId" AS "Abuse.FlaggedAccount.applicationId",
124 "Abuse->FlaggedAccount"."createdAt" AS "Abuse.FlaggedAccount.createdAt",
125 "Abuse->FlaggedAccount"."updatedAt" AS "Abuse.FlaggedAccount.updatedAt",
126 "Abuse->FlaggedAccount->Actor"."id" AS "Abuse.FlaggedAccount.Actor.id",
127 "Abuse->FlaggedAccount->Actor"."preferredUsername" AS "Abuse.FlaggedAccount.Actor.preferredUsername",
128 "Abuse->FlaggedAccount->Actor->Avatars"."id" AS "Abuse.FlaggedAccount.Actor.Avatars.id",
129 "Abuse->FlaggedAccount->Actor->Avatars"."width" AS "Abuse.FlaggedAccount.Actor.Avatars.width",
130 "Abuse->FlaggedAccount->Actor->Avatars"."filename" AS "Abuse.FlaggedAccount.Actor.Avatars.filename",
131 "Abuse->FlaggedAccount->Actor->Server"."id" AS "Abuse.FlaggedAccount.Actor.Server.id",
132 "Abuse->FlaggedAccount->Actor->Server"."host" AS "Abuse.FlaggedAccount.Actor.Server.host",
133 "VideoBlacklist"."id" AS "VideoBlacklist.id",
134 "VideoBlacklist->Video"."id" AS "VideoBlacklist.Video.id",
135 "VideoBlacklist->Video"."uuid" AS "VideoBlacklist.Video.uuid",
136 "VideoBlacklist->Video"."name" AS "VideoBlacklist.Video.name",
137 "VideoImport"."id" AS "VideoImport.id",
138 "VideoImport"."magnetUri" AS "VideoImport.magnetUri",
139 "VideoImport"."targetUrl" AS "VideoImport.targetUrl",
140 "VideoImport"."torrentName" AS "VideoImport.torrentName",
141 "VideoImport->Video"."id" AS "VideoImport.Video.id",
142 "VideoImport->Video"."uuid" AS "VideoImport.Video.uuid",
143 "VideoImport->Video"."name" AS "VideoImport.Video.name",
144 "Plugin"."id" AS "Plugin.id",
145 "Plugin"."name" AS "Plugin.name",
146 "Plugin"."type" AS "Plugin.type",
147 "Plugin"."latestVersion" AS "Plugin.latestVersion",
148 "Application"."id" AS "Application.id",
149 "Application"."latestPeerTubeVersion" AS "Application.latestPeerTubeVersion",
150 "ActorFollow"."id" AS "ActorFollow.id",
151 "ActorFollow"."state" AS "ActorFollow.state",
152 "ActorFollow->ActorFollower"."id" AS "ActorFollow.ActorFollower.id",
153 "ActorFollow->ActorFollower"."preferredUsername" AS "ActorFollow.ActorFollower.preferredUsername",
154 "ActorFollow->ActorFollower->Account"."id" AS "ActorFollow.ActorFollower.Account.id",
155 "ActorFollow->ActorFollower->Account"."name" AS "ActorFollow.ActorFollower.Account.name",
156 "ActorFollow->ActorFollower->Avatars"."id" AS "ActorFollow.ActorFollower.Avatars.id",
157 "ActorFollow->ActorFollower->Avatars"."width" AS "ActorFollow.ActorFollower.Avatars.width",
158 "ActorFollow->ActorFollower->Avatars"."filename" AS "ActorFollow.ActorFollower.Avatars.filename",
159 "ActorFollow->ActorFollower->Server"."id" AS "ActorFollow.ActorFollower.Server.id",
160 "ActorFollow->ActorFollower->Server"."host" AS "ActorFollow.ActorFollower.Server.host",
161 "ActorFollow->ActorFollowing"."id" AS "ActorFollow.ActorFollowing.id",
162 "ActorFollow->ActorFollowing"."preferredUsername" AS "ActorFollow.ActorFollowing.preferredUsername",
163 "ActorFollow->ActorFollowing"."type" AS "ActorFollow.ActorFollowing.type",
164 "ActorFollow->ActorFollowing->VideoChannel"."id" AS "ActorFollow.ActorFollowing.VideoChannel.id",
165 "ActorFollow->ActorFollowing->VideoChannel"."name" AS "ActorFollow.ActorFollowing.VideoChannel.name",
166 "ActorFollow->ActorFollowing->Account"."id" AS "ActorFollow.ActorFollowing.Account.id",
167 "ActorFollow->ActorFollowing->Account"."name" AS "ActorFollow.ActorFollowing.Account.name",
168 "ActorFollow->ActorFollowing->Server"."id" AS "ActorFollow.ActorFollowing.Server.id",
169 "ActorFollow->ActorFollowing->Server"."host" AS "ActorFollow.ActorFollowing.Server.host",
170 "Account"."id" AS "Account.id",
171 "Account"."name" AS "Account.name",
172 "Account->Actor"."id" AS "Account.Actor.id",
173 "Account->Actor"."preferredUsername" AS "Account.Actor.preferredUsername",
174 "Account->Actor->Avatars"."id" AS "Account.Actor.Avatars.id",
175 "Account->Actor->Avatars"."width" AS "Account.Actor.Avatars.width",
176 "Account->Actor->Avatars"."filename" AS "Account.Actor.Avatars.filename",
177 "Account->Actor->Server"."id" AS "Account.Actor.Server.id",
178 "Account->Actor->Server"."host" AS "Account.Actor.Server.host"`
179 }
180
181 private getJoins () {
182 return `
183 LEFT JOIN (
184 "video" AS "Video"
185 INNER JOIN "videoChannel" AS "Video->VideoChannel" ON "Video"."channelId" = "Video->VideoChannel"."id"
186 INNER JOIN "actor" AS "Video->VideoChannel->Actor" ON "Video->VideoChannel"."actorId" = "Video->VideoChannel->Actor"."id"
187 LEFT JOIN "actorImage" AS "Video->VideoChannel->Actor->Avatars"
188 ON "Video->VideoChannel->Actor"."id" = "Video->VideoChannel->Actor->Avatars"."actorId"
189 AND "Video->VideoChannel->Actor->Avatars"."type" = ${ActorImageType.AVATAR}
190 LEFT JOIN "server" AS "Video->VideoChannel->Actor->Server"
191 ON "Video->VideoChannel->Actor"."serverId" = "Video->VideoChannel->Actor->Server"."id"
192 ) ON "UserNotificationModel"."videoId" = "Video"."id"
193
194 LEFT JOIN (
195 "videoComment" AS "VideoComment"
196 INNER JOIN "account" AS "VideoComment->Account" ON "VideoComment"."accountId" = "VideoComment->Account"."id"
197 INNER JOIN "actor" AS "VideoComment->Account->Actor" ON "VideoComment->Account"."actorId" = "VideoComment->Account->Actor"."id"
198 LEFT JOIN "actorImage" AS "VideoComment->Account->Actor->Avatars"
199 ON "VideoComment->Account->Actor"."id" = "VideoComment->Account->Actor->Avatars"."actorId"
200 AND "VideoComment->Account->Actor->Avatars"."type" = ${ActorImageType.AVATAR}
201 LEFT JOIN "server" AS "VideoComment->Account->Actor->Server"
202 ON "VideoComment->Account->Actor"."serverId" = "VideoComment->Account->Actor->Server"."id"
203 INNER JOIN "video" AS "VideoComment->Video" ON "VideoComment"."videoId" = "VideoComment->Video"."id"
204 ) ON "UserNotificationModel"."commentId" = "VideoComment"."id"
205
206 LEFT JOIN "abuse" AS "Abuse" ON "UserNotificationModel"."abuseId" = "Abuse"."id"
207 LEFT JOIN "videoAbuse" AS "Abuse->VideoAbuse" ON "Abuse"."id" = "Abuse->VideoAbuse"."abuseId"
208 LEFT JOIN "video" AS "Abuse->VideoAbuse->Video" ON "Abuse->VideoAbuse"."videoId" = "Abuse->VideoAbuse->Video"."id"
209 LEFT JOIN "commentAbuse" AS "Abuse->VideoCommentAbuse" ON "Abuse"."id" = "Abuse->VideoCommentAbuse"."abuseId"
210 LEFT JOIN "videoComment" AS "Abuse->VideoCommentAbuse->VideoComment"
211 ON "Abuse->VideoCommentAbuse"."videoCommentId" = "Abuse->VideoCommentAbuse->VideoComment"."id"
212 LEFT JOIN "video" AS "Abuse->VideoCommentAbuse->VideoComment->Video"
213 ON "Abuse->VideoCommentAbuse->VideoComment"."videoId" = "Abuse->VideoCommentAbuse->VideoComment->Video"."id"
214 LEFT JOIN (
215 "account" AS "Abuse->FlaggedAccount"
216 INNER JOIN "actor" AS "Abuse->FlaggedAccount->Actor" ON "Abuse->FlaggedAccount"."actorId" = "Abuse->FlaggedAccount->Actor"."id"
217 LEFT JOIN "actorImage" AS "Abuse->FlaggedAccount->Actor->Avatars"
218 ON "Abuse->FlaggedAccount->Actor"."id" = "Abuse->FlaggedAccount->Actor->Avatars"."actorId"
219 AND "Abuse->FlaggedAccount->Actor->Avatars"."type" = ${ActorImageType.AVATAR}
220 LEFT JOIN "server" AS "Abuse->FlaggedAccount->Actor->Server"
221 ON "Abuse->FlaggedAccount->Actor"."serverId" = "Abuse->FlaggedAccount->Actor->Server"."id"
222 ) ON "Abuse"."flaggedAccountId" = "Abuse->FlaggedAccount"."id"
223
224 LEFT JOIN (
225 "videoBlacklist" AS "VideoBlacklist"
226 INNER JOIN "video" AS "VideoBlacklist->Video" ON "VideoBlacklist"."videoId" = "VideoBlacklist->Video"."id"
227 ) ON "UserNotificationModel"."videoBlacklistId" = "VideoBlacklist"."id"
228
229 LEFT JOIN "videoImport" AS "VideoImport" ON "UserNotificationModel"."videoImportId" = "VideoImport"."id"
230 LEFT JOIN "video" AS "VideoImport->Video" ON "VideoImport"."videoId" = "VideoImport->Video"."id"
231
232 LEFT JOIN "plugin" AS "Plugin" ON "UserNotificationModel"."pluginId" = "Plugin"."id"
233
234 LEFT JOIN "application" AS "Application" ON "UserNotificationModel"."applicationId" = "Application"."id"
235
236 LEFT JOIN (
237 "actorFollow" AS "ActorFollow"
238 INNER JOIN "actor" AS "ActorFollow->ActorFollower" ON "ActorFollow"."actorId" = "ActorFollow->ActorFollower"."id"
239 INNER JOIN "account" AS "ActorFollow->ActorFollower->Account"
240 ON "ActorFollow->ActorFollower"."id" = "ActorFollow->ActorFollower->Account"."actorId"
241 LEFT JOIN "actorImage" AS "ActorFollow->ActorFollower->Avatars"
242 ON "ActorFollow->ActorFollower"."id" = "ActorFollow->ActorFollower->Avatars"."actorId"
243 AND "ActorFollow->ActorFollower->Avatars"."type" = ${ActorImageType.AVATAR}
244 LEFT JOIN "server" AS "ActorFollow->ActorFollower->Server"
245 ON "ActorFollow->ActorFollower"."serverId" = "ActorFollow->ActorFollower->Server"."id"
246 INNER JOIN "actor" AS "ActorFollow->ActorFollowing" ON "ActorFollow"."targetActorId" = "ActorFollow->ActorFollowing"."id"
247 LEFT JOIN "videoChannel" AS "ActorFollow->ActorFollowing->VideoChannel"
248 ON "ActorFollow->ActorFollowing"."id" = "ActorFollow->ActorFollowing->VideoChannel"."actorId"
249 LEFT JOIN "account" AS "ActorFollow->ActorFollowing->Account"
250 ON "ActorFollow->ActorFollowing"."id" = "ActorFollow->ActorFollowing->Account"."actorId"
251 LEFT JOIN "server" AS "ActorFollow->ActorFollowing->Server"
252 ON "ActorFollow->ActorFollowing"."serverId" = "ActorFollow->ActorFollowing->Server"."id"
253 ) ON "UserNotificationModel"."actorFollowId" = "ActorFollow"."id"
254
255 LEFT JOIN (
256 "account" AS "Account"
257 INNER JOIN "actor" AS "Account->Actor" ON "Account"."actorId" = "Account->Actor"."id"
258 LEFT JOIN "actorImage" AS "Account->Actor->Avatars"
259 ON "Account->Actor"."id" = "Account->Actor->Avatars"."actorId"
260 AND "Account->Actor->Avatars"."type" = ${ActorImageType.AVATAR}
261 LEFT JOIN "server" AS "Account->Actor->Server" ON "Account->Actor"."serverId" = "Account->Actor->Server"."id"
262 ) ON "UserNotificationModel"."accountId" = "Account"."id"`
263 }
264}