2017-12-20 14:36:29 +00:00
|
|
|
import { ActivityAnnounce } from '../../../../shared/models/activitypub'
|
2017-12-28 10:16:08 +00:00
|
|
|
import { retryTransactionWrapper } from '../../../helpers/database-utils'
|
2017-12-12 16:53:50 +00:00
|
|
|
import { sequelizeTypescript } from '../../../initializers'
|
2017-12-14 16:38:41 +00:00
|
|
|
import { ActorModel } from '../../../models/activitypub/actor'
|
2017-12-12 16:53:50 +00:00
|
|
|
import { VideoModel } from '../../../models/video/video'
|
|
|
|
import { VideoShareModel } from '../../../models/video/video-share'
|
2017-12-14 16:38:41 +00:00
|
|
|
import { getOrCreateActorAndServerAndModel } from '../actor'
|
2018-05-31 08:23:56 +00:00
|
|
|
import { forwardVideoRelatedActivity } from '../send/utils'
|
2018-01-10 16:18:12 +00:00
|
|
|
import { getOrCreateAccountAndVideoAndChannel } from '../videos'
|
2017-11-15 16:56:21 +00:00
|
|
|
|
|
|
|
async function processAnnounceActivity (activity: ActivityAnnounce) {
|
2017-12-14 16:38:41 +00:00
|
|
|
const actorAnnouncer = await getOrCreateActorAndServerAndModel(activity.actor)
|
2017-11-15 16:56:21 +00:00
|
|
|
|
2018-06-13 12:27:40 +00:00
|
|
|
return retryTransactionWrapper(processVideoShare, actorAnnouncer, activity)
|
2017-11-15 16:56:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
processAnnounceActivity
|
|
|
|
}
|
2017-11-27 13:44:51 +00:00
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2018-06-13 12:27:40 +00:00
|
|
|
async function processVideoShare (actorAnnouncer: ActorModel, activity: ActivityAnnounce) {
|
2018-01-26 11:02:18 +00:00
|
|
|
const objectUri = typeof activity.object === 'string' ? activity.object : activity.object.id
|
2018-01-10 16:18:12 +00:00
|
|
|
let video: VideoModel
|
|
|
|
|
2018-01-26 11:02:18 +00:00
|
|
|
const res = await getOrCreateAccountAndVideoAndChannel(objectUri)
|
|
|
|
video = res.video
|
2017-11-27 13:44:51 +00:00
|
|
|
|
2017-12-12 16:53:50 +00:00
|
|
|
return sequelizeTypescript.transaction(async t => {
|
2017-11-27 13:44:51 +00:00
|
|
|
// Add share entry
|
|
|
|
|
|
|
|
const share = {
|
2017-12-14 16:38:41 +00:00
|
|
|
actorId: actorAnnouncer.id,
|
2018-01-26 14:49:57 +00:00
|
|
|
videoId: video.id,
|
|
|
|
url: activity.id
|
2017-11-27 13:44:51 +00:00
|
|
|
}
|
|
|
|
|
2017-12-12 16:53:50 +00:00
|
|
|
const [ , created ] = await VideoShareModel.findOrCreate({
|
2018-01-26 14:49:57 +00:00
|
|
|
where: {
|
|
|
|
url: activity.id
|
|
|
|
},
|
2017-11-27 13:44:51 +00:00
|
|
|
defaults: share,
|
|
|
|
transaction: t
|
|
|
|
})
|
|
|
|
|
|
|
|
if (video.isOwned() && created === true) {
|
|
|
|
// Don't resend the activity to the sender
|
2017-12-14 16:38:41 +00:00
|
|
|
const exceptions = [ actorAnnouncer ]
|
2018-05-31 08:23:56 +00:00
|
|
|
|
|
|
|
await forwardVideoRelatedActivity(activity, t, exceptions, video)
|
2017-11-27 13:44:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return undefined
|
|
|
|
})
|
|
|
|
}
|