UIAngular/Server/src/routes/api/share.js

232 lines
8.9 KiB
JavaScript

var express = require('express');
var router = express.Router();
var dbSermons = require('../../database/sermons');
var dbEvents = require('../../database/events');
const calendarImageUrl = '/api2/cim/';
const eventUrl = '/api2/share/e';
const sermonImageUrl = '/assets/images/tiny/facebookplay.png';
const sermonUrl = '/api2/share/s';
const otherUrl = '/api2/share/o';
router.get("/:shareCode",function(req,res){
var fullUrl = req.protocol + '://' + req.hostname + req.originalUrl;
console.log("Full");
console.log(fullUrl);
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);
case "o":
return getOther(req, res, param)
}
});
function getOther(req, res, param) {
param = decodeURIComponent(param);
var params = JSON.parse(param);
var userAgent = req.headers['user-agent'];
var userAgentOverride = req.headers['user-agent-override'];
var referrer = req.headers['referer'] || "";
userAgent = userAgentOverride || userAgent;
if (userAgent.startsWith('facebookexternalhit/1.1') ||
userAgent === 'Facebot' ||
userAgent.startsWith('Twitterbot') ||
referrer.startsWith('http://opengraphcheck.com')) {
getOtherMeta(req, res, params.title, params.description, params.imageUrl, params.imageWidth, params.imageHeight, params.url);
} else {
var url = params.url || (req.protocol + "://" + req.hostname);
res.redirect(url);
}
}
function getSermon(req, res, id){
console.log(req.headers);
var userAgent = req.headers['user-agent'];
var userAgentOverride = req.headers['user-agent-override'];
var referrer = req.headers['referer'] || "";
userAgent = userAgentOverride || userAgent;
if (userAgent.startsWith('facebookexternalhit/1.1') ||
userAgent === 'Facebot' ||
userAgent.startsWith('Twitterbot') ||
referrer.startsWith('http://opengraphcheck.com')) {
getSermonMeta(req, res, id);
} else {
var url = req.protocol + "://" + req.hostname + "/sermons/" + id;
res.redirect(url);
}
}
function getOtherMeta(req, res, title, description, imageUrl, imageWidth, imageHeight, url) {
imageWidth = imageWidth || 200;
imageHeight = imageHeight || 200;
const data = {
title: title,
description: description,
imageUrl: imageUrl,
imageWidth: imageWidth,
imageHeight: imageHeight,
url: url
};
url = req.protocol + '//' + req.hostname + otherUrl + encodeURIComponent(JSON.stringify(data));
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="`+ url +`"/>
<meta property="og:type" content="website" />
<meta property="og:title" content="` + title + `" />
<meta property="og:description" content="` + description + `" />
<meta property="og:image" content="` + imageUrl + `" />
<meta property="og:image:width" content="`+ imageWidth +`" />
<meta property="og:image:height" content="`+ imageHeight +`" />
</head>
<body>
</body>
</html>`;
res.set('Content-Type', 'text/html');
res.send(new Buffer(result));
}
function getSermonMeta(req, 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 port = req.socket.localPort;
var imageUrl = req.protocol + "://" + req.hostname + sermonImageUrl;
var srmnUrl = req.protocol + "://" + req.hostname + sermonUrl + sermon.id;
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="`+ srmnUrl +`"/>
<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="` + imageUrl + `" />
<meta property="og:image:width" content="200" />
<meta property="og:image:height" content="200" />
</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'];
var userAgentOverride = req.headers['user-agent-override'];
var referrer = req.headers['referer'] || "";
userAgent = userAgentOverride || userAgent;
if (userAgent.startsWith('facebookexternalhit/1.1') ||
userAgent === 'Facebot' ||
userAgent.startsWith('Twitterbot' ||
referrer.startsWith('http://opengraphcheck.com'))) {
getEventMeta(req, res, id);
} else {
var url = req.protocol + "://" + req.hostname + "/events/" + id;
res.redirect(url);
}
}
function getEventMeta(req, res, id){
dbEvents.getEvent(id,function(error,event){
if (error){
res.status(404).json({"status":404,"message":"Error processing request"});
return;
}
if (event == null){
res.status(404).json({"status":404,"message":"Event does not exist"});
return;
}
var startDate = event.startDate;
//Calculate time with local timezone offset but keep it in UTC
startDate.setTime(startDate.getTime() - event.timezoneOffset * 60 * 1000);
var monthNum = startDate.getMonth();
var monthName = MONTHS_FULL[monthNum];
var day = startDate.getDate();
var port = req.socket.localPort;
var imageUrl = req.protocol + "://" + req.hostname + calendarImageUrl + monthName + "/" + day;
var evntUrl = req.protocol + "://" + req.hostname + eventUrl + event.id;
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="`+ evntUrl +`"/>
<meta property="og:type" content="website" />
<meta property="og:title" content="` + event.title + `" />
<meta property="og:description" content="` + dateToString(startDate) + ` | ` + event.description + `" />
<meta property="og:image" content="` + imageUrl + `"/>
<meta property="og:image:width" content="200" />
<meta property="og:image:height" content="200" />
</head>
<body>
</body>
</html>`;
res.set('Content-Type', 'text/html');
res.send(new Buffer(result));
});
}
function dateToString(dte){
var val = dte.toLocaleString([],{ day: 'numeric', month: 'numeric', year: 'numeric', hour: 'numeric', minute:'2-digit'});
var month = dte.getUTCMonth() + 1;
var day = dte.getUTCDate();
var year = dte.getUTCFullYear();
var hour = dte.getUTCHours();
var minute = dte.getUTCMinutes();
minute < 10 ? "0" + minute : minute;
var pmOrAm = hour > 11 ? " PM" : " AM";
hour = hour > 11 ? hour - 12 : hour;
val = month + "/" + day + "/" + year + " - " + hour + (minute > 0 ? ":" + minute : "") + pmOrAm;
console.log(val);
return val;
}
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;