bring back the homepage query, update for new data format

This commit is contained in:
Will Murphy 2021-11-04 22:59:37 -05:00
parent f46de1ba0a
commit 6216ad02a8
4 changed files with 36 additions and 9 deletions

View file

@ -10,7 +10,7 @@ Creation of new groups is automatic, users simply search for or mention a new gr
Mostly powered by [activitypub-express](https://github.com/immers-space/activitypub-express) Mostly powered by [activitypub-express](https://github.com/immers-space/activitypub-express)
from [Immers Space](https://web.immers.space). from [Immers Space](https://web.immers.space).
The gup.pe server app, `index.js` is < 200 lines of code, The gup.pe server app, `index.js` is 200 lines of code,
just adding the auto-create actor, auto-accept follow, and auto-boost from inbox behaviors just adding the auto-create actor, auto-accept follow, and auto-boost from inbox behaviors
to the base apex setup. to the base apex setup.

View file

@ -12,15 +12,12 @@ const ActivitypubExpress = require('activitypub-express')
const { DOMAIN, KEY_PATH, CERT_PATH, CA_PATH, PORT_HTTPS, DB_URL, DB_NAME } = process.env const { DOMAIN, KEY_PATH, CERT_PATH, CA_PATH, PORT_HTTPS, DB_URL, DB_NAME } = process.env
const app = express() const app = express()
const client = new MongoClient(DB_URL, { useUnifiedTopology: true, useNewUrlParser: true }) const client = new MongoClient(DB_URL, { useUnifiedTopology: true, useNewUrlParser: true })
const sslOptions = { const sslOptions = {
key: KEY_PATH && fs.readFileSync(path.join(__dirname, KEY_PATH)), key: KEY_PATH && fs.readFileSync(path.join(__dirname, KEY_PATH)),
cert: CERT_PATH && fs.readFileSync(path.join(__dirname, CERT_PATH)), cert: CERT_PATH && fs.readFileSync(path.join(__dirname, CERT_PATH)),
ca: CA_PATH && fs.readFileSync(path.join(__dirname, CA_PATH)) ca: CA_PATH && fs.readFileSync(path.join(__dirname, CA_PATH))
} }
const icon = { const icon = {
type: 'Image', type: 'Image',
mediaType: 'image/jpeg', mediaType: 'image/jpeg',
@ -89,7 +86,6 @@ app.get(routes.object, apex.net.object.get)
app.get(routes.activity, apex.net.activityStream.get) app.get(routes.activity, apex.net.activityStream.get)
app.get(routes.shares, apex.net.shares.get) app.get(routes.shares, apex.net.shares.get)
app.get(routes.likes, apex.net.likes.get) app.get(routes.likes, apex.net.likes.get)
app.get( app.get(
'/.well-known/webfinger', '/.well-known/webfinger',
apex.net.wellKnown.parseWebfinger, apex.net.wellKnown.parseWebfinger,
@ -100,9 +96,10 @@ app.get(
app.on('apex-inbox', async ({ actor, activity, recipient, object }) => { app.on('apex-inbox', async ({ actor, activity, recipient, object }) => {
switch (activity.type.toLowerCase()) { switch (activity.type.toLowerCase()) {
// automatically reshare incoming posts
case 'create': { case 'create': {
// ignore forwarded messages that aren't directly adddressed to group
if (!activity.to?.includes(recipient.id)) { if (!activity.to?.includes(recipient.id)) {
// ignore forwarded messages that aren't directly adddressed to group
return return
} }
const to = [ const to = [
@ -115,6 +112,7 @@ app.on('apex-inbox', async ({ actor, activity, recipient, object }) => {
apex.addToOutbox(recipient, share) apex.addToOutbox(recipient, share)
break break
} }
// automatically accept follow requests
case 'follow': { case 'follow': {
const accept = await apex.buildActivity('Accept', recipient.id, actor.id, { const accept = await apex.buildActivity('Accept', recipient.id, actor.id, {
object: activity.id object: activity.id
@ -137,6 +135,35 @@ app.use(history({
})) }))
app.use('/f', express.static('public/files')) app.use('/f', express.static('public/files'))
app.use('/web', express.static('web/dist')) app.use('/web', express.static('web/dist'))
// web json routes
app.get('/groups', (req, res, next) => {
apex.store.db.collection('streams')
.aggregate([
{ $sort: { _id: -1 } }, // start from most recent
{ $limit: 10000 }, // don't traverse the entire history
{ $match: { type: 'Announce' } },
{ $group: { _id: '$actor', postCount: { $sum: 1 } } },
{ $lookup: { from: 'objects', localField: '_id', foreignField: 'id', as: 'actor' } },
// merge joined actor up
{ $replaceRoot: { newRoot: { $mergeObjects: [{ $arrayElemAt: ['$actor', 0] }, '$$ROOT'] } } },
{ $project: { _id: 0, _meta: 0, actor: 0 } }
])
.sort({ postCount: -1 })
.limit(Number.parseInt(req.query.n) || 50)
.toArray()
.then(groups => apex.toJSONLD({
id: `https://${DOMAIN}/groups`,
type: 'OrderedCollection',
totalItems: groups.length,
orderedItems: groups
}))
// .then(groups => { console.log(JSON.stringify(groups)); return groups })
.then(groups => res.json(groups))
.catch(err => {
console.log(err.message)
return res.status(500).send()
})
})
app.use(function (err, req, res, next) { app.use(function (err, req, res, next) {
console.error(err.message, req.body, err.stack) console.error(err.message, req.body, err.stack)

View file

@ -47,14 +47,14 @@ export default {
} }
}, },
created () { created () {
window.fetch(`/u/`, { window.fetch(`/groups`, {
method: 'get', method: 'get',
headers: { headers: {
accept: 'application/json' accept: 'application/json'
} }
}).then(res => res.json()) }).then(res => res.json())
.then(json => { .then(json => {
this.groups = json this.groups = json.orderedItems
}) })
.catch(err => this.error = err.message) .catch(err => this.error = err.message)
} }

View file

@ -1,6 +1,6 @@
<template> <template>
<div class="w3-container w3-content w3-center w3-padding-32"> <div class="w3-container w3-content w3-center w3-padding-32">
<profile-summary :actor="actor"/> <profile-summary v-if="actor.id" :actor="actor"/>
<p class="w3-left-align">To join {{ actor.preferredUsername }}, enter your handle below and you'll be <p class="w3-left-align">To join {{ actor.preferredUsername }}, enter your handle below and you'll be
redirected back to this group's profile in your app where you can follow it.</p> redirected back to this group's profile in your app where you can follow it.</p>
<form class="w3-container"> <form class="w3-container">