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