]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/activitypub/actor.ts
b7be9c32c62d87d0087a74fe3cc05520fc1ad4b1
[github/Chocobozzz/PeerTube.git] / server / models / activitypub / actor.ts
1 import { values } from 'lodash'
2 import { extname } from 'path'
3 import * as Sequelize from 'sequelize'
4 import {
5 AllowNull, BelongsTo, Column, CreatedAt, DataType, Default, DefaultScope, ForeignKey, HasMany, HasOne, Is, IsUUID, Model, Scopes,
6 Table, UpdatedAt
7 } from 'sequelize-typescript'
8 import { ActivityPubActorType } from '../../../shared/models/activitypub'
9 import { Avatar } from '../../../shared/models/avatars/avatar.model'
10 import { activityPubContextify } from '../../helpers/activitypub'
11 import {
12 isActorFollowersCountValid, isActorFollowingCountValid, isActorPreferredUsernameValid, isActorPrivateKeyValid,
13 isActorPublicKeyValid
14 } from '../../helpers/custom-validators/activitypub/actor'
15 import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
16 import { ACTIVITY_PUB, ACTIVITY_PUB_ACTOR_TYPES, CONFIG, CONSTRAINTS_FIELDS } from '../../initializers'
17 import { AccountModel } from '../account/account'
18 import { AvatarModel } from '../avatar/avatar'
19 import { ServerModel } from '../server/server'
20 import { throwIfNotValid } from '../utils'
21 import { VideoChannelModel } from '../video/video-channel'
22 import { ActorFollowModel } from './actor-follow'
23
24 enum ScopeNames {
25 FULL = 'FULL'
26 }
27
28 @DefaultScope({
29 include: [
30 {
31 model: () => ServerModel,
32 required: false
33 },
34 {
35 model: () => AvatarModel,
36 required: false
37 }
38 ]
39 })
40 @Scopes({
41 [ScopeNames.FULL]: {
42 include: [
43 {
44 model: () => AccountModel.unscoped(),
45 required: false
46 },
47 {
48 model: () => VideoChannelModel.unscoped(),
49 required: false
50 },
51 {
52 model: () => ServerModel,
53 required: false
54 },
55 {
56 model: () => AvatarModel,
57 required: false
58 }
59 ]
60 }
61 })
62 @Table({
63 tableName: 'actor',
64 indexes: [
65 {
66 fields: [ 'url' ]
67 },
68 {
69 fields: [ 'preferredUsername', 'serverId' ],
70 unique: true
71 },
72 {
73 fields: [ 'inboxUrl', 'sharedInboxUrl' ]
74 }
75 ]
76 })
77 export class ActorModel extends Model<ActorModel> {
78
79 @AllowNull(false)
80 @Column(DataType.ENUM(values(ACTIVITY_PUB_ACTOR_TYPES)))
81 type: ActivityPubActorType
82
83 @AllowNull(false)
84 @Default(DataType.UUIDV4)
85 @IsUUID(4)
86 @Column(DataType.UUID)
87 uuid: string
88
89 @AllowNull(false)
90 @Is('ActorPreferredUsername', value => throwIfNotValid(value, isActorPreferredUsernameValid, 'actor preferred username'))
91 @Column
92 preferredUsername: string
93
94 @AllowNull(false)
95 @Is('ActorUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'url'))
96 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
97 url: string
98
99 @AllowNull(true)
100 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPublicKeyValid, 'public key'))
101 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY.max))
102 publicKey: string
103
104 @AllowNull(true)
105 @Is('ActorPublicKey', value => throwIfNotValid(value, isActorPrivateKeyValid, 'private key'))
106 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.PRIVATE_KEY.max))
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'))
121 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
122 inboxUrl: string
123
124 @AllowNull(false)
125 @Is('ActorOutboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'outbox url'))
126 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
127 outboxUrl: string
128
129 @AllowNull(false)
130 @Is('ActorSharedInboxUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'shared inbox url'))
131 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
132 sharedInboxUrl: string
133
134 @AllowNull(false)
135 @Is('ActorFollowersUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'followers url'))
136 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
137 followersUrl: string
138
139 @AllowNull(false)
140 @Is('ActorFollowingUrl', value => throwIfNotValid(value, isActivityPubUrlValid, 'following url'))
141 @Column(DataType.STRING(CONSTRAINTS_FIELDS.ACTORS.URL.max))
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 },
158 onDelete: 'set null'
159 })
160 Avatar: AvatarModel
161
162 @HasMany(() => ActorFollowModel, {
163 foreignKey: {
164 name: 'actorId',
165 allowNull: false
166 },
167 onDelete: 'cascade'
168 })
169 AccountFollowing: ActorFollowModel[]
170
171 @HasMany(() => ActorFollowModel, {
172 foreignKey: {
173 name: 'targetActorId',
174 allowNull: false
175 },
176 as: 'followers',
177 onDelete: 'cascade'
178 })
179 AccountFollowers: ActorFollowModel[]
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
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) {
210 return ActorModel.unscoped().findById(id)
211 }
212
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
223 return ActorModel.scope(ScopeNames.FULL).findAll(query)
224 }
225
226 static loadLocalByName (preferredUsername: string) {
227 const query = {
228 where: {
229 preferredUsername,
230 serverId: null
231 }
232 }
233
234 return ActorModel.scope(ScopeNames.FULL).findOne(query)
235 }
236
237 static loadByNameAndHost (preferredUsername: string, host: string) {
238 const query = {
239 where: {
240 preferredUsername
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)
265 }
266
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
277 toFormattedJSON () {
278 let avatar: Avatar = null
279 if (this.Avatar) {
280 avatar = this.Avatar.toFormattedJSON()
281 }
282
283 return {
284 id: this.id,
285 url: this.url,
286 uuid: this.uuid,
287 name: this.preferredUsername,
288 host: this.getHost(),
289 followingCount: this.followingCount,
290 followersCount: this.followersCount,
291 avatar,
292 createdAt: this.createdAt,
293 updatedAt: this.updatedAt
294 }
295 }
296
297 toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') {
298 let activityPubType
299 if (type === 'Account') {
300 activityPubType = 'Person' as 'Person'
301 } else if (type === 'Application') {
302 activityPubType = 'Application' as 'Application'
303 } else { // VideoChannel
304 activityPubType = 'Group' as 'Group'
305 }
306
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
317 const json = {
318 type: activityPubType,
319 id: this.url,
320 following: this.getFollowingUrl(),
321 followers: this.getFollowersUrl(),
322 inbox: this.inboxUrl,
323 outbox: this.outboxUrl,
324 preferredUsername: this.preferredUsername,
325 url: this.url,
326 name,
327 endpoints: {
328 sharedInbox: this.sharedInboxUrl
329 },
330 uuid: this.uuid,
331 publicKey: {
332 id: this.getPublicKeyUrl(),
333 owner: this.url,
334 publicKeyPem: this.publicKey
335 },
336 icon
337 }
338
339 return activityPubContextify(json)
340 }
341
342 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
343 const query = {
344 attributes: [ 'sharedInboxUrl' ],
345 include: [
346 {
347 model: ActorFollowModel,
348 required: true,
349 as: 'followers',
350 where: {
351 targetActorId: this.id
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 }
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 }
385
386 getAvatarUrl () {
387 if (!this.avatarId) return undefined
388
389 return CONFIG.WEBSERVER.URL + this.Avatar.getWebserverPath()
390 }
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 }
402 }