]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/account/account.ts
Fix tests
[github/Chocobozzz/PeerTube.git] / server / models / account / account.ts
CommitLineData
e4f97bab 1import * as Sequelize from 'sequelize'
3fd3ab2d
C
2import {
3 AfterDestroy,
4 AllowNull,
5 BelongsTo,
6 Column,
7 CreatedAt,
50d6de9c 8 DefaultScope,
3fd3ab2d
C
9 ForeignKey,
10 HasMany,
11 Is,
3fd3ab2d
C
12 Model,
13 Table,
14 UpdatedAt
15} from 'sequelize-typescript'
3fd3ab2d 16import { isUserUsernameValid } from '../../helpers/custom-validators/users'
50d6de9c 17import { sendDeleteActor } from '../../lib/activitypub/send'
fadf619a 18import { ActorModel } from '../activitypub/actor'
3fd3ab2d 19import { ApplicationModel } from '../application/application'
3fd3ab2d
C
20import { ServerModel } from '../server/server'
21import { throwIfNotValid } from '../utils'
22import { VideoChannelModel } from '../video/video-channel'
3fd3ab2d
C
23import { UserModel } from './user'
24
50d6de9c
C
25@DefaultScope({
26 include: [
3fd3ab2d 27 {
50d6de9c
C
28 model: () => ActorModel,
29 required: true,
30 include: [
31 {
32 model: () => ServerModel,
33 required: false
34 }
35 ]
e4f97bab 36 }
e4f97bab 37 ]
3fd3ab2d 38})
50d6de9c
C
39@Table({
40 tableName: 'account'
41})
fadf619a 42export class AccountModel extends Model<AccountModel> {
3fd3ab2d 43
50d6de9c
C
44 @AllowNull(false)
45 @Is('AccountName', value => throwIfNotValid(value, isUserUsernameValid, 'account name'))
46 @Column
47 name: string
48
3fd3ab2d
C
49 @CreatedAt
50 createdAt: Date
51
52 @UpdatedAt
53 updatedAt: Date
54
fadf619a 55 @ForeignKey(() => ActorModel)
3fd3ab2d 56 @Column
fadf619a 57 actorId: number
e4f97bab 58
fadf619a 59 @BelongsTo(() => ActorModel, {
e4f97bab 60 foreignKey: {
fadf619a 61 allowNull: false
e4f97bab
C
62 },
63 onDelete: 'cascade'
64 })
fadf619a 65 Actor: ActorModel
e4f97bab 66
3fd3ab2d
C
67 @ForeignKey(() => UserModel)
68 @Column
69 userId: number
70
71 @BelongsTo(() => UserModel, {
e4f97bab 72 foreignKey: {
e4f97bab
C
73 allowNull: true
74 },
75 onDelete: 'cascade'
76 })
3fd3ab2d
C
77 User: UserModel
78
79 @ForeignKey(() => ApplicationModel)
80 @Column
81 applicationId: number
e4f97bab 82
3fd3ab2d 83 @BelongsTo(() => ApplicationModel, {
e4f97bab 84 foreignKey: {
e4f97bab
C
85 allowNull: true
86 },
87 onDelete: 'cascade'
88 })
50d6de9c 89 Account: ApplicationModel
e4f97bab 90
3fd3ab2d 91 @HasMany(() => VideoChannelModel, {
e4f97bab 92 foreignKey: {
e4f97bab
C
93 allowNull: false
94 },
95 onDelete: 'cascade',
96 hooks: true
97 })
3fd3ab2d 98 VideoChannels: VideoChannelModel[]
e4f97bab 99
3fd3ab2d
C
100 @AfterDestroy
101 static sendDeleteIfOwned (instance: AccountModel) {
102 if (instance.isOwned()) {
50d6de9c 103 return sendDeleteActor(instance.Actor, undefined)
3fd3ab2d 104 }
e4f97bab 105
3fd3ab2d 106 return undefined
e4f97bab
C
107 }
108
3fd3ab2d
C
109 static load (id: number) {
110 return AccountModel.findById(id)
111 }
2295ce6c 112
3fd3ab2d
C
113 static loadByUUID (uuid: string) {
114 const query = {
50d6de9c
C
115 include: [
116 {
117 model: ActorModel,
118 required: true,
119 where: {
120 uuid
121 }
122 }
123 ]
2295ce6c 124 }
60862425 125
3fd3ab2d 126 return AccountModel.findOne(query)
60862425 127 }
51548b31 128
3fd3ab2d
C
129 static loadLocalByName (name: string) {
130 const query = {
131 where: {
132 name,
133 [ Sequelize.Op.or ]: [
134 {
135 userId: {
136 [ Sequelize.Op.ne ]: null
137 }
138 },
139 {
140 applicationId: {
141 [ Sequelize.Op.ne ]: null
142 }
143 }
144 ]
145 }
146 }
7a7724e6 147
3fd3ab2d
C
148 return AccountModel.findOne(query)
149 }
7a7724e6 150
3fd3ab2d
C
151 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
152 const query = {
fadf619a
C
153 include: [
154 {
155 model: ActorModel,
156 required: true,
157 where: {
158 url
159 }
160 }
161 ],
3fd3ab2d
C
162 transaction
163 }
e4f97bab 164
3fd3ab2d
C
165 return AccountModel.findOne(query)
166 }
e4f97bab 167
3fd3ab2d 168 toFormattedJSON () {
fadf619a
C
169 const actor = this.Actor.toFormattedJSON()
170 const account = {
3fd3ab2d 171 id: this.id,
50d6de9c 172 name: this.name,
3fd3ab2d 173 createdAt: this.createdAt,
fadf619a 174 updatedAt: this.updatedAt
3fd3ab2d 175 }
fadf619a
C
176
177 return Object.assign(actor, account)
3fd3ab2d 178 }
e4f97bab 179
3fd3ab2d 180 toActivityPubObject () {
50d6de9c 181 return this.Actor.toActivityPubObject(this.name, 'Account')
e4f97bab
C
182 }
183
3fd3ab2d 184 isOwned () {
fadf619a 185 return this.Actor.isOwned()
3fd3ab2d 186 }
63c93323 187}