Skip to content

Commit

Permalink
Fix incorrect IDs in AP federation
Browse files Browse the repository at this point in the history
  • Loading branch information
Chocobozzz committed Nov 20, 2020
1 parent 3fba4b6 commit de94ac8
Show file tree
Hide file tree
Showing 34 changed files with 278 additions and 233 deletions.
20 changes: 10 additions & 10 deletions scripts/update-host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { ActorFollowModel } from '../server/models/activitypub/actor-follow'
import { VideoModel } from '../server/models/video/video'
import { ActorModel } from '../server/models/activitypub/actor'
import {
getAccountActivityPubUrl,
getVideoActivityPubUrl,
getVideoAnnounceActivityPubUrl,
getVideoChannelActivityPubUrl,
getVideoCommentActivityPubUrl
getLocalAccountActivityPubUrl,
getLocalVideoActivityPubUrl,
getLocalVideoAnnounceActivityPubUrl,
getLocalVideoChannelActivityPubUrl,
getLocalVideoCommentActivityPubUrl
} from '../server/lib/activitypub/url'
import { VideoShareModel } from '../server/models/video/video-share'
import { VideoCommentModel } from '../server/models/video/video-comment'
Expand Down Expand Up @@ -62,8 +62,8 @@ async function run () {
console.log('Updating actor ' + actor.url)

const newUrl = actor.Account
? getAccountActivityPubUrl(actor.preferredUsername)
: getVideoChannelActivityPubUrl(actor.preferredUsername)
? getLocalAccountActivityPubUrl(actor.preferredUsername)
: getLocalVideoChannelActivityPubUrl(actor.preferredUsername)

actor.url = newUrl
actor.inboxUrl = newUrl + '/inbox'
Expand All @@ -85,7 +85,7 @@ async function run () {

console.log('Updating video share ' + videoShare.url)

videoShare.url = getVideoAnnounceActivityPubUrl(videoShare.Actor, videoShare.Video)
videoShare.url = getLocalVideoAnnounceActivityPubUrl(videoShare.Actor, videoShare.Video)
await videoShare.save()
}

Expand All @@ -110,7 +110,7 @@ async function run () {

console.log('Updating comment ' + comment.url)

comment.url = getVideoCommentActivityPubUrl(comment.Video, comment)
comment.url = getLocalVideoCommentActivityPubUrl(comment.Video, comment)
await comment.save()
}

Expand All @@ -120,7 +120,7 @@ async function run () {
for (const video of videos) {
console.log('Updating video ' + video.uuid)

video.url = getVideoActivityPubUrl(video)
video.url = getLocalVideoActivityPubUrl(video)
await video.save()

for (const file of video.VideoFiles) {
Expand Down
56 changes: 39 additions & 17 deletions server/controllers/activitypub/client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as cors from 'cors'
import * as express from 'express'
import { getRateUrl } from '@server/lib/activitypub/video-rates'
import { getServerActor } from '@server/models/application/application'
import { MAccountId, MActorId, MChannelId, MVideoId } from '@server/types/models'
import { MAccountId, MActorId, MChannelId, MVideoId, MVideoUrl } from '@server/types/models'
import { VideoPrivacy, VideoRateType } from '../../../shared/models/videos'
import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model'
import { activityPubCollectionPagination, activityPubContextify } from '../../helpers/activitypub'
Expand All @@ -12,10 +11,10 @@ import { buildAnnounceWithVideoAudience, buildLikeActivity } from '../../lib/act
import { buildCreateActivity } from '../../lib/activitypub/send/send-create'
import { buildDislikeActivity } from '../../lib/activitypub/send/send-dislike'
import {
getVideoCommentsActivityPubUrl,
getVideoDislikesActivityPubUrl,
getVideoLikesActivityPubUrl,
getVideoSharesActivityPubUrl
getLocalVideoCommentsActivityPubUrl,
getLocalVideoDislikesActivityPubUrl,
getLocalVideoLikesActivityPubUrl,
getLocalVideoSharesActivityPubUrl
} from '../../lib/activitypub/url'
import {
asyncMiddleware,
Expand Down Expand Up @@ -212,10 +211,9 @@ function getAccountVideoRateFactory (rateType: VideoRateType) {
const accountVideoRate = res.locals.accountVideoRate

const byActor = accountVideoRate.Account.Actor
const url = getRateUrl(rateType, byActor, accountVideoRate.Video)
const APObject = rateType === 'like'
? buildLikeActivity(url, byActor, accountVideoRate.Video)
: buildDislikeActivity(url, byActor, accountVideoRate.Video)
? buildLikeActivity(accountVideoRate.url, byActor, accountVideoRate.Video)
: buildDislikeActivity(accountVideoRate.url, byActor, accountVideoRate.Video)

return activityPubResponse(activityPubContextify(APObject), res)
}
Expand All @@ -225,7 +223,7 @@ async function videoController (req: express.Request, res: express.Response) {
// We need more attributes
const video = await VideoModel.loadAndPopulateAccountAndServerAndTags(res.locals.onlyVideoWithRights.id)

if (video.url.startsWith(WEBSERVER.URL) === false) return res.redirect(video.url)
if (redirectIfNotOwned(video.url, res)) return

// We need captions to render AP object
const captions = await VideoCaptionModel.listVideoCaptions(video.id)
Expand All @@ -245,7 +243,7 @@ async function videoController (req: express.Request, res: express.Response) {
async function videoAnnounceController (req: express.Request, res: express.Response) {
const share = res.locals.videoShare

if (share.url.startsWith(WEBSERVER.URL) === false) return res.redirect(share.url)
if (redirectIfNotOwned(share.url, res)) return

const { activity } = await buildAnnounceWithVideoAudience(share.Actor, share, res.locals.videoAll, undefined)

Expand All @@ -255,43 +253,53 @@ async function videoAnnounceController (req: express.Request, res: express.Respo
async function videoAnnouncesController (req: express.Request, res: express.Response) {
const video = res.locals.onlyImmutableVideo

if (redirectIfNotOwned(video.url, res)) return

const handler = async (start: number, count: number) => {
const result = await VideoShareModel.listAndCountByVideoId(video.id, start, count)
return {
total: result.count,
data: result.rows.map(r => r.url)
}
}
const json = await activityPubCollectionPagination(getVideoSharesActivityPubUrl(video), handler, req.query.page)
const json = await activityPubCollectionPagination(getLocalVideoSharesActivityPubUrl(video), handler, req.query.page)

return activityPubResponse(activityPubContextify(json), res)
}

async function videoLikesController (req: express.Request, res: express.Response) {
const video = res.locals.onlyImmutableVideo
const json = await videoRates(req, 'like', video, getVideoLikesActivityPubUrl(video))

if (redirectIfNotOwned(video.url, res)) return

const json = await videoRates(req, 'like', video, getLocalVideoLikesActivityPubUrl(video))

return activityPubResponse(activityPubContextify(json), res)
}

async function videoDislikesController (req: express.Request, res: express.Response) {
const video = res.locals.onlyImmutableVideo
const json = await videoRates(req, 'dislike', video, getVideoDislikesActivityPubUrl(video))

if (redirectIfNotOwned(video.url, res)) return

const json = await videoRates(req, 'dislike', video, getLocalVideoDislikesActivityPubUrl(video))

return activityPubResponse(activityPubContextify(json), res)
}

async function videoCommentsController (req: express.Request, res: express.Response) {
const video = res.locals.onlyImmutableVideo

if (redirectIfNotOwned(video.url, res)) return

const handler = async (start: number, count: number) => {
const result = await VideoCommentModel.listAndCountByVideoForAP(video, start, count)
return {
total: result.count,
data: result.rows.map(r => r.url)
}
}
const json = await activityPubCollectionPagination(getVideoCommentsActivityPubUrl(video), handler, req.query.page)
const json = await activityPubCollectionPagination(getLocalVideoCommentsActivityPubUrl(video), handler, req.query.page)

return activityPubResponse(activityPubContextify(json), res)
}
Expand Down Expand Up @@ -319,7 +327,7 @@ async function videoChannelFollowingController (req: express.Request, res: expre
async function videoCommentController (req: express.Request, res: express.Response) {
const videoComment = res.locals.videoCommentFull

if (videoComment.url.startsWith(WEBSERVER.URL) === false) return res.redirect(videoComment.url)
if (redirectIfNotOwned(videoComment.url, res)) return

const threadParentComments = await VideoCommentModel.listThreadParentComments(videoComment, undefined)
const isPublic = true // Comments are always public
Expand All @@ -340,7 +348,8 @@ async function videoCommentController (req: express.Request, res: express.Respon

async function videoRedundancyController (req: express.Request, res: express.Response) {
const videoRedundancy = res.locals.videoRedundancy
if (videoRedundancy.url.startsWith(WEBSERVER.URL) === false) return res.redirect(videoRedundancy.url)

if (redirectIfNotOwned(videoRedundancy.url, res)) return

const serverActor = await getServerActor()

Expand All @@ -358,6 +367,8 @@ async function videoRedundancyController (req: express.Request, res: express.Res
async function videoPlaylistController (req: express.Request, res: express.Response) {
const playlist = res.locals.videoPlaylistFull

if (redirectIfNotOwned(playlist.url, res)) return

// We need more attributes
playlist.OwnerAccount = await AccountModel.load(playlist.ownerAccountId)

Expand All @@ -371,6 +382,8 @@ async function videoPlaylistController (req: express.Request, res: express.Respo
function videoPlaylistElementController (req: express.Request, res: express.Response) {
const videoPlaylistElement = res.locals.videoPlaylistElementAP

if (redirectIfNotOwned(videoPlaylistElement.url, res)) return

const json = videoPlaylistElement.toActivityPubObject()
return activityPubResponse(activityPubContextify(json), res)
}
Expand Down Expand Up @@ -411,3 +424,12 @@ function videoRates (req: express.Request, rateType: VideoRateType, video: MVide
}
return activityPubCollectionPagination(url, handler, req.query.page)
}

function redirectIfNotOwned (url: string, res: express.Response) {
if (url.startsWith(WEBSERVER.URL) === false) {
res.redirect(url)
return true
}

return false
}
2 changes: 1 addition & 1 deletion server/controllers/api/server/follows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ async function removeFollowing (req: express.Request, res: express.Response) {
async function removeOrRejectFollower (req: express.Request, res: express.Response) {
const follow = res.locals.follow

await sendReject(follow.ActorFollower, follow.ActorFollowing)
await sendReject(follow.url, follow.ActorFollower, follow.ActorFollowing)

await follow.destroy()

Expand Down
46 changes: 23 additions & 23 deletions server/controllers/api/video-playlist.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import * as express from 'express'
import { join } from 'path'
import { getServerActor } from '@server/models/application/application'
import { MVideoPlaylistFull, MVideoPlaylistThumbnail, MVideoThumbnail } from '@server/types/models'
import { VideoPlaylistCreate } from '../../../shared/models/videos/playlist/video-playlist-create.model'
import { VideoPlaylistElementCreate } from '../../../shared/models/videos/playlist/video-playlist-element-create.model'
import { VideoPlaylistElementUpdate } from '../../../shared/models/videos/playlist/video-playlist-element-update.model'
import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model'
import { VideoPlaylistReorder } from '../../../shared/models/videos/playlist/video-playlist-reorder.model'
import { VideoPlaylistUpdate } from '../../../shared/models/videos/playlist/video-playlist-update.model'
import { resetSequelizeInstance } from '../../helpers/database-utils'
import { buildNSFWFilter, createReqFiles } from '../../helpers/express-utils'
import { logger } from '../../helpers/logger'
import { getFormattedObjects } from '../../helpers/utils'
import { CONFIG } from '../../initializers/config'
import { MIMETYPES, VIDEO_PLAYLIST_PRIVACIES } from '../../initializers/constants'
import { sequelizeTypescript } from '../../initializers/database'
import { sendCreateVideoPlaylist, sendDeleteVideoPlaylist, sendUpdateVideoPlaylist } from '../../lib/activitypub/send'
import { getLocalVideoPlaylistActivityPubUrl, getLocalVideoPlaylistElementActivityPubUrl } from '../../lib/activitypub/url'
import { JobQueue } from '../../lib/job-queue'
import { createPlaylistMiniatureFromExisting } from '../../lib/thumbnail'
import {
asyncMiddleware,
asyncRetryTransactionMiddleware,
Expand All @@ -10,11 +29,6 @@ import {
setDefaultSort
} from '../../middlewares'
import { videoPlaylistsSortValidator } from '../../middlewares/validators'
import { buildNSFWFilter, createReqFiles } from '../../helpers/express-utils'
import { MIMETYPES, VIDEO_PLAYLIST_PRIVACIES } from '../../initializers/constants'
import { logger } from '../../helpers/logger'
import { resetSequelizeInstance } from '../../helpers/database-utils'
import { VideoPlaylistModel } from '../../models/video/video-playlist'
import {
commonVideoPlaylistFiltersValidator,
videoPlaylistsAddValidator,
Expand All @@ -25,23 +39,9 @@ import {
videoPlaylistsUpdateOrRemoveVideoValidator,
videoPlaylistsUpdateValidator
} from '../../middlewares/validators/videos/video-playlists'
import { VideoPlaylistCreate } from '../../../shared/models/videos/playlist/video-playlist-create.model'
import { VideoPlaylistPrivacy } from '../../../shared/models/videos/playlist/video-playlist-privacy.model'
import { join } from 'path'
import { sendCreateVideoPlaylist, sendDeleteVideoPlaylist, sendUpdateVideoPlaylist } from '../../lib/activitypub/send'
import { getVideoPlaylistActivityPubUrl, getVideoPlaylistElementActivityPubUrl } from '../../lib/activitypub/url'
import { VideoPlaylistUpdate } from '../../../shared/models/videos/playlist/video-playlist-update.model'
import { VideoPlaylistElementModel } from '../../models/video/video-playlist-element'
import { VideoPlaylistElementCreate } from '../../../shared/models/videos/playlist/video-playlist-element-create.model'
import { VideoPlaylistElementUpdate } from '../../../shared/models/videos/playlist/video-playlist-element-update.model'
import { AccountModel } from '../../models/account/account'
import { VideoPlaylistReorder } from '../../../shared/models/videos/playlist/video-playlist-reorder.model'
import { JobQueue } from '../../lib/job-queue'
import { CONFIG } from '../../initializers/config'
import { sequelizeTypescript } from '../../initializers/database'
import { createPlaylistMiniatureFromExisting } from '../../lib/thumbnail'
import { MVideoPlaylistFull, MVideoPlaylistThumbnail, MVideoThumbnail } from '@server/types/models'
import { getServerActor } from '@server/models/application/application'
import { VideoPlaylistModel } from '../../models/video/video-playlist'
import { VideoPlaylistElementModel } from '../../models/video/video-playlist-element'

const reqThumbnailFile = createReqFiles([ 'thumbnailfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT, { thumbnailfile: CONFIG.STORAGE.TMP_DIR })

Expand Down Expand Up @@ -161,7 +161,7 @@ async function addVideoPlaylist (req: express.Request, res: express.Response) {
ownerAccountId: user.Account.id
}) as MVideoPlaylistFull

videoPlaylist.url = getVideoPlaylistActivityPubUrl(videoPlaylist) // We use the UUID, so set the URL after building the object
videoPlaylist.url = getLocalVideoPlaylistActivityPubUrl(videoPlaylist) // We use the UUID, so set the URL after building the object

if (videoPlaylistInfo.videoChannelId) {
const videoChannel = res.locals.videoChannel
Expand Down Expand Up @@ -304,7 +304,7 @@ async function addVideoInPlaylist (req: express.Request, res: express.Response)
videoId: video.id
}, { transaction: t })

playlistElement.url = getVideoPlaylistElementActivityPubUrl(videoPlaylist, playlistElement)
playlistElement.url = getLocalVideoPlaylistElementActivityPubUrl(videoPlaylist, playlistElement)
await playlistElement.save({ transaction: t })

videoPlaylist.changed('updatedAt', true)
Expand Down
4 changes: 2 additions & 2 deletions server/controllers/api/videos/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { getYoutubeDLInfo, getYoutubeDLSubs, YoutubeDLInfo } from '../../../help
import { CONFIG } from '../../../initializers/config'
import { MIMETYPES } from '../../../initializers/constants'
import { sequelizeTypescript } from '../../../initializers/database'
import { getVideoActivityPubUrl } from '../../../lib/activitypub/url'
import { getLocalVideoActivityPubUrl } from '../../../lib/activitypub/url'
import { JobQueue } from '../../../lib/job-queue/job-queue'
import { createVideoMiniatureFromExisting, createVideoMiniatureFromUrl } from '../../../lib/thumbnail'
import { autoBlacklistVideoIfNeeded } from '../../../lib/video-blacklist'
Expand Down Expand Up @@ -250,7 +250,7 @@ function buildVideo (channelId: number, body: VideoImportCreate, importData: You
originallyPublishedAt: body.originallyPublishedAt || importData.originallyPublishedAt
}
const video = new VideoModel(videoData)
video.url = getVideoActivityPubUrl(video)
video.url = getLocalVideoActivityPubUrl(video)

return video
}
Expand Down
4 changes: 2 additions & 2 deletions server/controllers/api/videos/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import toInt from 'validator/lib/toInt'
import { addOptimizeOrMergeAudioJob } from '@server/helpers/video'
import { createTorrentAndSetInfoHash } from '@server/helpers/webtorrent'
import { changeVideoChannelShare } from '@server/lib/activitypub/share'
import { getVideoActivityPubUrl } from '@server/lib/activitypub/url'
import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url'
import { LiveManager } from '@server/lib/live-manager'
import { buildLocalVideoFromReq, buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video'
import { getVideoFilePath } from '@server/lib/video-paths'
Expand Down Expand Up @@ -189,7 +189,7 @@ async function addVideo (req: express.Request, res: express.Response) {
videoData.duration = videoPhysicalFile['duration'] // duration was added by a previous middleware

const video = new VideoModel(videoData) as MVideoFullLight
video.url = getVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object
video.url = getLocalVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object

const videoFile = new VideoFileModel({
extname: extname(videoPhysicalFile.filename),
Expand Down
4 changes: 2 additions & 2 deletions server/controllers/api/videos/live.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { v4 as uuidv4 } from 'uuid'
import { createReqFiles } from '@server/helpers/express-utils'
import { CONFIG } from '@server/initializers/config'
import { ASSETS_PATH, MIMETYPES } from '@server/initializers/constants'
import { getVideoActivityPubUrl } from '@server/lib/activitypub/url'
import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url'
import { federateVideoIfNeeded } from '@server/lib/activitypub/videos'
import { Hooks } from '@server/lib/plugins/hooks'
import { buildLocalVideoFromReq, buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video'
Expand Down Expand Up @@ -86,7 +86,7 @@ async function addLiveVideo (req: express.Request, res: express.Response) {
videoData.duration = 0

const video = new VideoModel(videoData) as MVideoDetails
video.url = getVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object
video.url = getLocalVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object

const videoLive = new VideoLiveModel()
videoLive.saveReplay = videoInfo.saveReplay || false
Expand Down
Loading

0 comments on commit de94ac8

Please sign in to comment.