mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2025-12-13 07:45:56 +00:00
Create and send Post via ActivityPub inbox of the server
This commit is contained in:
parent
577157c505
commit
9e5b6865b8
@ -6,6 +6,10 @@ import uuid from 'uuid/v4'
|
||||
import { fixUrl } from './middleware/fixImageUrlsMiddleware'
|
||||
import { AuthenticationError } from 'apollo-server'
|
||||
import { neo4jgraphql } from 'neo4j-graphql-js'
|
||||
import as from 'activitystrea.ms'
|
||||
import request from 'request'
|
||||
|
||||
const debug = require('debug')('backend:schema')
|
||||
|
||||
export const typeDefs =
|
||||
fs.readFileSync(process.env.GRAPHQL_SCHEMA || path.join(__dirname, 'schema.graphql'))
|
||||
@ -163,10 +167,10 @@ export const resolvers = {
|
||||
return data
|
||||
},
|
||||
CreatePost: async (object, params, ctx, resolveInfo) => {
|
||||
params.activityId = uuid()
|
||||
const result = await neo4jgraphql(object, params, ctx, resolveInfo, false)
|
||||
|
||||
const session = ctx.driver.session()
|
||||
await session.run(
|
||||
const author = await session.run(
|
||||
'MATCH (author:User {id: $userId}), (post:Post {id: $postId}) ' +
|
||||
'MERGE (post)<-[:WROTE]-(author) ' +
|
||||
'RETURN author', {
|
||||
@ -174,7 +178,45 @@ export const resolvers = {
|
||||
postId: result.id
|
||||
})
|
||||
session.close()
|
||||
|
||||
const actorId = author.records[0]._fields[0].properties.actorId
|
||||
const createActivity = await new Promise((resolve, reject) => {
|
||||
as.create()
|
||||
.id(`${actorId}/status/${params.activityId}`)
|
||||
.actor(`${actorId}`)
|
||||
.object(
|
||||
as.article()
|
||||
.id(`${actorId}/status/${result.id}`)
|
||||
.content(result.content)
|
||||
.to('https://www.w3.org/ns/activitystreams#Public')
|
||||
.attributedTo(`${actorId}`)
|
||||
).prettyWrite((err, doc) => {
|
||||
if (err) {
|
||||
reject(err)
|
||||
} else {
|
||||
debug(doc)
|
||||
const parsedDoc = JSON.parse(doc)
|
||||
parsedDoc.send = true
|
||||
resolve(JSON.stringify(parsedDoc))
|
||||
}
|
||||
})
|
||||
})
|
||||
// try sending post via ActivityPub
|
||||
await new Promise((resolve) => {
|
||||
const url = new URL(actorId)
|
||||
request(`${url.origin}/activitypub/inbox`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/activity+json'
|
||||
},
|
||||
body: createActivity
|
||||
}, (err) => {
|
||||
if (err) {
|
||||
debug(`error = ${JSON.stringify(err, null, 2)}`)
|
||||
resolve(err)
|
||||
}
|
||||
resolve(null)
|
||||
})
|
||||
})
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user