flarum-msteams-webhook/flarum-notifiy-v2.js

136 lines
2.7 KiB
JavaScript

(function () {
'use strict';
var forumOrigin = window.location.origin;
function showTarget(targetValue) {
var targetUrl;
var forumLink;
var targetUrlLabel;
var postPanel;
var fallbackPanel;
if (!targetValue) {
return false;
}
try {
targetUrl = new URL(targetValue);
if (targetUrl.origin !== forumOrigin) {
console.warn(
'Rejected forum target with unexpected origin:',
targetUrl.origin
);
return false;
}
if (targetUrl.pathname.indexOf('/d/') !== 0) {
console.warn(
'Rejected forum target with unexpected path:',
targetUrl.pathname
);
return false;
}
forumLink = document.getElementById('forum-link');
targetUrlLabel = document.getElementById('target-url');
postPanel = document.getElementById('post-panel');
fallbackPanel = document.getElementById('fallback-panel');
if (
!forumLink ||
!targetUrlLabel ||
!postPanel ||
!fallbackPanel
) {
console.warn(
'Required landing-page elements were not found.'
);
return false;
}
forumLink.href = targetUrl.href;
targetUrlLabel.textContent = targetUrl.href;
postPanel.hidden = false;
fallbackPanel.hidden = true;
return true;
} catch (error) {
console.warn(
'The forum target URL is invalid.',
error
);
return false;
}
}
async function initialize() {
var parameters;
var queryTarget;
var context;
var subPageId;
parameters = new URLSearchParams(
window.location.search
);
queryTarget = parameters.get('target');
/*
* Allow direct browser tests with ?target=...
*/
if (showTarget(queryTarget)) {
return;
}
/*
* In Microsoft Teams the target is passed as subEntityId.
* TeamsJS v2 exposes that value as context.page.subPageId.
*/
if (
!window.microsoftTeams ||
!window.microsoftTeams.app
) {
console.info(
'TeamsJS is unavailable; showing the notifications fallback.'
);
return;
}
try {
await window.microsoftTeams.app.initialize();
context =
await window.microsoftTeams.app.getContext();
subPageId =
context &&
context.page &&
context.page.subPageId
? context.page.subPageId
: null;
console.info(
'Teams page.subPageId:',
subPageId || '[none]'
);
showTarget(subPageId);
} catch (error) {
console.warn(
'Could not read the Microsoft Teams context.',
error
);
}
}
initialize();
})();