]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/activitypub/actor.ts
Fix tests
[github/Chocobozzz/PeerTube.git] / server / models / activitypub / actor.ts
CommitLineData
50d6de9c 1import { values } from 'lodash'
fadf619a
C
2import { join } from 'path'
3import * as Sequelize from 'sequelize'
4import {
e12a0092
C
5 AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, DefaultScope, ForeignKey, HasMany, HasOne, Is, IsUUID, Model, Scopes,
6 Table, UpdatedAt
fadf619a 7} from 'sequelize-typescript'
50d6de9c 8import { ActivityPubActorType } from '../../../shared/models/activitypub'
fadf619a 9import { Avatar } from '../../../shared/models/avatars/avatar.model'
da854ddd 10import { activityPubContextify } from '../../helpers/activitypub'
fadf619a 11import {
da854ddd
C
12 isActorFollowersCountValid, isActorFollowingCountValid, isActorPreferredUsernameValid, isActorPrivateKeyValid,
13 isActorPublicKeyValid
14} from '../../helpers/custom-validators/activitypub/actor'
15import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
50d6de9c
C
16import { ACTIVITY_PUB_ACTOR_TYPES, AVATARS_DIR, CONFIG, CONSTRAINTS_FIELDS } from '../../initializers'
17import { AccountModel } from '../account/account'
fadf619a
C
18import { AvatarModel } from '../avatar/avatar'
19import { ServerModel } from '../server/server'
20import { throwIfNotValid } from '../utils'
50d6de9c
C
21import { VideoChannelModel } from '../video/video-channel'
22import { ActorFollowModel } from './actor-follow'
fadf619a 23
50d6de9c
C
24enum ScopeNames {
25 FULL = 'FULL'
26}
27
ce33ee01
C
28@DefaultScope({
29 include: [
30 {
31 model: () => ServerModel,
32 required: false
33 }
34 ]
35})
50d6de9c
C
36@Scopes({
37 [ScopeNames.FULL]: {
38 include: [
39 {
40 model: () => AccountModel,
41 required: false
42 },
43 {
44 model: () => VideoChannelModel,
45 required: false
ce33ee01
C
46 },
47 {
48 model: () => ServerModel,
49 required: false
50d6de9c
C
50 }
51 ]
52 }
53})
fadf619a 54@Table({
50d6de9c
C
55 tableName: 'actor',
56 indexes: [
57 {
e12a0092 58 fields: [ 'preferredUsername', 'serverId' ],
50d6de9c
C
59 unique: true
60 }
61 ]
fadf619a
C
62})
63export class ActorModel extends Model<ActorModel> {
64
50d6de9c
C
65 @AllowNull(false)
66 @Column(DataType.ENUM(values(ACTIVITY_PUB_ACTOR_TYPES)))
67 type: ActivityPubActorType
68
fadf619a
C
69 @AllowNull(false)
70 @Default(DataType.UUIDV4)
71 @IsUUID(4)
72 @Column(DataType.UUID)
73 uuid: string
74
75 @AllowNull(false)
e12a0092 76 @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username'))
fadf619a 77 @Column
e12a0092 78 preferredUsername: string
fadf619a
C
79
80 @AllowNull(false)
81 @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
82 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max))
83 url: string
84
85 @AllowNull(true)
86 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key'))
87 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.PUBLIC_KEY.max))
88 publicKey: string
89
90 @AllowNull(true)
91 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key'))
92 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.PRIVATE_KEY.max))
93 privateKey: string
94
95 @AllowNull(false)
96 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowersCountValid, 'followers count'))
97 @Column
98 followersCount: number
99
100 @AllowNull(false)
101 @Is('ActorFollowersCount', value => throwIfNotValid(value, isActorFollowingCountValid, 'following count'))
102 @Column
103 followingCount: number
104
105 @AllowNull(false)
106 @Is('ActorInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'inbox url'))
107 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max))
108 inboxUrl: string
109
110 @AllowNull(false)
111 @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url'))
112 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max))
113 outboxUrl: string
114
115 @AllowNull(false)
116 @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url'))
117 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max))
118 sharedInboxUrl: string
119
120 @AllowNull(false)
121 @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url'))
122 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max))
123 followersUrl: string
124
125 @AllowNull(false)
126 @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url'))
127 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTOR.URL.max))
128 followingUrl: string
129
130 @CreatedAt
131 createdAt: Date
132
133 @UpdatedAt
134 updatedAt: Date
135
136 @ForeignKey(() => AvatarModel)
137 @Column
138 avatarId: number
139
140 @BelongsTo(() => AvatarModel, {
141 foreignKey: {
142 allowNull: true
143 },
144 onDelete: 'cascade'
145 })
146 Avatar: AvatarModel
147
50d6de9c 148 @HasMany(() => ActorFollowModel, {
fadf619a 149 foreignKey: {
50d6de9c 150 name: 'actorId',
fadf619a
C
151 allowNull: false
152 },
153 onDelete: 'cascade'
154 })
50d6de9c 155 AccountFollowing: ActorFollowModel[]
fadf619a 156
50d6de9c 157 @HasMany(() => ActorFollowModel, {
fadf619a 158 foreignKey: {
50d6de9c 159 name: 'targetActorId',
fadf619a
C
160 allowNull: false
161 },
162 as: 'followers',
163 onDelete: 'cascade'
164 })
50d6de9c 165 AccountFollowers: ActorFollowModel[]
fadf619a
C
166
167 @ForeignKey(() => ServerModel)
168 @Column
169 serverId: number
170
171 @BelongsTo(() => ServerModel, {
172 foreignKey: {
173 allowNull: true
174 },
175 onDelete: 'cascade'
176 })
177 Server: ServerModel
178
50d6de9c
C
179 @HasOne(() => AccountModel, {
180 foreignKey: {
181 allowNull: true
182 },
183 onDelete: 'cascade'
184 })
185 Account: AccountModel
186
187 @HasOne(() => VideoChannelModel, {
188 foreignKey: {
189 allowNull: true
190 },
191 onDelete: 'cascade'
192 })
193 VideoChannel: VideoChannelModel
194
195 static load (id: number) {
196 return ActorModel.scope(ScopeNames.FULL).findById(id)
197 }
198
fadf619a
C
199 static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) {
200 const query = {
201 where: {
202 followersUrl: {
203 [ Sequelize.Op.in ]: followersUrls
204 }
205 },
206 transaction
207 }
208
50d6de9c
C
209 return ActorModel.scope(ScopeNames.FULL).findAll(query)
210 }
211
e12a0092 212 static loadLocalByName (preferredUsername: string) {
50d6de9c
C
213 const query = {
214 where: {
e12a0092 215 preferredUsername,
50d6de9c
C
216 serverId: null
217 }
218 }
219
220 return ActorModel.scope(ScopeNames.FULL).findOne(query)
221 }
222
e12a0092 223 static loadByNameAndHost (preferredUsername: string, host: string) {
50d6de9c
C
224 const query = {
225 where: {
e12a0092 226 preferredUsername
50d6de9c
C
227 },
228 include: [
229 {
230 model: ServerModel,
231 required: true,
232 where: {
233 host
234 }
235 }
236 ]
237 }
238
239 return ActorModel.scope(ScopeNames.FULL).findOne(query)
240 }
241
242 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
243 const query = {
244 where: {
245 url
246 },
247 transaction
248 }
249
250 return ActorModel.scope(ScopeNames.FULL).findOne(query)
fadf619a
C
251 }
252
253 toFormattedJSON () {
254 let avatar: Avatar = null
255 if (this.Avatar) {
256 avatar = {
257 path: join(AVATARS_DIR.ACCOUNT, this.Avatar.filename),
258 createdAt: this.Avatar.createdAt,
259 updatedAt: this.Avatar.updatedAt
260 }
261 }
262
fadf619a
C
263 let score: number
264 if (this.Server) {
fadf619a
C
265 score = this.Server.score
266 }
267
268 return {
269 id: this.id,
50d6de9c 270 uuid: this.uuid,
e12a0092 271 host: this.getHost(),
fadf619a
C
272 score,
273 followingCount: this.followingCount,
274 followersCount: this.followersCount,
275 avatar
276 }
277 }
278
e12a0092 279 toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') {
fadf619a
C
280 let activityPubType
281 if (type === 'Account') {
50d6de9c
C
282 activityPubType = 'Person' as 'Person'
283 } else if (type === 'Application') {
284 activityPubType = 'Application' as 'Application'
fadf619a 285 } else { // VideoChannel
50d6de9c 286 activityPubType = 'Group' as 'Group'
fadf619a
C
287 }
288
289 const json = {
50d6de9c 290 type: activityPubType,
fadf619a
C
291 id: this.url,
292 following: this.getFollowingUrl(),
293 followers: this.getFollowersUrl(),
294 inbox: this.inboxUrl,
295 outbox: this.outboxUrl,
e12a0092 296 preferredUsername: this.preferredUsername,
fadf619a 297 url: this.url,
e12a0092 298 name,
fadf619a
C
299 endpoints: {
300 sharedInbox: this.sharedInboxUrl
301 },
50d6de9c 302 uuid: this.uuid,
fadf619a
C
303 publicKey: {
304 id: this.getPublicKeyUrl(),
305 owner: this.url,
306 publicKeyPem: this.publicKey
307 }
308 }
309
310 return activityPubContextify(json)
311 }
312
313 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
314 const query = {
315 attributes: [ 'sharedInboxUrl' ],
316 include: [
317 {
50d6de9c 318 model: ActorFollowModel,
fadf619a
C
319 required: true,
320 as: 'followers',
321 where: {
50d6de9c 322 targetActorId: this.id
fadf619a
C
323 }
324 }
325 ],
326 transaction: t
327 }
328
329 return ActorModel.findAll(query)
330 .then(accounts => accounts.map(a => a.sharedInboxUrl))
331 }
332
333 getFollowingUrl () {
334 return this.url + '/following'
335 }
336
337 getFollowersUrl () {
338 return this.url + '/followers'
339 }
340
341 getPublicKeyUrl () {
342 return this.url + '#main-key'
343 }
344
345 isOwned () {
346 return this.serverId === null
347 }
e12a0092
C
348
349 getWebfingerUrl () {
350 return 'acct:' + this.preferredUsername + '@' + this.getHost()
351 }
352
353 getHost () {
354 return this.Server ? this.Server.host : CONFIG.WEBSERVER.HOST
355 }
fadf619a 356}