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