]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/account/account.ts
feature: initial syndication feeds support
[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'
f05a1c30 19import { logger } from '../../helpers/logger'
50d6de9c 20import { sendDeleteActor } from '../../lib/activitypub/send'
fadf619a 21import { ActorModel } from '../activitypub/actor'
3fd3ab2d 22import { ApplicationModel } from '../application/application'
265ba139 23import { AvatarModel } from '../avatar/avatar'
3fd3ab2d 24import { ServerModel } from '../server/server'
2422c46b 25import { getSort, throwIfNotValid } from '../utils'
3fd3ab2d 26import { VideoChannelModel } from '../video/video-channel'
f05a1c30 27import { VideoCommentModel } from '../video/video-comment'
3fd3ab2d
C
28import { UserModel } from './user'
29
50d6de9c
C
30@DefaultScope({
31 include: [
3fd3ab2d 32 {
50d6de9c
C
33 model: () => ActorModel,
34 required: true,
35 include: [
36 {
37 model: () => ServerModel,
38 required: false
265ba139
C
39 },
40 {
41 model: () => AvatarModel,
42 required: false
50d6de9c
C
43 }
44 ]
e4f97bab 45 }
e4f97bab 46 ]
3fd3ab2d 47})
50d6de9c
C
48@Table({
49 tableName: 'account'
50})
fadf619a 51export class AccountModel extends Model<AccountModel> {
3fd3ab2d 52
50d6de9c 53 @AllowNull(false)
50d6de9c
C
54 @Column
55 name: string
56
2422c46b
C
57 @AllowNull(true)
58 @Default(null)
59 @Is('AccountDescription', value => throwIfNotValid(value, isAccountDescriptionValid, 'description'))
60 @Column
61 description: string
62
3fd3ab2d
C
63 @CreatedAt
64 createdAt: Date
65
66 @UpdatedAt
67 updatedAt: Date
68
fadf619a 69 @ForeignKey(() => ActorModel)
3fd3ab2d 70 @Column
fadf619a 71 actorId: number
e4f97bab 72
fadf619a 73 @BelongsTo(() => ActorModel, {
e4f97bab 74 foreignKey: {
fadf619a 75 allowNull: false
e4f97bab
C
76 },
77 onDelete: 'cascade'
78 })
fadf619a 79 Actor: ActorModel
e4f97bab 80
3fd3ab2d
C
81 @ForeignKey(() => UserModel)
82 @Column
83 userId: number
84
85 @BelongsTo(() => UserModel, {
e4f97bab 86 foreignKey: {
e4f97bab
C
87 allowNull: true
88 },
89 onDelete: 'cascade'
90 })
3fd3ab2d
C
91 User: UserModel
92
93 @ForeignKey(() => ApplicationModel)
94 @Column
95 applicationId: number
e4f97bab 96
3fd3ab2d 97 @BelongsTo(() => ApplicationModel, {
e4f97bab 98 foreignKey: {
e4f97bab
C
99 allowNull: true
100 },
101 onDelete: 'cascade'
102 })
f05a1c30 103 Application: ApplicationModel
e4f97bab 104
3fd3ab2d 105 @HasMany(() => VideoChannelModel, {
e4f97bab 106 foreignKey: {
e4f97bab
C
107 allowNull: false
108 },
109 onDelete: 'cascade',
110 hooks: true
111 })
3fd3ab2d 112 VideoChannels: VideoChannelModel[]
e4f97bab 113
f05a1c30
C
114 @HasMany(() => VideoCommentModel, {
115 foreignKey: {
116 allowNull: false
117 },
118 onDelete: 'cascade',
119 hooks: true
120 })
121 VideoComments: VideoCommentModel[]
122
123 @BeforeDestroy
124 static async sendDeleteIfOwned (instance: AccountModel, options) {
125 if (!instance.Actor) {
126 instance.Actor = await instance.$get('Actor', { transaction: options.transaction }) as ActorModel
127 }
128
3fd3ab2d 129 if (instance.isOwned()) {
f05a1c30
C
130 logger.debug('Sending delete of actor of account %s.', instance.Actor.url)
131 return sendDeleteActor(instance.Actor, options.transaction)
3fd3ab2d 132 }
e4f97bab 133
3fd3ab2d 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
187 static loadLocalByNameAndHost (name: string, host: string) {
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)
237 .then(({ rows, count }) => {
238 return {
239 data: rows,
240 total: count
241 }
242 })
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}