import * as Sequelize from 'sequelize'
-
-import { isVideoChannelNameValid, isVideoChannelDescriptionValid } from '../../helpers'
-import { removeVideoChannelToFriends } from '../../lib'
+import { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../../helpers'
+import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
+import { sendDeleteVideoChannel } from '../../lib/activitypub/send/send-delete'
import { addMethodsToModel, getSort } from '../utils'
-import {
- VideoChannelInstance,
- VideoChannelAttributes,
-
- VideoChannelMethods
-} from './video-channel-interface'
+import { VideoChannelAttributes, VideoChannelInstance, VideoChannelMethods } from './video-channel-interface'
+import { getAnnounceActivityPubUrl } from '../../lib/activitypub/url'
+import { activityPubCollection } from '../../helpers/activitypub'
+import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
let VideoChannel: Sequelize.Model<VideoChannelInstance, VideoChannelAttributes>
let toFormattedJSON: VideoChannelMethods.ToFormattedJSON
let toActivityPubObject: VideoChannelMethods.ToActivityPubObject
let isOwned: VideoChannelMethods.IsOwned
let countByAccount: VideoChannelMethods.CountByAccount
-let listOwned: VideoChannelMethods.ListOwned
let listForApi: VideoChannelMethods.ListForApi
let listByAccount: VideoChannelMethods.ListByAccount
let loadByIdAndAccount: VideoChannelMethods.LoadByIdAndAccount
let loadByUUIDAndPopulateAccount: VideoChannelMethods.LoadByUUIDAndPopulateAccount
let loadByHostAndUUID: VideoChannelMethods.LoadByHostAndUUID
let loadAndPopulateAccountAndVideos: VideoChannelMethods.LoadAndPopulateAccountAndVideos
+let loadByUrl: VideoChannelMethods.LoadByUrl
+let loadByUUIDOrUrl: VideoChannelMethods.LoadByUUIDOrUrl
export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.DataTypes) {
VideoChannel = sequelize.define<VideoChannelInstance, VideoChannelAttributes>('VideoChannel',
defaultValue: false
},
url: {
- type: DataTypes.STRING,
+ type: DataTypes.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.URL.max),
allowNull: false,
validate: {
- isUrl: true
+ urlValid: value => {
+ const res = isActivityPubUrlValid(value)
+ if (res === false) throw new Error('Video channel URL is not valid.')
+ }
}
}
},
listForApi,
listByAccount,
- listOwned,
loadByIdAndAccount,
loadAndPopulateAccount,
loadByUUIDAndPopulateAccount,
loadByUUID,
loadByHostAndUUID,
loadAndPopulateAccountAndVideos,
- countByAccount
+ countByAccount,
+ loadByUrl,
+ loadByUUIDOrUrl
]
const instanceMethods = [
isOwned,
toFormattedJSON,
- toActivityPubObject,
+ toActivityPubObject
]
addMethodsToModel(VideoChannel, classMethods, instanceMethods)
}
toActivityPubObject = function (this: VideoChannelInstance) {
+ let sharesObject
+ if (Array.isArray(this.VideoChannelShares)) {
+ const shares: string[] = []
+
+ for (const videoChannelShare of this.VideoChannelShares) {
+ const shareUrl = getAnnounceActivityPubUrl(this.url, videoChannelShare.Account)
+ shares.push(shareUrl)
+ }
+
+ sharesObject = activityPubCollection(shares)
+ }
+
const json = {
+ type: 'VideoChannel' as 'VideoChannel',
+ id: this.url,
uuid: this.uuid,
+ content: this.description,
name: this.name,
- description: this.description,
- createdAt: this.createdAt,
- updatedAt: this.updatedAt,
- ownerUUID: this.Account.uuid
+ published: this.createdAt.toISOString(),
+ updated: this.updatedAt.toISOString(),
+ shares: sharesObject
}
return json
function afterDestroy (videoChannel: VideoChannelInstance) {
if (videoChannel.isOwned()) {
- const removeVideoChannelToFriendsParams = {
- uuid: videoChannel.uuid
- }
-
- return removeVideoChannelToFriends(removeVideoChannelToFriendsParams)
+ return sendDeleteVideoChannel(videoChannel, undefined)
}
return undefined
return VideoChannel.count(query)
}
-listOwned = function () {
- const query = {
- where: {
- remote: false
- },
- include: [ VideoChannel['sequelize'].models.Account ]
- }
-
- return VideoChannel.findAll(query)
-}
-
listForApi = function (start: number, count: number, sort: string) {
const query = {
offset: start,
{
model: VideoChannel['sequelize'].models.Account,
required: true,
- include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
+ include: [ { model: VideoChannel['sequelize'].models.Server, required: false } ]
}
]
}
id: accountId
},
required: true,
- include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
+ include: [ { model: VideoChannel['sequelize'].models.Server, required: false } ]
}
]
}
return VideoChannel.findOne(query)
}
+loadByUrl = function (url: string, t?: Sequelize.Transaction) {
+ const query: Sequelize.FindOptions<VideoChannelAttributes> = {
+ where: {
+ url
+ },
+ include: [ VideoChannel['sequelize'].models.Account ]
+ }
+
+ if (t !== undefined) query.transaction = t
+
+ return VideoChannel.findOne(query)
+}
+
+loadByUUIDOrUrl = function (uuid: string, url: string, t?: Sequelize.Transaction) {
+ const query: Sequelize.FindOptions<VideoChannelAttributes> = {
+ where: {
+ [Sequelize.Op.or]: [
+ { uuid },
+ { url }
+ ]
+ }
+ }
+
+ if (t !== undefined) query.transaction = t
+
+ return VideoChannel.findOne(query)
+}
+
loadByHostAndUUID = function (fromHost: string, uuid: string, t?: Sequelize.Transaction) {
const query: Sequelize.FindOptions<VideoChannelAttributes> = {
where: {
model: VideoChannel['sequelize'].models.Account,
include: [
{
- model: VideoChannel['sequelize'].models.Pod,
+ model: VideoChannel['sequelize'].models.Server,
required: true,
where: {
host: fromHost
include: [
{
model: VideoChannel['sequelize'].models.Account,
- include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
+ include: [ { model: VideoChannel['sequelize'].models.Server, required: false } ]
}
]
}
include: [
{
model: VideoChannel['sequelize'].models.Account,
- include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
+ include: [ { model: VideoChannel['sequelize'].models.Server, required: false } ]
}
]
}
include: [
{
model: VideoChannel['sequelize'].models.Account,
- include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
+ include: [ { model: VideoChannel['sequelize'].models.Server, required: false } ]
}
]
}
include: [
{
model: VideoChannel['sequelize'].models.Account,
- include: [ { model: VideoChannel['sequelize'].models.Pod, required: false } ]
+ include: [ { model: VideoChannel['sequelize'].models.Server, required: false } ]
},
VideoChannel['sequelize'].models.Video
]