Added API method to create "Note" with different visibilities

This commit is contained in:
Niko 2022-03-05 20:55:12 +01:00
parent 5610cde131
commit c6958de973
1 changed files with 49 additions and 25 deletions

View File

@ -111,11 +111,57 @@ async function getQuizs(toActor, filters, proj) {
return quizs return quizs
} }
async function sendNote(from, to, html, modc) {
modc = modc || function(a){ return a }
const actor = await apex.store.getObject(from, true)
if (actor === null)
return false
const topublic = to === 'public'
const tounlisted = to === 'unlisted'
const tofollowers = to === 'followers'
if (typeof to === 'string')
to = (tofollowers || topublic || tounlisted)
? { url: utils.firstIfArray(actor.followers) }
: await api.accounts.getAccount(to)
if ([false, undefined, null].includes(to))
return false
const id = utils.apRandomURL()
var payload = {
id: id+'#activity',
type: 'Create',
actor: actor.id,
to: [ to.url ],
object: {
id,
type: 'Note',
content: html,
attributedTo: actor.id,
to: [ to.url ],
},
}
if (typeof to.acct === 'string')
payload.object.tag = [{
type: 'Mention', href: to.url, name: to.acct,
}]
if (tounlisted || topublic)
payload.cc = ['as:Public']
if (topublic)
payload.object.to.unshift('as:Public')
payload = modc(payload)
await apex.addToOutbox(actor, payload)
await apex.store.saveObject(payload.object)
return true
}
module.exports = { module.exports = {
quizParse, quizParse,
quizCreate, quizCreate,
isFediloveQuiz, isFediloveQuiz,
getQuizs, getQuizs,
sendNote,
quizs: { quizs: {
get: [auth.enforceSession, async (req, res) => { get: [auth.enforceSession, async (req, res) => {
var filters = {} var filters = {}
@ -135,7 +181,7 @@ module.exports = {
if ([false, undefined, null].includes(quiz)) if ([false, undefined, null].includes(quiz))
return res.json({ error: 'invalid_params' }) return res.json({ error: 'invalid_params' })
if (quiz.responses !== undefined) if (false && quiz.responses !== undefined)
return res.json({ error: 'already_sent_quiz' }) return res.json({ error: 'already_sent_quiz' })
var responses = [] var responses = []
@ -154,30 +200,8 @@ module.exports = {
if (html === false) if (html === false)
return res.json({ error: 'invalid_params' }) return res.json({ error: 'invalid_params' })
const id = utils.apRandomURL() await sendNote(apID(res.locals.user.username), quiz.from, html,
const actor = await apex.store.getObject( function(o){ o.object.inReplyTo = quiz.replyTo; return o })
apID(res.locals.user.username), true)
const payload = {
id: id+'#activity',
type: 'Create',
actor: actor.id,
to: quiz.from.url,
object: {
id,
type: 'Note',
content: html,
to: quiz.from.url,
attributedTo: actor.id,
inReplyTo: quiz.replyTo,
tag: [{
type: 'Mention',
href: quiz.from.url,
name: quiz.from.acct,
}],
},
}
await apex.addToOutbox(actor, payload)
await apex.store.saveObject(payload.object)
return res.json({ result: 1 }) return res.json({ result: 1 })
}], }],
}, },