diff options
author | Chocobozzz <me@florianbigard.com> | 2019-02-11 11:52:34 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2019-02-11 11:52:34 +0100 |
commit | 88108880bbdba473cfe36ecbebc1c3c4f972e102 (patch) | |
tree | b242efb3b4f0d7e49d88f2d1f2063b5b3b0489c0 /server/models/account | |
parent | 53a94c7cfa8368da4cd248d65df8346905938f0c (diff) | |
parent | 9b712a2017e4ab3cf12cd6bd58278905520159d0 (diff) | |
download | PeerTube-88108880bbdba473cfe36ecbebc1c3c4f972e102.tar.gz PeerTube-88108880bbdba473cfe36ecbebc1c3c4f972e102.tar.zst PeerTube-88108880bbdba473cfe36ecbebc1c3c4f972e102.zip |
Merge branch 'develop' into pr/1217
Diffstat (limited to 'server/models/account')
-rw-r--r-- | server/models/account/account-blocklist.ts | 142 | ||||
-rw-r--r-- | server/models/account/account-video-rate.ts | 60 | ||||
-rw-r--r-- | server/models/account/account.ts | 25 | ||||
-rw-r--r-- | server/models/account/user-notification-setting.ts | 150 | ||||
-rw-r--r-- | server/models/account/user-notification.ts | 472 | ||||
-rw-r--r-- | server/models/account/user-video-history.ts | 33 | ||||
-rw-r--r-- | server/models/account/user.ts | 217 |
7 files changed, 1085 insertions, 14 deletions
diff --git a/server/models/account/account-blocklist.ts b/server/models/account/account-blocklist.ts new file mode 100644 index 000000000..efd6ed59e --- /dev/null +++ b/server/models/account/account-blocklist.ts | |||
@@ -0,0 +1,142 @@ | |||
1 | import { BelongsTo, Column, CreatedAt, ForeignKey, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' | ||
2 | import { AccountModel } from './account' | ||
3 | import { getSort } from '../utils' | ||
4 | import { AccountBlock } from '../../../shared/models/blocklist' | ||
5 | import { Op } from 'sequelize' | ||
6 | |||
7 | enum ScopeNames { | ||
8 | WITH_ACCOUNTS = 'WITH_ACCOUNTS' | ||
9 | } | ||
10 | |||
11 | @Scopes({ | ||
12 | [ScopeNames.WITH_ACCOUNTS]: { | ||
13 | include: [ | ||
14 | { | ||
15 | model: () => AccountModel, | ||
16 | required: true, | ||
17 | as: 'ByAccount' | ||
18 | }, | ||
19 | { | ||
20 | model: () => AccountModel, | ||
21 | required: true, | ||
22 | as: 'BlockedAccount' | ||
23 | } | ||
24 | ] | ||
25 | } | ||
26 | }) | ||
27 | |||
28 | @Table({ | ||
29 | tableName: 'accountBlocklist', | ||
30 | indexes: [ | ||
31 | { | ||
32 | fields: [ 'accountId', 'targetAccountId' ], | ||
33 | unique: true | ||
34 | }, | ||
35 | { | ||
36 | fields: [ 'targetAccountId' ] | ||
37 | } | ||
38 | ] | ||
39 | }) | ||
40 | export class AccountBlocklistModel extends Model<AccountBlocklistModel> { | ||
41 | |||
42 | @CreatedAt | ||
43 | createdAt: Date | ||
44 | |||
45 | @UpdatedAt | ||
46 | updatedAt: Date | ||
47 | |||
48 | @ForeignKey(() => AccountModel) | ||
49 | @Column | ||
50 | accountId: number | ||
51 | |||
52 | @BelongsTo(() => AccountModel, { | ||
53 | foreignKey: { | ||
54 | name: 'accountId', | ||
55 | allowNull: false | ||
56 | }, | ||
57 | as: 'ByAccount', | ||
58 | onDelete: 'CASCADE' | ||
59 | }) | ||
60 | ByAccount: AccountModel | ||
61 | |||
62 | @ForeignKey(() => AccountModel) | ||
63 | @Column | ||
64 | targetAccountId: number | ||
65 | |||
66 | @BelongsTo(() => AccountModel, { | ||
67 | foreignKey: { | ||
68 | name: 'targetAccountId', | ||
69 | allowNull: false | ||
70 | }, | ||
71 | as: 'BlockedAccount', | ||
72 | onDelete: 'CASCADE' | ||
73 | }) | ||
74 | BlockedAccount: AccountModel | ||
75 | |||
76 | static isAccountMutedBy (accountId: number, targetAccountId: number) { | ||
77 | return AccountBlocklistModel.isAccountMutedByMulti([ accountId ], targetAccountId) | ||
78 | .then(result => result[accountId]) | ||
79 | } | ||
80 | |||
81 | static isAccountMutedByMulti (accountIds: number[], targetAccountId: number) { | ||
82 | const query = { | ||
83 | attributes: [ 'accountId', 'id' ], | ||
84 | where: { | ||
85 | accountId: { | ||
86 | [Op.any]: accountIds | ||
87 | }, | ||
88 | targetAccountId | ||
89 | }, | ||
90 | raw: true | ||
91 | } | ||
92 | |||
93 | return AccountBlocklistModel.unscoped() | ||
94 | .findAll(query) | ||
95 | .then(rows => { | ||
96 | const result: { [accountId: number]: boolean } = {} | ||
97 | |||
98 | for (const accountId of accountIds) { | ||
99 | result[accountId] = !!rows.find(r => r.accountId === accountId) | ||
100 | } | ||
101 | |||
102 | return result | ||
103 | }) | ||
104 | } | ||
105 | |||
106 | static loadByAccountAndTarget (accountId: number, targetAccountId: number) { | ||
107 | const query = { | ||
108 | where: { | ||
109 | accountId, | ||
110 | targetAccountId | ||
111 | } | ||
112 | } | ||
113 | |||
114 | return AccountBlocklistModel.findOne(query) | ||
115 | } | ||
116 | |||
117 | static listForApi (accountId: number, start: number, count: number, sort: string) { | ||
118 | const query = { | ||
119 | offset: start, | ||
120 | limit: count, | ||
121 | order: getSort(sort), | ||
122 | where: { | ||
123 | accountId | ||
124 | } | ||
125 | } | ||
126 | |||
127 | return AccountBlocklistModel | ||
128 | .scope([ ScopeNames.WITH_ACCOUNTS ]) | ||
129 | .findAndCountAll(query) | ||
130 | .then(({ rows, count }) => { | ||
131 | return { total: count, data: rows } | ||
132 | }) | ||
133 | } | ||
134 | |||
135 | toFormattedJSON (): AccountBlock { | ||
136 | return { | ||
137 | byAccount: this.ByAccount.toFormattedJSON(), | ||
138 | blockedAccount: this.BlockedAccount.toFormattedJSON(), | ||
139 | createdAt: this.createdAt | ||
140 | } | ||
141 | } | ||
142 | } | ||
diff --git a/server/models/account/account-video-rate.ts b/server/models/account/account-video-rate.ts index c99e32012..18762f0c5 100644 --- a/server/models/account/account-video-rate.ts +++ b/server/models/account/account-video-rate.ts | |||
@@ -1,12 +1,14 @@ | |||
1 | import { values } from 'lodash' | 1 | import { values } from 'lodash' |
2 | import { Transaction } from 'sequelize' | 2 | import { Transaction } from 'sequelize' |
3 | import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Model, Table, UpdatedAt } from 'sequelize-typescript' | 3 | import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Table, UpdatedAt } from 'sequelize-typescript' |
4 | import { IFindOptions } from 'sequelize-typescript/lib/interfaces/IFindOptions' | 4 | import { IFindOptions } from 'sequelize-typescript/lib/interfaces/IFindOptions' |
5 | import { VideoRateType } from '../../../shared/models/videos' | 5 | import { VideoRateType } from '../../../shared/models/videos' |
6 | import { VIDEO_RATE_TYPES } from '../../initializers' | 6 | import { CONSTRAINTS_FIELDS, VIDEO_RATE_TYPES } from '../../initializers' |
7 | import { VideoModel } from '../video/video' | 7 | import { VideoModel } from '../video/video' |
8 | import { AccountModel } from './account' | 8 | import { AccountModel } from './account' |
9 | import { ActorModel } from '../activitypub/actor' | 9 | import { ActorModel } from '../activitypub/actor' |
10 | import { throwIfNotValid } from '../utils' | ||
11 | import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' | ||
10 | 12 | ||
11 | /* | 13 | /* |
12 | Account rates per video. | 14 | Account rates per video. |
@@ -26,6 +28,10 @@ import { ActorModel } from '../activitypub/actor' | |||
26 | }, | 28 | }, |
27 | { | 29 | { |
28 | fields: [ 'videoId', 'type' ] | 30 | fields: [ 'videoId', 'type' ] |
31 | }, | ||
32 | { | ||
33 | fields: [ 'url' ], | ||
34 | unique: true | ||
29 | } | 35 | } |
30 | ] | 36 | ] |
31 | }) | 37 | }) |
@@ -35,6 +41,11 @@ export class AccountVideoRateModel extends Model<AccountVideoRateModel> { | |||
35 | @Column(DataType.ENUM(values(VIDEO_RATE_TYPES))) | 41 | @Column(DataType.ENUM(values(VIDEO_RATE_TYPES))) |
36 | type: VideoRateType | 42 | type: VideoRateType |
37 | 43 | ||
44 | @AllowNull(false) | ||
45 | @Is('AccountVideoRateUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url')) | ||
46 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_RATES.URL.max)) | ||
47 | url: string | ||
48 | |||
38 | @CreatedAt | 49 | @CreatedAt |
39 | createdAt: Date | 50 | createdAt: Date |
40 | 51 | ||
@@ -65,7 +76,7 @@ export class AccountVideoRateModel extends Model<AccountVideoRateModel> { | |||
65 | }) | 76 | }) |
66 | Account: AccountModel | 77 | Account: AccountModel |
67 | 78 | ||
68 | static load (accountId: number, videoId: number, transaction: Transaction) { | 79 | static load (accountId: number, videoId: number, transaction?: Transaction) { |
69 | const options: IFindOptions<AccountVideoRateModel> = { | 80 | const options: IFindOptions<AccountVideoRateModel> = { |
70 | where: { | 81 | where: { |
71 | accountId, | 82 | accountId, |
@@ -77,6 +88,49 @@ export class AccountVideoRateModel extends Model<AccountVideoRateModel> { | |||
77 | return AccountVideoRateModel.findOne(options) | 88 | return AccountVideoRateModel.findOne(options) |
78 | } | 89 | } |
79 | 90 | ||
91 | static loadLocalAndPopulateVideo (rateType: VideoRateType, accountName: string, videoId: number, transaction?: Transaction) { | ||
92 | const options: IFindOptions<AccountVideoRateModel> = { | ||
93 | where: { | ||
94 | videoId, | ||
95 | type: rateType | ||
96 | }, | ||
97 | include: [ | ||
98 | { | ||
99 | model: AccountModel.unscoped(), | ||
100 | required: true, | ||
101 | include: [ | ||
102 | { | ||
103 | attributes: [ 'id', 'url', 'preferredUsername' ], | ||
104 | model: ActorModel.unscoped(), | ||
105 | required: true, | ||
106 | where: { | ||
107 | preferredUsername: accountName | ||
108 | } | ||
109 | } | ||
110 | ] | ||
111 | }, | ||
112 | { | ||
113 | model: VideoModel.unscoped(), | ||
114 | required: true | ||
115 | } | ||
116 | ] | ||
117 | } | ||
118 | if (transaction) options.transaction = transaction | ||
119 | |||
120 | return AccountVideoRateModel.findOne(options) | ||
121 | } | ||
122 | |||
123 | static loadByUrl (url: string, transaction: Transaction) { | ||
124 | const options: IFindOptions<AccountVideoRateModel> = { | ||
125 | where: { | ||
126 | url | ||
127 | } | ||
128 | } | ||
129 | if (transaction) options.transaction = transaction | ||
130 | |||
131 | return AccountVideoRateModel.findOne(options) | ||
132 | } | ||
133 | |||
80 | static listAndCountAccountUrlsByVideoId (rateType: VideoRateType, videoId: number, start: number, count: number, t?: Transaction) { | 134 | static listAndCountAccountUrlsByVideoId (rateType: VideoRateType, videoId: number, start: number, count: number, t?: Transaction) { |
81 | const query = { | 135 | const query = { |
82 | offset: start, | 136 | offset: start, |
diff --git a/server/models/account/account.ts b/server/models/account/account.ts index 5a237d733..84ef0b30d 100644 --- a/server/models/account/account.ts +++ b/server/models/account/account.ts | |||
@@ -241,6 +241,27 @@ export class AccountModel extends Model<AccountModel> { | |||
241 | }) | 241 | }) |
242 | } | 242 | } |
243 | 243 | ||
244 | static listLocalsForSitemap (sort: string) { | ||
245 | const query = { | ||
246 | attributes: [ ], | ||
247 | offset: 0, | ||
248 | order: getSort(sort), | ||
249 | include: [ | ||
250 | { | ||
251 | attributes: [ 'preferredUsername', 'serverId' ], | ||
252 | model: ActorModel.unscoped(), | ||
253 | where: { | ||
254 | serverId: null | ||
255 | } | ||
256 | } | ||
257 | ] | ||
258 | } | ||
259 | |||
260 | return AccountModel | ||
261 | .unscoped() | ||
262 | .findAll(query) | ||
263 | } | ||
264 | |||
244 | toFormattedJSON (): Account { | 265 | toFormattedJSON (): Account { |
245 | const actor = this.Actor.toFormattedJSON() | 266 | const actor = this.Actor.toFormattedJSON() |
246 | const account = { | 267 | const account = { |
@@ -267,6 +288,10 @@ export class AccountModel extends Model<AccountModel> { | |||
267 | return this.Actor.isOwned() | 288 | return this.Actor.isOwned() |
268 | } | 289 | } |
269 | 290 | ||
291 | isOutdated () { | ||
292 | return this.Actor.isOutdated() | ||
293 | } | ||
294 | |||
270 | getDisplayName () { | 295 | getDisplayName () { |
271 | return this.name | 296 | return this.name |
272 | } | 297 | } |
diff --git a/server/models/account/user-notification-setting.ts b/server/models/account/user-notification-setting.ts new file mode 100644 index 000000000..f1c3ac223 --- /dev/null +++ b/server/models/account/user-notification-setting.ts | |||
@@ -0,0 +1,150 @@ | |||
1 | import { | ||
2 | AfterDestroy, | ||
3 | AfterUpdate, | ||
4 | AllowNull, | ||
5 | BelongsTo, | ||
6 | Column, | ||
7 | CreatedAt, | ||
8 | Default, | ||
9 | ForeignKey, | ||
10 | Is, | ||
11 | Model, | ||
12 | Table, | ||
13 | UpdatedAt | ||
14 | } from 'sequelize-typescript' | ||
15 | import { throwIfNotValid } from '../utils' | ||
16 | import { UserModel } from './user' | ||
17 | import { isUserNotificationSettingValid } from '../../helpers/custom-validators/user-notifications' | ||
18 | import { UserNotificationSetting, UserNotificationSettingValue } from '../../../shared/models/users/user-notification-setting.model' | ||
19 | import { clearCacheByUserId } from '../../lib/oauth-model' | ||
20 | |||
21 | @Table({ | ||
22 | tableName: 'userNotificationSetting', | ||
23 | indexes: [ | ||
24 | { | ||
25 | fields: [ 'userId' ], | ||
26 | unique: true | ||
27 | } | ||
28 | ] | ||
29 | }) | ||
30 | export class UserNotificationSettingModel extends Model<UserNotificationSettingModel> { | ||
31 | |||
32 | @AllowNull(false) | ||
33 | @Default(null) | ||
34 | @Is( | ||
35 | 'UserNotificationSettingNewVideoFromSubscription', | ||
36 | value => throwIfNotValid(value, isUserNotificationSettingValid, 'newVideoFromSubscription') | ||
37 | ) | ||
38 | @Column | ||
39 | newVideoFromSubscription: UserNotificationSettingValue | ||
40 | |||
41 | @AllowNull(false) | ||
42 | @Default(null) | ||
43 | @Is( | ||
44 | 'UserNotificationSettingNewCommentOnMyVideo', | ||
45 | value => throwIfNotValid(value, isUserNotificationSettingValid, 'newCommentOnMyVideo') | ||
46 | ) | ||
47 | @Column | ||
48 | newCommentOnMyVideo: UserNotificationSettingValue | ||
49 | |||
50 | @AllowNull(false) | ||
51 | @Default(null) | ||
52 | @Is( | ||
53 | 'UserNotificationSettingVideoAbuseAsModerator', | ||
54 | value => throwIfNotValid(value, isUserNotificationSettingValid, 'videoAbuseAsModerator') | ||
55 | ) | ||
56 | @Column | ||
57 | videoAbuseAsModerator: UserNotificationSettingValue | ||
58 | |||
59 | @AllowNull(false) | ||
60 | @Default(null) | ||
61 | @Is( | ||
62 | 'UserNotificationSettingBlacklistOnMyVideo', | ||
63 | value => throwIfNotValid(value, isUserNotificationSettingValid, 'blacklistOnMyVideo') | ||
64 | ) | ||
65 | @Column | ||
66 | blacklistOnMyVideo: UserNotificationSettingValue | ||
67 | |||
68 | @AllowNull(false) | ||
69 | @Default(null) | ||
70 | @Is( | ||
71 | 'UserNotificationSettingMyVideoPublished', | ||
72 | value => throwIfNotValid(value, isUserNotificationSettingValid, 'myVideoPublished') | ||
73 | ) | ||
74 | @Column | ||
75 | myVideoPublished: UserNotificationSettingValue | ||
76 | |||
77 | @AllowNull(false) | ||
78 | @Default(null) | ||
79 | @Is( | ||
80 | 'UserNotificationSettingMyVideoImportFinished', | ||
81 | value => throwIfNotValid(value, isUserNotificationSettingValid, 'myVideoImportFinished') | ||
82 | ) | ||
83 | @Column | ||
84 | myVideoImportFinished: UserNotificationSettingValue | ||
85 | |||
86 | @AllowNull(false) | ||
87 | @Default(null) | ||
88 | @Is( | ||
89 | 'UserNotificationSettingNewUserRegistration', | ||
90 | value => throwIfNotValid(value, isUserNotificationSettingValid, 'newUserRegistration') | ||
91 | ) | ||
92 | @Column | ||
93 | newUserRegistration: UserNotificationSettingValue | ||
94 | |||
95 | @AllowNull(false) | ||
96 | @Default(null) | ||
97 | @Is( | ||
98 | 'UserNotificationSettingNewFollow', | ||
99 | value => throwIfNotValid(value, isUserNotificationSettingValid, 'newFollow') | ||
100 | ) | ||
101 | @Column | ||
102 | newFollow: UserNotificationSettingValue | ||
103 | |||
104 | @AllowNull(false) | ||
105 | @Default(null) | ||
106 | @Is( | ||
107 | 'UserNotificationSettingCommentMention', | ||
108 | value => throwIfNotValid(value, isUserNotificationSettingValid, 'commentMention') | ||
109 | ) | ||
110 | @Column | ||
111 | commentMention: UserNotificationSettingValue | ||
112 | |||
113 | @ForeignKey(() => UserModel) | ||
114 | @Column | ||
115 | userId: number | ||
116 | |||
117 | @BelongsTo(() => UserModel, { | ||
118 | foreignKey: { | ||
119 | allowNull: false | ||
120 | }, | ||
121 | onDelete: 'cascade' | ||
122 | }) | ||
123 | User: UserModel | ||
124 | |||
125 | @CreatedAt | ||
126 | createdAt: Date | ||
127 | |||
128 | @UpdatedAt | ||
129 | updatedAt: Date | ||
130 | |||
131 | @AfterUpdate | ||
132 | @AfterDestroy | ||
133 | static removeTokenCache (instance: UserNotificationSettingModel) { | ||
134 | return clearCacheByUserId(instance.userId) | ||
135 | } | ||
136 | |||
137 | toFormattedJSON (): UserNotificationSetting { | ||
138 | return { | ||
139 | newCommentOnMyVideo: this.newCommentOnMyVideo, | ||
140 | newVideoFromSubscription: this.newVideoFromSubscription, | ||
141 | videoAbuseAsModerator: this.videoAbuseAsModerator, | ||
142 | blacklistOnMyVideo: this.blacklistOnMyVideo, | ||
143 | myVideoPublished: this.myVideoPublished, | ||
144 | myVideoImportFinished: this.myVideoImportFinished, | ||
145 | newUserRegistration: this.newUserRegistration, | ||
146 | commentMention: this.commentMention, | ||
147 | newFollow: this.newFollow | ||
148 | } | ||
149 | } | ||
150 | } | ||
diff --git a/server/models/account/user-notification.ts b/server/models/account/user-notification.ts new file mode 100644 index 000000000..6cdbb827b --- /dev/null +++ b/server/models/account/user-notification.ts | |||
@@ -0,0 +1,472 @@ | |||
1 | import { | ||
2 | AllowNull, | ||
3 | BelongsTo, | ||
4 | Column, | ||
5 | CreatedAt, | ||
6 | Default, | ||
7 | ForeignKey, | ||
8 | IFindOptions, | ||
9 | Is, | ||
10 | Model, | ||
11 | Scopes, | ||
12 | Table, | ||
13 | UpdatedAt | ||
14 | } from 'sequelize-typescript' | ||
15 | import { UserNotification, UserNotificationType } from '../../../shared' | ||
16 | import { getSort, throwIfNotValid } from '../utils' | ||
17 | import { isBooleanValid } from '../../helpers/custom-validators/misc' | ||
18 | import { isUserNotificationTypeValid } from '../../helpers/custom-validators/user-notifications' | ||
19 | import { UserModel } from './user' | ||
20 | import { VideoModel } from '../video/video' | ||
21 | import { VideoCommentModel } from '../video/video-comment' | ||
22 | import { Op } from 'sequelize' | ||
23 | import { VideoChannelModel } from '../video/video-channel' | ||
24 | import { AccountModel } from './account' | ||
25 | import { VideoAbuseModel } from '../video/video-abuse' | ||
26 | import { VideoBlacklistModel } from '../video/video-blacklist' | ||
27 | import { VideoImportModel } from '../video/video-import' | ||
28 | import { ActorModel } from '../activitypub/actor' | ||
29 | import { ActorFollowModel } from '../activitypub/actor-follow' | ||
30 | import { AvatarModel } from '../avatar/avatar' | ||
31 | import { ServerModel } from '../server/server' | ||
32 | |||
33 | enum ScopeNames { | ||
34 | WITH_ALL = 'WITH_ALL' | ||
35 | } | ||
36 | |||
37 | function buildActorWithAvatarInclude () { | ||
38 | return { | ||
39 | attributes: [ 'preferredUsername' ], | ||
40 | model: () => ActorModel.unscoped(), | ||
41 | required: true, | ||
42 | include: [ | ||
43 | { | ||
44 | attributes: [ 'filename' ], | ||
45 | model: () => AvatarModel.unscoped(), | ||
46 | required: false | ||
47 | }, | ||
48 | { | ||
49 | attributes: [ 'host' ], | ||
50 | model: () => ServerModel.unscoped(), | ||
51 | required: false | ||
52 | } | ||
53 | ] | ||
54 | } | ||
55 | } | ||
56 | |||
57 | function buildVideoInclude (required: boolean) { | ||
58 | return { | ||
59 | attributes: [ 'id', 'uuid', 'name' ], | ||
60 | model: () => VideoModel.unscoped(), | ||
61 | required | ||
62 | } | ||
63 | } | ||
64 | |||
65 | function buildChannelInclude (required: boolean, withActor = false) { | ||
66 | return { | ||
67 | required, | ||
68 | attributes: [ 'id', 'name' ], | ||
69 | model: () => VideoChannelModel.unscoped(), | ||
70 | include: withActor === true ? [ buildActorWithAvatarInclude() ] : [] | ||
71 | } | ||
72 | } | ||
73 | |||
74 | function buildAccountInclude (required: boolean, withActor = false) { | ||
75 | return { | ||
76 | required, | ||
77 | attributes: [ 'id', 'name' ], | ||
78 | model: () => AccountModel.unscoped(), | ||
79 | include: withActor === true ? [ buildActorWithAvatarInclude() ] : [] | ||
80 | } | ||
81 | } | ||
82 | |||
83 | @Scopes({ | ||
84 | [ScopeNames.WITH_ALL]: { | ||
85 | include: [ | ||
86 | Object.assign(buildVideoInclude(false), { | ||
87 | include: [ buildChannelInclude(true, true) ] | ||
88 | }), | ||
89 | |||
90 | { | ||
91 | attributes: [ 'id', 'originCommentId' ], | ||
92 | model: () => VideoCommentModel.unscoped(), | ||
93 | required: false, | ||
94 | include: [ | ||
95 | buildAccountInclude(true, true), | ||
96 | buildVideoInclude(true) | ||
97 | ] | ||
98 | }, | ||
99 | |||
100 | { | ||
101 | attributes: [ 'id' ], | ||
102 | model: () => VideoAbuseModel.unscoped(), | ||
103 | required: false, | ||
104 | include: [ buildVideoInclude(true) ] | ||
105 | }, | ||
106 | |||
107 | { | ||
108 | attributes: [ 'id' ], | ||
109 | model: () => VideoBlacklistModel.unscoped(), | ||
110 | required: false, | ||
111 | include: [ buildVideoInclude(true) ] | ||
112 | }, | ||
113 | |||
114 | { | ||
115 | attributes: [ 'id', 'magnetUri', 'targetUrl', 'torrentName' ], | ||
116 | model: () => VideoImportModel.unscoped(), | ||
117 | required: false, | ||
118 | include: [ buildVideoInclude(false) ] | ||
119 | }, | ||
120 | |||
121 | { | ||
122 | attributes: [ 'id' ], | ||
123 | model: () => ActorFollowModel.unscoped(), | ||
124 | required: false, | ||
125 | include: [ | ||
126 | { | ||
127 | attributes: [ 'preferredUsername' ], | ||
128 | model: () => ActorModel.unscoped(), | ||
129 | required: true, | ||
130 | as: 'ActorFollower', | ||
131 | include: [ | ||
132 | { | ||
133 | attributes: [ 'id', 'name' ], | ||
134 | model: () => AccountModel.unscoped(), | ||
135 | required: true | ||
136 | }, | ||
137 | { | ||
138 | attributes: [ 'filename' ], | ||
139 | model: () => AvatarModel.unscoped(), | ||
140 | required: false | ||
141 | }, | ||
142 | { | ||
143 | attributes: [ 'host' ], | ||
144 | model: () => ServerModel.unscoped(), | ||
145 | required: false | ||
146 | } | ||
147 | ] | ||
148 | }, | ||
149 | { | ||
150 | attributes: [ 'preferredUsername' ], | ||
151 | model: () => ActorModel.unscoped(), | ||
152 | required: true, | ||
153 | as: 'ActorFollowing', | ||
154 | include: [ | ||
155 | buildChannelInclude(false), | ||
156 | buildAccountInclude(false) | ||
157 | ] | ||
158 | } | ||
159 | ] | ||
160 | }, | ||
161 | |||
162 | buildAccountInclude(false, true) | ||
163 | ] | ||
164 | } | ||
165 | }) | ||
166 | @Table({ | ||
167 | tableName: 'userNotification', | ||
168 | indexes: [ | ||
169 | { | ||
170 | fields: [ 'userId' ] | ||
171 | }, | ||
172 | { | ||
173 | fields: [ 'videoId' ], | ||
174 | where: { | ||
175 | videoId: { | ||
176 | [Op.ne]: null | ||
177 | } | ||
178 | } | ||
179 | }, | ||
180 | { | ||
181 | fields: [ 'commentId' ], | ||
182 | where: { | ||
183 | commentId: { | ||
184 | [Op.ne]: null | ||
185 | } | ||
186 | } | ||
187 | }, | ||
188 | { | ||
189 | fields: [ 'videoAbuseId' ], | ||
190 | where: { | ||
191 | videoAbuseId: { | ||
192 | [Op.ne]: null | ||
193 | } | ||
194 | } | ||
195 | }, | ||
196 | { | ||
197 | fields: [ 'videoBlacklistId' ], | ||
198 | where: { | ||
199 | videoBlacklistId: { | ||
200 | [Op.ne]: null | ||
201 | } | ||
202 | } | ||
203 | }, | ||
204 | { | ||
205 | fields: [ 'videoImportId' ], | ||
206 | where: { | ||
207 | videoImportId: { | ||
208 | [Op.ne]: null | ||
209 | } | ||
210 | } | ||
211 | }, | ||
212 | { | ||
213 | fields: [ 'accountId' ], | ||
214 | where: { | ||
215 | accountId: { | ||
216 | [Op.ne]: null | ||
217 | } | ||
218 | } | ||
219 | }, | ||
220 | { | ||
221 | fields: [ 'actorFollowId' ], | ||
222 | where: { | ||
223 | actorFollowId: { | ||
224 | [Op.ne]: null | ||
225 | } | ||
226 | } | ||
227 | } | ||
228 | ] | ||
229 | }) | ||
230 | export class UserNotificationModel extends Model<UserNotificationModel> { | ||
231 | |||
232 | @AllowNull(false) | ||
233 | @Default(null) | ||
234 | @Is('UserNotificationType', value => throwIfNotValid(value, isUserNotificationTypeValid, 'type')) | ||
235 | @Column | ||
236 | type: UserNotificationType | ||
237 | |||
238 | @AllowNull(false) | ||
239 | @Default(false) | ||
240 | @Is('UserNotificationRead', value => throwIfNotValid(value, isBooleanValid, 'read')) | ||
241 | @Column | ||
242 | read: boolean | ||
243 | |||
244 | @CreatedAt | ||
245 | createdAt: Date | ||
246 | |||
247 | @UpdatedAt | ||
248 | updatedAt: Date | ||
249 | |||
250 | @ForeignKey(() => UserModel) | ||
251 | @Column | ||
252 | userId: number | ||
253 | |||
254 | @BelongsTo(() => UserModel, { | ||
255 | foreignKey: { | ||
256 | allowNull: false | ||
257 | }, | ||
258 | onDelete: 'cascade' | ||
259 | }) | ||
260 | User: UserModel | ||
261 | |||
262 | @ForeignKey(() => VideoModel) | ||
263 | @Column | ||
264 | videoId: number | ||
265 | |||
266 | @BelongsTo(() => VideoModel, { | ||
267 | foreignKey: { | ||
268 | allowNull: true | ||
269 | }, | ||
270 | onDelete: 'cascade' | ||
271 | }) | ||
272 | Video: VideoModel | ||
273 | |||
274 | @ForeignKey(() => VideoCommentModel) | ||
275 | @Column | ||
276 | commentId: number | ||
277 | |||
278 | @BelongsTo(() => VideoCommentModel, { | ||
279 | foreignKey: { | ||
280 | allowNull: true | ||
281 | }, | ||
282 | onDelete: 'cascade' | ||
283 | }) | ||
284 | Comment: VideoCommentModel | ||
285 | |||
286 | @ForeignKey(() => VideoAbuseModel) | ||
287 | @Column | ||
288 | videoAbuseId: number | ||
289 | |||
290 | @BelongsTo(() => VideoAbuseModel, { | ||
291 | foreignKey: { | ||
292 | allowNull: true | ||
293 | }, | ||
294 | onDelete: 'cascade' | ||
295 | }) | ||
296 | VideoAbuse: VideoAbuseModel | ||
297 | |||
298 | @ForeignKey(() => VideoBlacklistModel) | ||
299 | @Column | ||
300 | videoBlacklistId: number | ||
301 | |||
302 | @BelongsTo(() => VideoBlacklistModel, { | ||
303 | foreignKey: { | ||
304 | allowNull: true | ||
305 | }, | ||
306 | onDelete: 'cascade' | ||
307 | }) | ||
308 | VideoBlacklist: VideoBlacklistModel | ||
309 | |||
310 | @ForeignKey(() => VideoImportModel) | ||
311 | @Column | ||
312 | videoImportId: number | ||
313 | |||
314 | @BelongsTo(() => VideoImportModel, { | ||
315 | foreignKey: { | ||
316 | allowNull: true | ||
317 | }, | ||
318 | onDelete: 'cascade' | ||
319 | }) | ||
320 | VideoImport: VideoImportModel | ||
321 | |||
322 | @ForeignKey(() => AccountModel) | ||
323 | @Column | ||
324 | accountId: number | ||
325 | |||
326 | @BelongsTo(() => AccountModel, { | ||
327 | foreignKey: { | ||
328 | allowNull: true | ||
329 | }, | ||
330 | onDelete: 'cascade' | ||
331 | }) | ||
332 | Account: AccountModel | ||
333 | |||
334 | @ForeignKey(() => ActorFollowModel) | ||
335 | @Column | ||
336 | actorFollowId: number | ||
337 | |||
338 | @BelongsTo(() => ActorFollowModel, { | ||
339 | foreignKey: { | ||
340 | allowNull: true | ||
341 | }, | ||
342 | onDelete: 'cascade' | ||
343 | }) | ||
344 | ActorFollow: ActorFollowModel | ||
345 | |||
346 | static listForApi (userId: number, start: number, count: number, sort: string, unread?: boolean) { | ||
347 | const query: IFindOptions<UserNotificationModel> = { | ||
348 | offset: start, | ||
349 | limit: count, | ||
350 | order: getSort(sort), | ||
351 | where: { | ||
352 | userId | ||
353 | } | ||
354 | } | ||
355 | |||
356 | if (unread !== undefined) query.where['read'] = !unread | ||
357 | |||
358 | return UserNotificationModel.scope(ScopeNames.WITH_ALL) | ||
359 | .findAndCountAll(query) | ||
360 | .then(({ rows, count }) => { | ||
361 | return { | ||
362 | data: rows, | ||
363 | total: count | ||
364 | } | ||
365 | }) | ||
366 | } | ||
367 | |||
368 | static markAsRead (userId: number, notificationIds: number[]) { | ||
369 | const query = { | ||
370 | where: { | ||
371 | userId, | ||
372 | id: { | ||
373 | [Op.any]: notificationIds | ||
374 | } | ||
375 | } | ||
376 | } | ||
377 | |||
378 | return UserNotificationModel.update({ read: true }, query) | ||
379 | } | ||
380 | |||
381 | static markAllAsRead (userId: number) { | ||
382 | const query = { where: { userId } } | ||
383 | |||
384 | return UserNotificationModel.update({ read: true }, query) | ||
385 | } | ||
386 | |||
387 | toFormattedJSON (): UserNotification { | ||
388 | const video = this.Video | ||
389 | ? Object.assign(this.formatVideo(this.Video),{ channel: this.formatActor(this.Video.VideoChannel) }) | ||
390 | : undefined | ||
391 | |||
392 | const videoImport = this.VideoImport ? { | ||
393 | id: this.VideoImport.id, | ||
394 | video: this.VideoImport.Video ? this.formatVideo(this.VideoImport.Video) : undefined, | ||
395 | torrentName: this.VideoImport.torrentName, | ||
396 | magnetUri: this.VideoImport.magnetUri, | ||
397 | targetUrl: this.VideoImport.targetUrl | ||
398 | } : undefined | ||
399 | |||
400 | const comment = this.Comment ? { | ||
401 | id: this.Comment.id, | ||
402 | threadId: this.Comment.getThreadId(), | ||
403 | account: this.formatActor(this.Comment.Account), | ||
404 | video: this.formatVideo(this.Comment.Video) | ||
405 | } : undefined | ||
406 | |||
407 | const videoAbuse = this.VideoAbuse ? { | ||
408 | id: this.VideoAbuse.id, | ||
409 | video: this.formatVideo(this.VideoAbuse.Video) | ||
410 | } : undefined | ||
411 | |||
412 | const videoBlacklist = this.VideoBlacklist ? { | ||
413 | id: this.VideoBlacklist.id, | ||
414 | video: this.formatVideo(this.VideoBlacklist.Video) | ||
415 | } : undefined | ||
416 | |||
417 | const account = this.Account ? this.formatActor(this.Account) : undefined | ||
418 | |||
419 | const actorFollow = this.ActorFollow ? { | ||
420 | id: this.ActorFollow.id, | ||
421 | follower: { | ||
422 | id: this.ActorFollow.ActorFollower.Account.id, | ||
423 | displayName: this.ActorFollow.ActorFollower.Account.getDisplayName(), | ||
424 | name: this.ActorFollow.ActorFollower.preferredUsername, | ||
425 | avatar: this.ActorFollow.ActorFollower.Avatar ? { path: this.ActorFollow.ActorFollower.Avatar.getWebserverPath() } : undefined, | ||
426 | host: this.ActorFollow.ActorFollower.getHost() | ||
427 | }, | ||
428 | following: { | ||
429 | type: this.ActorFollow.ActorFollowing.VideoChannel ? 'channel' as 'channel' : 'account' as 'account', | ||
430 | displayName: (this.ActorFollow.ActorFollowing.VideoChannel || this.ActorFollow.ActorFollowing.Account).getDisplayName(), | ||
431 | name: this.ActorFollow.ActorFollowing.preferredUsername | ||
432 | } | ||
433 | } : undefined | ||
434 | |||
435 | return { | ||
436 | id: this.id, | ||
437 | type: this.type, | ||
438 | read: this.read, | ||
439 | video, | ||
440 | videoImport, | ||
441 | comment, | ||
442 | videoAbuse, | ||
443 | videoBlacklist, | ||
444 | account, | ||
445 | actorFollow, | ||
446 | createdAt: this.createdAt.toISOString(), | ||
447 | updatedAt: this.updatedAt.toISOString() | ||
448 | } | ||
449 | } | ||
450 | |||
451 | private formatVideo (video: VideoModel) { | ||
452 | return { | ||
453 | id: video.id, | ||
454 | uuid: video.uuid, | ||
455 | name: video.name | ||
456 | } | ||
457 | } | ||
458 | |||
459 | private formatActor (accountOrChannel: AccountModel | VideoChannelModel) { | ||
460 | const avatar = accountOrChannel.Actor.Avatar | ||
461 | ? { path: accountOrChannel.Actor.Avatar.getWebserverPath() } | ||
462 | : undefined | ||
463 | |||
464 | return { | ||
465 | id: accountOrChannel.id, | ||
466 | displayName: accountOrChannel.getDisplayName(), | ||
467 | name: accountOrChannel.Actor.preferredUsername, | ||
468 | host: accountOrChannel.Actor.getHost(), | ||
469 | avatar | ||
470 | } | ||
471 | } | ||
472 | } | ||
diff --git a/server/models/account/user-video-history.ts b/server/models/account/user-video-history.ts index 0476cad9d..15cb399c9 100644 --- a/server/models/account/user-video-history.ts +++ b/server/models/account/user-video-history.ts | |||
@@ -1,6 +1,7 @@ | |||
1 | import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, IsInt, Min, Model, Table, UpdatedAt } from 'sequelize-typescript' | 1 | import { AllowNull, BelongsTo, Column, CreatedAt, ForeignKey, IsInt, Model, Table, UpdatedAt } from 'sequelize-typescript' |
2 | import { VideoModel } from '../video/video' | 2 | import { VideoModel } from '../video/video' |
3 | import { UserModel } from './user' | 3 | import { UserModel } from './user' |
4 | import { Transaction, Op, DestroyOptions } from 'sequelize' | ||
4 | 5 | ||
5 | @Table({ | 6 | @Table({ |
6 | tableName: 'userVideoHistory', | 7 | tableName: 'userVideoHistory', |
@@ -52,4 +53,34 @@ export class UserVideoHistoryModel extends Model<UserVideoHistoryModel> { | |||
52 | onDelete: 'CASCADE' | 53 | onDelete: 'CASCADE' |
53 | }) | 54 | }) |
54 | User: UserModel | 55 | User: UserModel |
56 | |||
57 | static listForApi (user: UserModel, start: number, count: number) { | ||
58 | return VideoModel.listForApi({ | ||
59 | start, | ||
60 | count, | ||
61 | sort: '-UserVideoHistories.updatedAt', | ||
62 | nsfw: null, // All | ||
63 | includeLocalVideos: true, | ||
64 | withFiles: false, | ||
65 | user, | ||
66 | historyOfUser: user | ||
67 | }) | ||
68 | } | ||
69 | |||
70 | static removeHistoryBefore (user: UserModel, beforeDate: string, t: Transaction) { | ||
71 | const query: DestroyOptions = { | ||
72 | where: { | ||
73 | userId: user.id | ||
74 | }, | ||
75 | transaction: t | ||
76 | } | ||
77 | |||
78 | if (beforeDate) { | ||
79 | query.where.updatedAt = { | ||
80 | [Op.lt]: beforeDate | ||
81 | } | ||
82 | } | ||
83 | |||
84 | return UserVideoHistoryModel.destroy(query) | ||
85 | } | ||
55 | } | 86 | } |
diff --git a/server/models/account/user.ts b/server/models/account/user.ts index e56b0bf40..017a96657 100644 --- a/server/models/account/user.ts +++ b/server/models/account/user.ts | |||
@@ -1,6 +1,6 @@ | |||
1 | import * as Sequelize from 'sequelize' | 1 | import * as Sequelize from 'sequelize' |
2 | import { | 2 | import { |
3 | AfterDelete, | 3 | AfterDestroy, |
4 | AfterUpdate, | 4 | AfterUpdate, |
5 | AllowNull, | 5 | AllowNull, |
6 | BeforeCreate, | 6 | BeforeCreate, |
@@ -31,7 +31,9 @@ import { | |||
31 | isUserRoleValid, | 31 | isUserRoleValid, |
32 | isUserUsernameValid, | 32 | isUserUsernameValid, |
33 | isUserVideoQuotaDailyValid, | 33 | isUserVideoQuotaDailyValid, |
34 | isUserVideoQuotaValid | 34 | isUserVideoQuotaValid, |
35 | isUserVideosHistoryEnabledValid, | ||
36 | isUserWebTorrentEnabledValid | ||
35 | } from '../../helpers/custom-validators/users' | 37 | } from '../../helpers/custom-validators/users' |
36 | import { comparePassword, cryptPassword } from '../../helpers/peertube-crypto' | 38 | import { comparePassword, cryptPassword } from '../../helpers/peertube-crypto' |
37 | import { OAuthTokenModel } from '../oauth/oauth-token' | 39 | import { OAuthTokenModel } from '../oauth/oauth-token' |
@@ -42,6 +44,11 @@ import { NSFWPolicyType } from '../../../shared/models/videos/nsfw-policy.type' | |||
42 | import { values } from 'lodash' | 44 | import { values } from 'lodash' |
43 | import { NSFW_POLICY_TYPES } from '../../initializers' | 45 | import { NSFW_POLICY_TYPES } from '../../initializers' |
44 | import { clearCacheByUserId } from '../../lib/oauth-model' | 46 | import { clearCacheByUserId } from '../../lib/oauth-model' |
47 | import { UserNotificationSettingModel } from './user-notification-setting' | ||
48 | import { VideoModel } from '../video/video' | ||
49 | import { ActorModel } from '../activitypub/actor' | ||
50 | import { ActorFollowModel } from '../activitypub/actor-follow' | ||
51 | import { VideoImportModel } from '../video/video-import' | ||
45 | 52 | ||
46 | enum ScopeNames { | 53 | enum ScopeNames { |
47 | WITH_VIDEO_CHANNEL = 'WITH_VIDEO_CHANNEL' | 54 | WITH_VIDEO_CHANNEL = 'WITH_VIDEO_CHANNEL' |
@@ -52,6 +59,10 @@ enum ScopeNames { | |||
52 | { | 59 | { |
53 | model: () => AccountModel, | 60 | model: () => AccountModel, |
54 | required: true | 61 | required: true |
62 | }, | ||
63 | { | ||
64 | model: () => UserNotificationSettingModel, | ||
65 | required: true | ||
55 | } | 66 | } |
56 | ] | 67 | ] |
57 | }) | 68 | }) |
@@ -62,6 +73,10 @@ enum ScopeNames { | |||
62 | model: () => AccountModel, | 73 | model: () => AccountModel, |
63 | required: true, | 74 | required: true, |
64 | include: [ () => VideoChannelModel ] | 75 | include: [ () => VideoChannelModel ] |
76 | }, | ||
77 | { | ||
78 | model: () => UserNotificationSettingModel, | ||
79 | required: true | ||
65 | } | 80 | } |
66 | ] | 81 | ] |
67 | } | 82 | } |
@@ -109,6 +124,18 @@ export class UserModel extends Model<UserModel> { | |||
109 | 124 | ||
110 | @AllowNull(false) | 125 | @AllowNull(false) |
111 | @Default(true) | 126 | @Default(true) |
127 | @Is('UserWebTorrentEnabled', value => throwIfNotValid(value, isUserWebTorrentEnabledValid, 'WebTorrent enabled')) | ||
128 | @Column | ||
129 | webTorrentEnabled: boolean | ||
130 | |||
131 | @AllowNull(false) | ||
132 | @Default(true) | ||
133 | @Is('UserVideosHistoryEnabled', value => throwIfNotValid(value, isUserVideosHistoryEnabledValid, 'Videos history enabled')) | ||
134 | @Column | ||
135 | videosHistoryEnabled: boolean | ||
136 | |||
137 | @AllowNull(false) | ||
138 | @Default(true) | ||
112 | @Is('UserAutoPlayVideo', value => throwIfNotValid(value, isUserAutoPlayVideoValid, 'auto play video boolean')) | 139 | @Is('UserAutoPlayVideo', value => throwIfNotValid(value, isUserAutoPlayVideoValid, 'auto play video boolean')) |
113 | @Column | 140 | @Column |
114 | autoPlayVideo: boolean | 141 | autoPlayVideo: boolean |
@@ -153,6 +180,19 @@ export class UserModel extends Model<UserModel> { | |||
153 | }) | 180 | }) |
154 | Account: AccountModel | 181 | Account: AccountModel |
155 | 182 | ||
183 | @HasOne(() => UserNotificationSettingModel, { | ||
184 | foreignKey: 'userId', | ||
185 | onDelete: 'cascade', | ||
186 | hooks: true | ||
187 | }) | ||
188 | NotificationSetting: UserNotificationSettingModel | ||
189 | |||
190 | @HasMany(() => VideoImportModel, { | ||
191 | foreignKey: 'userId', | ||
192 | onDelete: 'cascade' | ||
193 | }) | ||
194 | VideoImports: VideoImportModel[] | ||
195 | |||
156 | @HasMany(() => OAuthTokenModel, { | 196 | @HasMany(() => OAuthTokenModel, { |
157 | foreignKey: 'userId', | 197 | foreignKey: 'userId', |
158 | onDelete: 'cascade' | 198 | onDelete: 'cascade' |
@@ -172,7 +212,7 @@ export class UserModel extends Model<UserModel> { | |||
172 | } | 212 | } |
173 | 213 | ||
174 | @AfterUpdate | 214 | @AfterUpdate |
175 | @AfterDelete | 215 | @AfterDestroy |
176 | static removeTokenCache (instance: UserModel) { | 216 | static removeTokenCache (instance: UserModel) { |
177 | return clearCacheByUserId(instance.id) | 217 | return clearCacheByUserId(instance.id) |
178 | } | 218 | } |
@@ -181,7 +221,25 @@ export class UserModel extends Model<UserModel> { | |||
181 | return this.count() | 221 | return this.count() |
182 | } | 222 | } |
183 | 223 | ||
184 | static listForApi (start: number, count: number, sort: string) { | 224 | static listForApi (start: number, count: number, sort: string, search?: string) { |
225 | let where = undefined | ||
226 | if (search) { | ||
227 | where = { | ||
228 | [Sequelize.Op.or]: [ | ||
229 | { | ||
230 | email: { | ||
231 | [Sequelize.Op.iLike]: '%' + search + '%' | ||
232 | } | ||
233 | }, | ||
234 | { | ||
235 | username: { | ||
236 | [ Sequelize.Op.iLike ]: '%' + search + '%' | ||
237 | } | ||
238 | } | ||
239 | ] | ||
240 | } | ||
241 | } | ||
242 | |||
185 | const query = { | 243 | const query = { |
186 | attributes: { | 244 | attributes: { |
187 | include: [ | 245 | include: [ |
@@ -204,7 +262,8 @@ export class UserModel extends Model<UserModel> { | |||
204 | }, | 262 | }, |
205 | offset: start, | 263 | offset: start, |
206 | limit: count, | 264 | limit: count, |
207 | order: getSort(sort) | 265 | order: getSort(sort), |
266 | where | ||
208 | } | 267 | } |
209 | 268 | ||
210 | return UserModel.findAndCountAll(query) | 269 | return UserModel.findAndCountAll(query) |
@@ -216,13 +275,12 @@ export class UserModel extends Model<UserModel> { | |||
216 | }) | 275 | }) |
217 | } | 276 | } |
218 | 277 | ||
219 | static listEmailsWithRight (right: UserRight) { | 278 | static listWithRight (right: UserRight) { |
220 | const roles = Object.keys(USER_ROLE_LABELS) | 279 | const roles = Object.keys(USER_ROLE_LABELS) |
221 | .map(k => parseInt(k, 10) as UserRole) | 280 | .map(k => parseInt(k, 10) as UserRole) |
222 | .filter(role => hasUserRight(role, right)) | 281 | .filter(role => hasUserRight(role, right)) |
223 | 282 | ||
224 | const query = { | 283 | const query = { |
225 | attribute: [ 'email' ], | ||
226 | where: { | 284 | where: { |
227 | role: { | 285 | role: { |
228 | [Sequelize.Op.in]: roles | 286 | [Sequelize.Op.in]: roles |
@@ -230,9 +288,56 @@ export class UserModel extends Model<UserModel> { | |||
230 | } | 288 | } |
231 | } | 289 | } |
232 | 290 | ||
233 | return UserModel.unscoped() | 291 | return UserModel.findAll(query) |
234 | .findAll(query) | 292 | } |
235 | .then(u => u.map(u => u.email)) | 293 | |
294 | static listUserSubscribersOf (actorId: number) { | ||
295 | const query = { | ||
296 | include: [ | ||
297 | { | ||
298 | model: UserNotificationSettingModel.unscoped(), | ||
299 | required: true | ||
300 | }, | ||
301 | { | ||
302 | attributes: [ 'userId' ], | ||
303 | model: AccountModel.unscoped(), | ||
304 | required: true, | ||
305 | include: [ | ||
306 | { | ||
307 | attributes: [ ], | ||
308 | model: ActorModel.unscoped(), | ||
309 | required: true, | ||
310 | where: { | ||
311 | serverId: null | ||
312 | }, | ||
313 | include: [ | ||
314 | { | ||
315 | attributes: [ ], | ||
316 | as: 'ActorFollowings', | ||
317 | model: ActorFollowModel.unscoped(), | ||
318 | required: true, | ||
319 | where: { | ||
320 | targetActorId: actorId | ||
321 | } | ||
322 | } | ||
323 | ] | ||
324 | } | ||
325 | ] | ||
326 | } | ||
327 | ] | ||
328 | } | ||
329 | |||
330 | return UserModel.unscoped().findAll(query) | ||
331 | } | ||
332 | |||
333 | static listByUsernames (usernames: string[]) { | ||
334 | const query = { | ||
335 | where: { | ||
336 | username: usernames | ||
337 | } | ||
338 | } | ||
339 | |||
340 | return UserModel.findAll(query) | ||
236 | } | 341 | } |
237 | 342 | ||
238 | static loadById (id: number) { | 343 | static loadById (id: number) { |
@@ -281,6 +386,95 @@ export class UserModel extends Model<UserModel> { | |||
281 | return UserModel.findOne(query) | 386 | return UserModel.findOne(query) |
282 | } | 387 | } |
283 | 388 | ||
389 | static loadByVideoId (videoId: number) { | ||
390 | const query = { | ||
391 | include: [ | ||
392 | { | ||
393 | required: true, | ||
394 | attributes: [ 'id' ], | ||
395 | model: AccountModel.unscoped(), | ||
396 | include: [ | ||
397 | { | ||
398 | required: true, | ||
399 | attributes: [ 'id' ], | ||
400 | model: VideoChannelModel.unscoped(), | ||
401 | include: [ | ||
402 | { | ||
403 | required: true, | ||
404 | attributes: [ 'id' ], | ||
405 | model: VideoModel.unscoped(), | ||
406 | where: { | ||
407 | id: videoId | ||
408 | } | ||
409 | } | ||
410 | ] | ||
411 | } | ||
412 | ] | ||
413 | } | ||
414 | ] | ||
415 | } | ||
416 | |||
417 | return UserModel.findOne(query) | ||
418 | } | ||
419 | |||
420 | static loadByVideoImportId (videoImportId: number) { | ||
421 | const query = { | ||
422 | include: [ | ||
423 | { | ||
424 | required: true, | ||
425 | attributes: [ 'id' ], | ||
426 | model: VideoImportModel.unscoped(), | ||
427 | where: { | ||
428 | id: videoImportId | ||
429 | } | ||
430 | } | ||
431 | ] | ||
432 | } | ||
433 | |||
434 | return UserModel.findOne(query) | ||
435 | } | ||
436 | |||
437 | static loadByChannelActorId (videoChannelActorId: number) { | ||
438 | const query = { | ||
439 | include: [ | ||
440 | { | ||
441 | required: true, | ||
442 | attributes: [ 'id' ], | ||
443 | model: AccountModel.unscoped(), | ||
444 | include: [ | ||
445 | { | ||
446 | required: true, | ||
447 | attributes: [ 'id' ], | ||
448 | model: VideoChannelModel.unscoped(), | ||
449 | where: { | ||
450 | actorId: videoChannelActorId | ||
451 | } | ||
452 | } | ||
453 | ] | ||
454 | } | ||
455 | ] | ||
456 | } | ||
457 | |||
458 | return UserModel.findOne(query) | ||
459 | } | ||
460 | |||
461 | static loadByAccountActorId (accountActorId: number) { | ||
462 | const query = { | ||
463 | include: [ | ||
464 | { | ||
465 | required: true, | ||
466 | attributes: [ 'id' ], | ||
467 | model: AccountModel.unscoped(), | ||
468 | where: { | ||
469 | actorId: accountActorId | ||
470 | } | ||
471 | } | ||
472 | ] | ||
473 | } | ||
474 | |||
475 | return UserModel.findOne(query) | ||
476 | } | ||
477 | |||
284 | static getOriginalVideoFileTotalFromUser (user: UserModel) { | 478 | static getOriginalVideoFileTotalFromUser (user: UserModel) { |
285 | // Don't use sequelize because we need to use a sub query | 479 | // Don't use sequelize because we need to use a sub query |
286 | const query = UserModel.generateUserQuotaBaseSQL() | 480 | const query = UserModel.generateUserQuotaBaseSQL() |
@@ -336,6 +530,8 @@ export class UserModel extends Model<UserModel> { | |||
336 | email: this.email, | 530 | email: this.email, |
337 | emailVerified: this.emailVerified, | 531 | emailVerified: this.emailVerified, |
338 | nsfwPolicy: this.nsfwPolicy, | 532 | nsfwPolicy: this.nsfwPolicy, |
533 | webTorrentEnabled: this.webTorrentEnabled, | ||
534 | videosHistoryEnabled: this.videosHistoryEnabled, | ||
339 | autoPlayVideo: this.autoPlayVideo, | 535 | autoPlayVideo: this.autoPlayVideo, |
340 | role: this.role, | 536 | role: this.role, |
341 | roleLabel: USER_ROLE_LABELS[ this.role ], | 537 | roleLabel: USER_ROLE_LABELS[ this.role ], |
@@ -345,6 +541,7 @@ export class UserModel extends Model<UserModel> { | |||
345 | blocked: this.blocked, | 541 | blocked: this.blocked, |
346 | blockedReason: this.blockedReason, | 542 | blockedReason: this.blockedReason, |
347 | account: this.Account.toFormattedJSON(), | 543 | account: this.Account.toFormattedJSON(), |
544 | notificationSettings: this.NotificationSetting ? this.NotificationSetting.toFormattedJSON() : undefined, | ||
348 | videoChannels: [], | 545 | videoChannels: [], |
349 | videoQuotaUsed: videoQuotaUsed !== undefined | 546 | videoQuotaUsed: videoQuotaUsed !== undefined |
350 | ? parseInt(videoQuotaUsed, 10) | 547 | ? parseInt(videoQuotaUsed, 10) |