1
0
Fork 0

Try to refractor activities sending

There is still a need for work on this part though
This commit is contained in:
Chocobozzz 2018-03-27 13:33:56 +02:00
parent da99ccf268
commit 07197db4c5
No known key found for this signature in database
GPG key ID: 583A612D890159BE
11 changed files with 100 additions and 169 deletions

View file

@ -4,7 +4,7 @@ import { VideoPrivacy } from '../../../shared/models/videos'
import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub'
import { pageToStartAndCount } from '../../helpers/core-utils'
import { ACTIVITY_PUB, CONFIG } from '../../initializers'
import { buildVideoAnnounceToFollowers } from '../../lib/activitypub/send'
import { buildVideoAnnounce } from '../../lib/activitypub/send'
import { audiencify, getAudience } from '../../lib/activitypub/send/misc'
import { createActivityData } from '../../lib/activitypub/send/send-create'
import { asyncMiddleware, executeIfActivityPub, localAccountValidator } from '../../middlewares'
@ -130,7 +130,7 @@ async function videoController (req: express.Request, res: express.Response, nex
async function videoAnnounceController (req: express.Request, res: express.Response, next: express.NextFunction) {
const share = res.locals.videoShare as VideoShareModel
const object = await buildVideoAnnounceToFollowers(share.Actor, share, res.locals.video, undefined)
const object = await buildVideoAnnounce(share.Actor, share, res.locals.video, undefined)
return res.json(activityPubContextify(object))
}

View file

@ -20,7 +20,7 @@ import {
VIDEO_PRIVACIES
} from '../../../initializers'
import { fetchRemoteVideoDescription, getVideoActivityPubUrl, shareVideoByServerAndChannel } from '../../../lib/activitypub'
import { sendCreateVideo, sendCreateViewToOrigin, sendCreateViewToVideoFollowers, sendUpdateVideo } from '../../../lib/activitypub/send'
import { sendCreateVideo, sendCreateView, sendUpdateVideo } from '../../../lib/activitypub/send'
import { JobQueue } from '../../../lib/job-queue'
import { Redis } from '../../../lib/redis'
import {
@ -365,11 +365,7 @@ async function viewVideo (req: express.Request, res: express.Response) {
const serverAccount = await getServerActor()
if (videoInstance.isOwned()) {
await sendCreateViewToVideoFollowers(serverAccount, videoInstance, undefined)
} else {
await sendCreateViewToOrigin(serverAccount, videoInstance, undefined)
}
await sendCreateView(serverAccount, videoInstance, undefined)
return res.status(204).end()
}

View file

@ -3,7 +3,7 @@ import { UserVideoRateUpdate } from '../../../../shared'
import { retryTransactionWrapper } from '../../../helpers/database-utils'
import { logger } from '../../../helpers/logger'
import { sequelizeTypescript, VIDEO_RATE_TYPES } from '../../../initializers'
import { sendVideoRateChangeToFollowers, sendVideoRateChangeToOrigin } from '../../../lib/activitypub'
import { sendVideoRateChange } from '../../../lib/activitypub'
import { asyncMiddleware, authenticate, videoRateValidator } from '../../../middlewares'
import { AccountModel } from '../../../models/account/account'
import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
@ -83,11 +83,7 @@ async function rateVideo (req: express.Request, res: express.Response) {
// It is useful for the user to have a feedback
await videoInstance.increment(incrementQuery, sequelizeOptions)
if (videoInstance.isOwned()) {
await sendVideoRateChangeToFollowers(accountInstance, videoInstance, likesToIncrement, dislikesToIncrement, t)
} else {
await sendVideoRateChangeToOrigin(accountInstance, videoInstance, likesToIncrement, dislikesToIncrement, t)
}
await sendVideoRateChange(accountInstance, videoInstance, likesToIncrement, dislikesToIncrement, t)
})
logger.info('Account video rate for video %s of account %s updated.', videoInstance.name, accountInstance.name)

View file

@ -4,9 +4,6 @@ export * from './actor'
export * from './fetch'
export * from './share'
export * from './videos'
export * from './video-comments'
export * from './video-rates'
export * from './url'
export { videoCommentActivityObjectToDBAttributes } from './video-comments'
export { addVideoComments } from './video-comments'
export { addVideoComment } from './video-comments'
export { sendVideoRateChangeToFollowers } from './video-rates'
export { sendVideoRateChangeToOrigin } from './video-rates'

View file

@ -5,7 +5,7 @@ import { VideoModel } from '../../../models/video/video'
import { VideoShareModel } from '../../../models/video/video-share'
import { broadcastToFollowers, getActorsInvolvedInVideo, getAudience, getObjectFollowersAudience } from './misc'
async function buildVideoAnnounceToFollowers (byActor: ActorModel, videoShare: VideoShareModel, video: VideoModel, t: Transaction) {
async function buildVideoAnnounce (byActor: ActorModel, videoShare: VideoShareModel, video: VideoModel, t: Transaction) {
const announcedObject = video.url
const accountsToForwardView = await getActorsInvolvedInVideo(video, t)
@ -13,8 +13,8 @@ async function buildVideoAnnounceToFollowers (byActor: ActorModel, videoShare: V
return announceActivityData(videoShare.url, byActor, announcedObject, t, audience)
}
async function sendVideoAnnounceToFollowers (byActor: ActorModel, videoShare: VideoShareModel, video: VideoModel, t: Transaction) {
const data = await buildVideoAnnounceToFollowers(byActor, videoShare, video, t)
async function sendVideoAnnounce (byActor: ActorModel, videoShare: VideoShareModel, video: VideoModel, t: Transaction) {
const data = await buildVideoAnnounce(byActor, videoShare, video, t)
return broadcastToFollowers(data, byActor, [ byActor ], t)
}
@ -43,7 +43,7 @@ async function announceActivityData (
// ---------------------------------------------------------------------------
export {
sendVideoAnnounceToFollowers,
sendVideoAnnounce,
announceActivityData,
buildVideoAnnounceToFollowers
buildVideoAnnounce
}

View file

@ -40,29 +40,9 @@ async function sendVideoAbuse (byActor: ActorModel, videoAbuse: VideoAbuseModel,
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
}
async function sendCreateVideoCommentToOrigin (comment: VideoCommentModel, t: Transaction) {
const byActor = comment.Account.Actor
const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
const commentObject = comment.toActivityPubObject(threadParentComments)
async function sendCreateVideoComment (comment: VideoCommentModel, t: Transaction) {
const isOrigin = comment.Video.isOwned()
const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
actorsInvolvedInComment.push(byActor)
const audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment)
const data = await createActivityData(comment.url, byActor, commentObject, t, audience)
// This was a reply, send it to the parent actors
const actorsException = [ byActor ]
await broadcastToActors(data, byActor, threadParentComments.map(c => c.Account.Actor), actorsException)
// Broadcast to our followers
await broadcastToFollowers(data, byActor, [ byActor ], t)
// Send to origin
return unicastTo(data, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
}
async function sendCreateVideoCommentToVideoFollowers (comment: VideoCommentModel, t: Transaction) {
const byActor = comment.Account.Actor
const threadParentComments = await VideoCommentModel.listThreadParentComments(comment, t)
const commentObject = comment.toActivityPubObject(threadParentComments)
@ -70,75 +50,82 @@ async function sendCreateVideoCommentToVideoFollowers (comment: VideoCommentMode
const actorsInvolvedInComment = await getActorsInvolvedInVideo(comment.Video, t)
actorsInvolvedInComment.push(byActor)
const audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, true)
const parentsCommentActors = threadParentComments.map(c => c.Account.Actor)
let audience: ActivityAudience
if (isOrigin) {
audience = getVideoCommentAudience(comment, threadParentComments, actorsInvolvedInComment, isOrigin)
} else {
audience = getObjectFollowersAudience(actorsInvolvedInComment.concat(parentsCommentActors))
}
const data = await createActivityData(comment.url, byActor, commentObject, t, audience)
// This was a reply, send it to the parent actors
const actorsException = [ byActor ]
await broadcastToActors(data, byActor, threadParentComments.map(c => c.Account.Actor), actorsException)
await broadcastToActors(data, byActor, parentsCommentActors, actorsException)
// Broadcast to our followers
await broadcastToFollowers(data, byActor, [ byActor ], t)
// Send to actors involved in the comment
return broadcastToFollowers(data, byActor, actorsInvolvedInComment, t, actorsException)
if (isOrigin) return broadcastToFollowers(data, byActor, actorsInvolvedInComment, t, actorsException)
// Send to origin
return unicastTo(data, byActor, comment.Video.VideoChannel.Account.Actor.sharedInboxUrl)
}
async function sendCreateViewToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
async function sendCreateView (byActor: ActorModel, video: VideoModel, t: Transaction) {
const url = getVideoViewActivityPubUrl(byActor, video)
const viewActivityData = createViewActivityData(byActor, video)
const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
const data = await createActivityData(url, byActor, viewActivityData, t, audience)
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
}
// Send to origin
if (video.isOwned() === false) {
const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
const data = await createActivityData(url, byActor, viewActivityData, t, audience)
async function sendCreateViewToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
const url = getVideoViewActivityPubUrl(byActor, video)
const viewActivityData = createViewActivityData(byActor, video)
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
}
const actorsToForwardView = await getActorsInvolvedInVideo(video, t)
const audience = getObjectFollowersAudience(actorsToForwardView)
// Send to followers
const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
const data = await createActivityData(url, byActor, viewActivityData, t, audience)
// Use the server actor to send the view
const serverActor = await getServerActor()
const actorsException = [ byActor ]
return broadcastToFollowers(data, serverActor, actorsToForwardView, t, actorsException)
return broadcastToFollowers(data, serverActor, actorsInvolvedInVideo, t, actorsException)
}
async function sendCreateDislikeToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
async function sendCreateDislike (byActor: ActorModel, video: VideoModel, t: Transaction) {
const url = getVideoDislikeActivityPubUrl(byActor, video)
const dislikeActivityData = createDislikeActivityData(byActor, video)
const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
const data = await createActivityData(url, byActor, dislikeActivityData, t, audience)
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
}
// Send to origin
if (video.isOwned() === false) {
const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
const data = await createActivityData(url, byActor, dislikeActivityData, t, audience)
async function sendCreateDislikeToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
const url = getVideoDislikeActivityPubUrl(byActor, video)
const dislikeActivityData = createDislikeActivityData(byActor, video)
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
}
const actorsToForwardView = await getActorsInvolvedInVideo(video, t)
const audience = getObjectFollowersAudience(actorsToForwardView)
// Send to followers
const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
const data = await createActivityData(url, byActor, dislikeActivityData, t, audience)
const actorsException = [ byActor ]
return broadcastToFollowers(data, byActor, actorsToForwardView, t, actorsException)
return broadcastToFollowers(data, byActor, actorsInvolvedInVideo, t, actorsException)
}
async function createActivityData (
url: string,
byActor: ActorModel,
object: any,
t: Transaction,
audience?: ActivityAudience
): Promise<ActivityCreate> {
async function createActivityData (url: string,
byActor: ActorModel,
object: any,
t: Transaction,
audience?: ActivityAudience): Promise<ActivityCreate> {
if (!audience) {
audience = await getAudience(byActor, t)
}
@ -173,11 +160,8 @@ export {
sendCreateVideo,
sendVideoAbuse,
createActivityData,
sendCreateViewToOrigin,
sendCreateViewToVideoFollowers,
sendCreateDislikeToOrigin,
sendCreateDislikeToVideoFollowers,
sendCreateView,
sendCreateDislike,
createDislikeActivityData,
sendCreateVideoCommentToOrigin,
sendCreateVideoCommentToVideoFollowers
sendCreateVideoComment
}

View file

@ -13,20 +13,20 @@ import {
unicastTo
} from './misc'
async function sendLikeToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
async function sendLike (byActor: ActorModel, video: VideoModel, t: Transaction) {
const url = getVideoLikeActivityPubUrl(byActor, video)
const accountsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
const audience = getOriginVideoAudience(video, accountsInvolvedInVideo)
const data = await likeActivityData(url, byActor, video, t, audience)
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
}
// Send to origin
if (video.isOwned() === false) {
const audience = getOriginVideoAudience(video, accountsInvolvedInVideo)
const data = await likeActivityData(url, byActor, video, t, audience)
async function sendLikeToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
const url = getVideoLikeActivityPubUrl(byActor, video)
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
}
const accountsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
// Send to followers
const audience = getObjectFollowersAudience(accountsInvolvedInVideo)
const data = await likeActivityData(url, byActor, video, t, audience)
@ -56,7 +56,6 @@ async function likeActivityData (
// ---------------------------------------------------------------------------
export {
sendLikeToOrigin,
sendLikeToVideoFollowers,
sendLike,
likeActivityData
}

View file

@ -30,68 +30,55 @@ async function sendUndoFollow (actorFollow: ActorFollowModel, t: Transaction) {
return unicastTo(data, me, following.inboxUrl)
}
async function sendUndoLikeToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
async function sendUndoLike (byActor: ActorModel, video: VideoModel, t: Transaction) {
const likeUrl = getVideoLikeActivityPubUrl(byActor, video)
const undoUrl = getUndoActivityPubUrl(likeUrl)
const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
const object = await likeActivityData(likeUrl, byActor, video, t)
const data = await undoActivityData(undoUrl, byActor, object, t, audience)
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
}
// Send to origin
if (video.isOwned() === false) {
const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
const data = await undoActivityData(undoUrl, byActor, object, t, audience)
async function sendUndoLikeToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
const likeUrl = getVideoLikeActivityPubUrl(byActor, video)
const undoUrl = getUndoActivityPubUrl(likeUrl)
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
}
const toActorsFollowers = await getActorsInvolvedInVideo(video, t)
const audience = getObjectFollowersAudience(toActorsFollowers)
const object = await likeActivityData(likeUrl, byActor, video, t)
const audience = getObjectFollowersAudience(actorsInvolvedInVideo)
const data = await undoActivityData(undoUrl, byActor, object, t, audience)
const followersException = [ byActor ]
return broadcastToFollowers(data, byActor, toActorsFollowers, t, followersException)
return broadcastToFollowers(data, byActor, actorsInvolvedInVideo, t, followersException)
}
async function sendUndoDislikeToOrigin (byActor: ActorModel, video: VideoModel, t: Transaction) {
async function sendUndoDislike (byActor: ActorModel, video: VideoModel, t: Transaction) {
const dislikeUrl = getVideoDislikeActivityPubUrl(byActor, video)
const undoUrl = getUndoActivityPubUrl(dislikeUrl)
const actorsInvolvedInVideo = await getActorsInvolvedInVideo(video, t)
const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
const dislikeActivity = createDislikeActivityData(byActor, video)
const object = await createActivityData(undoUrl, byActor, dislikeActivity, t)
const data = await undoActivityData(undoUrl, byActor, object, t, audience)
if (video.isOwned() === false) {
const audience = getOriginVideoAudience(video, actorsInvolvedInVideo)
const data = await undoActivityData(undoUrl, byActor, object, t, audience)
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
}
async function sendUndoDislikeToVideoFollowers (byActor: ActorModel, video: VideoModel, t: Transaction) {
const dislikeUrl = getVideoDislikeActivityPubUrl(byActor, video)
const undoUrl = getUndoActivityPubUrl(dislikeUrl)
const dislikeActivity = createDislikeActivityData(byActor, video)
const object = await createActivityData(undoUrl, byActor, dislikeActivity, t)
return unicastTo(data, byActor, video.VideoChannel.Account.Actor.sharedInboxUrl)
}
const data = await undoActivityData(undoUrl, byActor, object, t)
const toActorsFollowers = await getActorsInvolvedInVideo(video, t)
const followersException = [ byActor ]
return broadcastToFollowers(data, byActor, toActorsFollowers, t, followersException)
return broadcastToFollowers(data, byActor, actorsInvolvedInVideo, t, followersException)
}
// ---------------------------------------------------------------------------
export {
sendUndoFollow,
sendUndoLikeToOrigin,
sendUndoLikeToVideoFollowers,
sendUndoDislikeToOrigin,
sendUndoDislikeToVideoFollowers
sendUndoLike,
sendUndoDislike
}
// ---------------------------------------------------------------------------

View file

@ -3,7 +3,7 @@ import { VideoPrivacy } from '../../../shared/models/videos'
import { getServerActor } from '../../helpers/utils'
import { VideoModel } from '../../models/video/video'
import { VideoShareModel } from '../../models/video/video-share'
import { sendVideoAnnounceToFollowers } from './send'
import { sendVideoAnnounce } from './send'
import { getAnnounceActivityPubUrl } from './url'
async function shareVideoByServerAndChannel (video: VideoModel, t: Transaction) {
@ -23,7 +23,7 @@ async function shareVideoByServerAndChannel (video: VideoModel, t: Transaction)
},
transaction: t
}).then(([ serverShare, created ]) => {
if (created) return sendVideoAnnounceToFollowers(serverActor, serverShare, video, t)
if (created) return sendVideoAnnounce(serverActor, serverShare, video, t)
return undefined
})
@ -40,7 +40,7 @@ async function shareVideoByServerAndChannel (video: VideoModel, t: Transaction)
},
transaction: t
}).then(([ videoChannelShare, created ]) => {
if (created) return sendVideoAnnounceToFollowers(serverActor, videoChannelShare, video, t)
if (created) return sendVideoAnnounce(serverActor, videoChannelShare, video, t)
return undefined
})

View file

@ -1,52 +1,28 @@
import { Transaction } from 'sequelize'
import { AccountModel } from '../../models/account/account'
import { VideoModel } from '../../models/video/video'
import {
sendCreateDislikeToOrigin, sendCreateDislikeToVideoFollowers, sendLikeToOrigin, sendLikeToVideoFollowers, sendUndoDislikeToOrigin,
sendUndoDislikeToVideoFollowers, sendUndoLikeToOrigin, sendUndoLikeToVideoFollowers
} from './send'
import { sendCreateDislike, sendLike, sendUndoDislike, sendUndoLike } from './send'
async function sendVideoRateChangeToFollowers (account: AccountModel,
video: VideoModel,
likes: number,
dislikes: number,
t: Transaction) {
async function sendVideoRateChange (account: AccountModel,
video: VideoModel,
likes: number,
dislikes: number,
t: Transaction) {
const actor = account.Actor
// Keep the order: first we undo and then we create
// Undo Like
if (likes < 0) await sendUndoLikeToVideoFollowers(actor, video, t)
if (likes < 0) await sendUndoLike(actor, video, t)
// Undo Dislike
if (dislikes < 0) await sendUndoDislikeToVideoFollowers(actor, video, t)
if (dislikes < 0) await sendUndoDislike(actor, video, t)
// Like
if (likes > 0) await sendLikeToVideoFollowers(actor, video, t)
if (likes > 0) await sendLike(actor, video, t)
// Dislike
if (dislikes > 0) await sendCreateDislikeToVideoFollowers(actor, video, t)
}
async function sendVideoRateChangeToOrigin (account: AccountModel,
video: VideoModel,
likes: number,
dislikes: number,
t: Transaction) {
const actor = account.Actor
// Keep the order: first we undo and then we create
// Undo Like
if (likes < 0) await sendUndoLikeToOrigin(actor, video, t)
// Undo Dislike
if (dislikes < 0) await sendUndoDislikeToOrigin(actor, video, t)
// Like
if (likes > 0) await sendLikeToOrigin(actor, video, t)
// Dislike
if (dislikes > 0) await sendCreateDislikeToOrigin(actor, video, t)
if (dislikes > 0) await sendCreateDislike(actor, video, t)
}
export {
sendVideoRateChangeToFollowers,
sendVideoRateChangeToOrigin
sendVideoRateChange
}

View file

@ -5,7 +5,7 @@ import { AccountModel } from '../models/account/account'
import { VideoModel } from '../models/video/video'
import { VideoCommentModel } from '../models/video/video-comment'
import { getVideoCommentActivityPubUrl } from './activitypub'
import { sendCreateVideoCommentToOrigin, sendCreateVideoCommentToVideoFollowers } from './activitypub/send'
import { sendCreateVideoComment } from './activitypub/send'
async function createVideoComment (obj: {
text: string,
@ -37,11 +37,7 @@ async function createVideoComment (obj: {
savedComment.Video = obj.video
savedComment.Account = obj.account
if (savedComment.Video.isOwned()) {
await sendCreateVideoCommentToVideoFollowers(savedComment, t)
} else {
await sendCreateVideoCommentToOrigin(savedComment, t)
}
await sendCreateVideoComment(savedComment, t)
return savedComment
}