]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/account/account.ts
4930681279e1424066c8910b7ac64fd5052b11fb
[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 @Is('AccountName', value => throwIfNotValid(value, isUserUsernameValid, 'account name'))
52 @Column
53 name: string
54
55 @CreatedAt
56 createdAt: Date
57
58 @UpdatedAt
59 updatedAt: Date
60
61 @ForeignKey(() => ActorModel)
62 @Column
63 actorId: number
64
65 @BelongsTo(() => ActorModel, {
66 foreignKey: {
67 allowNull: false
68 },
69 onDelete: 'cascade'
70 })
71 Actor: ActorModel
72
73 @ForeignKey(() => UserModel)
74 @Column
75 userId: number
76
77 @BelongsTo(() => UserModel, {
78 foreignKey: {
79 allowNull: true
80 },
81 onDelete: 'cascade'
82 })
83 User: UserModel
84
85 @ForeignKey(() => ApplicationModel)
86 @Column
87 applicationId: number
88
89 @BelongsTo(() => ApplicationModel, {
90 foreignKey: {
91 allowNull: true
92 },
93 onDelete: 'cascade'
94 })
95 Account: ApplicationModel
96
97 @HasMany(() => VideoChannelModel, {
98 foreignKey: {
99 allowNull: false
100 },
101 onDelete: 'cascade',
102 hooks: true
103 })
104 VideoChannels: VideoChannelModel[]
105
106 @AfterDestroy
107 static sendDeleteIfOwned (instance: AccountModel) {
108 if (instance.isOwned()) {
109 return sendDeleteActor(instance.Actor, undefined)
110 }
111
112 return undefined
113 }
114
115 static load (id: number) {
116 return AccountModel.findById(id)
117 }
118
119 static loadByUUID (uuid: string) {
120 const query = {
121 include: [
122 {
123 model: ActorModel,
124 required: true,
125 where: {
126 uuid
127 }
128 }
129 ]
130 }
131
132 return AccountModel.findOne(query)
133 }
134
135 static loadLocalByName (name: string) {
136 const query = {
137 where: {
138 name,
139 [ Sequelize.Op.or ]: [
140 {
141 userId: {
142 [ Sequelize.Op.ne ]: null
143 }
144 },
145 {
146 applicationId: {
147 [ Sequelize.Op.ne ]: null
148 }
149 }
150 ]
151 }
152 }
153
154 return AccountModel.findOne(query)
155 }
156
157 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
158 const query = {
159 include: [
160 {
161 model: ActorModel,
162 required: true,
163 where: {
164 url
165 }
166 }
167 ],
168 transaction
169 }
170
171 return AccountModel.findOne(query)
172 }
173
174 static listForApi (start: number, count: number, sort: string) {
175 const query = {
176 offset: start,
177 limit: count,
178 order: [ getSort(sort) ]
179 }
180
181 return AccountModel.findAndCountAll(query)
182 .then(({ rows, count }) => {
183 return {
184 data: rows,
185 total: count
186 }
187 })
188 }
189
190 toFormattedJSON (): Account {
191 const actor = this.Actor.toFormattedJSON()
192 const account = {
193 id: this.id,
194 name: this.Actor.preferredUsername,
195 displayName: this.name,
196 createdAt: this.createdAt,
197 updatedAt: this.updatedAt
198 }
199
200 return Object.assign(actor, account)
201 }
202
203 toActivityPubObject () {
204 return this.Actor.toActivityPubObject(this.name, 'Account')
205 }
206
207 isOwned () {
208 return this.Actor.isOwned()
209 }
210 }