aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/misc.ts
blob: 2cf0c4fd172e31694b2b90605b839b750ce76266 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import * as magnetUtil from 'magnet-uri'
import * as Sequelize from 'sequelize'
import { VideoTorrentObject } from '../../../shared'
import { isVideoFileInfoHashValid } from '../../helpers/custom-validators/videos'
import { database as db } from '../../initializers'
import { VIDEO_MIMETYPE_EXT } from '../../initializers/constants'
import { VideoChannelInstance } from '../../models/video/video-channel-interface'
import { VideoFileAttributes } from '../../models/video/video-file-interface'
import { VideoAttributes, VideoInstance } from '../../models/video/video-interface'

async function videoActivityObjectToDBAttributes (
  videoChannel: VideoChannelInstance,
  videoObject: VideoTorrentObject,
  t: Sequelize.Transaction
) {
  const videoFromDatabase = await db.Video.loadByUUIDOrURL(videoObject.uuid, videoObject.id, t)
  if (videoFromDatabase) throw new Error('Video with this UUID/Url already exists.')

  const duration = videoObject.duration.replace(/[^\d]+/, '')
  const videoData: VideoAttributes = {
    name: videoObject.name,
    uuid: videoObject.uuid,
    url: videoObject.id,
    category: parseInt(videoObject.category.identifier, 10),
    licence: parseInt(videoObject.licence.identifier, 10),
    language: parseInt(videoObject.language.identifier, 10),
    nsfw: videoObject.nsfw,
    description: videoObject.content,
    channelId: videoChannel.id,
    duration: parseInt(duration, 10),
    createdAt: videoObject.published,
    // FIXME: updatedAt does not seems to be considered by Sequelize
    updatedAt: videoObject.updated,
    views: videoObject.views,
    likes: 0,
    dislikes: 0,
    // likes: videoToCreateData.likes,
    // dislikes: videoToCreateData.dislikes,
    remote: true,
    privacy: 1
    // privacy: videoToCreateData.privacy
  }

  return videoData
}

function videoFileActivityUrlToDBAttributes (videoCreated: VideoInstance, videoObject: VideoTorrentObject) {
  const fileUrls = videoObject.url
    .filter(u => Object.keys(VIDEO_MIMETYPE_EXT).indexOf(u.mimeType) !== -1)

  const attributes: VideoFileAttributes[] = []
  for (const url of fileUrls) {
    // Fetch associated magnet uri
    const magnet = videoObject.url
      .find(u => {
        return u.mimeType === 'application/x-bittorrent;x-scheme-handler/magnet' && u.width === url.width
      })
    if (!magnet) throw new Error('Cannot find associated magnet uri for file ' + url.url)

    const parsed = magnetUtil.decode(magnet.url)
    if (!parsed || isVideoFileInfoHashValid(parsed.infoHash) === false) throw new Error('Cannot parse magnet URI ' + magnet.url)

    const attribute = {
      extname: VIDEO_MIMETYPE_EXT[url.mimeType],
      infoHash: parsed.infoHash,
      resolution: url.width,
      size: url.size,
      videoId: videoCreated.id
    }
    attributes.push(attribute)
  }

  return attributes
}

// ---------------------------------------------------------------------------

export {
  videoFileActivityUrlToDBAttributes,
  videoActivityObjectToDBAttributes
}