mastofeed-iframe-embed/index.js

215 lines
5.6 KiB
JavaScript
Raw Normal View History

var Express = require('express');
2019-07-31 18:13:56 +02:00
// v2 api
var convertv2 = require('./lib/convertv2');
var serveStatic = require('serve-static');
2018-09-09 23:28:19 +02:00
var cors = require('cors');
2019-08-02 17:57:13 +02:00
var errorPage = require('./lib/errorPage');
2019-08-02 19:24:31 +02:00
var morgan = require('morgan');
2023-01-11 02:22:37 +01:00
var compression = require('compression')
2024-10-13 16:50:46 +02:00
const apCryptoShit = require('./lib/apCryptoShit')
var app = Express();
2019-08-05 22:19:27 +02:00
var logger = morgan(':method :url :status via :referrer - :response-time ms')
2023-01-11 02:22:37 +01:00
app.use(compression());
app.use(
serveStatic('static',{
maxAge:'1d'
})
);
2019-08-02 04:15:33 +02:00
function doCache(res,durationSecs){
res.set({
"Cache-Control":"max-age="+durationSecs
})
}
// this just redirects to the v2 API
2018-09-09 23:28:19 +02:00
app.options('/api/feed',cors());
2019-08-05 22:34:09 +02:00
app.get('/api/feed',cors(),logger,function(req,res){
// get feed url
var feedUrl = req.query.url;
if (!feedUrl){
res.status(400);
2019-08-02 17:57:13 +02:00
res.send(errorPage(400,'You need to specify a feed URL'));
return;
}
2019-08-05 22:31:57 +02:00
var userUrl = feedUrl.replace(/\.atom.*/i,'');
2018-03-25 00:33:22 +01:00
var redirectUrl = '/apiv2/feed?';
2019-08-05 22:19:27 +02:00
var qs = ['userurl='+encodeURIComponent(userUrl),"api=v1"];
(['size','theme','boosts','replies']).forEach(key=>{
if (typeof req.query[key] != 'undefined'){
qs.push(key+'='+encodeURIComponent(req.query[key]));
}
})
res.redirect(redirectUrl + qs.join('&'));
});
2019-07-31 18:13:56 +02:00
app.options('/apiv2/feed',cors());
// http://localhost:8000/apiv2/feed?userurl=https%3A%2F%2Foctodon.social%2Fusers%2Ffenwick67
2019-08-02 19:24:31 +02:00
app.get('/apiv2/feed',cors(),logger,function(req,res){
2019-07-31 18:13:56 +02:00
// get feed url
var userUrl = req.query.userurl;
if (!userUrl){
res.status(400);
2019-08-02 17:57:13 +02:00
res.send(errorPage(400,'You need to specify a user URL'));
return;
2019-07-31 18:13:56 +02:00
}
2019-07-31 20:48:51 +02:00
var feedUrl = req.query.feedurl;
2019-07-31 18:13:56 +02:00
var opts = {};
if (req.query.size){
opts.size = req.query.size;
}
if (req.query.theme){
opts.theme = req.query.theme;
}
if (req.query.header){
if (req.query.header.toLowerCase() == 'no' || req.query.header.toLowerCase() == 'false'){
opts.header = false;
}else{
opts.header = true;
}
}
opts.boosts = true;
if (req.query.boosts){
if (req.query.boosts.toLowerCase() == 'no' || req.query.boosts.toLowerCase() == 'false'){
opts.boosts = false;
}else{
opts.boosts = true;
}
}
opts.replies = true;
if (req.query.replies){
if (req.query.replies.toLowerCase() == 'no' || req.query.replies.toLowerCase() == 'false'){
opts.replies = false;
}else{
opts.replies = true;
}
}
opts.userUrl = userUrl;
opts.feedUrl = feedUrl;
opts.mastofeedUrl = req.url;
convertv2(opts).then((data)=>{
res.status(200);
2019-08-02 04:15:33 +02:00
doCache(res,60*60);
2019-07-31 18:13:56 +02:00
res.send(data);
}).catch((er)=>{
res.status(500);
2024-10-13 17:09:36 +02:00
res.send(errorPage(500,er.toString(),{theme:opts.theme,size:opts.size}));
// log the error
2019-07-31 20:48:51 +02:00
console.error(er,er.stack);
2019-07-31 18:13:56 +02:00
})
})
2024-10-13 16:50:46 +02:00
app.get('/actor', logger, function(req,res){
// return something like what https://mastodon.social/actor does...
res.status(200);
let j = {
"@context": [
"https://www.w3.org/ns/activitystreams",
"https://w3id.org/security/v1",
{
"manuallyApprovesFollowers": "as:manuallyApprovesFollowers",
"toot": "http://joinmastodon.org/ns#",
"featured": {
"@id": "toot:featured",
"@type": "@id"
},
"featuredTags": {
"@id": "toot:featuredTags",
"@type": "@id"
},
"alsoKnownAs": {
"@id": "as:alsoKnownAs",
"@type": "@id"
},
"movedTo": {
"@id": "as:movedTo",
"@type": "@id"
},
"schema": "http://schema.org#",
"PropertyValue": "schema:PropertyValue",
"value": "schema:value",
"discoverable": "toot:discoverable",
"suspended": "toot:suspended",
"memorial": "toot:memorial",
"indexable": "toot:indexable",
"attributionDomains": {
"@id": "toot:attributionDomains",
"@type": "@id"
}
}
],
"id": `https://${apCryptoShit.getDomainName()}/actor`,
"type": "Application",
"inbox": `https://${apCryptoShit.getDomainName()}/actor/inbox`,
"outbox": `https://${apCryptoShit.getDomainName()}/actor/outbox`,
"preferredUsername": `${apCryptoShit.getDomainName()}`,
"url": `https://${apCryptoShit.getDomainName()}`,
"manuallyApprovesFollowers": true,
"publicKey": {
"id": `https://${apCryptoShit.getDomainName()}/actor#main-key`,
"owner": `https://${apCryptoShit.getDomainName()}/actor`,
"publicKeyPem": apCryptoShit.getPublicKey()
},
"endpoints": {
"sharedInbox": `https://${apCryptoShit.getDomainName()}/inbox`
}
};
2024-10-13 17:54:28 +02:00
res.setHeader("content-type","application/activity+json; charset=utf-8")
res.send(JSON.stringify(j,null,2));
2024-10-13 16:50:46 +02:00
})
2024-10-13 18:14:45 +02:00
app.get('/.well-known/webfinger', function(req,res){
let domainName = apCryptoShit.getDomainName();
if (req.query.resource == `acct:${domainName}@${domainName}`){
res.setHeader("content-type","application/jrd+json; charset=utf-8");
var resJson = {
"subject": `acct:${domainName}@${domainName}`,
"aliases": [
`https://${domainName}/actor`
],
"links": [
{
"rel": "http://webfinger.net/rel/profile-page",
"type": "text/html",
"href": `https://${domainName}`
},
{
"rel": "self",
"type": "application/activity+json",
"href": `https://${domainName}/actor`
}
// ,
// {
// "rel": "http://ostatus.org/schema/1.0/subscribe",
// "template": "https://mastodon.social/authorize_interaction?uri={uri}"
// }
]
};
return res.send(JSON.stringify(resJson));
} else {
res.status(404);
2024-10-13 18:38:08 +02:00
res.send("unknown user");
2024-10-13 18:14:45 +02:00
}
})
app.listen(process.env.PORT || 8080,function(){
console.log('Mastofeed started, listening on '+(process.env.PORT || 8080));
});