video-channel.ts 2.1 KB

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