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