]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/account/account.ts
Fix sort inconsistency
[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: {
160 name,
161 [ Sequelize.Op.or ]: [
162 {
163 userId: {
164 [ Sequelize.Op.ne ]: null
165 }
166 },
167 {
168 applicationId: {
169 [ Sequelize.Op.ne ]: null
170 }
171 }
172 ]
173 }
174 }
7a7724e6 175
3fd3ab2d
C
176 return AccountModel.findOne(query)
177 }
7a7724e6 178
3fd3ab2d
C
179 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
180 const query = {
fadf619a
C
181 include: [
182 {
183 model: ActorModel,
184 required: true,
185 where: {
186 url
187 }
188 }
189 ],
3fd3ab2d
C
190 transaction
191 }
e4f97bab 192
3fd3ab2d
C
193 return AccountModel.findOne(query)
194 }
e4f97bab 195
265ba139
C
196 static listForApi (start: number, count: number, sort: string) {
197 const query = {
198 offset: start,
199 limit: count,
3bb6c526 200 order: getSort(sort),
265ba139
C
201 }
202
203 return AccountModel.findAndCountAll(query)
204 .then(({ rows, count }) => {
205 return {
206 data: rows,
207 total: count
208 }
209 })
210 }
211
c5911fd3 212 toFormattedJSON (): Account {
fadf619a
C
213 const actor = this.Actor.toFormattedJSON()
214 const account = {
3fd3ab2d 215 id: this.id,
c5911fd3 216 displayName: this.name,
2422c46b 217 description: this.description,
3fd3ab2d 218 createdAt: this.createdAt,
fadf619a 219 updatedAt: this.updatedAt
3fd3ab2d 220 }
fadf619a
C
221
222 return Object.assign(actor, account)
3fd3ab2d 223 }
e4f97bab 224
3fd3ab2d 225 toActivityPubObject () {
2422c46b
C
226 const obj = this.Actor.toActivityPubObject(this.name, 'Account')
227
228 return Object.assign(obj, {
229 summary: this.description
230 })
e4f97bab
C
231 }
232
3fd3ab2d 233 isOwned () {
fadf619a 234 return this.Actor.isOwned()
3fd3ab2d 235 }
63c93323 236}