UIAngular/Server/src/routes/share.js

125 lines
4.3 KiB
JavaScript

var express = require('express');
var router = express.Router();
var dbSermons = require('../database/sermons');
router.get("/:shareCode",function(req,res){
var sharecode = req.params.shareCode;
var type = sharecode.substring(0,1);
var param = sharecode.substring(1);
console.log("type: " + type + "; param: " + param);
switch(type){
case "s":
return getSermon(req, res, param);
case "e":
return getEvent(req,res,param);
}
});
function getSermon(req, res, id){
var userAgent = req.headers['user-agent'];
if (userAgent.startsWith('facebookexternalhit/1.1') ||
userAgent === 'Facebot' ||
userAgent.startsWith('Twitterbot')) {
getSermonMeta(res, id);
} else {
res.redirect("https://ofbbutte.com/sermons/" + id);
}
}
function getSermonMeta(res, id){
dbSermons.getSermon(id,function(error,sermon){
if (error){
res.status(404).json({"status":404,"message":"Error processing request"});
return;
}
if (sermon == null){
res.status(404).json({"status":404,"message":"Sermon does not exist"});
return;
}
var result = `<html>
<head>
<title>Old Fashion Baptist Church</title>
<!-- You can use Open Graph tags to customize link previews.
Learn more: https://developers.facebook.com/docs/sharing/webmasters -->
<meta property="og:url" content="https://ofbbutte.com/api/share/s`+ sermon.id +`"/>
<meta property="og:type" content="website" />
<meta property="og:title" content="` + sermon.title + `" />
<meta property="og:description" content="` + sermon.description + `" />
<meta property="og:image" content="https://ofbbutte.com/static/ofbmain/images/facebookplay.png" />
</head>
<body>
</body>
</html>`;
res.set('Content-Type', 'text/html');
res.send(new Buffer(result));
});
}
function getEvent(req, res, id){
var userAgent = req.headers['user-agent'];
if (userAgent.startsWith('facebookexternalhit/1.1') ||
userAgent === 'Facebot' ||
userAgent.startsWith('Twitterbot')) {
getEventMeta(res, id);
} else {
res.redirect("https://ofbbutte.com/events/" + id);
}
}
function getEventMeta(res, id){
dbSermons.getEvent(id,function(error,event){
if (error){
res.status(404).json({"status":404,"message":"Error processing request"});
return;
}
if (sermon == null){
res.status(404).json({"status":404,"message":"Event does not exist"});
return;
}
var monthNum = event.startDate.getMonth();
var monthName = MONTHS_FULL[monthNum];
var day = event.startDate.getDate();
var result = `<html>
<head>
<title>Old Fashion Baptist Church</title>
<!-- You can use Open Graph tags to customize link previews.
Learn more: https://developers.facebook.com/docs/sharing/webmasters -->
<meta property="og:url" content="https/share/e`+ event.id +`"/>
<meta property="og:type" content="website" />
<meta property="og:title" content="` + event.title + `" />
<meta property="og:description" content="` + event.description + `" />
<meta property="og:image" content="/cim/` + monthName + `/` + day + `"/>
</head>
<body>
</body>
</html>`;
res.set('Content-Type', 'text/html');
res.send(new Buffer(result));
});
}
const MONTHS_FULL = {
0: "JANUARY",
1: "FEBRUARY",
2: "MARCH",
3: "APRIL",
4: "MAY",
5: "JUNE",
6: "JULY",
7: "AUGUST",
8: "SEPTEMBER",
9: "OCTOBER",
10: "NOVEMBER",
11: "DECEMBER"
};
module.exports = router;