implemented oembed to hopefully simplify things
This commit is contained in:
parent
5d42c96f7d
commit
dda0eceb65
1 changed files with 51 additions and 14 deletions
65
index.js
65
index.js
|
@ -42,16 +42,23 @@ async function getapiurls(str){
|
||||||
}
|
}
|
||||||
|
|
||||||
async function inviteres(req,res){
|
async function inviteres(req,res){
|
||||||
//console.log(req.rawHeaders);
|
let url;
|
||||||
|
if(URL.canParse(req.query.url)){
|
||||||
|
url=new URL(req.query.url);
|
||||||
|
}else{
|
||||||
|
const scheme = req.secure ? 'https' : 'http';
|
||||||
|
const host=`${scheme}://${req.get("Host")}`;
|
||||||
|
url=new URL(host);
|
||||||
|
}
|
||||||
try{
|
try{
|
||||||
|
if(url.pathname.startsWith("invite")){
|
||||||
let embed=isembed(req.get("User-Agent"));
|
throw -1;
|
||||||
if(!embed){return false};
|
}
|
||||||
const code=req.path.split("/")[2];
|
const code=url.pathname.split("/")[2];
|
||||||
let title="";
|
let title="";
|
||||||
let description="";
|
let description="";
|
||||||
let icon="";
|
let thumbnail="";
|
||||||
const urls=await getapiurls(req.query.instance);
|
const urls=await getapiurls(url.searchParams.get("instance"));
|
||||||
await fetch(`${urls.api}/invites/${code}`,{
|
await fetch(`${urls.api}/invites/${code}`,{
|
||||||
method:"GET"
|
method:"GET"
|
||||||
}).then(_=>_.json()).then(json=>{
|
}).then(_=>_.json()).then(json=>{
|
||||||
|
@ -62,10 +69,28 @@ async function inviteres(req,res){
|
||||||
description="you've been invited to "+json.guild.name+(json.guild.description?json.guild.description+"\n":"");
|
description="you've been invited to "+json.guild.name+(json.guild.description?json.guild.description+"\n":"");
|
||||||
}
|
}
|
||||||
if(json.guild.icon){
|
if(json.guild.icon){
|
||||||
icon=`${urls.cdn}/icons/${json.guild.id}/${json.guild.icon}.png`;
|
thumbnail=`${urls.cdn}/icons/${json.guild.id}/${json.guild.icon}.png`;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
const json={type:"link",
|
||||||
|
title,
|
||||||
|
thumbnail,
|
||||||
|
description,
|
||||||
|
};
|
||||||
|
res.send(JSON.stringify(json));
|
||||||
|
}catch(e){
|
||||||
|
console.error(e);
|
||||||
|
const json={
|
||||||
|
type:"link",
|
||||||
|
title:"Jank Client",
|
||||||
|
thumbnail:"/logo.webp",
|
||||||
|
description:"A spacebar client that has DMs, replying and more",
|
||||||
|
url:url.toString()
|
||||||
|
};
|
||||||
|
res.send(JSON.stringify(json));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*
|
||||||
function htmlEnc(s) {//https://stackoverflow.com/a/11561642
|
function htmlEnc(s) {//https://stackoverflow.com/a/11561642
|
||||||
return s.replaceAll(/&/g, '&')
|
return s.replaceAll(/&/g, '&')
|
||||||
.replaceAll(/</g, '<')
|
.replaceAll(/</g, '<')
|
||||||
|
@ -93,17 +118,29 @@ async function inviteres(req,res){
|
||||||
console.error(e);
|
console.error(e);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
*/
|
||||||
|
|
||||||
|
app.use('/services/oembed', (req, res) => {
|
||||||
|
inviteres(req, res);
|
||||||
|
})
|
||||||
app.use('/', async (req, res) => {
|
app.use('/', async (req, res) => {
|
||||||
|
const scheme = req.secure ? 'https' : 'http';
|
||||||
|
const host=`${scheme}://${req.get("Host")}`;
|
||||||
|
const ref=host+req.originalUrl;
|
||||||
|
if(host&&ref){
|
||||||
|
|
||||||
|
const link=`${host}/services/oembed?url=${encodeURIComponent(ref)}`;
|
||||||
|
res.set("Link",`<${link}>; rel="alternate"; type="application/json+oembed"; title="Jank Client oEmbed format"`);
|
||||||
|
}else{
|
||||||
|
console.log(req);
|
||||||
|
}
|
||||||
|
|
||||||
if(debugging&&req.path.startsWith("/service.js")){
|
if(debugging&&req.path.startsWith("/service.js")){
|
||||||
res.send("dud");
|
res.send("dud");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(req.path.startsWith("/invite/")){
|
if(req.path.startsWith("/invite/")){
|
||||||
const condition=await inviteres(req,res);
|
res.sendFile(`./webpage/invite.html`, {root: __dirname});
|
||||||
if(!condition){
|
|
||||||
res.sendFile(`./webpage/invite.html`, {root: __dirname});
|
|
||||||
}
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(fs.existsSync(`${__dirname}/webpage${req.path}`)) {
|
if(fs.existsSync(`${__dirname}/webpage${req.path}`)) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue