Skip to content

Commit

Permalink
Implement FEP-2677
Browse files Browse the repository at this point in the history
Better way to find and follow remote instance actor
  • Loading branch information
Chocobozzz committed Dec 27, 2023
1 parent b9077c8 commit 4f3fb47
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/models/src/server/job.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export type ActivitypubHttpBroadcastPayload = {

export type ActivitypubFollowPayload = {
followerActorId: number
name: string
name?: string
host: string
isAutoFollow?: boolean
assertIsChannel?: boolean
Expand Down
2 changes: 0 additions & 2 deletions server/core/controllers/api/server/follows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { HttpStatusCode, ServerFollowCreate, UserRight } from '@peertube/peertub
import { getServerActor } from '@server/models/application/application.js'
import { logger } from '../../../helpers/logger.js'
import { getFormattedObjects } from '../../../helpers/utils.js'
import { SERVER_ACTOR_NAME } from '../../../initializers/constants.js'
import { sequelizeTypescript } from '../../../initializers/database.js'
import { autoFollowBackIfNeeded } from '../../../lib/activitypub/follow.js'
import { sendAccept, sendReject, sendUndoFollow } from '../../../lib/activitypub/send/index.js'
Expand Down Expand Up @@ -132,7 +131,6 @@ async function addFollow (req: express.Request, res: express.Response) {
for (const host of hosts) {
const payload = {
host,
name: SERVER_ACTOR_NAME,
followerActorId: follower.id
}

Expand Down
8 changes: 7 additions & 1 deletion server/core/controllers/api/videos/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url.js'
import { CreateJobArgument, CreateJobOptions, JobQueue } from '@server/lib/job-queue/index.js'
import { Redis } from '@server/lib/redis.js'
import { uploadx } from '@server/lib/uploadx.js'
import { buildLocalVideoFromReq, buildMoveJob, buildStoryboardJobIfNeeded, buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video.js'
import {
buildLocalVideoFromReq,
buildMoveJob,
buildStoryboardJobIfNeeded,
buildVideoThumbnailsFromReq,
setVideoTags
} from '@server/lib/video.js'
import { buildNewFile } from '@server/lib/video-file.js'
import { VideoPathManager } from '@server/lib/video-path-manager.js'
import { buildNextVideoState } from '@server/lib/video-state.js'
Expand Down
4 changes: 4 additions & 0 deletions server/core/controllers/well-known.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ wellKnownRouter.use('/.well-known/nodeinfo',
{
rel: 'http://nodeinfo.diaspora.software/ns/schema/2.0',
href: WEBSERVER.URL + '/nodeinfo/2.0.json'
},
{
rel: 'https://www.w3.org/ns/activitystreams#Application',
href: WEBSERVER.URL + '/accounts/peertube'
}
]
})
Expand Down
15 changes: 14 additions & 1 deletion server/core/helpers/activity-pub-utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { ContextType } from '@peertube/peertube-models'
import { ACTIVITY_PUB } from '@server/initializers/constants.js'
import { ACTIVITY_PUB, REMOTE_SCHEME } from '@server/initializers/constants.js'
import { buildDigest } from './peertube-crypto.js'
import type { signJsonLDObject } from './peertube-jsonld.js'
import { doJSONRequest } from './requests.js'
import { isArray } from './custom-validators/misc.js'

export type ContextFilter = <T> (arg: T) => Promise<T>

Expand Down Expand Up @@ -36,6 +38,17 @@ export async function signAndContextify <T> (options: {
return signerFunction({ byActor, data: activity })
}

export async function getApplicationActorOfHost (host: string) {
const url = REMOTE_SCHEME.HTTP + '://' + host + '/.well-known/nodeinfo'
const { body } = await doJSONRequest<{ links: { rel: string, href: string }[] }>(url)

if (!isArray(body.links)) return undefined

const found = body.links.find(l => l.rel === 'https://www.w3.org/ns/activitystreams#Application')

return found?.href || undefined
}

// ---------------------------------------------------------------------------
// Private
// ---------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion server/core/initializers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,7 @@ export function reloadConfig () {
return process.env.NODE_CONFIG_DIR.split(':')
}

return [join(root(), 'config')]
return [ join(root(), 'config') ]
}

function purge () {
Expand Down
4 changes: 2 additions & 2 deletions server/core/lib/activitypub/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ function getAbuseTargetUrl (abuse: MAbuseFull) {

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

function buildRemoteVideoBaseUrl (video: MVideoWithHost, path: string, scheme?: string) {
function buildRemoteUrl (video: MVideoWithHost, path: string, scheme?: string) {
if (!scheme) scheme = REMOTE_SCHEME.HTTP

const host = video.VideoChannel.Actor.Server.host
Expand Down Expand Up @@ -178,5 +178,5 @@ export {

getAbuseTargetUrl,
checkUrlsSameHost,
buildRemoteVideoBaseUrl
buildRemoteUrl
}
6 changes: 3 additions & 3 deletions server/core/lib/activitypub/videos/shared/trackers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { REMOTE_SCHEME } from '@server/initializers/constants.js'
import { TrackerModel } from '@server/models/server/tracker.js'
import { MVideo, MVideoWithHost } from '@server/types/models/index.js'
import { ActivityTrackerUrlObject, VideoObject } from '@peertube/peertube-models'
import { buildRemoteVideoBaseUrl } from '../../url.js'
import { buildRemoteUrl } from '../../url.js'

function getTrackerUrls (object: VideoObject, video: MVideoWithHost) {
let wsFound = false
Expand All @@ -20,8 +20,8 @@ function getTrackerUrls (object: VideoObject, video: MVideoWithHost) {
if (wsFound) return trackers

return [
buildRemoteVideoBaseUrl(video, '/tracker/socket', REMOTE_SCHEME.WS),
buildRemoteVideoBaseUrl(video, '/tracker/announce')
buildRemoteUrl(video, '/tracker/socket', REMOTE_SCHEME.WS),
buildRemoteUrl(video, '/tracker/announce')
]
}

Expand Down
12 changes: 10 additions & 2 deletions server/core/lib/job-queue/handlers/activitypub-follow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import { ActivitypubFollowPayload } from '@peertube/peertube-models'
import { sanitizeHost } from '../../../helpers/core-utils.js'
import { retryTransactionWrapper } from '../../../helpers/database-utils.js'
import { logger } from '../../../helpers/logger.js'
import { REMOTE_SCHEME, WEBSERVER } from '../../../initializers/constants.js'
import { REMOTE_SCHEME, SERVER_ACTOR_NAME, WEBSERVER } from '../../../initializers/constants.js'
import { sequelizeTypescript } from '../../../initializers/database.js'
import { ActorModel } from '../../../models/actor/actor.js'
import { ActorFollowModel } from '../../../models/actor/actor-follow.js'
import { MActor, MActorFull } from '../../../types/models/index.js'
import { getOrCreateAPActor, loadActorUrlOrGetFromWebfinger } from '../../activitypub/actors/index.js'
import { sendFollow } from '../../activitypub/send/index.js'
import { Notifier } from '../../notifier/index.js'
import { getApplicationActorOfHost } from '@server/helpers/activity-pub-utils.js'

async function processActivityPubFollow (job: Job) {
const payload = job.data as ActivitypubFollowPayload
Expand All @@ -21,10 +22,17 @@ async function processActivityPubFollow (job: Job) {

let targetActor: MActorFull
if (!host || host === WEBSERVER.HOST) {
if (!payload.name) throw new Error('Payload name is mandatory for local follow')

targetActor = await ActorModel.loadLocalByName(payload.name)
} else {
const sanitizedHost = sanitizeHost(host, REMOTE_SCHEME.HTTP)
const actorUrl = await loadActorUrlOrGetFromWebfinger(payload.name + '@' + sanitizedHost)

let actorUrl: string

if (!payload.name) actorUrl = await getApplicationActorOfHost(sanitizedHost)
if (!actorUrl) actorUrl = await loadActorUrlOrGetFromWebfinger((payload.name || SERVER_ACTOR_NAME) + '@' + sanitizedHost)

targetActor = await getOrCreateAPActor(actorUrl, 'all')
}

Expand Down
4 changes: 2 additions & 2 deletions server/core/models/video/video-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { AttributesOnly } from '@peertube/peertube-typescript-utils'
import { logger } from '@server/helpers/logger.js'
import { extractVideo } from '@server/helpers/video.js'
import { CONFIG } from '@server/initializers/config.js'
import { buildRemoteVideoBaseUrl } from '@server/lib/activitypub/url.js'
import { buildRemoteUrl } from '@server/lib/activitypub/url.js'
import {
getHLSPrivateFileUrl,
getHLSPublicFileUrl,
Expand Down Expand Up @@ -582,7 +582,7 @@ export class VideoFileModel extends Model<Partial<AttributesOnly<VideoFileModel>
if (video.isOwned()) return WEBSERVER.URL + path

// FIXME: don't guess remote URL
return buildRemoteVideoBaseUrl(video, path)
return buildRemoteUrl(video, path)
}

getRemoteTorrentUrl (video: MVideo) {
Expand Down

0 comments on commit 4f3fb47

Please sign in to comment.