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