]>
Commit | Line | Data |
---|---|---|
3fd3ab2d | 1 | import { |
2422c46b C |
2 | AllowNull, |
3 | BeforeDestroy, | |
4 | BelongsTo, | |
5 | Column, | |
241c3357 | 6 | CreatedAt, DataType, |
2422c46b C |
7 | Default, |
8 | DefaultScope, | |
9 | ForeignKey, | |
10 | HasMany, | |
11 | Is, | |
09979f89 C |
12 | Model, |
13 | Scopes, | |
2422c46b | 14 | Table, |
3fd3ab2d C |
15 | UpdatedAt |
16 | } from 'sequelize-typescript' | |
418d092a | 17 | import { Account, AccountSummary } from '../../../shared/models/actors' |
2422c46b | 18 | import { isAccountDescriptionValid } from '../../helpers/custom-validators/accounts' |
c5a893d5 | 19 | import { sendDeleteActor } from '../../lib/activitypub/send' |
fadf619a | 20 | import { ActorModel } from '../activitypub/actor' |
3fd3ab2d | 21 | import { ApplicationModel } from '../application/application' |
3fd3ab2d | 22 | import { ServerModel } from '../server/server' |
2422c46b | 23 | import { getSort, throwIfNotValid } from '../utils' |
3fd3ab2d | 24 | import { VideoChannelModel } from '../video/video-channel' |
f05a1c30 | 25 | import { VideoCommentModel } from '../video/video-comment' |
3fd3ab2d | 26 | import { UserModel } from './user' |
418d092a | 27 | import { AvatarModel } from '../avatar/avatar' |
418d092a | 28 | import { VideoPlaylistModel } from '../video/video-playlist' |
241c3357 | 29 | import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants' |
1735c825 | 30 | import { Op, Transaction, WhereOptions } from 'sequelize' |
418d092a C |
31 | |
32 | export enum ScopeNames { | |
33 | SUMMARY = 'SUMMARY' | |
34 | } | |
3fd3ab2d | 35 | |
3acc5084 | 36 | @DefaultScope(() => ({ |
50d6de9c | 37 | include: [ |
3fd3ab2d | 38 | { |
3acc5084 | 39 | model: ActorModel, // Default scope includes avatar and server |
f37dc0dd | 40 | required: true |
e4f97bab | 41 | } |
e4f97bab | 42 | ] |
3acc5084 C |
43 | })) |
44 | @Scopes(() => ({ | |
1735c825 | 45 | [ ScopeNames.SUMMARY ]: (whereActor?: WhereOptions) => { |
418d092a C |
46 | return { |
47 | attributes: [ 'id', 'name' ], | |
48 | include: [ | |
49 | { | |
57cfff78 | 50 | attributes: [ 'id', 'preferredUsername', 'url', 'serverId', 'avatarId' ], |
418d092a C |
51 | model: ActorModel.unscoped(), |
52 | required: true, | |
53 | where: whereActor, | |
54 | include: [ | |
55 | { | |
56 | attributes: [ 'host' ], | |
57 | model: ServerModel.unscoped(), | |
58 | required: false | |
59 | }, | |
60 | { | |
61 | model: AvatarModel.unscoped(), | |
62 | required: false | |
63 | } | |
64 | ] | |
65 | } | |
66 | ] | |
67 | } | |
68 | } | |
3acc5084 | 69 | })) |
50d6de9c | 70 | @Table({ |
8cd72bd3 C |
71 | tableName: 'account', |
72 | indexes: [ | |
73 | { | |
74 | fields: [ 'actorId' ], | |
75 | unique: true | |
76 | }, | |
77 | { | |
78 | fields: [ 'applicationId' ] | |
79 | }, | |
80 | { | |
81 | fields: [ 'userId' ] | |
82 | } | |
83 | ] | |
50d6de9c | 84 | }) |
fadf619a | 85 | export class AccountModel extends Model<AccountModel> { |
3fd3ab2d | 86 | |
50d6de9c | 87 | @AllowNull(false) |
50d6de9c C |
88 | @Column |
89 | name: string | |
90 | ||
2422c46b C |
91 | @AllowNull(true) |
92 | @Default(null) | |
1735c825 | 93 | @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description', true)) |
241c3357 | 94 | @Column(DataType.STRING(CONSTRAINTS_FIELDS.USERS.DESCRIPTION.max)) |
2422c46b C |
95 | description: string |
96 | ||
3fd3ab2d C |
97 | @CreatedAt |
98 | createdAt: Date | |
99 | ||
100 | @UpdatedAt | |
101 | updatedAt: Date | |
102 | ||
fadf619a | 103 | @ForeignKey(() => ActorModel) |
3fd3ab2d | 104 | @Column |
fadf619a | 105 | actorId: number |
e4f97bab | 106 | |
fadf619a | 107 | @BelongsTo(() => ActorModel, { |
e4f97bab | 108 | foreignKey: { |
fadf619a | 109 | allowNull: false |
e4f97bab C |
110 | }, |
111 | onDelete: 'cascade' | |
112 | }) | |
fadf619a | 113 | Actor: ActorModel |
e4f97bab | 114 | |
3fd3ab2d C |
115 | @ForeignKey(() => UserModel) |
116 | @Column | |
117 | userId: number | |
118 | ||
119 | @BelongsTo(() => UserModel, { | |
e4f97bab | 120 | foreignKey: { |
e4f97bab C |
121 | allowNull: true |
122 | }, | |
123 | onDelete: 'cascade' | |
124 | }) | |
3fd3ab2d C |
125 | User: UserModel |
126 | ||
127 | @ForeignKey(() => ApplicationModel) | |
128 | @Column | |
129 | applicationId: number | |
e4f97bab | 130 | |
3fd3ab2d | 131 | @BelongsTo(() => ApplicationModel, { |
e4f97bab | 132 | foreignKey: { |
e4f97bab C |
133 | allowNull: true |
134 | }, | |
135 | onDelete: 'cascade' | |
136 | }) | |
f05a1c30 | 137 | Application: ApplicationModel |
e4f97bab | 138 | |
3fd3ab2d | 139 | @HasMany(() => VideoChannelModel, { |
e4f97bab | 140 | foreignKey: { |
e4f97bab C |
141 | allowNull: false |
142 | }, | |
143 | onDelete: 'cascade', | |
144 | hooks: true | |
145 | }) | |
3fd3ab2d | 146 | VideoChannels: VideoChannelModel[] |
e4f97bab | 147 | |
418d092a C |
148 | @HasMany(() => VideoPlaylistModel, { |
149 | foreignKey: { | |
150 | allowNull: false | |
151 | }, | |
152 | onDelete: 'cascade', | |
153 | hooks: true | |
154 | }) | |
155 | VideoPlaylists: VideoPlaylistModel[] | |
156 | ||
f05a1c30 C |
157 | @HasMany(() => VideoCommentModel, { |
158 | foreignKey: { | |
159 | allowNull: false | |
160 | }, | |
161 | onDelete: 'cascade', | |
162 | hooks: true | |
163 | }) | |
164 | VideoComments: VideoCommentModel[] | |
165 | ||
166 | @BeforeDestroy | |
167 | static async sendDeleteIfOwned (instance: AccountModel, options) { | |
168 | if (!instance.Actor) { | |
169 | instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel | |
170 | } | |
171 | ||
c5a893d5 | 172 | if (instance.isOwned()) { |
c5a893d5 C |
173 | return sendDeleteActor(instance.Actor, options.transaction) |
174 | } | |
175 | ||
176 | return undefined | |
e4f97bab C |
177 | } |
178 | ||
1735c825 | 179 | static load (id: number, transaction?: Transaction) { |
9b39106d | 180 | return AccountModel.findByPk(id, { transaction }) |
3fd3ab2d | 181 | } |
2295ce6c | 182 | |
92bf2f62 C |
183 | static loadByNameWithHost (nameWithHost: string) { |
184 | const [ accountName, host ] = nameWithHost.split('@') | |
185 | ||
6dd9de95 | 186 | if (!host || host === WEBSERVER.HOST) return AccountModel.loadLocalByName(accountName) |
92bf2f62 C |
187 | |
188 | return AccountModel.loadByNameAndHost(accountName, host) | |
189 | } | |
190 | ||
3fd3ab2d C |
191 | static loadLocalByName (name: string) { |
192 | const query = { | |
193 | where: { | |
1735c825 | 194 | [ Op.or ]: [ |
3fd3ab2d C |
195 | { |
196 | userId: { | |
1735c825 | 197 | [ Op.ne ]: null |
3fd3ab2d C |
198 | } |
199 | }, | |
200 | { | |
201 | applicationId: { | |
1735c825 | 202 | [ Op.ne ]: null |
3fd3ab2d C |
203 | } |
204 | } | |
205 | ] | |
e8cb4409 C |
206 | }, |
207 | include: [ | |
208 | { | |
209 | model: ActorModel, | |
210 | required: true, | |
211 | where: { | |
212 | preferredUsername: name | |
213 | } | |
214 | } | |
215 | ] | |
216 | } | |
217 | ||
218 | return AccountModel.findOne(query) | |
219 | } | |
220 | ||
8a19bee1 | 221 | static loadByNameAndHost (name: string, host: string) { |
e8cb4409 C |
222 | const query = { |
223 | include: [ | |
224 | { | |
225 | model: ActorModel, | |
226 | required: true, | |
227 | where: { | |
228 | preferredUsername: name | |
229 | }, | |
230 | include: [ | |
231 | { | |
232 | model: ServerModel, | |
233 | required: true, | |
234 | where: { | |
235 | host | |
236 | } | |
237 | } | |
238 | ] | |
239 | } | |
240 | ] | |
3fd3ab2d | 241 | } |
7a7724e6 | 242 | |
3fd3ab2d C |
243 | return AccountModel.findOne(query) |
244 | } | |
7a7724e6 | 245 | |
1735c825 | 246 | static loadByUrl (url: string, transaction?: Transaction) { |
3fd3ab2d | 247 | const query = { |
fadf619a C |
248 | include: [ |
249 | { | |
250 | model: ActorModel, | |
251 | required: true, | |
252 | where: { | |
253 | url | |
254 | } | |
255 | } | |
256 | ], | |
3fd3ab2d C |
257 | transaction |
258 | } | |
e4f97bab | 259 | |
3fd3ab2d C |
260 | return AccountModel.findOne(query) |
261 | } | |
e4f97bab | 262 | |
265ba139 C |
263 | static listForApi (start: number, count: number, sort: string) { |
264 | const query = { | |
265 | offset: start, | |
266 | limit: count, | |
6ff9c676 | 267 | order: getSort(sort) |
265ba139 C |
268 | } |
269 | ||
270 | return AccountModel.findAndCountAll(query) | |
c5a893d5 C |
271 | .then(({ rows, count }) => { |
272 | return { | |
273 | data: rows, | |
274 | total: count | |
275 | } | |
276 | }) | |
265ba139 C |
277 | } |
278 | ||
2feebf3e C |
279 | static listLocalsForSitemap (sort: string) { |
280 | const query = { | |
281 | attributes: [ ], | |
282 | offset: 0, | |
283 | order: getSort(sort), | |
284 | include: [ | |
285 | { | |
286 | attributes: [ 'preferredUsername', 'serverId' ], | |
287 | model: ActorModel.unscoped(), | |
288 | where: { | |
289 | serverId: null | |
290 | } | |
291 | } | |
292 | ] | |
293 | } | |
294 | ||
295 | return AccountModel | |
296 | .unscoped() | |
297 | .findAll(query) | |
298 | } | |
299 | ||
c5911fd3 | 300 | toFormattedJSON (): Account { |
fadf619a C |
301 | const actor = this.Actor.toFormattedJSON() |
302 | const account = { | |
3fd3ab2d | 303 | id: this.id, |
244e76a5 | 304 | displayName: this.getDisplayName(), |
2422c46b | 305 | description: this.description, |
3fd3ab2d | 306 | createdAt: this.createdAt, |
79bd2632 C |
307 | updatedAt: this.updatedAt, |
308 | userId: this.userId ? this.userId : undefined | |
3fd3ab2d | 309 | } |
fadf619a C |
310 | |
311 | return Object.assign(actor, account) | |
3fd3ab2d | 312 | } |
e4f97bab | 313 | |
418d092a C |
314 | toFormattedSummaryJSON (): AccountSummary { |
315 | const actor = this.Actor.toFormattedJSON() | |
316 | ||
317 | return { | |
318 | id: this.id, | |
418d092a C |
319 | name: actor.name, |
320 | displayName: this.getDisplayName(), | |
321 | url: actor.url, | |
322 | host: actor.host, | |
323 | avatar: actor.avatar | |
324 | } | |
325 | } | |
326 | ||
3fd3ab2d | 327 | toActivityPubObject () { |
2422c46b C |
328 | const obj = this.Actor.toActivityPubObject(this.name, 'Account') |
329 | ||
330 | return Object.assign(obj, { | |
331 | summary: this.description | |
332 | }) | |
e4f97bab C |
333 | } |
334 | ||
3fd3ab2d | 335 | isOwned () { |
fadf619a | 336 | return this.Actor.isOwned() |
3fd3ab2d | 337 | } |
244e76a5 | 338 | |
744d0eca C |
339 | isOutdated () { |
340 | return this.Actor.isOutdated() | |
341 | } | |
342 | ||
244e76a5 RK |
343 | getDisplayName () { |
344 | return this.name | |
345 | } | |
63c93323 | 346 | } |