2017-11-23 07:19:55 -06:00
|
|
|
import { ActivityLike } from '../../../../shared/models/activitypub/activity'
|
2017-11-24 06:41:10 -06:00
|
|
|
import { retryTransactionWrapper } from '../../../helpers/database-utils'
|
2017-11-23 07:19:55 -06:00
|
|
|
import { database as db } from '../../../initializers'
|
|
|
|
import { AccountInstance } from '../../../models/account/account-interface'
|
|
|
|
import { getOrCreateAccountAndServer } from '../account'
|
2017-11-24 06:41:10 -06:00
|
|
|
import { forwardActivity } from '../send/misc'
|
2017-11-23 07:19:55 -06:00
|
|
|
|
|
|
|
async function processLikeActivity (activity: ActivityLike) {
|
|
|
|
const account = await getOrCreateAccountAndServer(activity.actor)
|
|
|
|
|
2017-11-24 06:41:10 -06:00
|
|
|
return processLikeVideo(account, activity)
|
2017-11-23 07:19:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
processLikeActivity
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2017-11-24 06:41:10 -06:00
|
|
|
async function processLikeVideo (byAccount: AccountInstance, activity: ActivityLike) {
|
2017-11-23 07:19:55 -06:00
|
|
|
const options = {
|
2017-11-24 06:41:10 -06:00
|
|
|
arguments: [ byAccount, activity ],
|
2017-11-23 07:19:55 -06:00
|
|
|
errorMessage: 'Cannot like the video with many retries.'
|
|
|
|
}
|
|
|
|
|
|
|
|
return retryTransactionWrapper(createVideoLike, options)
|
|
|
|
}
|
|
|
|
|
2017-11-24 06:41:10 -06:00
|
|
|
function createVideoLike (byAccount: AccountInstance, activity: ActivityLike) {
|
|
|
|
const videoUrl = activity.object
|
|
|
|
|
2017-11-23 07:19:55 -06:00
|
|
|
return db.sequelize.transaction(async t => {
|
|
|
|
const video = await db.Video.loadByUrlAndPopulateAccount(videoUrl)
|
|
|
|
|
|
|
|
if (!video) throw new Error('Unknown video ' + videoUrl)
|
|
|
|
|
|
|
|
const rate = {
|
|
|
|
type: 'like' as 'like',
|
|
|
|
videoId: video.id,
|
|
|
|
accountId: byAccount.id
|
|
|
|
}
|
|
|
|
const [ , created ] = await db.AccountVideoRate.findOrCreate({
|
|
|
|
where: rate,
|
2017-11-24 06:41:10 -06:00
|
|
|
defaults: rate,
|
|
|
|
transaction: t
|
2017-11-23 07:19:55 -06:00
|
|
|
})
|
2017-11-30 06:51:53 -06:00
|
|
|
if (created === true) await video.increment('likes', { transaction: t })
|
2017-11-23 07:19:55 -06:00
|
|
|
|
2017-11-24 06:41:10 -06:00
|
|
|
if (video.isOwned() && created === true) {
|
|
|
|
// Don't resend the activity to the sender
|
|
|
|
const exceptions = [ byAccount ]
|
|
|
|
await forwardActivity(activity, t, exceptions)
|
|
|
|
}
|
2017-11-23 07:19:55 -06:00
|
|
|
})
|
|
|
|
}
|