Fetch outbox when searching an actor

pull/974/head
Chocobozzz 2018-08-24 15:36:50 +02:00
parent f5b0af50c8
commit 687d638c2b
9 changed files with 118 additions and 83 deletions

View File

@ -113,8 +113,6 @@
}
.video-channel-info {
flex-grow: 1;
width: fit-content;

View File

@ -1,26 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<svg width="24px" height="24px" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<!-- Generator: Sketch 43.2 (39069) - http://www.bohemiancoding.com/sketch -->
<title>podcasts</title>
<desc>Created with Sketch.</desc>
<defs>
<linearGradient x1="50%" y1="0%" x2="50%" y2="97.3333865%" id="linearGradient-1">
<stop stop-color="#808080" offset="0%"></stop>
<stop stop-color="#808080" stop-opacity="0.247310915" offset="100%"></stop>
</linearGradient>
<linearGradient x1="50%" y1="0%" x2="50%" y2="97.8635204%" id="linearGradient-2">
<stop stop-color="#808080" offset="0%"></stop>
<stop stop-color="#808080" stop-opacity="0.250707654" offset="100%"></stop>
</linearGradient>
</defs>
<g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
<g id="Artboard-4" transform="translate(-532.000000, -775.000000)">
<g id="312" transform="translate(532.000000, 775.000000)">
<circle id="Oval-169" fill="#808080" cx="12" cy="10" r="3"></circle>
<path d="M16.3851456,13.8501206 C17.4222377,12.6991612 18,11.4167199 18,10 C18,6.74089158 15.2591084,4 12,4 C8.74089158,4 6,6.74089158 6,10 C6,11.4186069 6.57916224,12.7027674 7.61838071,13.8540306 C7.80341316,14.0590125 8.11958231,14.0751848 8.32456427,13.8901523 C8.52954623,13.7051199 8.5457185,13.3889507 8.36068606,13.1839688 C7.47616718,12.2040844 7,11.148292 7,10 C7,7.29317633 9.29317633,5 12,5 C14.7068237,5 17,7.29317633 17,10 C17,11.1466944 16.5249958,12.2010466 15.6422459,13.1807178 C15.4573954,13.3858639 15.4738483,13.7020185 15.6789944,13.886869 C15.8841405,14.0717195 16.2002951,14.0552666 16.3851456,13.8501206 Z" id="Oval-169" fill="url(#linearGradient-1)" fill-rule="nonzero"></path>
<path d="M17.5678226,18.3077078 C20.3159646,16.4626239 22,13.3733223 22,10 C22,4.4771525 17.5228475,0 12,0 C6.4771525,0 2,4.4771525 2,10 C2,13.3762414 3.68696556,16.4678678 6.43901638,18.3122954 C6.89779529,18.6197696 7.51896613,18.4971129 7.82644029,18.0383339 C8.13391444,17.579555 8.0112577,16.9583842 7.55247879,16.65091 C5.34877306,15.1739839 4,12.7021478 4,10 C4,5.581722 7.581722,2 12,2 C16.418278,2 20,5.581722 20,10 C20,12.699815 18.6535741,15.1697843 16.4529947,16.6472384 C15.9944687,16.9550897 15.8723227,17.5763611 16.180174,18.0348871 C16.4880252,18.4934131 17.1092967,18.6155591 17.5678226,18.3077078 Z" id="Oval-169" fill="url(#linearGradient-2)" fill-rule="nonzero"></path>
<path d="M9.32918137,15.9750882 C9.14737952,14.8842771 9.89826062,14 10.9979131,14 L13.0020869,14 C14.1055038,14 14.8534426,14.8793447 14.6708186,15.9750882 L13.6633817,22.0197096 C13.5731485,22.561109 13.0573397,23 12.5010434,23 L11.4989566,23 C10.9472481,23 10.4276519,22.5659113 10.3366183,22.0197096 L9.32918137,15.9750882 Z" id="Rectangle-217" fill="#808080"></path>
</g>
</g>
</g>
</svg>

Before

Width:  |  Height:  |  Size: 3.0 KiB

View File

@ -11,7 +11,7 @@ import {
import { accountsNameWithHostGetValidator, accountsSortValidator, videosSortValidator } from '../../middlewares/validators'
import { AccountModel } from '../../models/account/account'
import { VideoModel } from '../../models/video/video'
import { buildNSFWFilter } from '../../helpers/express-utils'
import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
import { VideoChannelModel } from '../../models/video/video-channel'
const accountsRouter = express.Router()
@ -73,8 +73,10 @@ async function listVideoAccountChannels (req: express.Request, res: express.Resp
async function listAccountVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
const account: AccountModel = res.locals.account
const actorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
const resultList = await VideoModel.listForApi({
actorId,
start: req.query.start,
count: req.query.count,
sort: req.query.sort,

View File

@ -1,5 +1,5 @@
import * as express from 'express'
import { buildNSFWFilter } from '../../helpers/express-utils'
import { buildNSFWFilter, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
import { getFormattedObjects, getServerActor } from '../../helpers/utils'
import { VideoModel } from '../../models/video/video'
import {
@ -88,7 +88,7 @@ async function searchVideoChannelURI (search: string, isWebfingerSearch: boolean
if (isUserAbleToSearchRemoteURI(res)) {
try {
const actor = await getOrCreateActorAndServerAndModel(uri)
const actor = await getOrCreateActorAndServerAndModel(uri, true, true)
videoChannel = actor.VideoChannel
} catch (err) {
logger.info('Cannot search remote video channel %s.', uri, { err })
@ -152,10 +152,3 @@ async function searchVideoURI (url: string, res: express.Response) {
data: video ? [ video.toFormattedJSON() ] : []
})
}
function isUserAbleToSearchRemoteURI (res: express.Response) {
const user: User = res.locals.oauth ? res.locals.oauth.token.User : undefined
return CONFIG.SEARCH.REMOTE_URI.ANONYMOUS === true ||
(CONFIG.SEARCH.REMOTE_URI.USERS === true && user !== undefined)
}

View File

@ -19,7 +19,7 @@ import { videoChannelsNameWithHostValidator, videosSortValidator } from '../../m
import { sendUpdateActor } from '../../lib/activitypub/send'
import { VideoChannelCreate, VideoChannelUpdate } from '../../../shared'
import { createVideoChannel } from '../../lib/video-channel'
import { buildNSFWFilter, createReqFiles } from '../../helpers/express-utils'
import { buildNSFWFilter, createReqFiles, isUserAbleToSearchRemoteURI } from '../../helpers/express-utils'
import { setAsyncActorKeys } from '../../lib/activitypub'
import { AccountModel } from '../../models/account/account'
import { CONFIG, IMAGE_MIMETYPE_EXT, sequelizeTypescript } from '../../initializers'
@ -210,8 +210,10 @@ async function getVideoChannel (req: express.Request, res: express.Response, nex
async function listVideoChannelVideos (req: express.Request, res: express.Response, next: express.NextFunction) {
const videoChannelInstance: VideoChannelModel = res.locals.videoChannel
const actorId = isUserAbleToSearchRemoteURI(res) ? null : undefined
const resultList = await VideoModel.listForApi({
actorId,
start: req.query.start,
count: req.query.count,
sort: req.query.sort,

View File

@ -95,11 +95,19 @@ function createReqFiles (
return multer({ storage }).fields(fields)
}
function isUserAbleToSearchRemoteURI (res: express.Response) {
const user: User = res.locals.oauth ? res.locals.oauth.token.User : undefined
return CONFIG.SEARCH.REMOTE_URI.ANONYMOUS === true ||
(CONFIG.SEARCH.REMOTE_URI.USERS === true && user !== undefined)
}
// ---------------------------------------------------------------------------
export {
buildNSFWFilter,
getHostWithPort,
isUserAbleToSearchRemoteURI,
badRequest,
createReqFiles,
cleanUpReqFiles

View File

@ -36,8 +36,13 @@ function setAsyncActorKeys (actor: ActorModel) {
})
}
async function getOrCreateActorAndServerAndModel (activityActor: string | ActivityPubActor, recurseIfNeeded = true) {
async function getOrCreateActorAndServerAndModel (
activityActor: string | ActivityPubActor,
recurseIfNeeded = true,
updateCollections = false
) {
const actorUrl = getActorUrl(activityActor)
let created = false
let actor = await ActorModel.loadByUrl(actorUrl)
// Orphan actor (not associated to an account of channel) so recreate it
@ -68,15 +73,21 @@ async function getOrCreateActorAndServerAndModel (activityActor: string | Activi
}
actor = await retryTransactionWrapper(saveActorAndServerAndModelIfNotExist, result, ownerActor)
created = true
}
if (actor.Account) actor.Account.Actor = actor
if (actor.VideoChannel) actor.VideoChannel.Actor = actor
actor = await retryTransactionWrapper(refreshActorIfNeeded, actor)
if (!actor) throw new Error('Actor ' + actor.url + ' does not exist anymore.')
const { actor: actorRefreshed, refreshed } = await retryTransactionWrapper(refreshActorIfNeeded, actor)
if (!actorRefreshed) throw new Error('Actor ' + actorRefreshed.url + ' does not exist anymore.')
return actor
if ((created === true || refreshed === true) && updateCollections === true) {
const payload = { uri: actor.outboxUrl, type: 'activity' as 'activity' }
await JobQueue.Instance.createJob({ type: 'activitypub-http-fetcher', payload })
}
return actorRefreshed
}
function buildActorInstance (type: ActivityPubActorType, url: string, preferredUsername: string, uuid?: string) {
@ -359,8 +370,8 @@ async function saveVideoChannel (actor: ActorModel, result: FetchRemoteActorResu
return videoChannelCreated
}
async function refreshActorIfNeeded (actor: ActorModel): Promise<ActorModel> {
if (!actor.isOutdated()) return actor
async function refreshActorIfNeeded (actor: ActorModel): Promise<{ actor: ActorModel, refreshed: boolean }> {
if (!actor.isOutdated()) return { actor, refreshed: false }
try {
const actorUrl = await getUrlFromWebfinger(actor.preferredUsername + '@' + actor.getHost())
@ -369,12 +380,12 @@ async function refreshActorIfNeeded (actor: ActorModel): Promise<ActorModel> {
if (statusCode === 404) {
logger.info('Deleting actor %s because there is a 404 in refresh actor.', actor.url)
actor.Account ? actor.Account.destroy() : actor.VideoChannel.destroy()
return undefined
return { actor: undefined, refreshed: false }
}
if (result === undefined) {
logger.warn('Cannot fetch remote actor in refresh actor.')
return actor
return { actor, refreshed: false }
}
return sequelizeTypescript.transaction(async t => {
@ -403,10 +414,10 @@ async function refreshActorIfNeeded (actor: ActorModel): Promise<ActorModel> {
await actor.VideoChannel.save({ transaction: t })
}
return actor
return { refreshed: true, actor }
})
} catch (err) {
logger.warn('Cannot refresh actor.', { err })
return actor
return { actor, refreshed: false }
}
}

View File

@ -201,39 +201,12 @@ type AvailableForListOptions = {
]
}
// Force actorId to be a number to avoid SQL injections
const actorIdNumber = parseInt(options.actorId.toString(), 10)
let localVideosReq = ''
if (options.includeLocalVideos === true) {
localVideosReq = ' UNION ALL ' +
'SELECT "video"."id" AS "id" FROM "video" ' +
'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' +
'INNER JOIN "actor" ON "account"."actorId" = "actor"."id" ' +
'WHERE "actor"."serverId" IS NULL'
}
// FIXME: It would be more efficient to use a CTE so we join AFTER the filters, but sequelize does not support it...
const query: IFindOptions<VideoModel> = {
where: {
id: {
[Sequelize.Op.notIn]: Sequelize.literal(
'(SELECT "videoBlacklist"."videoId" FROM "videoBlacklist")'
),
[ Sequelize.Op.in ]: Sequelize.literal(
'(' +
'SELECT "videoShare"."videoId" AS "id" FROM "videoShare" ' +
'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "videoShare"."actorId" ' +
'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
' UNION ALL ' +
'SELECT "video"."id" AS "id" FROM "video" ' +
'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' +
'INNER JOIN "actor" ON "account"."actorId" = "actor"."id" ' +
'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "actor"."id" ' +
'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
localVideosReq +
')'
)
},
// Always list public videos
@ -254,6 +227,36 @@ type AvailableForListOptions = {
include: [ videoChannelInclude ]
}
if (options.actorId) {
let localVideosReq = ''
if (options.includeLocalVideos === true) {
localVideosReq = ' UNION ALL ' +
'SELECT "video"."id" AS "id" FROM "video" ' +
'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' +
'INNER JOIN "actor" ON "account"."actorId" = "actor"."id" ' +
'WHERE "actor"."serverId" IS NULL'
}
// Force actorId to be a number to avoid SQL injections
const actorIdNumber = parseInt(options.actorId.toString(), 10)
query.where['id'][ Sequelize.Op.in ] = Sequelize.literal(
'(' +
'SELECT "videoShare"."videoId" AS "id" FROM "videoShare" ' +
'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "videoShare"."actorId" ' +
'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
' UNION ALL ' +
'SELECT "video"."id" AS "id" FROM "video" ' +
'INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ' +
'INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ' +
'INNER JOIN "actor" ON "account"."actorId" = "actor"."id" ' +
'INNER JOIN "actorFollow" ON "actorFollow"."targetActorId" = "actor"."id" ' +
'WHERE "actorFollow"."actorId" = ' + actorIdNumber +
localVideosReq +
')'
)
}
if (options.withFiles === true) {
query.include.push({
model: VideoFileModel.unscoped(),
@ -849,7 +852,8 @@ export class VideoModel extends Model<VideoModel> {
order: getSort(options.sort)
}
const actorId = options.actorId || (await getServerActor()).id
// actorId === null has a meaning, so just check undefined
const actorId = options.actorId !== undefined ? options.actorId : (await getServerActor()).id
const scopes = {
method: [
@ -926,7 +930,8 @@ export class VideoModel extends Model<VideoModel> {
id: {
[ Sequelize.Op.in ]: Sequelize.literal(
'(' +
'SELECT "video"."id" FROM "video" WHERE ' +
'SELECT "video"."id" FROM "video" ' +
'WHERE ' +
'lower(immutable_unaccent("video"."name")) % lower(immutable_unaccent(' + escapedSearch + ')) OR ' +
'lower(immutable_unaccent("video"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))' +
'UNION ALL ' +

View File

@ -8,11 +8,11 @@ import {
deleteVideoChannel,
flushAndRunMultipleServers,
flushTests,
getVideoChannelsList,
getVideoChannelsList, getVideoChannelVideos,
killallServers,
ServerInfo,
setAccessTokensToServers,
updateMyUser,
updateMyUser, updateVideo,
updateVideoChannel,
uploadVideo,
userLogin,
@ -27,6 +27,8 @@ const expect = chai.expect
describe('Test a ActivityPub video channels search', function () {
let servers: ServerInfo[]
let userServer2Token: string
let videoServer2UUID: string
let channelIdServer2: number
before(async function () {
this.timeout(120000)
@ -56,10 +58,10 @@ describe('Test a ActivityPub video channels search', function () {
displayName: 'Channel 1 server 2'
}
const resChannel = await addVideoChannel(servers[1].url, userServer2Token, channel)
const channelId = resChannel.body.videoChannel.id
channelIdServer2 = resChannel.body.videoChannel.id
await uploadVideo(servers[1].url, userServer2Token, { name: 'video 1 server 2', channelId })
await uploadVideo(servers[1].url, userServer2Token, { name: 'video 2 server 2', channelId })
const res = await uploadVideo(servers[1].url, userServer2Token, { name: 'video 1 server 2', channelId: channelIdServer2 })
videoServer2UUID = res.body.video.uuid
}
await waitJobs(servers)
@ -129,6 +131,23 @@ describe('Test a ActivityPub video channels search', function () {
expect(res.body.data[2].name).to.equal('root_channel')
})
it('Should list video channel videos of server 2 without token', async function () {
this.timeout(30000)
await waitJobs(servers)
const res = await getVideoChannelVideos(servers[0].url, null, 'channel1_server2@localhost:9002', 0, 5)
expect(res.body.total).to.equal(0)
expect(res.body.data).to.have.lengthOf(0)
})
it('Should list video channel videos of server 2 with token', async function () {
const res = await getVideoChannelVideos(servers[0].url, servers[0].accessToken, 'channel1_server2@localhost:9002', 0, 5)
expect(res.body.total).to.equal(1)
expect(res.body.data[0].name).to.equal('video 1 server 2')
})
it('Should update video channel of server 2, and refresh it on server 1', async function () {
this.timeout(60000)
@ -151,6 +170,29 @@ describe('Test a ActivityPub video channels search', function () {
// expect(videoChannel.ownerAccount.displayName).to.equal('user updated')
})
it('Should update and add a video on server 2, and update it on server 1 after a search', async function () {
this.timeout(60000)
await updateVideo(servers[1].url, userServer2Token, videoServer2UUID, { name: 'video 1 updated' })
await uploadVideo(servers[1].url, userServer2Token, { name: 'video 2 server 2', channelId: channelIdServer2 })
await waitJobs(servers)
// Expire video channel
await wait(10000)
const search = 'http://localhost:9002/video-channels/channel1_server2'
await searchVideoChannel(servers[0].url, search, servers[0].accessToken)
await waitJobs(servers)
const res = await getVideoChannelVideos(servers[0].url, servers[0].accessToken, 'channel1_server2@localhost:9002', 0, 5, '-createdAt')
expect(res.body.total).to.equal(2)
expect(res.body.data[0].name).to.equal('video 2 server 2')
expect(res.body.data[1].name).to.equal('video 1 updated')
})
it('Should delete video channel of server 2, and delete it on server 1', async function () {
this.timeout(60000)