video-channel.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import * as Sequelize from 'sequelize'
  2. import { v4 as uuidv4 } from 'uuid'
  3. import { VideoChannelCreate } from '../../shared/models'
  4. import { VideoModel } from '../models/video/video'
  5. import { VideoChannelModel } from '../models/video/video-channel'
  6. import { MAccountId, MChannelDefault, MChannelId } from '../types/models'
  7. import { buildActorInstance } from './activitypub/actor'
  8. import { getLocalVideoChannelActivityPubUrl } from './activitypub/url'
  9. import { federateVideoIfNeeded } from './activitypub/videos'
  10. type CustomVideoChannelModelAccount <T extends MAccountId> = MChannelDefault & { Account?: T }
  11. async function createLocalVideoChannel <T extends MAccountId> (
  12. videoChannelInfo: VideoChannelCreate,
  13. account: T,
  14. t: Sequelize.Transaction
  15. ): Promise<CustomVideoChannelModelAccount<T>> {
  16. const uuid = uuidv4()
  17. const url = getLocalVideoChannelActivityPubUrl(videoChannelInfo.name)
  18. const actorInstance = buildActorInstance('Group', url, videoChannelInfo.name, uuid)
  19. const actorInstanceCreated = await actorInstance.save({ transaction: t })
  20. const videoChannelData = {
  21. name: videoChannelInfo.displayName,
  22. description: videoChannelInfo.description,
  23. support: videoChannelInfo.support,
  24. accountId: account.id,
  25. actorId: actorInstanceCreated.id
  26. }
  27. const videoChannel = new VideoChannelModel(videoChannelData)
  28. const options = { transaction: t }
  29. const videoChannelCreated: CustomVideoChannelModelAccount<T> = await videoChannel.save(options) as MChannelDefault
  30. // Do not forget to add Account/Actor information to the created video channel
  31. videoChannelCreated.Account = account
  32. videoChannelCreated.Actor = actorInstanceCreated
  33. // No need to seed this empty video channel to followers
  34. return videoChannelCreated
  35. }
  36. async function federateAllVideosOfChannel (videoChannel: MChannelId) {
  37. const videoIds = await VideoModel.getAllIdsFromChannel(videoChannel)
  38. for (const videoId of videoIds) {
  39. const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(videoId)
  40. await federateVideoIfNeeded(video, false)
  41. }
  42. }
  43. // ---------------------------------------------------------------------------
  44. export {
  45. createLocalVideoChannel,
  46. federateAllVideosOfChannel
  47. }