]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/activitypub/actor.ts
17f69f7a7aaa157caef66c60695211be230d0f48
[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 toFormattedJSON () {
268 let avatar: Avatar = null
269 if (this.Avatar) {
270 avatar = this.Avatar.toFormattedJSON()
271 }
272
273 return {
274 id: this.id,
275 url: this.url,
276 uuid: this.uuid,
277 name: this.preferredUsername,
278 host: this.getHost(),
279 followingCount: this.followingCount,
280 followersCount: this.followersCount,
281 avatar,
282 createdAt: this.createdAt,
283 updatedAt: this.updatedAt
284 }
285 }
286
287 toActivityPubObject (name: string, type: 'Account' | 'Application' | 'VideoChannel') {
288 let activityPubType
289 if (type === 'Account') {
290 activityPubType = 'Person' as 'Person'
291 } else if (type === 'Application') {
292 activityPubType = 'Application' as 'Application'
293 } else { // VideoChannel
294 activityPubType = 'Group' as 'Group'
295 }
296
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
307 const json = {
308 type: activityPubType,
309 id: this.url,
310 following: this.getFollowingUrl(),
311 followers: this.getFollowersUrl(),
312 inbox: this.inboxUrl,
313 outbox: this.outboxUrl,
314 preferredUsername: this.preferredUsername,
315 url: this.url,
316 name,
317 endpoints: {
318 sharedInbox: this.sharedInboxUrl
319 },
320 uuid: this.uuid,
321 publicKey: {
322 id: this.getPublicKeyUrl(),
323 owner: this.url,
324 publicKeyPem: this.publicKey
325 },
326 icon
327 }
328
329 return activityPubContextify(json)
330 }
331
332 getFollowerSharedInboxUrls (t: Sequelize.Transaction) {
333 const query = {
334 attributes: [ 'sharedInboxUrl' ],
335 include: [
336 {
337 model: ActorFollowModel,
338 required: true,
339 as: 'followers',
340 where: {
341 targetActorId: this.id
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 }
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 }
375
376 getAvatarUrl () {
377 if (!this.avatarId) return undefined
378
379 return CONFIG.WEBSERVER.URL + this.Avatar.getWebserverPath()
380 }
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 }
392 }