Handle mastodon shares

pull/177/head
Chocobozzz 2017-12-19 17:07:58 +01:00
parent 85414add64
commit d7a7c248b3
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
3 changed files with 18 additions and 7 deletions

View File

@ -1,9 +1,11 @@
import { isBaseActivityValid } from './misc' import { isActivityPubUrlValid, isBaseActivityValid } from './misc'
import { isVideoTorrentCreateActivityValid } from './videos' import { isVideoTorrentCreateActivityValid } from './videos'
function isAnnounceActivityValid (activity: any) { function isAnnounceActivityValid (activity: any) {
console.log(activity)
return isBaseActivityValid(activity, 'Announce') && return isBaseActivityValid(activity, 'Announce') &&
( (
isActivityPubUrlValid(activity.object) ||
isVideoTorrentCreateActivityValid(activity.object) isVideoTorrentCreateActivityValid(activity.object)
) )
} }

View File

@ -1,4 +1,4 @@
import { ActivityAnnounce } from '../../../../shared/models/activitypub' import { ActivityAnnounce, ActivityCreate } from '../../../../shared/models/activitypub'
import { logger, retryTransactionWrapper } from '../../../helpers' import { logger, retryTransactionWrapper } from '../../../helpers'
import { sequelizeTypescript } from '../../../initializers' import { sequelizeTypescript } from '../../../initializers'
import { ActorModel } from '../../../models/activitypub/actor' import { ActorModel } from '../../../models/activitypub/actor'
@ -12,7 +12,9 @@ async function processAnnounceActivity (activity: ActivityAnnounce) {
const announcedActivity = activity.object const announcedActivity = activity.object
const actorAnnouncer = await getOrCreateActorAndServerAndModel(activity.actor) const actorAnnouncer = await getOrCreateActorAndServerAndModel(activity.actor)
if (announcedActivity.type === 'Create' && announcedActivity.object.type === 'Video') { if (typeof announcedActivity === 'string') {
return processVideoShare(actorAnnouncer, activity)
} else if (announcedActivity.type === 'Create' && announcedActivity.object.type === 'Video') {
return processVideoShare(actorAnnouncer, activity) return processVideoShare(actorAnnouncer, activity)
} }
@ -35,18 +37,25 @@ export {
function processVideoShare (actorAnnouncer: ActorModel, activity: ActivityAnnounce) { function processVideoShare (actorAnnouncer: ActorModel, activity: ActivityAnnounce) {
const options = { const options = {
arguments: [ actorAnnouncer, activity ], arguments: [ actorAnnouncer, activity ],
errorMessage: 'Cannot share the video with many retries.' errorMessage: 'Cannot share the video activity with many retries.'
} }
return retryTransactionWrapper(shareVideo, options) return retryTransactionWrapper(shareVideo, options)
} }
function shareVideo (actorAnnouncer: ActorModel, activity: ActivityAnnounce) { function shareVideo (actorAnnouncer: ActorModel, activity: ActivityAnnounce) {
const announcedActivity = activity.object const announced = activity.object
return sequelizeTypescript.transaction(async t => { return sequelizeTypescript.transaction(async t => {
// Add share entry // Add share entry
const video: VideoModel = await processCreateActivity(announcedActivity) let video: VideoModel
if (typeof announced === 'string') {
video = await VideoModel.loadByUrlAndPopulateAccount(announced as string)
if (!video) throw new Error('Unknown video to share ' + announced)
} else {
video = await processCreateActivity(announced as ActivityCreate)
}
const share = { const share = {
actorId: actorAnnouncer.id, actorId: actorAnnouncer.id,

View File

@ -51,7 +51,7 @@ export interface ActivityAccept extends BaseActivity {
export interface ActivityAnnounce extends BaseActivity { export interface ActivityAnnounce extends BaseActivity {
type: 'Announce' type: 'Announce'
object: ActivityCreate object: ActivityCreate | string
} }
export interface ActivityUndo extends BaseActivity { export interface ActivityUndo extends BaseActivity {