Fetch outbox to grab old activities
authorChocobozzz <florian.bigard@gmail.com>
Wed, 22 Nov 2017 09:29:55 +0000 (10:29 +0100)
committerChocobozzz <florian.bigard@gmail.com>
Mon, 27 Nov 2017 18:40:53 +0000 (19:40 +0100)
15 files changed:
.codeclimate.yml
client/src/app/+admin/follows/followers-list/followers-list.component.html
client/src/app/+admin/follows/following-list/following-list.component.html
server/controllers/activitypub/inbox.ts
server/controllers/activitypub/outbox.ts
server/controllers/api/server/follows.ts
server/helpers/activitypub.ts
server/initializers/constants.ts
server/lib/activitypub/fetch.ts [new file with mode: 0644]
server/lib/activitypub/index.ts
server/lib/activitypub/process/index.ts
server/lib/activitypub/process/process.ts [new file with mode: 0644]
server/lib/activitypub/send/send-create.ts
server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-fetcher-handler.ts [new file with mode: 0644]
server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler.ts

index 2318cfa4b5a9fb58dec46a93ede19483370721e1..2b58e82b21c4abe1516b7155d19fb80fb9f91001 100644 (file)
@@ -6,7 +6,7 @@ engines:
     enabled: true
     config:
       languages:
-      - javascript
+      - typescript
   eslint:
     enabled: true
   fixme:
@@ -18,7 +18,6 @@ ratings:
 exclude_paths:
 - config/
 - node_modules/
-- client
 - scripts/
 - server/tests/
 - .tmp/
index 84c49ae80b86e7259581cf3d828d3a678f78bb15..473801822a02c5a19725134101cedca4558d3165 100644 (file)
@@ -8,7 +8,6 @@
     >
       <p-column field="id" header="ID"></p-column>
       <p-column field="follower.host" header="Host"></p-column>
-      <p-column field="email" header="Email"></p-column>
       <p-column field="follower.score" header="Score"></p-column>
       <p-column field="state" header="State"></p-column>
       <p-column field="createdAt" header="Created date" [sortable]="true"></p-column>
index dbc9852d0bee7cec54c0b4fe19b1ff63cda82424..a7308431209a657b5922848542ab16580f17534e 100644 (file)
@@ -8,7 +8,6 @@
     >
       <p-column field="id" header="ID"></p-column>
       <p-column field="following.host" header="Host"></p-column>
-      <p-column field="email" header="Email"></p-column>
       <p-column field="state" header="State"></p-column>
       <p-column field="createdAt" header="Created date" [sortable]="true"></p-column>
       <p-column header="Unfollow" styleClass="action-cell">
index 807d0bdf4e53b378ed932f7c371f75adbef01aa1..30e7f706b95448bf45d2ad012625d22c3b1f6775 100644 (file)
@@ -1,27 +1,10 @@
 import * as express from 'express'
-import { Activity, ActivityPubCollection, ActivityPubOrderedCollection, ActivityType, RootActivity } from '../../../shared'
+import { Activity, ActivityPubCollection, ActivityPubOrderedCollection, RootActivity } from '../../../shared'
 import { logger } from '../../helpers'
 import { isActivityValid } from '../../helpers/custom-validators/activitypub/activity'
-import { processCreateActivity, processUpdateActivity, processUndoActivity } from '../../lib'
-import { processAcceptActivity } from '../../lib/activitypub/process/process-accept'
-import { processAddActivity } from '../../lib/activitypub/process/process-add'
-import { processAnnounceActivity } from '../../lib/activitypub/process/process-announce'
-import { processDeleteActivity } from '../../lib/activitypub/process/process-delete'
-import { processFollowActivity } from '../../lib/activitypub/process/process-follow'
+import { processActivities } from '../../lib/activitypub/process/process'
 import { asyncMiddleware, checkSignature, localAccountValidator, signatureValidator } from '../../middlewares'
 import { activityPubValidator } from '../../middlewares/validators/activitypub/activity'
-import { AccountInstance } from '../../models/account/account-interface'
-
-const processActivity: { [ P in ActivityType ]: (activity: Activity, inboxAccount?: AccountInstance) => Promise<any> } = {
-  Create: processCreateActivity,
-  Add: processAddActivity,
-  Update: processUpdateActivity,
-  Delete: processDeleteActivity,
-  Follow: processFollowActivity,
-  Accept: processAcceptActivity,
-  Announce: processAnnounceActivity,
-  Undo: processUndoActivity
-}
 
 const inboxRouter = express.Router()
 
@@ -69,15 +52,3 @@ async function inboxController (req: express.Request, res: express.Response, nex
 
   res.status(204).end()
 }
-
-async function processActivities (activities: Activity[], inboxAccount?: AccountInstance) {
-  for (const activity of activities) {
-    const activityProcessor = processActivity[activity.type]
-    if (activityProcessor === undefined) {
-      logger.warn('Unknown activity type %s.', activity.type, { activityId: activity.id })
-      continue
-    }
-
-    await activityProcessor(activity, inboxAccount)
-  }
-}
index 396fa2db56ef7b7e66bf1170c1349f7c3ce4e0bb..1a74bde3390a5d5a7fa44b72c40259ce9ce52c10 100644 (file)
@@ -34,8 +34,6 @@ async function outboxController (req: express.Request, res: express.Response, ne
   const data = await db.Video.listAllAndSharedByAccountForOutbox(account.id, start, count)
   const activities: Activity[] = []
 
-  console.log(account.url)
-
   for (const video of data.data) {
     const videoObject = video.toActivityPubObject()
     let addActivity: ActivityAdd = await addActivityData(video.url, account, video, video.VideoChannel.url, videoObject)
index c759824e055cc45f70045455de68affa1a7e7247..4b54afc8d9f798ae4879b548ab7a20ba9489c972 100644 (file)
@@ -19,6 +19,7 @@ import { sendUndoFollow } from '../../../lib/activitypub/send/send-undo'
 import { AccountInstance } from '../../../models/account/account-interface'
 import { retryTransactionWrapper } from '../../../helpers/database-utils'
 import { saveAccountAndServerIfNotExist } from '../../../lib/activitypub/account'
+import { addFetchOutboxJob } from '../../../lib/activitypub/fetch'
 
 const serverFollowsRouter = express.Router()
 
@@ -136,6 +137,8 @@ async function follow (fromAccount: AccountInstance, targetAccount: AccountInsta
       if (accountFollow.state === 'pending') {
         await sendFollow(accountFollow, t)
       }
+
+      await addFetchOutboxJob(targetAccount, t)
     })
   } catch (err) {
     // Reset target account
index 04d85b8e619764758676b96e1de5fe472bbe6fde..fb4a43a0103934bc51c42405c3a12331105f0304 100644 (file)
@@ -46,14 +46,16 @@ function activityPubCollectionPagination (url: string, page: number, result: Res
     orderedItems: result.data
   }
 
-  const obj = {
-    id: url,
-    type: 'OrderedCollection',
-    totalItems: result.total,
-    orderedItems: orderedCollectionPagination
+  if (page === 1) {
+    return activityPubContextify({
+      id: url,
+      type: 'OrderedCollection',
+      totalItems: result.total,
+      first: orderedCollectionPagination
+    })
   }
 
-  return activityPubContextify(obj)
+  return orderedCollectionPagination
 }
 
 function buildSignedActivity (byAccount: AccountInstance, data: Object) {
index 7c0640cc0902588b8817c6807a9ac74bbbd71457..398691ebae90e6697a262ca32998a4c146c6c3e7 100644 (file)
@@ -228,6 +228,7 @@ const ACTIVITY_PUB = {
   ACCEPT_HEADER: 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
   PUBLIC: 'https://www.w3.org/ns/activitystreams#Public',
   COLLECTION_ITEMS_PER_PAGE: 10,
+  FETCH_PAGE_LIMIT: 100,
   URL_MIME_TYPES: {
     VIDEO: [ 'video/mp4', 'video/webm', 'video/ogg' ], // TODO: Merge with VIDEO_MIMETYPE_EXT
     TORRENT: [ 'application/x-bittorrent' ],
diff --git a/server/lib/activitypub/fetch.ts b/server/lib/activitypub/fetch.ts
new file mode 100644 (file)
index 0000000..fd2af07
--- /dev/null
@@ -0,0 +1,15 @@
+import { Transaction } from 'sequelize'
+import { AccountInstance } from '../../models/account/account-interface'
+import { activitypubHttpJobScheduler, ActivityPubHttpPayload } from '../jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler'
+
+async function addFetchOutboxJob (account: AccountInstance, t: Transaction) {
+  const jobPayload: ActivityPubHttpPayload = {
+    uris: [ account.outboxUrl ]
+  }
+
+  return activitypubHttpJobScheduler.createJob(t, 'activitypubHttpFetcherHandler', jobPayload)
+}
+
+export {
+  addFetchOutboxJob
+}
index b1daa70bb49faefee711e5cfd9ba50c3a6cbceb5..fcea662a6e2cac979642f1c56d34aa9745a6134d 100644 (file)
@@ -1,6 +1,7 @@
 export * from './process'
 export * from './send'
 export * from './account'
+export * from './fetch'
 export * from './share'
 export * from './video-channels'
 export * from './videos'
index e80b46b6f2cbd8324a7a8b5a250c7926fc68c86b..c68312053a0d4026eb2363b06c786dbbf1f346a0 100644 (file)
@@ -1,3 +1,4 @@
+export * from './process'
 export * from './process-accept'
 export * from './process-add'
 export * from './process-announce'
diff --git a/server/lib/activitypub/process/process.ts b/server/lib/activitypub/process/process.ts
new file mode 100644 (file)
index 0000000..6135973
--- /dev/null
@@ -0,0 +1,38 @@
+import { Activity, ActivityType } from '../../../../shared/models/activitypub/activity'
+import { AccountInstance } from '../../../models/account/account-interface'
+import { processAcceptActivity } from './process-accept'
+import { processAddActivity } from './process-add'
+import { processAnnounceActivity } from './process-announce'
+import { processCreateActivity } from './process-create'
+import { processDeleteActivity } from './process-delete'
+import { processFollowActivity } from './process-follow'
+import { processUndoActivity } from './process-undo'
+import { processUpdateActivity } from './process-update'
+import { logger } from '../../../helpers/logger'
+
+const processActivity: { [ P in ActivityType ]: (activity: Activity, inboxAccount?: AccountInstance) => Promise<any> } = {
+  Create: processCreateActivity,
+  Add: processAddActivity,
+  Update: processUpdateActivity,
+  Delete: processDeleteActivity,
+  Follow: processFollowActivity,
+  Accept: processAcceptActivity,
+  Announce: processAnnounceActivity,
+  Undo: processUndoActivity
+}
+
+async function processActivities (activities: Activity[], inboxAccount?: AccountInstance) {
+  for (const activity of activities) {
+    const activityProcessor = processActivity[activity.type]
+    if (activityProcessor === undefined) {
+      logger.warn('Unknown activity type %s.', activity.type, { activityId: activity.id })
+      continue
+    }
+
+    await activityProcessor(activity, inboxAccount)
+  }
+}
+
+export {
+  processActivities
+}
index 14b666fc9edc61828a9795fa7cafb4464a69da48..df8e0a64211021b7c2b2a746e1b2236b2efdfe1a 100644 (file)
@@ -21,6 +21,8 @@ async function sendVideoAbuse (byAccount: AccountInstance, videoAbuse: VideoAbus
   return unicastTo(data, byAccount, video.VideoChannel.Account.sharedInboxUrl, t)
 }
 
+// async function sendCreateView ()
+
 async function createActivityData (url: string, byAccount: AccountInstance, object: any) {
   const { to, cc } = await getAudience(byAccount)
   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 (file)
index 0000000..b8ead32
--- /dev/null
@@ -0,0 +1,69 @@
+import { logger } from '../../../helpers'
+import { buildSignedActivity } from '../../../helpers/activitypub'
+import { doRequest } from '../../../helpers/requests'
+import { database as db } from '../../../initializers'
+import { ActivityPubHttpPayload } from './activitypub-http-job-scheduler'
+import { processActivities } from '../../activitypub/process/process'
+import { ACTIVITY_PUB } from '../../../initializers/constants'
+
+async function process (payload: ActivityPubHttpPayload, jobId: number) {
+  logger.info('Processing ActivityPub fetcher in job %d.', jobId)
+
+  const options = {
+    method: 'GET',
+    uri: '',
+    json: true
+  }
+
+  for (const uri of payload.uris) {
+    options.uri = uri
+    logger.info('Fetching ActivityPub data on %s.', uri)
+
+    const response = await doRequest(options)
+    const firstBody = response.body
+
+    if (firstBody.first && Array.isArray(firstBody.first.orderedItems)) {
+      const activities = firstBody.first.orderedItems
+
+      logger.info('Processing %i items ActivityPub fetcher for %s.', activities.length, uri)
+
+      await processActivities(activities)
+    }
+
+    let limit = ACTIVITY_PUB.FETCH_PAGE_LIMIT
+    let i = 0
+    let nextLink = firstBody.first.next
+    while (nextLink && i < limit) {
+      options.uri = nextLink
+
+      const { body } = await doRequest(options)
+      nextLink = body.nextLink
+      i++
+
+      if (Array.isArray(body.orderedItems)) {
+        const activities = body.orderedItems
+        logger.info('Processing %i items ActivityPub fetcher for %s.', activities.length, uri)
+
+        await processActivities(activities)
+      }
+    }
+  }
+}
+
+function onError (err: Error, jobId: number) {
+  logger.error('Error when broadcasting ActivityPub request in job %d.', jobId, err)
+  return Promise.resolve()
+}
+
+function onSuccess (jobId: number) {
+  logger.info('Job %d is a success.', jobId)
+  return Promise.resolve()
+}
+
+// ---------------------------------------------------------------------------
+
+export {
+  process,
+  onError,
+  onSuccess
+}
index e4f6c94a57acf32fd9098828d55c367519314505..aef217ce71c0df6766164142ebe69a4b5c89784d 100644 (file)
@@ -2,16 +2,18 @@ import { JobScheduler, JobHandler } from '../job-scheduler'
 
 import * as activitypubHttpBroadcastHandler from './activitypub-http-broadcast-handler'
 import * as activitypubHttpUnicastHandler from './activitypub-http-unicast-handler'
+import * as activitypubHttpFetcherHandler from './activitypub-http-fetcher-handler'
 import { JobCategory } from '../../../../shared'
 
 type ActivityPubHttpPayload = {
   uris: string[]
-  signatureAccountId: number
-  body: any
+  signatureAccountId?: number
+  body?: any
 }
 const jobHandlers: { [ handlerName: string ]: JobHandler<ActivityPubHttpPayload, void> } = {
   activitypubHttpBroadcastHandler,
-  activitypubHttpUnicastHandler
+  activitypubHttpUnicastHandler,
+  activitypubHttpFetcherHandler
 }
 const jobCategory: JobCategory = 'activitypub-http'