]>
Commit | Line | Data |
---|---|---|
3fd3ab2d C |
1 | import { |
2 | AfterDestroy, | |
3 | AllowNull, | |
4 | BelongsTo, | |
5 | Column, | |
6 | CreatedAt, | |
50d6de9c | 7 | DefaultScope, |
3fd3ab2d C |
8 | ForeignKey, |
9 | HasMany, | |
10 | Is, | |
fadf619a C |
11 | Model, |
12 | Scopes, | |
3fd3ab2d C |
13 | Table, |
14 | UpdatedAt | |
15 | } from 'sequelize-typescript' | |
50d6de9c | 16 | import { ActivityPubActor } from '../../../shared/models/activitypub' |
3fd3ab2d | 17 | import { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../../helpers/custom-validators/video-channels' |
50d6de9c | 18 | import { sendDeleteActor } from '../../lib/activitypub/send' |
3fd3ab2d | 19 | import { AccountModel } from '../account/account' |
fadf619a | 20 | import { ActorModel } from '../activitypub/actor' |
3fd3ab2d C |
21 | import { getSort, throwIfNotValid } from '../utils' |
22 | import { VideoModel } from './video' | |
3fd3ab2d | 23 | |
d48ff09d C |
24 | enum ScopeNames { |
25 | WITH_ACCOUNT = 'WITH_ACCOUNT', | |
50d6de9c | 26 | WITH_ACTOR = 'WITH_ACTOR', |
d48ff09d C |
27 | WITH_VIDEOS = 'WITH_VIDEOS' |
28 | } | |
29 | ||
50d6de9c C |
30 | @DefaultScope({ |
31 | include: [ | |
32 | { | |
33 | model: () => ActorModel, | |
34 | required: true | |
35 | } | |
36 | ] | |
37 | }) | |
d48ff09d C |
38 | @Scopes({ |
39 | [ScopeNames.WITH_ACCOUNT]: { | |
40 | include: [ | |
41 | { | |
42 | model: () => AccountModel, | |
50d6de9c C |
43 | required: true, |
44 | include: [ | |
45 | { | |
46 | model: () => ActorModel, | |
47 | required: true | |
48 | } | |
49 | ] | |
d48ff09d C |
50 | } |
51 | ] | |
52 | }, | |
53 | [ScopeNames.WITH_VIDEOS]: { | |
54 | include: [ | |
55 | () => VideoModel | |
56 | ] | |
50d6de9c C |
57 | }, |
58 | [ScopeNames.WITH_ACTOR]: { | |
59 | include: [ | |
60 | () => ActorModel | |
61 | ] | |
d48ff09d C |
62 | } |
63 | }) | |
3fd3ab2d C |
64 | @Table({ |
65 | tableName: 'videoChannel', | |
66 | indexes: [ | |
72c7248b | 67 | { |
3fd3ab2d | 68 | fields: [ 'accountId' ] |
72c7248b | 69 | } |
72c7248b | 70 | ] |
3fd3ab2d C |
71 | }) |
72 | export class VideoChannelModel extends Model<VideoChannelModel> { | |
72c7248b | 73 | |
3fd3ab2d C |
74 | @AllowNull(false) |
75 | @Is('VideoChannelName', value => throwIfNotValid(value, isVideoChannelNameValid, 'name')) | |
76 | @Column | |
77 | name: string | |
72c7248b | 78 | |
3fd3ab2d C |
79 | @AllowNull(true) |
80 | @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description')) | |
81 | @Column | |
82 | description: string | |
72c7248b | 83 | |
3fd3ab2d C |
84 | @CreatedAt |
85 | createdAt: Date | |
72c7248b | 86 | |
3fd3ab2d C |
87 | @UpdatedAt |
88 | updatedAt: Date | |
4e50b6a1 | 89 | |
fadf619a C |
90 | @ForeignKey(() => ActorModel) |
91 | @Column | |
92 | actorId: number | |
93 | ||
94 | @BelongsTo(() => ActorModel, { | |
95 | foreignKey: { | |
96 | allowNull: false | |
97 | }, | |
98 | onDelete: 'cascade' | |
99 | }) | |
100 | Actor: ActorModel | |
101 | ||
3fd3ab2d C |
102 | @ForeignKey(() => AccountModel) |
103 | @Column | |
104 | accountId: number | |
4e50b6a1 | 105 | |
3fd3ab2d C |
106 | @BelongsTo(() => AccountModel, { |
107 | foreignKey: { | |
108 | allowNull: false | |
109 | }, | |
110 | onDelete: 'CASCADE' | |
111 | }) | |
112 | Account: AccountModel | |
72c7248b | 113 | |
3fd3ab2d | 114 | @HasMany(() => VideoModel, { |
72c7248b | 115 | foreignKey: { |
3fd3ab2d | 116 | name: 'channelId', |
72c7248b C |
117 | allowNull: false |
118 | }, | |
119 | onDelete: 'CASCADE' | |
120 | }) | |
3fd3ab2d | 121 | Videos: VideoModel[] |
72c7248b | 122 | |
3fd3ab2d C |
123 | @AfterDestroy |
124 | static sendDeleteIfOwned (instance: VideoChannelModel) { | |
50d6de9c C |
125 | if (instance.Actor.isOwned()) { |
126 | return sendDeleteActor(instance.Actor, undefined) | |
3fd3ab2d | 127 | } |
72c7248b | 128 | |
3fd3ab2d C |
129 | return undefined |
130 | } | |
72c7248b | 131 | |
3fd3ab2d C |
132 | static countByAccount (accountId: number) { |
133 | const query = { | |
134 | where: { | |
135 | accountId | |
136 | } | |
72c7248b | 137 | } |
3fd3ab2d C |
138 | |
139 | return VideoChannelModel.count(query) | |
72c7248b C |
140 | } |
141 | ||
3fd3ab2d C |
142 | static listForApi (start: number, count: number, sort: string) { |
143 | const query = { | |
144 | offset: start, | |
145 | limit: count, | |
d48ff09d | 146 | order: [ getSort(sort) ] |
3fd3ab2d | 147 | } |
72c7248b | 148 | |
50d6de9c C |
149 | return VideoChannelModel |
150 | .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) | |
151 | .findAndCountAll(query) | |
3fd3ab2d C |
152 | .then(({ rows, count }) => { |
153 | return { total: count, data: rows } | |
154 | }) | |
72c7248b C |
155 | } |
156 | ||
3fd3ab2d C |
157 | static listByAccount (accountId: number) { |
158 | const query = { | |
159 | order: [ getSort('createdAt') ], | |
160 | include: [ | |
161 | { | |
162 | model: AccountModel, | |
163 | where: { | |
164 | id: accountId | |
165 | }, | |
50d6de9c | 166 | required: true |
3fd3ab2d C |
167 | } |
168 | ] | |
169 | } | |
72c7248b | 170 | |
50d6de9c C |
171 | return VideoChannelModel |
172 | .findAndCountAll(query) | |
3fd3ab2d C |
173 | .then(({ rows, count }) => { |
174 | return { total: count, data: rows } | |
175 | }) | |
72c7248b C |
176 | } |
177 | ||
3fd3ab2d C |
178 | static loadByIdAndAccount (id: number, accountId: number) { |
179 | const options = { | |
180 | where: { | |
181 | id, | |
182 | accountId | |
d48ff09d | 183 | } |
571389d4 | 184 | } |
3fd3ab2d | 185 | |
50d6de9c C |
186 | return VideoChannelModel |
187 | .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) | |
188 | .findOne(options) | |
0d0e8dd0 C |
189 | } |
190 | ||
3fd3ab2d | 191 | static loadAndPopulateAccount (id: number) { |
50d6de9c C |
192 | return VideoChannelModel |
193 | .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) | |
194 | .findById(id) | |
3fd3ab2d | 195 | } |
0d0e8dd0 | 196 | |
3fd3ab2d C |
197 | static loadByUUIDAndPopulateAccount (uuid: string) { |
198 | const options = { | |
50d6de9c C |
199 | include: [ |
200 | { | |
201 | model: ActorModel, | |
202 | required: true, | |
203 | where: { | |
204 | uuid | |
205 | } | |
206 | } | |
207 | ] | |
3fd3ab2d C |
208 | } |
209 | ||
50d6de9c C |
210 | return VideoChannelModel |
211 | .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT ]) | |
212 | .findOne(options) | |
72c7248b C |
213 | } |
214 | ||
3fd3ab2d C |
215 | static loadAndPopulateAccountAndVideos (id: number) { |
216 | const options = { | |
217 | include: [ | |
3fd3ab2d C |
218 | VideoModel |
219 | ] | |
220 | } | |
72c7248b | 221 | |
50d6de9c C |
222 | return VideoChannelModel |
223 | .scope([ ScopeNames.WITH_ACTOR, ScopeNames.WITH_ACCOUNT, ScopeNames.WITH_VIDEOS ]) | |
224 | .findById(id, options) | |
72c7248b C |
225 | } |
226 | ||
3fd3ab2d | 227 | toFormattedJSON () { |
50d6de9c C |
228 | const actor = this.Actor.toFormattedJSON() |
229 | const account = { | |
3fd3ab2d | 230 | id: this.id, |
3fd3ab2d C |
231 | name: this.name, |
232 | description: this.description, | |
50d6de9c | 233 | isLocal: this.Actor.isOwned(), |
3fd3ab2d C |
234 | createdAt: this.createdAt, |
235 | updatedAt: this.updatedAt | |
236 | } | |
72c7248b | 237 | |
50d6de9c | 238 | return Object.assign(actor, account) |
72c7248b C |
239 | } |
240 | ||
50d6de9c C |
241 | toActivityPubObject (): ActivityPubActor { |
242 | const obj = this.Actor.toActivityPubObject(this.name, 'VideoChannel') | |
243 | ||
244 | return Object.assign(obj, { | |
245 | summary: this.description, | |
246 | attributedTo: [ | |
247 | { | |
248 | type: 'Person' as 'Person', | |
249 | id: this.Account.Actor.url | |
250 | } | |
251 | ] | |
252 | }) | |
72c7248b | 253 | } |
72c7248b | 254 | } |