diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/controllers/activitypub/inbox.ts | 33 | ||||
-rw-r--r-- | server/controllers/activitypub/outbox.ts | 2 | ||||
-rw-r--r-- | server/controllers/api/server/follows.ts | 3 | ||||
-rw-r--r-- | server/helpers/activitypub.ts | 14 | ||||
-rw-r--r-- | server/initializers/constants.ts | 1 | ||||
-rw-r--r-- | server/lib/activitypub/fetch.ts | 15 | ||||
-rw-r--r-- | server/lib/activitypub/index.ts | 1 | ||||
-rw-r--r-- | server/lib/activitypub/process/index.ts | 1 | ||||
-rw-r--r-- | server/lib/activitypub/process/process.ts | 38 | ||||
-rw-r--r-- | server/lib/activitypub/send/send-create.ts | 2 | ||||
-rw-r--r-- | server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-fetcher-handler.ts | 69 | ||||
-rw-r--r-- | server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler.ts | 8 |
12 files changed, 145 insertions, 42 deletions
diff --git a/server/controllers/activitypub/inbox.ts b/server/controllers/activitypub/inbox.ts index 807d0bdf4..30e7f706b 100644 --- a/server/controllers/activitypub/inbox.ts +++ b/server/controllers/activitypub/inbox.ts | |||
@@ -1,27 +1,10 @@ | |||
1 | import * as express from 'express' | 1 | import * as express from 'express' |
2 | import { Activity, ActivityPubCollection, ActivityPubOrderedCollection, ActivityType, RootActivity } from '../../../shared' | 2 | import { Activity, ActivityPubCollection, ActivityPubOrderedCollection, RootActivity } from '../../../shared' |
3 | import { logger } from '../../helpers' | 3 | import { logger } from '../../helpers' |
4 | import { isActivityValid } from '../../helpers/custom-validators/activitypub/activity' | 4 | import { isActivityValid } from '../../helpers/custom-validators/activitypub/activity' |
5 | import { processCreateActivity, processUpdateActivity, processUndoActivity } from '../../lib' | 5 | import { processActivities } from '../../lib/activitypub/process/process' |
6 | import { processAcceptActivity } from '../../lib/activitypub/process/process-accept' | ||
7 | import { processAddActivity } from '../../lib/activitypub/process/process-add' | ||
8 | import { processAnnounceActivity } from '../../lib/activitypub/process/process-announce' | ||
9 | import { processDeleteActivity } from '../../lib/activitypub/process/process-delete' | ||
10 | import { processFollowActivity } from '../../lib/activitypub/process/process-follow' | ||
11 | import { asyncMiddleware, checkSignature, localAccountValidator, signatureValidator } from '../../middlewares' | 6 | import { asyncMiddleware, checkSignature, localAccountValidator, signatureValidator } from '../../middlewares' |
12 | import { activityPubValidator } from '../../middlewares/validators/activitypub/activity' | 7 | import { activityPubValidator } from '../../middlewares/validators/activitypub/activity' |
13 | import { AccountInstance } from '../../models/account/account-interface' | ||
14 | |||
15 | const processActivity: { [ P in ActivityType ]: (activity: Activity, inboxAccount?: AccountInstance) => Promise<any> } = { | ||
16 | Create: processCreateActivity, | ||
17 | Add: processAddActivity, | ||
18 | Update: processUpdateActivity, | ||
19 | Delete: processDeleteActivity, | ||
20 | Follow: processFollowActivity, | ||
21 | Accept: processAcceptActivity, | ||
22 | Announce: processAnnounceActivity, | ||
23 | Undo: processUndoActivity | ||
24 | } | ||
25 | 8 | ||
26 | const inboxRouter = express.Router() | 9 | const inboxRouter = express.Router() |
27 | 10 | ||
@@ -69,15 +52,3 @@ async function inboxController (req: express.Request, res: express.Response, nex | |||
69 | 52 | ||
70 | res.status(204).end() | 53 | res.status(204).end() |
71 | } | 54 | } |
72 | |||
73 | async function processActivities (activities: Activity[], inboxAccount?: AccountInstance) { | ||
74 | for (const activity of activities) { | ||
75 | const activityProcessor = processActivity[activity.type] | ||
76 | if (activityProcessor === undefined) { | ||
77 | logger.warn('Unknown activity type %s.', activity.type, { activityId: activity.id }) | ||
78 | continue | ||
79 | } | ||
80 | |||
81 | await activityProcessor(activity, inboxAccount) | ||
82 | } | ||
83 | } | ||
diff --git a/server/controllers/activitypub/outbox.ts b/server/controllers/activitypub/outbox.ts index 396fa2db5..1a74bde33 100644 --- a/server/controllers/activitypub/outbox.ts +++ b/server/controllers/activitypub/outbox.ts | |||
@@ -34,8 +34,6 @@ async function outboxController (req: express.Request, res: express.Response, ne | |||
34 | const data = await db.Video.listAllAndSharedByAccountForOutbox(account.id, start, count) | 34 | const data = await db.Video.listAllAndSharedByAccountForOutbox(account.id, start, count) |
35 | const activities: Activity[] = [] | 35 | const activities: Activity[] = [] |
36 | 36 | ||
37 | console.log(account.url) | ||
38 | |||
39 | for (const video of data.data) { | 37 | for (const video of data.data) { |
40 | const videoObject = video.toActivityPubObject() | 38 | const videoObject = video.toActivityPubObject() |
41 | let addActivity: ActivityAdd = await addActivityData(video.url, account, video, video.VideoChannel.url, videoObject) | 39 | let addActivity: ActivityAdd = await addActivityData(video.url, account, video, video.VideoChannel.url, videoObject) |
diff --git a/server/controllers/api/server/follows.ts b/server/controllers/api/server/follows.ts index c759824e0..4b54afc8d 100644 --- a/server/controllers/api/server/follows.ts +++ b/server/controllers/api/server/follows.ts | |||
@@ -19,6 +19,7 @@ import { sendUndoFollow } from '../../../lib/activitypub/send/send-undo' | |||
19 | import { AccountInstance } from '../../../models/account/account-interface' | 19 | import { AccountInstance } from '../../../models/account/account-interface' |
20 | import { retryTransactionWrapper } from '../../../helpers/database-utils' | 20 | import { retryTransactionWrapper } from '../../../helpers/database-utils' |
21 | import { saveAccountAndServerIfNotExist } from '../../../lib/activitypub/account' | 21 | import { saveAccountAndServerIfNotExist } from '../../../lib/activitypub/account' |
22 | import { addFetchOutboxJob } from '../../../lib/activitypub/fetch' | ||
22 | 23 | ||
23 | const serverFollowsRouter = express.Router() | 24 | const serverFollowsRouter = express.Router() |
24 | 25 | ||
@@ -136,6 +137,8 @@ async function follow (fromAccount: AccountInstance, targetAccount: AccountInsta | |||
136 | if (accountFollow.state === 'pending') { | 137 | if (accountFollow.state === 'pending') { |
137 | await sendFollow(accountFollow, t) | 138 | await sendFollow(accountFollow, t) |
138 | } | 139 | } |
140 | |||
141 | await addFetchOutboxJob(targetAccount, t) | ||
139 | }) | 142 | }) |
140 | } catch (err) { | 143 | } catch (err) { |
141 | // Reset target account | 144 | // Reset target account |
diff --git a/server/helpers/activitypub.ts b/server/helpers/activitypub.ts index 04d85b8e6..fb4a43a01 100644 --- a/server/helpers/activitypub.ts +++ b/server/helpers/activitypub.ts | |||
@@ -46,14 +46,16 @@ function activityPubCollectionPagination (url: string, page: number, result: Res | |||
46 | orderedItems: result.data | 46 | orderedItems: result.data |
47 | } | 47 | } |
48 | 48 | ||
49 | const obj = { | 49 | if (page === 1) { |
50 | id: url, | 50 | return activityPubContextify({ |
51 | type: 'OrderedCollection', | 51 | id: url, |
52 | totalItems: result.total, | 52 | type: 'OrderedCollection', |
53 | orderedItems: orderedCollectionPagination | 53 | totalItems: result.total, |
54 | first: orderedCollectionPagination | ||
55 | }) | ||
54 | } | 56 | } |
55 | 57 | ||
56 | return activityPubContextify(obj) | 58 | return orderedCollectionPagination |
57 | } | 59 | } |
58 | 60 | ||
59 | function buildSignedActivity (byAccount: AccountInstance, data: Object) { | 61 | function buildSignedActivity (byAccount: AccountInstance, data: Object) { |
diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index 7c0640cc0..398691eba 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts | |||
@@ -228,6 +228,7 @@ const ACTIVITY_PUB = { | |||
228 | ACCEPT_HEADER: 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"', | 228 | ACCEPT_HEADER: 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"', |
229 | PUBLIC: 'https://www.w3.org/ns/activitystreams#Public', | 229 | PUBLIC: 'https://www.w3.org/ns/activitystreams#Public', |
230 | COLLECTION_ITEMS_PER_PAGE: 10, | 230 | COLLECTION_ITEMS_PER_PAGE: 10, |
231 | FETCH_PAGE_LIMIT: 100, | ||
231 | URL_MIME_TYPES: { | 232 | URL_MIME_TYPES: { |
232 | VIDEO: [ 'video/mp4', 'video/webm', 'video/ogg' ], // TODO: Merge with VIDEO_MIMETYPE_EXT | 233 | VIDEO: [ 'video/mp4', 'video/webm', 'video/ogg' ], // TODO: Merge with VIDEO_MIMETYPE_EXT |
233 | TORRENT: [ 'application/x-bittorrent' ], | 234 | TORRENT: [ 'application/x-bittorrent' ], |
diff --git a/server/lib/activitypub/fetch.ts b/server/lib/activitypub/fetch.ts new file mode 100644 index 000000000..fd2af0761 --- /dev/null +++ b/server/lib/activitypub/fetch.ts | |||
@@ -0,0 +1,15 @@ | |||
1 | import { Transaction } from 'sequelize' | ||
2 | import { AccountInstance } from '../../models/account/account-interface' | ||
3 | import { activitypubHttpJobScheduler, ActivityPubHttpPayload } from '../jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler' | ||
4 | |||
5 | async function addFetchOutboxJob (account: AccountInstance, t: Transaction) { | ||
6 | const jobPayload: ActivityPubHttpPayload = { | ||
7 | uris: [ account.outboxUrl ] | ||
8 | } | ||
9 | |||
10 | return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpFetcherHandler', jobPayload) | ||
11 | } | ||
12 | |||
13 | export { | ||
14 | addFetchOutboxJob | ||
15 | } | ||
diff --git a/server/lib/activitypub/index.ts b/server/lib/activitypub/index.ts index b1daa70bb..fcea662a6 100644 --- a/server/lib/activitypub/index.ts +++ b/server/lib/activitypub/index.ts | |||
@@ -1,6 +1,7 @@ | |||
1 | export * from './process' | 1 | export * from './process' |
2 | export * from './send' | 2 | export * from './send' |
3 | export * from './account' | 3 | export * from './account' |
4 | export * from './fetch' | ||
4 | export * from './share' | 5 | export * from './share' |
5 | export * from './video-channels' | 6 | export * from './video-channels' |
6 | export * from './videos' | 7 | export * from './videos' |
diff --git a/server/lib/activitypub/process/index.ts b/server/lib/activitypub/process/index.ts index e80b46b6f..c68312053 100644 --- a/server/lib/activitypub/process/index.ts +++ b/server/lib/activitypub/process/index.ts | |||
@@ -1,3 +1,4 @@ | |||
1 | export * from './process' | ||
1 | export * from './process-accept' | 2 | export * from './process-accept' |
2 | export * from './process-add' | 3 | export * from './process-add' |
3 | export * from './process-announce' | 4 | export * from './process-announce' |
diff --git a/server/lib/activitypub/process/process.ts b/server/lib/activitypub/process/process.ts new file mode 100644 index 000000000..613597341 --- /dev/null +++ b/server/lib/activitypub/process/process.ts | |||
@@ -0,0 +1,38 @@ | |||
1 | import { Activity, ActivityType } from '../../../../shared/models/activitypub/activity' | ||
2 | import { AccountInstance } from '../../../models/account/account-interface' | ||
3 | import { processAcceptActivity } from './process-accept' | ||
4 | import { processAddActivity } from './process-add' | ||
5 | import { processAnnounceActivity } from './process-announce' | ||
6 | import { processCreateActivity } from './process-create' | ||
7 | import { processDeleteActivity } from './process-delete' | ||
8 | import { processFollowActivity } from './process-follow' | ||
9 | import { processUndoActivity } from './process-undo' | ||
10 | import { processUpdateActivity } from './process-update' | ||
11 | import { logger } from '../../../helpers/logger' | ||
12 | |||
13 | const processActivity: { [ P in ActivityType ]: (activity: Activity, inboxAccount?: AccountInstance) => Promise<any> } = { | ||
14 | Create: processCreateActivity, | ||
15 | Add: processAddActivity, | ||
16 | Update: processUpdateActivity, | ||
17 | Delete: processDeleteActivity, | ||
18 | Follow: processFollowActivity, | ||
19 | Accept: processAcceptActivity, | ||
20 | Announce: processAnnounceActivity, | ||
21 | Undo: processUndoActivity | ||
22 | } | ||
23 | |||
24 | async function processActivities (activities: Activity[], inboxAccount?: AccountInstance) { | ||
25 | for (const activity of activities) { | ||
26 | const activityProcessor = processActivity[activity.type] | ||
27 | if (activityProcessor === undefined) { | ||
28 | logger.warn('Unknown activity type %s.', activity.type, { activityId: activity.id }) | ||
29 | continue | ||
30 | } | ||
31 | |||
32 | await activityProcessor(activity, inboxAccount) | ||
33 | } | ||
34 | } | ||
35 | |||
36 | export { | ||
37 | processActivities | ||
38 | } | ||
diff --git a/server/lib/activitypub/send/send-create.ts b/server/lib/activitypub/send/send-create.ts index 14b666fc9..df8e0a642 100644 --- a/server/lib/activitypub/send/send-create.ts +++ b/server/lib/activitypub/send/send-create.ts | |||
@@ -21,6 +21,8 @@ async function sendVideoAbuse (byAccount: AccountInstance, videoAbuse: VideoAbus | |||
21 | return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t) | 21 | return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t) |
22 | } | 22 | } |
23 | 23 | ||
24 | // async function sendCreateView () | ||
25 | |||
24 | async function createActivityData (url: string, byAccount: AccountInstance, object: any) { | 26 | async function createActivityData (url: string, byAccount: AccountInstance, object: any) { |
25 | const { to, cc } = await getAudience(byAccount) | 27 | const { to, cc } = await getAudience(byAccount) |
26 | const activity: ActivityCreate = { | 28 | const activity: ActivityCreate = { |
diff --git a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-fetcher-handler.ts b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-fetcher-handler.ts new file mode 100644 index 000000000..b8ead32a4 --- /dev/null +++ b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-fetcher-handler.ts | |||
@@ -0,0 +1,69 @@ | |||
1 | import { logger } from '../../../helpers' | ||
2 | import { buildSignedActivity } from '../../../helpers/activitypub' | ||
3 | import { doRequest } from '../../../helpers/requests' | ||
4 | import { database as db } from '../../../initializers' | ||
5 | import { ActivityPubHttpPayload } from './activitypub-http-job-scheduler' | ||
6 | import { processActivities } from '../../activitypub/process/process' | ||
7 | import { ACTIVITY_PUB } from '../../../initializers/constants' | ||
8 | |||
9 | async function process (payload: ActivityPubHttpPayload, jobId: number) { | ||
10 | logger.info('Processing ActivityPub fetcher in job %d.', jobId) | ||
11 | |||
12 | const options = { | ||
13 | method: 'GET', | ||
14 | uri: '', | ||
15 | json: true | ||
16 | } | ||
17 | |||
18 | for (const uri of payload.uris) { | ||
19 | options.uri = uri | ||
20 | logger.info('Fetching ActivityPub data on %s.', uri) | ||
21 | |||
22 | const response = await doRequest(options) | ||
23 | const firstBody = response.body | ||
24 | |||
25 | if (firstBody.first && Array.isArray(firstBody.first.orderedItems)) { | ||
26 | const activities = firstBody.first.orderedItems | ||
27 | |||
28 | logger.info('Processing %i items ActivityPub fetcher for %s.', activities.length, uri) | ||
29 | |||
30 | await processActivities(activities) | ||
31 | } | ||
32 | |||
33 | let limit = ACTIVITY_PUB.FETCH_PAGE_LIMIT | ||
34 | let i = 0 | ||
35 | let nextLink = firstBody.first.next | ||
36 | while (nextLink && i < limit) { | ||
37 | options.uri = nextLink | ||
38 | |||
39 | const { body } = await doRequest(options) | ||
40 | nextLink = body.nextLink | ||
41 | i++ | ||
42 | |||
43 | if (Array.isArray(body.orderedItems)) { | ||
44 | const activities = body.orderedItems | ||
45 | logger.info('Processing %i items ActivityPub fetcher for %s.', activities.length, uri) | ||
46 | |||
47 | await processActivities(activities) | ||
48 | } | ||
49 | } | ||
50 | } | ||
51 | } | ||
52 | |||
53 | function onError (err: Error, jobId: number) { | ||
54 | logger.error('Error when broadcasting ActivityPub request in job %d.', jobId, err) | ||
55 | return Promise.resolve() | ||
56 | } | ||
57 | |||
58 | function onSuccess (jobId: number) { | ||
59 | logger.info('Job %d is a success.', jobId) | ||
60 | return Promise.resolve() | ||
61 | } | ||
62 | |||
63 | // --------------------------------------------------------------------------- | ||
64 | |||
65 | export { | ||
66 | process, | ||
67 | onError, | ||
68 | onSuccess | ||
69 | } | ||
diff --git a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler.ts b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler.ts index e4f6c94a5..aef217ce7 100644 --- a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler.ts +++ b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler.ts | |||
@@ -2,16 +2,18 @@ import { JobScheduler, JobHandler } from '../job-scheduler' | |||
2 | 2 | ||
3 | import * as activitypubHttpBroadcastHandler from './activitypub-http-broadcast-handler' | 3 | import * as activitypubHttpBroadcastHandler from './activitypub-http-broadcast-handler' |
4 | import * as activitypubHttpUnicastHandler from './activitypub-http-unicast-handler' | 4 | import * as activitypubHttpUnicastHandler from './activitypub-http-unicast-handler' |
5 | import * as activitypubHttpFetcherHandler from './activitypub-http-fetcher-handler' | ||
5 | import { JobCategory } from '../../../../shared' | 6 | import { JobCategory } from '../../../../shared' |
6 | 7 | ||
7 | type ActivityPubHttpPayload = { | 8 | type ActivityPubHttpPayload = { |
8 | uris: string[] | 9 | uris: string[] |
9 | signatureAccountId: number | 10 | signatureAccountId?: number |
10 | body: any | 11 | body?: any |
11 | } | 12 | } |
12 | const jobHandlers: { [ handlerName: string ]: JobHandler<ActivityPubHttpPayload, void> } = { | 13 | const jobHandlers: { [ handlerName: string ]: JobHandler<ActivityPubHttpPayload, void> } = { |
13 | activitypubHttpBroadcastHandler, | 14 | activitypubHttpBroadcastHandler, |
14 | activitypubHttpUnicastHandler | 15 | activitypubHttpUnicastHandler, |
16 | activitypubHttpFetcherHandler | ||
15 | } | 17 | } |
16 | const jobCategory: JobCategory = 'activitypub-http' | 18 | const jobCategory: JobCategory = 'activitypub-http' |
17 | 19 | ||