]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/account/account.ts
rename manifest
[github/Chocobozzz/PeerTube.git] / server / models / account / account.ts
CommitLineData
e4f97bab 1import * as Sequelize from 'sequelize'
3fd3ab2d 2import {
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 17import { Account } from '../../../shared/models/actors'
2422c46b 18import { isAccountDescriptionValid } from '../../helpers/custom-validators/accounts'
c5a893d5 19import { sendDeleteActor } from '../../lib/activitypub/send'
fadf619a 20import { ActorModel } from '../activitypub/actor'
3fd3ab2d 21import { ApplicationModel } from '../application/application'
265ba139 22import { AvatarModel } from '../avatar/avatar'
3fd3ab2d 23import { ServerModel } from '../server/server'
2422c46b 24import { getSort, throwIfNotValid } from '../utils'
3fd3ab2d 25import { VideoChannelModel } from '../video/video-channel'
f05a1c30 26import { VideoCommentModel } from '../video/video-comment'
3fd3ab2d
C
27import { UserModel } from './user'
28
50d6de9c
C
29@DefaultScope({
30 include: [
3fd3ab2d 31 {
f37dc0dd
C
32 model: () => ActorModel, // Default scope includes avatar and server
33 required: true
e4f97bab 34 }
e4f97bab 35 ]
3fd3ab2d 36})
50d6de9c 37@Table({
8cd72bd3
C
38 tableName: 'account',
39 indexes: [
40 {
41 fields: [ 'actorId' ],
42 unique: true
43 },
44 {
45 fields: [ 'applicationId' ]
46 },
47 {
48 fields: [ 'userId' ]
49 }
50 ]
50d6de9c 51})
fadf619a 52export class AccountModel extends Model<AccountModel> {
3fd3ab2d 53
50d6de9c 54 @AllowNull(false)
50d6de9c
C
55 @Column
56 name: string
57
2422c46b
C
58 @AllowNull(true)
59 @Default(null)
60 @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description'))
61 @Column
62 description: string
63
3fd3ab2d
C
64 @CreatedAt
65 createdAt: Date
66
67 @UpdatedAt
68 updatedAt: Date
69
fadf619a 70 @ForeignKey(() => ActorModel)
3fd3ab2d 71 @Column
fadf619a 72 actorId: number
e4f97bab 73
fadf619a 74 @BelongsTo(() => ActorModel, {
e4f97bab 75 foreignKey: {
fadf619a 76 allowNull: false
e4f97bab
C
77 },
78 onDelete: 'cascade'
79 })
fadf619a 80 Actor: ActorModel
e4f97bab 81
3fd3ab2d
C
82 @ForeignKey(() => UserModel)
83 @Column
84 userId: number
85
86 @BelongsTo(() => UserModel, {
e4f97bab 87 foreignKey: {
e4f97bab
C
88 allowNull: true
89 },
90 onDelete: 'cascade'
91 })
3fd3ab2d
C
92 User: UserModel
93
94 @ForeignKey(() => ApplicationModel)
95 @Column
96 applicationId: number
e4f97bab 97
3fd3ab2d 98 @BelongsTo(() => ApplicationModel, {
e4f97bab 99 foreignKey: {
e4f97bab
C
100 allowNull: true
101 },
102 onDelete: 'cascade'
103 })
f05a1c30 104 Application: ApplicationModel
e4f97bab 105
3fd3ab2d 106 @HasMany(() => VideoChannelModel, {
e4f97bab 107 foreignKey: {
e4f97bab
C
108 allowNull: false
109 },
110 onDelete: 'cascade',
111 hooks: true
112 })
3fd3ab2d 113 VideoChannels: VideoChannelModel[]
e4f97bab 114
f05a1c30
C
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
c5a893d5 130 if (instance.isOwned()) {
c5a893d5
C
131 return sendDeleteActor(instance.Actor, options.transaction)
132 }
133
134 return undefined
e4f97bab
C
135 }
136
3fd3ab2d
C
137 static load (id: number) {
138 return AccountModel.findById(id)
139 }
2295ce6c 140
3fd3ab2d
C
141 static loadByUUID (uuid: string) {
142 const query = {
50d6de9c
C
143 include: [
144 {
145 model: ActorModel,
146 required: true,
147 where: {
148 uuid
149 }
150 }
151 ]
2295ce6c 152 }
60862425 153
3fd3ab2d 154 return AccountModel.findOne(query)
60862425 155 }
51548b31 156
3fd3ab2d
C
157 static loadLocalByName (name: string) {
158 const query = {
159 where: {
3fd3ab2d
C
160 [ Sequelize.Op.or ]: [
161 {
162 userId: {
163 [ Sequelize.Op.ne ]: null
164 }
165 },
166 {
167 applicationId: {
168 [ Sequelize.Op.ne ]: null
169 }
170 }
171 ]
e8cb4409
C
172 },
173 include: [
174 {
175 model: ActorModel,
176 required: true,
177 where: {
178 preferredUsername: name
179 }
180 }
181 ]
182 }
183
184 return AccountModel.findOne(query)
185 }
186
8a19bee1 187 static loadByNameAndHost (name: string, host: string) {
e8cb4409
C
188 const query = {
189 include: [
190 {
191 model: ActorModel,
192 required: true,
193 where: {
194 preferredUsername: name
195 },
196 include: [
197 {
198 model: ServerModel,
199 required: true,
200 where: {
201 host
202 }
203 }
204 ]
205 }
206 ]
3fd3ab2d 207 }
7a7724e6 208
3fd3ab2d
C
209 return AccountModel.findOne(query)
210 }
7a7724e6 211
3fd3ab2d
C
212 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
213 const query = {
fadf619a
C
214 include: [
215 {
216 model: ActorModel,
217 required: true,
218 where: {
219 url
220 }
221 }
222 ],
3fd3ab2d
C
223 transaction
224 }
e4f97bab 225
3fd3ab2d
C
226 return AccountModel.findOne(query)
227 }
e4f97bab 228
265ba139
C
229 static listForApi (start: number, count: number, sort: string) {
230 const query = {
231 offset: start,
232 limit: count,
6ff9c676 233 order: getSort(sort)
265ba139
C
234 }
235
236 return AccountModel.findAndCountAll(query)
c5a893d5
C
237 .then(({ rows, count }) => {
238 return {
239 data: rows,
240 total: count
241 }
242 })
265ba139
C
243 }
244
c5911fd3 245 toFormattedJSON (): Account {
fadf619a
C
246 const actor = this.Actor.toFormattedJSON()
247 const account = {
3fd3ab2d 248 id: this.id,
244e76a5 249 displayName: this.getDisplayName(),
2422c46b 250 description: this.description,
3fd3ab2d 251 createdAt: this.createdAt,
fadf619a 252 updatedAt: this.updatedAt
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}