aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/account/account.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-11-13 17:39:41 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-11-27 19:40:51 +0100
commit7a7724e66e4533523083e7336cd0d0c747c4a33b (patch)
tree805299eb9c6829158cd17e5a823a84a3a54d8209 /server/models/account/account.ts
parent571389d43b8fc8aaf27e77c06f19b320b08dbbc9 (diff)
downloadPeerTube-7a7724e66e4533523083e7336cd0d0c747c4a33b.tar.gz
PeerTube-7a7724e66e4533523083e7336cd0d0c747c4a33b.tar.zst
PeerTube-7a7724e66e4533523083e7336cd0d0c747c4a33b.zip
Handle follow/accept
Diffstat (limited to 'server/models/account/account.ts')
-rw-r--r--server/models/account/account.ts138
1 files changed, 118 insertions, 20 deletions
diff --git a/server/models/account/account.ts b/server/models/account/account.ts
index a79e13880..daf8f4703 100644
--- a/server/models/account/account.ts
+++ b/server/models/account/account.ts
@@ -15,25 +15,31 @@ import {
15 activityPubContextify 15 activityPubContextify
16} from '../../helpers' 16} from '../../helpers'
17 17
18import { addMethodsToModel } from '../utils' 18import { addMethodsToModel, getSort } from '../utils'
19import { 19import {
20 AccountInstance, 20 AccountInstance,
21 AccountAttributes, 21 AccountAttributes,
22 22
23 AccountMethods 23 AccountMethods
24} from './account-interface' 24} from './account-interface'
25import LoadApplication = AccountMethods.LoadApplication
26import { sendDeleteAccount } from '../../lib/activitypub/send-request'
25 27
26let Account: Sequelize.Model<AccountInstance, AccountAttributes> 28let Account: Sequelize.Model<AccountInstance, AccountAttributes>
27let loadAccountByPodAndUUID: AccountMethods.LoadAccountByPodAndUUID 29let loadAccountByPodAndUUID: AccountMethods.LoadAccountByPodAndUUID
28let load: AccountMethods.Load 30let load: AccountMethods.Load
31let loadApplication: AccountMethods.LoadApplication
29let loadByUUID: AccountMethods.LoadByUUID 32let loadByUUID: AccountMethods.LoadByUUID
30let loadByUrl: AccountMethods.LoadByUrl 33let loadByUrl: AccountMethods.LoadByUrl
31let loadLocalAccountByName: AccountMethods.LoadLocalAccountByName 34let loadLocalAccountByNameAndPod: AccountMethods.LoadLocalAccountByNameAndPod
32let listOwned: AccountMethods.ListOwned 35let listOwned: AccountMethods.ListOwned
33let listFollowerUrlsForApi: AccountMethods.ListFollowerUrlsForApi 36let listFollowerUrlsForApi: AccountMethods.ListFollowerUrlsForApi
34let listFollowingUrlsForApi: AccountMethods.ListFollowingUrlsForApi 37let listFollowingUrlsForApi: AccountMethods.ListFollowingUrlsForApi
38let listFollowingForApi: AccountMethods.ListFollowingForApi
39let listFollowersForApi: AccountMethods.ListFollowersForApi
35let isOwned: AccountMethods.IsOwned 40let isOwned: AccountMethods.IsOwned
36let toActivityPubObject: AccountMethods.ToActivityPubObject 41let toActivityPubObject: AccountMethods.ToActivityPubObject
42let toFormattedJSON: AccountMethods.ToFormattedJSON
37let getFollowerSharedInboxUrls: AccountMethods.GetFollowerSharedInboxUrls 43let getFollowerSharedInboxUrls: AccountMethods.GetFollowerSharedInboxUrls
38let getFollowingUrl: AccountMethods.GetFollowingUrl 44let getFollowingUrl: AccountMethods.GetFollowingUrl
39let getFollowersUrl: AccountMethods.GetFollowersUrl 45let getFollowersUrl: AccountMethods.GetFollowersUrl
@@ -189,16 +195,20 @@ export default function defineAccount (sequelize: Sequelize.Sequelize, DataTypes
189 const classMethods = [ 195 const classMethods = [
190 associate, 196 associate,
191 loadAccountByPodAndUUID, 197 loadAccountByPodAndUUID,
198 loadApplication,
192 load, 199 load,
193 loadByUUID, 200 loadByUUID,
194 loadLocalAccountByName, 201 loadLocalAccountByNameAndPod,
195 listOwned, 202 listOwned,
196 listFollowerUrlsForApi, 203 listFollowerUrlsForApi,
197 listFollowingUrlsForApi 204 listFollowingUrlsForApi,
205 listFollowingForApi,
206 listFollowersForApi
198 ] 207 ]
199 const instanceMethods = [ 208 const instanceMethods = [
200 isOwned, 209 isOwned,
201 toActivityPubObject, 210 toActivityPubObject,
211 toFormattedJSON,
202 getFollowerSharedInboxUrls, 212 getFollowerSharedInboxUrls,
203 getFollowingUrl, 213 getFollowingUrl,
204 getFollowersUrl, 214 getFollowersUrl,
@@ -250,6 +260,7 @@ function associate (models) {
250 name: 'accountId', 260 name: 'accountId',
251 allowNull: false 261 allowNull: false
252 }, 262 },
263 as: 'following',
253 onDelete: 'cascade' 264 onDelete: 'cascade'
254 }) 265 })
255 266
@@ -258,23 +269,29 @@ function associate (models) {
258 name: 'targetAccountId', 269 name: 'targetAccountId',
259 allowNull: false 270 allowNull: false
260 }, 271 },
272 as: 'followers',
261 onDelete: 'cascade' 273 onDelete: 'cascade'
262 }) 274 })
263} 275}
264 276
265function afterDestroy (account: AccountInstance) { 277function afterDestroy (account: AccountInstance) {
266 if (account.isOwned()) { 278 if (account.isOwned()) {
267 const removeVideoAccountToFriendsParams = { 279 return sendDeleteAccount(account, undefined)
268 uuid: account.uuid
269 }
270
271 // FIXME: remove account in followers
272 // return removeVideoAccountToFriends(removeVideoAccountToFriendsParams)
273 } 280 }
274 281
275 return undefined 282 return undefined
276} 283}
277 284
285toFormattedJSON = function (this: AccountInstance) {
286 const json = {
287 id: this.id,
288 host: this.Pod.host,
289 name: this.name
290 }
291
292 return json
293}
294
278toActivityPubObject = function (this: AccountInstance) { 295toActivityPubObject = function (this: AccountInstance) {
279 const type = this.podId ? 'Application' as 'Application' : 'Person' as 'Person' 296 const type = this.podId ? 'Application' as 'Application' : 'Person' as 'Person'
280 297
@@ -347,12 +364,85 @@ listOwned = function () {
347 return Account.findAll(query) 364 return Account.findAll(query)
348} 365}
349 366
350listFollowerUrlsForApi = function (name: string, start: number, count?: number) { 367listFollowerUrlsForApi = function (id: number, start: number, count?: number) {
351 return createListFollowForApiQuery('followers', name, start, count) 368 return createListFollowForApiQuery('followers', id, start, count)
369}
370
371listFollowingUrlsForApi = function (id: number, start: number, count?: number) {
372 return createListFollowForApiQuery('following', id, start, count)
373}
374
375listFollowingForApi = function (id: number, start: number, count: number, sort: string) {
376 const query = {
377 distinct: true,
378 offset: start,
379 limit: count,
380 order: [ getSort(sort) ],
381 include: [
382 {
383 model: Account['sequelize'].models.AccountFollow,
384 required: true,
385 as: 'following',
386 include: [
387 {
388 model: Account['sequelize'].models.Account,
389 as: 'following',
390 required: true,
391 include: [ Account['sequelize'].models.Pod ]
392 }
393 ]
394 }
395 ]
396 }
397
398 return Account.findAndCountAll(query).then(({ rows, count }) => {
399 return {
400 data: rows,
401 total: count
402 }
403 })
404}
405
406listFollowersForApi = function (id: number, start: number, count: number, sort: string) {
407 const query = {
408 distinct: true,
409 offset: start,
410 limit: count,
411 order: [ getSort(sort) ],
412 include: [
413 {
414 model: Account['sequelize'].models.AccountFollow,
415 required: true,
416 as: 'followers',
417 include: [
418 {
419 model: Account['sequelize'].models.Account,
420 as: 'followers',
421 required: true,
422 include: [ Account['sequelize'].models.Pod ]
423 }
424 ]
425 }
426 ]
427 }
428
429 return Account.findAndCountAll(query).then(({ rows, count }) => {
430 return {
431 data: rows,
432 total: count
433 }
434 })
352} 435}
353 436
354listFollowingUrlsForApi = function (name: string, start: number, count?: number) { 437loadApplication = function () {
355 return createListFollowForApiQuery('following', name, start, count) 438 return Account.findOne({
439 include: [
440 {
441 model: Account['sequelize'].model.Application,
442 required: true
443 }
444 ]
445 })
356} 446}
357 447
358load = function (id: number) { 448load = function (id: number) {
@@ -369,14 +459,22 @@ loadByUUID = function (uuid: string) {
369 return Account.findOne(query) 459 return Account.findOne(query)
370} 460}
371 461
372loadLocalAccountByName = function (name: string) { 462loadLocalAccountByNameAndPod = function (name: string, host: string) {
373 const query: Sequelize.FindOptions<AccountAttributes> = { 463 const query: Sequelize.FindOptions<AccountAttributes> = {
374 where: { 464 where: {
375 name, 465 name,
376 userId: { 466 userId: {
377 [Sequelize.Op.ne]: null 467 [Sequelize.Op.ne]: null
378 } 468 }
379 } 469 },
470 include: [
471 {
472 model: Account['sequelize'].models.Pod,
473 where: {
474 host
475 }
476 }
477 ]
380 } 478 }
381 479
382 return Account.findOne(query) 480 return Account.findOne(query)
@@ -406,7 +504,7 @@ loadAccountByPodAndUUID = function (uuid: string, podId: number, transaction: Se
406 504
407// ------------------------------ UTILS ------------------------------ 505// ------------------------------ UTILS ------------------------------
408 506
409async function createListFollowForApiQuery (type: 'followers' | 'following', name: string, start: number, count?: number) { 507async function createListFollowForApiQuery (type: 'followers' | 'following', id: number, start: number, count?: number) {
410 let firstJoin: string 508 let firstJoin: string
411 let secondJoin: string 509 let secondJoin: string
412 510
@@ -424,14 +522,14 @@ async function createListFollowForApiQuery (type: 'followers' | 'following', nam
424 for (const selection of selections) { 522 for (const selection of selections) {
425 let query = 'SELECT ' + selection + ' FROM "Account" ' + 523 let query = 'SELECT ' + selection + ' FROM "Account" ' +
426 'INNER JOIN "AccountFollower" ON "AccountFollower"."' + firstJoin + '" = "Account"."id" ' + 524 'INNER JOIN "AccountFollower" ON "AccountFollower"."' + firstJoin + '" = "Account"."id" ' +
427 'INNER JOIN "Account" AS "Followers" ON "Followers"."id" = "AccountFollower"."' + secondJoin + '" ' + 525 'INNER JOIN "Account" AS "Follows" ON "Followers"."id" = "Follows"."' + secondJoin + '" ' +
428 'WHERE "Account"."name" = \'$name\' ' + 526 'WHERE "Account"."id" = $id ' +
429 'LIMIT ' + start 527 'LIMIT ' + start
430 528
431 if (count !== undefined) query += ', ' + count 529 if (count !== undefined) query += ', ' + count
432 530
433 const options = { 531 const options = {
434 bind: { name }, 532 bind: { id },
435 type: Sequelize.QueryTypes.SELECT 533 type: Sequelize.QueryTypes.SELECT
436 } 534 }
437 tasks.push(Account['sequelize'].query(query, options)) 535 tasks.push(Account['sequelize'].query(query, options))