You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
6.0 KiB

// venue.js
// Copyright (C) 2022 Digital Telepresence, LLC
// License: Apache-2.0
'use strict';
const mongoose = require('mongoose');
const VenueChannel = mongoose.model('VenueChannel');
const VenueChannelStatus = mongoose.model('VenueChannelStatus');
const https = require('https');
const fetch = require('node-fetch'); // jshint ignore:line
const striptags = require('striptags');
const CACHE_DURATION = 60 * 5;
const { SiteService, SiteError } = require('../../lib/site-lib');
class VenueService extends SiteService {
constructor (dtp) {
super(dtp, module.exports);
this.soapboxDomain = process.env.DTP_SOAPBOX_HOST || 'shing.tv';
}
async start ( ) {
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
this.httpsAgent = new https.Agent({
rejectUnauthorized: false,
});
}
channelMiddleware ( ) {
return async (req, res, next) => {
try {
if (!res.locals.site || !res.locals.site.shingChannelSlug) {
return next();
}
res.locals.shingChannelFeed = await this.getChannelFeed(res.locals.site.shingChannelSlug, { allowCache: true });
res.locals.shingChannelStatus = await this.getChannelStatus(res.locals.site.shingChannelSlug, { allowCache: true });
this.log.debug('channel status', { status: res.locals.shingChannelStatus });
return next();
} catch (error) {
this.log.error('failed to populate Soapbox channel feed', { error });
return next();
}
};
}
async createChannel (owner, channelDefinition) {
const channel = new VenueChannel();
channel.ownerType = owner.type;
channel.owner = owner._id;
if (!channelDefinition.slug || (channelDefinition.slug === '')) {
throw new SiteError(400, 'Must provide a channel URL slug');
}
channel.slug = striptags(channelDefinition.slug.trim());
if (!channelDefinition['credentials.streamKey'] || (channelDefinition['credentials.streamKey'] === '')) {
throw new SiteError(400, 'Must provide a stream key');
}
channel['credentials.streamKey'] = channelDefinition['credentials.streamKey'].trim();
if (!channelDefinition['credentials.widgetKey'] || (channelDefinition['credentials.widgetKey'] === '')) {
throw new SiteError(400, 'Must provide a widget key');
}
channel['credentials.widgetKey'] = channelDefinition['credentials.widgetKey'].trim();
await channel.save();
return channel.toObject();
}
async updateChannel (channel, channelDefinition) {
const updateOp = { $set: { } };
if (!channelDefinition.slug || (channelDefinition.slug === '')) {
throw new SiteError(400, 'Must provide a channel URL slug');
}
updateOp.$set.slug = striptags(channelDefinition.slug.trim());
if (!channelDefinition['credentials.streamKey'] || (channelDefinition['credentials.streamKey'] === '')) {
throw new SiteError(400, 'Must provide a stream key');
}
updateOp.$set['credentials.streamKey'] = channelDefinition['credentials.streamKey'].trim();
if (!channelDefinition['credentials.widgetKey'] || (channelDefinition['credentials.widgetKey'] === '')) {
throw new SiteError(400, 'Must provide a widget key');
}
updateOp.$set['credentials.widgetKey'] = channelDefinition['credentials.widgetKey'].trim();
await VenueChannel.updateOne({ _id: channel._id }, updateOp);
}
async getChannels (pagination) {
pagination = Object.assign({ skip: 0, cpp: 10 }, pagination);
const search = { };
const channels = await VenueChannel
.find(search)
.sort({ slug: 1 })
.skip(pagination.skip)
.limit(pagination.cpp)
.populate(this.populateVenueChannel)
.lean();
return channels;
}
async getChannelById (channelId) {
const channel = await VenueChannel
.findOne({ _id: channelId })
.populate(this.populateVenueChannel)
.lean();
return channel;
}
async getChannelFeed (channelSlug, options) {
const { cache: cacheService } = this.dtp.services;
const cacheKey = `venue:ch:${channelSlug}`;
options = Object.assign({ allowCache: true }, options);
let json;
if (options.allowCache) {
json = await cacheService.getObject(cacheKey);
if (json) { return json; }
}
const requestUrl = `https://${this.soapboxDomain}/channel/${channelSlug}/feed/json`;
this.log.info('fetching Shing channel feed', { channelSlug, requestUrl });
const response = await fetch(requestUrl, {
agent: this.httpsAgent,
});
if (!response.ok) {
throw new SiteError(500, `Failed to fetch Shing channel feed: ${response.statusText}`);
}
json = await response.json();
await cacheService.setObjectEx(cacheKey, CACHE_DURATION, json);
return json;
}
async getChannelStatus (channel, options) {
const { cache: cacheService } = this.dtp.services;
const cacheKey = `venue:ch:${channel.slug}:status`;
options = Object.assign({ allowCache: true }, options);
let json;
if (options.allowCache) {
json = await cacheService.getObject(cacheKey);
if (json) { return json; }
}
const channelStatus = await this.updateChannelStatus(channel);
await cacheService.setObjectEx(cacheKey, 30, channelStatus);
return channel;
}
async updateChannelStatus (channel) {
const requestUrl = `https://${this.soapboxDomain}/channel/${channel.slug}/status`;
this.log.info('fetching Shing channel status', { slug: channel.slug, requestUrl });
const response = await fetch(requestUrl, { agent: this.httpsAgent });
if (!response.ok) {
throw new SiteError(500, `Failed to fetch channel status: ${response.statusText}`);
}
const json = await response.json();
if (!json.success) {
throw new Error(`failed to fetch channel status: ${json.message}`);
}
const status = new VenueChannelStatus(json.channel);
status.created = new Date();
await status.save();
return status.toObject();
}
}
module.exports = {
slug: 'venue',
name: 'venue',
create: (dtp) => { return new VenueService(dtp); },
};