aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/account/user.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-12-26 10:36:24 +0100
committerChocobozzz <chocobozzz@cpy.re>2019-01-09 11:15:15 +0100
commitcef534ed53e4518fe0acf581bfe880788d42fc36 (patch)
tree115b51ea5136849a2336d44915c7780649f25dc2 /server/models/account/user.ts
parent1de1d05f4c61fe059fa5e24e79c92582f0e7e4b3 (diff)
downloadPeerTube-cef534ed53e4518fe0acf581bfe880788d42fc36.tar.gz
PeerTube-cef534ed53e4518fe0acf581bfe880788d42fc36.tar.zst
PeerTube-cef534ed53e4518fe0acf581bfe880788d42fc36.zip
Add user notification base code
Diffstat (limited to 'server/models/account/user.ts')
-rw-r--r--server/models/account/user.ts101
1 files changed, 94 insertions, 7 deletions
diff --git a/server/models/account/user.ts b/server/models/account/user.ts
index 180ced810..55ec14d05 100644
--- a/server/models/account/user.ts
+++ b/server/models/account/user.ts
@@ -32,8 +32,8 @@ import {
32 isUserUsernameValid, 32 isUserUsernameValid,
33 isUserVideoQuotaDailyValid, 33 isUserVideoQuotaDailyValid,
34 isUserVideoQuotaValid, 34 isUserVideoQuotaValid,
35 isUserWebTorrentEnabledValid, 35 isUserVideosHistoryEnabledValid,
36 isUserVideosHistoryEnabledValid 36 isUserWebTorrentEnabledValid
37} from '../../helpers/custom-validators/users' 37} from '../../helpers/custom-validators/users'
38import { comparePassword, cryptPassword } from '../../helpers/peertube-crypto' 38import { comparePassword, cryptPassword } from '../../helpers/peertube-crypto'
39import { OAuthTokenModel } from '../oauth/oauth-token' 39import { OAuthTokenModel } from '../oauth/oauth-token'
@@ -44,6 +44,10 @@ import { NSFWPolicyType } from '../../../shared/models/videos/nsfw-policy.type'
44import { values } from 'lodash' 44import { values } from 'lodash'
45import { NSFW_POLICY_TYPES } from '../../initializers' 45import { NSFW_POLICY_TYPES } from '../../initializers'
46import { clearCacheByUserId } from '../../lib/oauth-model' 46import { clearCacheByUserId } from '../../lib/oauth-model'
47import { UserNotificationSettingModel } from './user-notification-setting'
48import { VideoModel } from '../video/video'
49import { ActorModel } from '../activitypub/actor'
50import { ActorFollowModel } from '../activitypub/actor-follow'
47 51
48enum ScopeNames { 52enum ScopeNames {
49 WITH_VIDEO_CHANNEL = 'WITH_VIDEO_CHANNEL' 53 WITH_VIDEO_CHANNEL = 'WITH_VIDEO_CHANNEL'
@@ -54,6 +58,10 @@ enum ScopeNames {
54 { 58 {
55 model: () => AccountModel, 59 model: () => AccountModel,
56 required: true 60 required: true
61 },
62 {
63 model: () => UserNotificationSettingModel,
64 required: true
57 } 65 }
58 ] 66 ]
59}) 67})
@@ -64,6 +72,10 @@ enum ScopeNames {
64 model: () => AccountModel, 72 model: () => AccountModel,
65 required: true, 73 required: true,
66 include: [ () => VideoChannelModel ] 74 include: [ () => VideoChannelModel ]
75 },
76 {
77 model: () => UserNotificationSettingModel,
78 required: true
67 } 79 }
68 ] 80 ]
69 } 81 }
@@ -167,6 +179,13 @@ export class UserModel extends Model<UserModel> {
167 }) 179 })
168 Account: AccountModel 180 Account: AccountModel
169 181
182 @HasOne(() => UserNotificationSettingModel, {
183 foreignKey: 'userId',
184 onDelete: 'cascade',
185 hooks: true
186 })
187 NotificationSetting: UserNotificationSettingModel
188
170 @HasMany(() => OAuthTokenModel, { 189 @HasMany(() => OAuthTokenModel, {
171 foreignKey: 'userId', 190 foreignKey: 'userId',
172 onDelete: 'cascade' 191 onDelete: 'cascade'
@@ -249,13 +268,12 @@ export class UserModel extends Model<UserModel> {
249 }) 268 })
250 } 269 }
251 270
252 static listEmailsWithRight (right: UserRight) { 271 static listWithRight (right: UserRight) {
253 const roles = Object.keys(USER_ROLE_LABELS) 272 const roles = Object.keys(USER_ROLE_LABELS)
254 .map(k => parseInt(k, 10) as UserRole) 273 .map(k => parseInt(k, 10) as UserRole)
255 .filter(role => hasUserRight(role, right)) 274 .filter(role => hasUserRight(role, right))
256 275
257 const query = { 276 const query = {
258 attribute: [ 'email' ],
259 where: { 277 where: {
260 role: { 278 role: {
261 [Sequelize.Op.in]: roles 279 [Sequelize.Op.in]: roles
@@ -263,9 +281,46 @@ export class UserModel extends Model<UserModel> {
263 } 281 }
264 } 282 }
265 283
266 return UserModel.unscoped() 284 return UserModel.findAll(query)
267 .findAll(query) 285 }
268 .then(u => u.map(u => u.email)) 286
287 static listUserSubscribersOf (actorId: number) {
288 const query = {
289 include: [
290 {
291 model: UserNotificationSettingModel.unscoped(),
292 required: true
293 },
294 {
295 attributes: [ 'userId' ],
296 model: AccountModel.unscoped(),
297 required: true,
298 include: [
299 {
300 attributes: [ ],
301 model: ActorModel.unscoped(),
302 required: true,
303 where: {
304 serverId: null
305 },
306 include: [
307 {
308 attributes: [ ],
309 as: 'ActorFollowings',
310 model: ActorFollowModel.unscoped(),
311 required: true,
312 where: {
313 targetActorId: actorId
314 }
315 }
316 ]
317 }
318 ]
319 }
320 ]
321 }
322
323 return UserModel.unscoped().findAll(query)
269 } 324 }
270 325
271 static loadById (id: number) { 326 static loadById (id: number) {
@@ -314,6 +369,37 @@ export class UserModel extends Model<UserModel> {
314 return UserModel.findOne(query) 369 return UserModel.findOne(query)
315 } 370 }
316 371
372 static loadByVideoId (videoId: number) {
373 const query = {
374 include: [
375 {
376 required: true,
377 attributes: [ 'id' ],
378 model: AccountModel.unscoped(),
379 include: [
380 {
381 required: true,
382 attributes: [ 'id' ],
383 model: VideoChannelModel.unscoped(),
384 include: [
385 {
386 required: true,
387 attributes: [ 'id' ],
388 model: VideoModel.unscoped(),
389 where: {
390 id: videoId
391 }
392 }
393 ]
394 }
395 ]
396 }
397 ]
398 }
399
400 return UserModel.findOne(query)
401 }
402
317 static getOriginalVideoFileTotalFromUser (user: UserModel) { 403 static getOriginalVideoFileTotalFromUser (user: UserModel) {
318 // Don't use sequelize because we need to use a sub query 404 // Don't use sequelize because we need to use a sub query
319 const query = UserModel.generateUserQuotaBaseSQL() 405 const query = UserModel.generateUserQuotaBaseSQL()
@@ -380,6 +466,7 @@ export class UserModel extends Model<UserModel> {
380 blocked: this.blocked, 466 blocked: this.blocked,
381 blockedReason: this.blockedReason, 467 blockedReason: this.blockedReason,
382 account: this.Account.toFormattedJSON(), 468 account: this.Account.toFormattedJSON(),
469 notificationSettings: this.NotificationSetting ? this.NotificationSetting.toFormattedJSON() : undefined,
383 videoChannels: [], 470 videoChannels: [],
384 videoQuotaUsed: videoQuotaUsed !== undefined 471 videoQuotaUsed: videoQuotaUsed !== undefined
385 ? parseInt(videoQuotaUsed, 10) 472 ? parseInt(videoQuotaUsed, 10)