video-channel.ts 2.1 KB

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