]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/activitypub/actor-follow.ts
Add get subscription endpoint
[github/Chocobozzz/PeerTube.git] / server / models / activitypub / actor-follow.ts
1 import * as Bluebird from 'bluebird'
2 import { values } from 'lodash'
3 import * as Sequelize from 'sequelize'
4 import {
5 AfterCreate,
6 AfterDestroy,
7 AfterUpdate,
8 AllowNull,
9 BelongsTo,
10 Column,
11 CreatedAt,
12 DataType,
13 Default,
14 ForeignKey,
15 IsInt,
16 Max,
17 Model,
18 Table,
19 UpdatedAt
20 } from 'sequelize-typescript'
21 import { FollowState } from '../../../shared/models/actors'
22 import { AccountFollow } from '../../../shared/models/actors/follow.model'
23 import { logger } from '../../helpers/logger'
24 import { getServerActor } from '../../helpers/utils'
25 import { ACTOR_FOLLOW_SCORE } from '../../initializers'
26 import { FOLLOW_STATES } from '../../initializers/constants'
27 import { ServerModel } from '../server/server'
28 import { getSort } from '../utils'
29 import { ActorModel } from './actor'
30 import { VideoChannelModel } from '../video/video-channel'
31 import { IIncludeOptions } from '../../../node_modules/sequelize-typescript/lib/interfaces/IIncludeOptions'
32
33 @Table({
34 tableName: 'actorFollow',
35 indexes: [
36 {
37 fields: [ 'actorId' ]
38 },
39 {
40 fields: [ 'targetActorId' ]
41 },
42 {
43 fields: [ 'actorId', 'targetActorId' ],
44 unique: true
45 },
46 {
47 fields: [ 'score' ]
48 }
49 ]
50 })
51 export class ActorFollowModel extends Model<ActorFollowModel> {
52
53 @AllowNull(false)
54 @Column(DataType.ENUM(values(FOLLOW_STATES)))
55 state: FollowState
56
57 @AllowNull(false)
58 @Default(ACTOR_FOLLOW_SCORE.BASE)
59 @IsInt
60 @Max(ACTOR_FOLLOW_SCORE.MAX)
61 @Column
62 score: number
63
64 @CreatedAt
65 createdAt: Date
66
67 @UpdatedAt
68 updatedAt: Date
69
70 @ForeignKey(() => ActorModel)
71 @Column
72 actorId: number
73
74 @BelongsTo(() => ActorModel, {
75 foreignKey: {
76 name: 'actorId',
77 allowNull: false
78 },
79 as: 'ActorFollower',
80 onDelete: 'CASCADE'
81 })
82 ActorFollower: ActorModel
83
84 @ForeignKey(() => ActorModel)
85 @Column
86 targetActorId: number
87
88 @BelongsTo(() => ActorModel, {
89 foreignKey: {
90 name: 'targetActorId',
91 allowNull: false
92 },
93 as: 'ActorFollowing',
94 onDelete: 'CASCADE'
95 })
96 ActorFollowing: ActorModel
97
98 @AfterCreate
99 @AfterUpdate
100 static incrementFollowerAndFollowingCount (instance: ActorFollowModel) {
101 if (instance.state !== 'accepted') return undefined
102
103 return Promise.all([
104 ActorModel.incrementFollows(instance.actorId, 'followingCount', 1),
105 ActorModel.incrementFollows(instance.targetActorId, 'followersCount', 1)
106 ])
107 }
108
109 @AfterDestroy
110 static decrementFollowerAndFollowingCount (instance: ActorFollowModel) {
111 return Promise.all([
112 ActorModel.incrementFollows(instance.actorId, 'followingCount',-1),
113 ActorModel.incrementFollows(instance.targetActorId, 'followersCount', -1)
114 ])
115 }
116
117 // Remove actor follows with a score of 0 (too many requests where they were unreachable)
118 static async removeBadActorFollows () {
119 const actorFollows = await ActorFollowModel.listBadActorFollows()
120
121 const actorFollowsRemovePromises = actorFollows.map(actorFollow => actorFollow.destroy())
122 await Promise.all(actorFollowsRemovePromises)
123
124 const numberOfActorFollowsRemoved = actorFollows.length
125
126 if (numberOfActorFollowsRemoved) logger.info('Removed bad %d actor follows.', numberOfActorFollowsRemoved)
127 }
128
129 static updateActorFollowsScore (goodInboxes: string[], badInboxes: string[], t: Sequelize.Transaction | undefined) {
130 if (goodInboxes.length === 0 && badInboxes.length === 0) return
131
132 logger.info('Updating %d good actor follows and %d bad actor follows scores.', goodInboxes.length, badInboxes.length)
133
134 if (goodInboxes.length !== 0) {
135 ActorFollowModel.incrementScores(goodInboxes, ACTOR_FOLLOW_SCORE.BONUS, t)
136 .catch(err => logger.error('Cannot increment scores of good actor follows.', { err }))
137 }
138
139 if (badInboxes.length !== 0) {
140 ActorFollowModel.incrementScores(badInboxes, ACTOR_FOLLOW_SCORE.PENALTY, t)
141 .catch(err => logger.error('Cannot decrement scores of bad actor follows.', { err }))
142 }
143 }
144
145 static loadByActorAndTarget (actorId: number, targetActorId: number, t?: Sequelize.Transaction) {
146 const query = {
147 where: {
148 actorId,
149 targetActorId: targetActorId
150 },
151 include: [
152 {
153 model: ActorModel,
154 required: true,
155 as: 'ActorFollower'
156 },
157 {
158 model: ActorModel,
159 required: true,
160 as: 'ActorFollowing'
161 }
162 ],
163 transaction: t
164 }
165
166 return ActorFollowModel.findOne(query)
167 }
168
169 static loadByActorAndTargetNameAndHost (actorId: number, targetName: string, targetHost: string, t?: Sequelize.Transaction) {
170 const actorFollowingPartInclude: IIncludeOptions = {
171 model: ActorModel,
172 required: true,
173 as: 'ActorFollowing',
174 where: {
175 preferredUsername: targetName
176 },
177 include: [
178 {
179 model: VideoChannelModel,
180 required: false
181 }
182 ]
183 }
184
185 if (targetHost === null) {
186 actorFollowingPartInclude.where['serverId'] = null
187 } else {
188 actorFollowingPartInclude.include.push({
189 model: ServerModel,
190 required: true,
191 where: {
192 host: targetHost
193 }
194 })
195 }
196
197 const query = {
198 where: {
199 actorId
200 },
201 include: [
202 {
203 model: ActorModel,
204 required: true,
205 as: 'ActorFollower'
206 },
207 actorFollowingPartInclude
208 ],
209 transaction: t
210 }
211
212 return ActorFollowModel.findOne(query)
213 }
214
215 static listFollowingForApi (id: number, start: number, count: number, sort: string) {
216 const query = {
217 distinct: true,
218 offset: start,
219 limit: count,
220 order: getSort(sort),
221 include: [
222 {
223 model: ActorModel,
224 required: true,
225 as: 'ActorFollower',
226 where: {
227 id
228 }
229 },
230 {
231 model: ActorModel,
232 as: 'ActorFollowing',
233 required: true,
234 include: [ ServerModel ]
235 }
236 ]
237 }
238
239 return ActorFollowModel.findAndCountAll(query)
240 .then(({ rows, count }) => {
241 return {
242 data: rows,
243 total: count
244 }
245 })
246 }
247
248 static listSubscriptionsForApi (id: number, start: number, count: number, sort: string) {
249 const query = {
250 distinct: true,
251 offset: start,
252 limit: count,
253 order: getSort(sort),
254 where: {
255 actorId: id
256 },
257 include: [
258 {
259 model: ActorModel,
260 as: 'ActorFollowing',
261 required: true,
262 include: [
263 {
264 model: VideoChannelModel,
265 required: true
266 }
267 ]
268 }
269 ]
270 }
271
272 return ActorFollowModel.findAndCountAll(query)
273 .then(({ rows, count }) => {
274 return {
275 data: rows.map(r => r.ActorFollowing.VideoChannel),
276 total: count
277 }
278 })
279 }
280
281 static listFollowersForApi (id: number, start: number, count: number, sort: string) {
282 const query = {
283 distinct: true,
284 offset: start,
285 limit: count,
286 order: getSort(sort),
287 include: [
288 {
289 model: ActorModel,
290 required: true,
291 as: 'ActorFollower',
292 include: [ ServerModel ]
293 },
294 {
295 model: ActorModel,
296 as: 'ActorFollowing',
297 required: true,
298 where: {
299 id
300 }
301 }
302 ]
303 }
304
305 return ActorFollowModel.findAndCountAll(query)
306 .then(({ rows, count }) => {
307 return {
308 data: rows,
309 total: count
310 }
311 })
312 }
313
314 static listAcceptedFollowerUrlsForApi (actorIds: number[], t: Sequelize.Transaction, start?: number, count?: number) {
315 return ActorFollowModel.createListAcceptedFollowForApiQuery('followers', actorIds, t, start, count)
316 }
317
318 static listAcceptedFollowerSharedInboxUrls (actorIds: number[], t: Sequelize.Transaction) {
319 return ActorFollowModel.createListAcceptedFollowForApiQuery(
320 'followers',
321 actorIds,
322 t,
323 undefined,
324 undefined,
325 'sharedInboxUrl',
326 true
327 )
328 }
329
330 static listAcceptedFollowingUrlsForApi (actorIds: number[], t: Sequelize.Transaction, start?: number, count?: number) {
331 return ActorFollowModel.createListAcceptedFollowForApiQuery('following', actorIds, t, start, count)
332 }
333
334 static async getStats () {
335 const serverActor = await getServerActor()
336
337 const totalInstanceFollowing = await ActorFollowModel.count({
338 where: {
339 actorId: serverActor.id
340 }
341 })
342
343 const totalInstanceFollowers = await ActorFollowModel.count({
344 where: {
345 targetActorId: serverActor.id
346 }
347 })
348
349 return {
350 totalInstanceFollowing,
351 totalInstanceFollowers
352 }
353 }
354
355 private static async createListAcceptedFollowForApiQuery (
356 type: 'followers' | 'following',
357 actorIds: number[],
358 t: Sequelize.Transaction,
359 start?: number,
360 count?: number,
361 columnUrl = 'url',
362 distinct = false
363 ) {
364 let firstJoin: string
365 let secondJoin: string
366
367 if (type === 'followers') {
368 firstJoin = 'targetActorId'
369 secondJoin = 'actorId'
370 } else {
371 firstJoin = 'actorId'
372 secondJoin = 'targetActorId'
373 }
374
375 const selections: string[] = []
376 if (distinct === true) selections.push('DISTINCT("Follows"."' + columnUrl + '") AS "url"')
377 else selections.push('"Follows"."' + columnUrl + '" AS "url"')
378
379 selections.push('COUNT(*) AS "total"')
380
381 const tasks: Bluebird<any>[] = []
382
383 for (let selection of selections) {
384 let query = 'SELECT ' + selection + ' FROM "actor" ' +
385 'INNER JOIN "actorFollow" ON "actorFollow"."' + firstJoin + '" = "actor"."id" ' +
386 'INNER JOIN "actor" AS "Follows" ON "actorFollow"."' + secondJoin + '" = "Follows"."id" ' +
387 'WHERE "actor"."id" = ANY ($actorIds) AND "actorFollow"."state" = \'accepted\' '
388
389 if (count !== undefined) query += 'LIMIT ' + count
390 if (start !== undefined) query += ' OFFSET ' + start
391
392 const options = {
393 bind: { actorIds },
394 type: Sequelize.QueryTypes.SELECT,
395 transaction: t
396 }
397 tasks.push(ActorFollowModel.sequelize.query(query, options))
398 }
399
400 const [ followers, [ { total } ] ] = await Promise.all(tasks)
401 const urls: string[] = followers.map(f => f.url)
402
403 return {
404 data: urls,
405 total: parseInt(total, 10)
406 }
407 }
408
409 private static incrementScores (inboxUrls: string[], value: number, t: Sequelize.Transaction | undefined) {
410 const inboxUrlsString = inboxUrls.map(url => `'${url}'`).join(',')
411
412 const query = `UPDATE "actorFollow" SET "score" = LEAST("score" + ${value}, ${ACTOR_FOLLOW_SCORE.MAX}) ` +
413 'WHERE id IN (' +
414 'SELECT "actorFollow"."id" FROM "actorFollow" ' +
415 'INNER JOIN "actor" ON "actor"."id" = "actorFollow"."actorId" ' +
416 'WHERE "actor"."inboxUrl" IN (' + inboxUrlsString + ') OR "actor"."sharedInboxUrl" IN (' + inboxUrlsString + ')' +
417 ')'
418
419 const options = t ? {
420 type: Sequelize.QueryTypes.BULKUPDATE,
421 transaction: t
422 } : undefined
423
424 return ActorFollowModel.sequelize.query(query, options)
425 }
426
427 private static listBadActorFollows () {
428 const query = {
429 where: {
430 score: {
431 [Sequelize.Op.lte]: 0
432 }
433 },
434 logging: false
435 }
436
437 return ActorFollowModel.findAll(query)
438 }
439
440 toFormattedJSON (): AccountFollow {
441 const follower = this.ActorFollower.toFormattedJSON()
442 const following = this.ActorFollowing.toFormattedJSON()
443
444 return {
445 id: this.id,
446 follower,
447 following,
448 score: this.score,
449 state: this.state,
450 createdAt: this.createdAt,
451 updatedAt: this.updatedAt
452 }
453 }
454 }