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