]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/lib/activitypub/process/process-announce.ts
Fix lint
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-announce.ts
... / ...
CommitLineData
1import { ActivityAnnounce } from '../../../../shared/models/activitypub'
2import { retryTransactionWrapper } from '../../../helpers/database-utils'
3import { logger } from '../../../helpers/logger'
4import { sequelizeTypescript } from '../../../initializers'
5import { ActorModel } from '../../../models/activitypub/actor'
6import { VideoModel } from '../../../models/video/video'
7import { VideoShareModel } from '../../../models/video/video-share'
8import { getOrCreateActorAndServerAndModel } from '../actor'
9import { forwardActivity } from '../send/misc'
10import { processCreateActivity } from './process-create'
11
12async function processAnnounceActivity (activity: ActivityAnnounce) {
13 const announcedActivity = activity.object
14 const actorAnnouncer = await getOrCreateActorAndServerAndModel(activity.actor)
15
16 if (typeof announcedActivity === 'string') {
17 return processVideoShare(actorAnnouncer, activity)
18 } else if (announcedActivity.type === 'Create' && announcedActivity.object.type === 'Video') {
19 return processVideoShare(actorAnnouncer, activity)
20 }
21
22 logger.warn(
23 'Unknown activity object type %s -> %s when announcing activity.', announcedActivity.type, announcedActivity.object.type,
24 { activity: activity.id }
25 )
26
27 return undefined
28}
29
30// ---------------------------------------------------------------------------
31
32export {
33 processAnnounceActivity
34}
35
36// ---------------------------------------------------------------------------
37
38function processVideoShare (actorAnnouncer: ActorModel, activity: ActivityAnnounce) {
39 const options = {
40 arguments: [ actorAnnouncer, activity ],
41 errorMessage: 'Cannot share the video activity with many retries.'
42 }
43
44 return retryTransactionWrapper(shareVideo, options)
45}
46
47function shareVideo (actorAnnouncer: ActorModel, activity: ActivityAnnounce) {
48 const announced = activity.object
49
50 return sequelizeTypescript.transaction(async t => {
51 // Add share entry
52 let video: VideoModel
53
54 if (typeof announced === 'string') {
55 video = await VideoModel.loadByUrlAndPopulateAccount(announced)
56 if (!video) throw new Error('Unknown video to share ' + announced)
57 } else {
58 video = await processCreateActivity(announced)
59 }
60
61 const share = {
62 actorId: actorAnnouncer.id,
63 videoId: video.id
64 }
65
66 const [ , created ] = await VideoShareModel.findOrCreate({
67 where: share,
68 defaults: share,
69 transaction: t
70 })
71
72 if (video.isOwned() && created === true) {
73 // Don't resend the activity to the sender
74 const exceptions = [ actorAnnouncer ]
75 await forwardActivity(activity, t, exceptions)
76 }
77
78 return undefined
79 })
80}