Move models to typescript-sequelize
[oweals/peertube.git] / server / lib / activitypub / send / send-update.ts
1 import { Transaction } from 'sequelize'
2 import { ActivityUpdate } from '../../../../shared/models/activitypub'
3 import { AccountModel } from '../../../models/account/account'
4 import { VideoModel } from '../../../models/video/video'
5 import { VideoChannelModel } from '../../../models/video/video-channel'
6 import { VideoChannelShareModel } from '../../../models/video/video-channel-share'
7 import { VideoShareModel } from '../../../models/video/video-share'
8 import { getUpdateActivityPubUrl } from '../url'
9 import { broadcastToFollowers, getAudience } from './misc'
10
11 async function sendUpdateVideoChannel (videoChannel: VideoChannelModel, t: Transaction) {
12   const byAccount = videoChannel.Account
13
14   const url = getUpdateActivityPubUrl(videoChannel.url, videoChannel.updatedAt.toISOString())
15   const videoChannelObject = videoChannel.toActivityPubObject()
16   const data = await updateActivityData(url, byAccount, videoChannelObject, t)
17
18   const accountsInvolved = await VideoChannelShareModel.loadAccountsByShare(videoChannel.id, t)
19   accountsInvolved.push(byAccount)
20
21   return broadcastToFollowers(data, byAccount, accountsInvolved, t)
22 }
23
24 async function sendUpdateVideo (video: VideoModel, t: Transaction) {
25   const byAccount = video.VideoChannel.Account
26
27   const url = getUpdateActivityPubUrl(video.url, video.updatedAt.toISOString())
28   const videoObject = video.toActivityPubObject()
29   const data = await updateActivityData(url, byAccount, videoObject, t)
30
31   const accountsInvolved = await VideoShareModel.loadAccountsByShare(video.id, t)
32   accountsInvolved.push(byAccount)
33
34   return broadcastToFollowers(data, byAccount, accountsInvolved, t)
35 }
36
37 // ---------------------------------------------------------------------------
38
39 export {
40   sendUpdateVideoChannel,
41   sendUpdateVideo
42 }
43
44 // ---------------------------------------------------------------------------
45
46 async function updateActivityData (url: string, byAccount: AccountModel, object: any, t: Transaction): Promise<ActivityUpdate> {
47   const { to, cc } = await getAudience(byAccount, t)
48   return {
49     type: 'Update',
50     id: url,
51     actor: byAccount.url,
52     to,
53     cc,
54     object
55   }
56 }