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