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