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