]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/activitypub/actor.ts
Fix actor followers/following counts
[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
32b2b43c
C
267 static incrementFollows (id: number, column: 'followersCount' | 'followingCount', by: number) {
268 // FIXME: typings
269 return (ActorModel as any).increment(column, {
270 by,
271 where: {
272 id
273 }
274 })
275 }
276
fadf619a
C
277 toFormattedJSON () {
278 let avatar: Avatar = null
279 if (this.Avatar) {
c5911fd3 280 avatar = this.Avatar.toFormattedJSON()
fadf619a
C
281 }
282
fadf619a
C
283 return {
284 id: this.id,
4cb6d457 285 url: this.url,
50d6de9c 286 uuid: this.uuid,
60650c77 287 name: this.preferredUsername,
e12a0092 288 host: this.getHost(),
fadf619a
C
289 followingCount: this.followingCount,
290 followersCount: this.followersCount,
60650c77
C
291 avatar,
292 createdAt: this.createdAt,
293 updatedAt: this.updatedAt
fadf619a
C
294 }
295 }
296
e12a0092 297 toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') {
fadf619a
C
298 let activityPubType
299 if (type === 'Account') {
50d6de9c
C
300 activityPubType = 'Person' as 'Person'
301 } else if (type === 'Application') {
302 activityPubType = 'Application' as 'Application'
fadf619a 303 } else { // VideoChannel
50d6de9c 304 activityPubType = 'Group' as 'Group'
fadf619a
C
305 }
306
c5911fd3
C
307 let icon = undefined
308 if (this.avatarId) {
309 const extension = extname(this.Avatar.filename)
310 icon = {
311 type: 'Image',
312 mediaType: extension === '.png' ? 'image/png' : 'image/jpeg',
313 url: this.getAvatarUrl()
314 }
315 }
316
fadf619a 317 const json = {
50d6de9c 318 type: activityPubType,
fadf619a
C
319 id: this.url,
320 following: this.getFollowingUrl(),
321 followers: this.getFollowersUrl(),
322 inbox: this.inboxUrl,
323 outbox: this.outboxUrl,
e12a0092 324 preferredUsername: this.preferredUsername,
fadf619a 325 url: this.url,
e12a0092 326 name,
fadf619a
C
327 endpoints: {
328 sharedInbox: this.sharedInboxUrl
329 },
50d6de9c 330 uuid: this.uuid,
fadf619a
C
331 publicKey: {
332 id: this.getPublicKeyUrl(),
333 owner: this.url,
334 publicKeyPem: this.publicKey
c5911fd3
C
335 },
336 icon
fadf619a
C
337 }
338
339 return activityPubContextify(json)
340 }
341
342 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
343 const query = {
344 attributes: [ 'sharedInboxUrl' ],
345 include: [
346 {
50d6de9c 347 model: ActorFollowModel,
fadf619a
C
348 required: true,
349 as: 'followers',
350 where: {
50d6de9c 351 targetActorId: this.id
fadf619a
C
352 }
353 }
354 ],
355 transaction: t
356 }
357
358 return ActorModel.findAll(query)
359 .then(accounts => accounts.map(a => a.sharedInboxUrl))
360 }
361
362 getFollowingUrl () {
363 return this.url + '/following'
364 }
365
366 getFollowersUrl () {
367 return this.url + '/followers'
368 }
369
370 getPublicKeyUrl () {
371 return this.url + '#main-key'
372 }
373
374 isOwned () {
375 return this.serverId === null
376 }
e12a0092
C
377
378 getWebfingerUrl () {
379 return 'acct:' + this.preferredUsername + '@' + this.getHost()
380 }
381
382 getHost () {
383 return this.Server ? this.Server.host : CONFIG.WEBSERVER.HOST
384 }
c5911fd3
C
385
386 getAvatarUrl () {
387 if (!this.avatarId) return undefined
388
265ba139 389 return CONFIG.WEBSERVER.URL + this.Avatar.getWebserverPath()
c5911fd3 390 }
a5625b41
C
391
392 isOutdated () {
393 if (this.isOwned()) return false
394
395 const now = Date.now()
396 const createdAtTime = this.createdAt.getTime()
397 const updatedAtTime = this.updatedAt.getTime()
398
399 return (now - createdAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL &&
400 (now - updatedAtTime) > ACTIVITY_PUB.ACTOR_REFRESH_INTERVAL
401 }
fadf619a 402}