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