Correctly send Flag/Dislike/View activities
authorChocobozzz <me@florianbigard.com>
Tue, 15 Jan 2019 13:52:33 +0000 (14:52 +0100)
committerChocobozzz <me@florianbigard.com>
Tue, 15 Jan 2019 13:59:40 +0000 (14:59 +0100)
server/controllers/activitypub/client.ts
server/controllers/api/videos/abuse.ts
server/controllers/api/videos/index.ts
server/lib/activitypub/send/send-create.ts
server/lib/activitypub/send/send-dislike.ts [new file with mode: 0644]
server/lib/activitypub/send/send-flag.ts [new file with mode: 0644]
server/lib/activitypub/send/send-undo.ts
server/lib/activitypub/send/send-view.ts [new file with mode: 0644]
server/lib/activitypub/video-rates.ts

index 1a4e28dc8856e26a3b0e5bf24fe74775ced4a402..7e87f6f3baad1f95707e746292c474dd3bd0dc72 100644 (file)
@@ -3,22 +3,18 @@ import * as express from 'express'
 import { VideoPrivacy, VideoRateType } from '../../../shared/models/videos'
 import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub'
 import { CONFIG, ROUTE_CACHE_LIFETIME } from '../../initializers'
-import { buildAnnounceWithVideoAudience, buildDislikeActivity, buildLikeActivity } from '../../lib/activitypub/send'
+import { buildAnnounceWithVideoAudience, buildLikeActivity } from '../../lib/activitypub/send'
 import { audiencify, getAudience } from '../../lib/activitypub/audience'
 import { buildCreateActivity } from '../../lib/activitypub/send/send-create'
 import {
   asyncMiddleware,
-  videosShareValidator,
   executeIfActivityPub,
   localAccountValidator,
   localVideoChannelValidator,
-  videosCustomGetValidator
+  videosCustomGetValidator,
+  videosShareValidator
 } from '../../middlewares'
-import {
-  getAccountVideoRateValidator,
-  videoCommentGetValidator,
-  videosGetValidator
-} from '../../middlewares/validators'
+import { getAccountVideoRateValidator, videoCommentGetValidator, videosGetValidator } from '../../middlewares/validators'
 import { AccountModel } from '../../models/account/account'
 import { ActorModel } from '../../models/activitypub/actor'
 import { ActorFollowModel } from '../../models/activitypub/actor-follow'
@@ -40,6 +36,7 @@ import { VideoCaptionModel } from '../../models/video/video-caption'
 import { videoRedundancyGetValidator } from '../../middlewares/validators/redundancy'
 import { getServerActor } from '../../helpers/utils'
 import { VideoRedundancyModel } from '../../models/redundancy/video-redundancy'
+import { buildDislikeActivity } from '../../lib/activitypub/send/send-dislike'
 
 const activityPubClientRouter = express.Router()
 
@@ -156,7 +153,7 @@ function getAccountVideoRate (rateType: VideoRateType) {
     const url = getRateUrl(rateType, byActor, accountVideoRate.Video)
     const APObject = rateType === 'like'
       ? buildLikeActivity(url, byActor, accountVideoRate.Video)
-      : buildCreateActivity(url, byActor, buildDislikeActivity(url, byActor, accountVideoRate.Video))
+      : buildDislikeActivity(url, byActor, accountVideoRate.Video)
 
     return activityPubResponse(activityPubContextify(APObject), res)
   }
index fe0a95cd51780eeacfe7c268896a7a3f77a8d636..32f9c47937d10b11ba9709824c51172e52b245f4 100644 (file)
@@ -3,7 +3,6 @@ import { UserRight, VideoAbuseCreate, VideoAbuseState } from '../../../../shared
 import { logger } from '../../../helpers/logger'
 import { getFormattedObjects } from '../../../helpers/utils'
 import { sequelizeTypescript } from '../../../initializers'
-import { sendVideoAbuse } from '../../../lib/activitypub/send'
 import {
   asyncMiddleware,
   asyncRetryTransactionMiddleware,
@@ -23,6 +22,7 @@ import { VideoAbuseModel } from '../../../models/video/video-abuse'
 import { auditLoggerFactory, VideoAbuseAuditView } from '../../../helpers/audit-logger'
 import { UserModel } from '../../../models/account/user'
 import { Notifier } from '../../../lib/notifier'
+import { sendVideoAbuse } from '../../../lib/activitypub/send/send-flag'
 
 const auditLogger = auditLoggerFactory('abuse')
 const abuseVideoRouter = express.Router()
index 2b2dfa7ca40bbbcf999a5de07dfdc662217c81d5..8414ca42ccff32fdc64cb0bea914edded4c3ff28 100644 (file)
@@ -23,7 +23,6 @@ import {
   fetchRemoteVideoDescription,
   getVideoActivityPubUrl
 } from '../../../lib/activitypub'
-import { sendCreateView } from '../../../lib/activitypub/send'
 import { JobQueue } from '../../../lib/job-queue'
 import { Redis } from '../../../lib/redis'
 import {
@@ -59,6 +58,7 @@ import { resetSequelizeInstance } from '../../../helpers/database-utils'
 import { move } from 'fs-extra'
 import { watchingRouter } from './watching'
 import { Notifier } from '../../../lib/notifier'
+import { sendView } from '../../../lib/activitypub/send/send-view'
 
 const auditLogger = auditLoggerFactory('videos')
 const videosRouter = express.Router()
@@ -422,7 +422,7 @@ async function viewVideo (req: express.Request, res: express.Response) {
   ])
 
   const serverActor = await getServerActor()
-  await sendCreateView(serverActor, videoInstance, undefined)
+  await sendView(serverActor, videoInstance, undefined)
 
   return res.status(204).end()
 }
index e3fca0a17adf108f4f93a1f123782dc03c00343e..73e667ad4252438d8832c61008c37ad823fbfdd9 100644 (file)
@@ -3,9 +3,7 @@ import { ActivityAudience, ActivityCreate } from '../../../../shared/models/acti
 import { VideoPrivacy } from '../../../../shared/models/videos'
 import { ActorModel } from '../../../models/activitypub/actor'
 import { VideoModel } from '../../../models/video/video'
-import { VideoAbuseModel } from '../../../models/video/video-abuse'
 import { VideoCommentModel } from '../../../models/video/video-comment'
-import { getVideoAbuseActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoViewActivityPubUrl } from '../url'
 import { broadcastToActors, broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './utils'
 import { audiencify, getActorsInvolvedInVideo, getAudience, getAudienceFromFollowersOf, getVideoCommentAudience } from '../audience'
 import { logger } from '../../../helpers/logger'
@@ -25,20 +23,6 @@ async function sendCreateVideo (video: VideoModel, t: Transaction) {
   return broadcastToFollowers(createActivity, byActor, [ byActor ], t)
 }
 
-async function sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel, video: VideoModel) {
-  if (!video.VideoChannel.Account.Actor.serverId) return // Local
-
-  const url = getVideoAbuseActivityPubUrl(videoAbuse)
-
-  logger.info('Creating job to send video abuse %s.', url)
-
-  // Custom audience, we only send the abuse to the origin instance
-  const audience = { to: [ video.VideoChannel.Account.Actor.url ], cc: [] }
-  const createActivity = buildCreateActivity(url, byActor, videoAbuse.toActivityPubObject(), audience)
-
-  return unicastTo(createActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
-}
-
 async function sendCreateCacheFile (byActor: ActorModel, fileRedundancy: VideoRedundancyModel) {
   logger.info('Creating job to send file cache of %s.', fileRedundancy.url)
 
@@ -91,37 +75,6 @@ async function sendCreateVideoComment (comment: VideoCommentModel, t: Transactio
   return unicastTo(createActivity, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
 }
 
-async function sendCreateView (byActor: ActorModel, video: VideoModel, t: Transaction) {
-  logger.info('Creating job to send view of %s.', video.url)
-
-  const url = getVideoViewActivityPubUrl(byActor, video)
-  const viewActivity = buildViewActivity(url, byActor, video)
-
-  return sendVideoRelatedCreateActivity({
-    // Use the server actor to send the view
-    byActor,
-    video,
-    url,
-    object: viewActivity,
-    transaction: t
-  })
-}
-
-async function sendCreateDislike (byActor: ActorModel, video: VideoModel, t: Transaction) {
-  logger.info('Creating job to dislike %s.', video.url)
-
-  const url = getVideoDislikeActivityPubUrl(byActor, video)
-  const dislikeActivity = buildDislikeActivity(url, byActor, video)
-
-  return sendVideoRelatedCreateActivity({
-    byActor,
-    video,
-    url,
-    object: dislikeActivity,
-    transaction: t
-  })
-}
-
 function buildCreateActivity (url: string, byActor: ActorModel, object: any, audience?: ActivityAudience): ActivityCreate {
   if (!audience) audience = getAudience(byActor)
 
@@ -136,33 +89,11 @@ function buildCreateActivity (url: string, byActor: ActorModel, object: any, aud
   )
 }
 
-function buildDislikeActivity (url: string, byActor: ActorModel, video: VideoModel) {
-  return {
-    id: url,
-    type: 'Dislike',
-    actor: byActor.url,
-    object: video.url
-  }
-}
-
-function buildViewActivity (url: string, byActor: ActorModel, video: VideoModel) {
-  return {
-    id: url,
-    type: 'View',
-    actor: byActor.url,
-    object: video.url
-  }
-}
-
 // ---------------------------------------------------------------------------
 
 export {
   sendCreateVideo,
-  sendVideoAbuse,
   buildCreateActivity,
-  sendCreateView,
-  sendCreateDislike,
-  buildDislikeActivity,
   sendCreateVideoComment,
   sendCreateCacheFile
 }
diff --git a/server/lib/activitypub/send/send-dislike.ts b/server/lib/activitypub/send/send-dislike.ts
new file mode 100644 (file)
index 0000000..a88436f
--- /dev/null
@@ -0,0 +1,41 @@
+import { Transaction } from 'sequelize'
+import { ActorModel } from '../../../models/activitypub/actor'
+import { VideoModel } from '../../../models/video/video'
+import { getVideoDislikeActivityPubUrl } from '../url'
+import { logger } from '../../../helpers/logger'
+import { ActivityAudience, ActivityDislike } from '../../../../shared/models/activitypub'
+import { sendVideoRelatedActivity } from './utils'
+import { audiencify, getAudience } from '../audience'
+
+async function sendDislike (byActor: ActorModel, video: VideoModel, t: Transaction) {
+  logger.info('Creating job to dislike %s.', video.url)
+
+  const activityBuilder = (audience: ActivityAudience) => {
+    const url = getVideoDislikeActivityPubUrl(byActor, video)
+
+    return buildDislikeActivity(url, byActor, video, audience)
+  }
+
+  return sendVideoRelatedActivity(activityBuilder, { byActor, video, transaction: t })
+}
+
+function buildDislikeActivity (url: string, byActor: ActorModel, video: VideoModel, audience?: ActivityAudience): ActivityDislike {
+  if (!audience) audience = getAudience(byActor)
+
+  return audiencify(
+    {
+      id: url,
+      type: 'Dislike' as 'Dislike',
+      actor: byActor.url,
+      object: video.url
+    },
+    audience
+  )
+}
+
+// ---------------------------------------------------------------------------
+
+export {
+  sendDislike,
+  buildDislikeActivity
+}
diff --git a/server/lib/activitypub/send/send-flag.ts b/server/lib/activitypub/send/send-flag.ts
new file mode 100644 (file)
index 0000000..96a7311
--- /dev/null
@@ -0,0 +1,39 @@
+import { ActorModel } from '../../../models/activitypub/actor'
+import { VideoModel } from '../../../models/video/video'
+import { VideoAbuseModel } from '../../../models/video/video-abuse'
+import { getVideoAbuseActivityPubUrl } from '../url'
+import { unicastTo } from './utils'
+import { logger } from '../../../helpers/logger'
+import { ActivityAudience, ActivityFlag } from '../../../../shared/models/activitypub'
+import { audiencify, getAudience } from '../audience'
+
+async function sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel, video: VideoModel) {
+  if (!video.VideoChannel.Account.Actor.serverId) return // Local user
+
+  const url = getVideoAbuseActivityPubUrl(videoAbuse)
+
+  logger.info('Creating job to send video abuse %s.', url)
+
+  // Custom audience, we only send the abuse to the origin instance
+  const audience = { to: [ video.VideoChannel.Account.Actor.url ], cc: [] }
+  const flagActivity = buildFlagActivity(url, byActor, videoAbuse, audience)
+
+  return unicastTo(flagActivity, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
+}
+
+function buildFlagActivity (url: string, byActor: ActorModel, videoAbuse: VideoAbuseModel, audience: ActivityAudience): ActivityFlag {
+  if (!audience) audience = getAudience(byActor)
+
+  const activity = Object.assign(
+    { id: url, actor: byActor.url },
+    videoAbuse.toActivityPubObject()
+  )
+
+  return audiencify(activity, audience)
+}
+
+// ---------------------------------------------------------------------------
+
+export {
+  sendVideoAbuse
+}
index bf1b6e1177fbaccbce7c7c86d3f78cd57a811a3f..eb18a6cb6b7976ab909c46d6e68468638394b499 100644 (file)
@@ -2,7 +2,7 @@ import { Transaction } from 'sequelize'
 import {
   ActivityAnnounce,
   ActivityAudience,
-  ActivityCreate,
+  ActivityCreate, ActivityDislike,
   ActivityFollow,
   ActivityLike,
   ActivityUndo
@@ -13,13 +13,14 @@ import { VideoModel } from '../../../models/video/video'
 import { getActorFollowActivityPubUrl, getUndoActivityPubUrl, getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from '../url'
 import { broadcastToFollowers, sendVideoRelatedActivity, unicastTo } from './utils'
 import { audiencify, getAudience } from '../audience'
-import { buildCreateActivity, buildDislikeActivity } from './send-create'
+import { buildCreateActivity } from './send-create'
 import { buildFollowActivity } from './send-follow'
 import { buildLikeActivity } from './send-like'
 import { VideoShareModel } from '../../../models/video/video-share'
 import { buildAnnounceWithVideoAudience } from './send-announce'
 import { logger } from '../../../helpers/logger'
 import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
+import { buildDislikeActivity } from './send-dislike'
 
 async function sendUndoFollow (actorFollow: ActorFollowModel, t: Transaction) {
   const me = actorFollow.ActorFollower
@@ -65,9 +66,8 @@ async function sendUndoDislike (byActor: ActorModel, video: VideoModel, t: Trans
 
   const dislikeUrl = getVideoDislikeActivityPubUrl(byActor, video)
   const dislikeActivity = buildDislikeActivity(dislikeUrl, byActor, video)
-  const createDislikeActivity = buildCreateActivity(dislikeUrl, byActor, dislikeActivity)
 
-  return sendUndoVideoRelatedActivity({ byActor, video, url: dislikeUrl, activity: createDislikeActivity, transaction: t })
+  return sendUndoVideoRelatedActivity({ byActor, video, url: dislikeUrl, activity: dislikeActivity, transaction: t })
 }
 
 async function sendUndoCacheFile (byActor: ActorModel, redundancyModel: VideoRedundancyModel, t: Transaction) {
@@ -94,7 +94,7 @@ export {
 function undoActivityData (
   url: string,
   byActor: ActorModel,
-  object: ActivityFollow | ActivityLike | ActivityCreate | ActivityAnnounce,
+  object: ActivityFollow | ActivityLike | ActivityDislike | ActivityCreate | ActivityAnnounce,
   audience?: ActivityAudience
 ): ActivityUndo {
   if (!audience) audience = getAudience(byActor)
@@ -114,7 +114,7 @@ async function sendUndoVideoRelatedActivity (options: {
   byActor: ActorModel,
   video: VideoModel,
   url: string,
-  activity: ActivityFollow | ActivityLike | ActivityCreate | ActivityAnnounce,
+  activity: ActivityFollow | ActivityLike | ActivityDislike | ActivityCreate | ActivityAnnounce,
   transaction: Transaction
 }) {
   const activityBuilder = (audience: ActivityAudience) => {
diff --git a/server/lib/activitypub/send/send-view.ts b/server/lib/activitypub/send/send-view.ts
new file mode 100644 (file)
index 0000000..8ad126b
--- /dev/null
@@ -0,0 +1,40 @@
+import { Transaction } from 'sequelize'
+import { ActivityAudience, ActivityView } from '../../../../shared/models/activitypub'
+import { ActorModel } from '../../../models/activitypub/actor'
+import { VideoModel } from '../../../models/video/video'
+import { getVideoLikeActivityPubUrl } from '../url'
+import { sendVideoRelatedActivity } from './utils'
+import { audiencify, getAudience } from '../audience'
+import { logger } from '../../../helpers/logger'
+
+async function sendView (byActor: ActorModel, video: VideoModel, t: Transaction) {
+  logger.info('Creating job to send view of %s.', video.url)
+
+  const activityBuilder = (audience: ActivityAudience) => {
+    const url = getVideoLikeActivityPubUrl(byActor, video)
+
+    return buildViewActivity(url, byActor, video, audience)
+  }
+
+  return sendVideoRelatedActivity(activityBuilder, { byActor, video, transaction: t })
+}
+
+function buildViewActivity (url: string, byActor: ActorModel, video: VideoModel, audience?: ActivityAudience): ActivityView {
+  if (!audience) audience = getAudience(byActor)
+
+  return audiencify(
+    {
+      id: url,
+      type: 'View' as 'View',
+      actor: byActor.url,
+      object: video.url
+    },
+    audience
+  )
+}
+
+// ---------------------------------------------------------------------------
+
+export {
+  sendView
+}
index 45a2b22eaea405a1db2b3a98a8fcdad91dd8d4db..7aac7911841e24fc05a4dcd875b07a14037499c4 100644 (file)
@@ -1,7 +1,7 @@
 import { Transaction } from 'sequelize'
 import { AccountModel } from '../../models/account/account'
 import { VideoModel } from '../../models/video/video'
-import { sendCreateDislike, sendLike, sendUndoDislike, sendUndoLike } from './send'
+import { sendLike, sendUndoDislike, sendUndoLike } from './send'
 import { VideoRateType } from '../../../shared/models/videos'
 import * as Bluebird from 'bluebird'
 import { getOrCreateActorAndServerAndModel } from './actor'
@@ -12,6 +12,7 @@ import { doRequest } from '../../helpers/requests'
 import { checkUrlsSameHost, getAPId } from '../../helpers/activitypub'
 import { ActorModel } from '../../models/activitypub/actor'
 import { getVideoDislikeActivityPubUrl, getVideoLikeActivityPubUrl } from './url'
+import { sendDislike } from './send/send-dislike'
 
 async function createRates (ratesUrl: string[], video: VideoModel, rate: VideoRateType) {
   let rateCounts = 0
@@ -82,7 +83,7 @@ async function sendVideoRateChange (account: AccountModel,
   // Like
   if (likes > 0) await sendLike(actor, video, t)
   // Dislike
-  if (dislikes > 0) await sendCreateDislike(actor, video, t)
+  if (dislikes > 0) await sendDislike(actor, video, t)
 }
 
 function getRateUrl (rateType: VideoRateType, actor: ActorModel, video: VideoModel) {