actor-follow.ts 15 KB

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