]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/activitypub/actor.ts
Don't show videos of remote instance after unfollow
[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 },
f05a1c30
C
158 onDelete: 'set null',
159 hooks: true
fadf619a
C
160 })
161 Avatar: AvatarModel
162
50d6de9c 163 @HasMany(() => ActorFollowModel, {
fadf619a 164 foreignKey: {
50d6de9c 165 name: 'actorId',
fadf619a
C
166 allowNull: false
167 },
168 onDelete: 'cascade'
169 })
50d6de9c 170 AccountFollowing: ActorFollowModel[]
fadf619a 171
50d6de9c 172 @HasMany(() => ActorFollowModel, {
fadf619a 173 foreignKey: {
50d6de9c 174 name: 'targetActorId',
fadf619a
C
175 allowNull: false
176 },
177 as: 'followers',
178 onDelete: 'cascade'
179 })
50d6de9c 180 AccountFollowers: ActorFollowModel[]
fadf619a
C
181
182 @ForeignKey(() => ServerModel)
183 @Column
184 serverId: number
185
186 @BelongsTo(() => ServerModel, {
187 foreignKey: {
188 allowNull: true
189 },
190 onDelete: 'cascade'
191 })
192 Server: ServerModel
193
50d6de9c
C
194 @HasOne(() => AccountModel, {
195 foreignKey: {
196 allowNull: true
197 },
f05a1c30
C
198 onDelete: 'cascade',
199 hooks: true
50d6de9c
C
200 })
201 Account: AccountModel
202
203 @HasOne(() => VideoChannelModel, {
204 foreignKey: {
205 allowNull: true
206 },
f05a1c30
C
207 onDelete: 'cascade',
208 hooks: true
50d6de9c
C
209 })
210 VideoChannel: VideoChannelModel
211
212 static load (id: number) {
60650c77 213 return ActorModel.unscoped().findById(id)
50d6de9c
C
214 }
215
fadf619a
C
216 static listByFollowersUrls (followersUrls: string[], transaction?: Sequelize.Transaction) {
217 const query = {
218 where: {
219 followersUrl: {
220 [ Sequelize.Op.in ]: followersUrls
221 }
222 },
223 transaction
224 }
225
50d6de9c
C
226 return ActorModel.scope(ScopeNames.FULL).findAll(query)
227 }
228
e12a0092 229 static loadLocalByName (preferredUsername: string) {
50d6de9c
C
230 const query = {
231 where: {
e12a0092 232 preferredUsername,
50d6de9c
C
233 serverId: null
234 }
235 }
236
237 return ActorModel.scope(ScopeNames.FULL).findOne(query)
238 }
239
e12a0092 240 static loadByNameAndHost (preferredUsername: string, host: string) {
50d6de9c
C
241 const query = {
242 where: {
e12a0092 243 preferredUsername
50d6de9c
C
244 },
245 include: [
246 {
247 model: ServerModel,
248 required: true,
249 where: {
250 host
251 }
252 }
253 ]
254 }
255
256 return ActorModel.scope(ScopeNames.FULL).findOne(query)
257 }
258
259 static loadByUrl (url: string, transaction?: Sequelize.Transaction) {
260 const query = {
261 where: {
262 url
263 },
264 transaction
265 }
266
267 return ActorModel.scope(ScopeNames.FULL).findOne(query)
fadf619a
C
268 }
269
32b2b43c
C
270 static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) {
271 // FIXME: typings
272 return (ActorModel as any).increment(column, {
273 by,
274 where: {
275 id
276 }
277 })
278 }
279
fadf619a
C
280 toFormattedJSON () {
281 let avatar: Avatar = null
282 if (this.Avatar) {
c5911fd3 283 avatar = this.Avatar.toFormattedJSON()
fadf619a
C
284 }
285
fadf619a
C
286 return {
287 id: this.id,
4cb6d457 288 url: this.url,
50d6de9c 289 uuid: this.uuid,
60650c77 290 name: this.preferredUsername,
e12a0092 291 host: this.getHost(),
fadf619a
C
292 followingCount: this.followingCount,
293 followersCount: this.followersCount,
60650c77
C
294 avatar,
295 createdAt: this.createdAt,
296 updatedAt: this.updatedAt
fadf619a
C
297 }
298 }
299
e12a0092 300 toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') {
fadf619a
C
301 let activityPubType
302 if (type === 'Account') {
50d6de9c
C
303 activityPubType = 'Person' as 'Person'
304 } else if (type === 'Application') {
305 activityPubType = 'Application' as 'Application'
fadf619a 306 } else { // VideoChannel
50d6de9c 307 activityPubType = 'Group' as 'Group'
fadf619a
C
308 }
309
c5911fd3
C
310 let icon = undefined
311 if (this.avatarId) {
312 const extension = extname(this.Avatar.filename)
313 icon = {
314 type: 'Image',
315 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
316 url: this.getAvatarUrl()
317 }
318 }
319
fadf619a 320 const json = {
50d6de9c 321 type: activityPubType,
fadf619a
C
322 id: this.url,
323 following: this.getFollowingUrl(),
324 followers: this.getFollowersUrl(),
325 inbox: this.inboxUrl,
326 outbox: this.outboxUrl,
e12a0092 327 preferredUsername: this.preferredUsername,
fadf619a 328 url: this.url,
e12a0092 329 name,
fadf619a
C
330 endpoints: {
331 sharedInbox: this.sharedInboxUrl
332 },
50d6de9c 333 uuid: this.uuid,
fadf619a
C
334 publicKey: {
335 id: this.getPublicKeyUrl(),
336 owner: this.url,
337 publicKeyPem: this.publicKey
c5911fd3
C
338 },
339 icon
fadf619a
C
340 }
341
342 return activityPubContextify(json)
343 }
344
345 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
346 const query = {
347 attributes: [ 'sharedInboxUrl' ],
348 include: [
349 {
50d6de9c 350 model: ActorFollowModel,
fadf619a
C
351 required: true,
352 as: 'followers',
353 where: {
50d6de9c 354 targetActorId: this.id
fadf619a
C
355 }
356 }
357 ],
358 transaction: t
359 }
360
361 return ActorModel.findAll(query)
362 .then(accounts => accounts.map(a => a.sharedInboxUrl))
363 }
364
365 getFollowingUrl () {
366 return this.url + '/following'
367 }
368
369 getFollowersUrl () {
370 return this.url + '/followers'
371 }
372
373 getPublicKeyUrl () {
374 return this.url + '#main-key'
375 }
376
377 isOwned () {
378 return this.serverId === null
379 }
e12a0092
C
380
381 getWebfingerUrl () {
382 return 'acct:' + this.preferredUsername + '@' + this.getHost()
383 }
384
385 getHost () {
386 return this.Server ? this.Server.host : CONFIG.WEBSERVER.HOST
387 }
c5911fd3
C
388
389 getAvatarUrl () {
390 if (!this.avatarId) return undefined
391
265ba139 392 return CONFIG.WEBSERVER.URL + this.Avatar.getWebserverPath()
c5911fd3 393 }
a5625b41
C
394
395 isOutdated () {
396 if (this.isOwned()) return false
397
398 const now = Date.now()
399 const createdAtTime = this.createdAt.getTime()
400 const updatedAtTime = this.updatedAt.getTime()
401
402 return (now - createdAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL &&
403 (now - updatedAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL
404 }
fadf619a 405}