73 lines
2.0 KiB
TypeScript
73 lines
2.0 KiB
TypeScript
import { httpGET, sanitizeEscapedText, toHHMMSS } from '@squeebot/core/lib/common';
|
|
import {
|
|
Plugin,
|
|
EventListener,
|
|
DependencyLoad
|
|
} from '@squeebot/core/lib/plugin';
|
|
|
|
import { IMessage } from '@squeebot/core/lib/types';
|
|
|
|
async function peertubeResponse(url: URL, msg: IMessage): Promise<boolean> {
|
|
const url2go = `${url.origin}/api/v1/videos/${url.pathname.split('/')[3]}`;
|
|
let data;
|
|
|
|
try {
|
|
data = await httpGET(url2go);
|
|
if (!data) {
|
|
throw new Error('No API response, probably not peertube after all.');
|
|
}
|
|
data = JSON.parse(data);
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
|
|
if (!data.id || !data.uuid || data.errors) {
|
|
return false;
|
|
}
|
|
|
|
const keys = [];
|
|
if (data.isLive) {
|
|
keys.push(['field', '[LIVE]', {color: 'red'}]);
|
|
}
|
|
|
|
keys.push(['field', 'PeerTube', { color: 'gold', type: 'title' }]);
|
|
keys.push(['field', data.name, {type: 'description'}]);
|
|
|
|
keys.push(['field', data.views.toString(), { type: 'metric', label: 'Views' }]);
|
|
|
|
if (!data.isLive) {
|
|
keys.push(['field', toHHMMSS(data.duration.toString()), { label: 'Duration' }]);
|
|
}
|
|
|
|
if (data.likes != null) {
|
|
const likeCount = data.likes.toString();
|
|
const dislikeCount = data.dislikes.toString();
|
|
|
|
keys.push(['field', likeCount, { color: 'limegreen', label: ['▲', 'Likes'], type: 'metric' }]);
|
|
keys.push(['field', dislikeCount, { color: 'red', label: ['▼', 'Dislikes'], type: 'metric' }]);
|
|
}
|
|
|
|
keys.push(['field', data.channel.displayName || data.channel.name, { label: ['By'] }]);
|
|
|
|
msg.resolve(keys);
|
|
return true;
|
|
}
|
|
|
|
class FediResponsePlugin extends Plugin {
|
|
@EventListener('pluginUnload')
|
|
public unloadEventHandler(plugin: string | Plugin): void {
|
|
if (plugin === this.name || plugin === this) {
|
|
this.emit('pluginUnloaded', this);
|
|
}
|
|
}
|
|
|
|
@DependencyLoad('urlreply')
|
|
addUrlReply(urlreply: any): void {
|
|
urlreply.registerHandler(this.name, '/videos/watch/', async (url: URL, msg: IMessage) => {
|
|
return peertubeResponse(url, msg);
|
|
});
|
|
}
|
|
}
|
|
|
|
module.exports = FediResponsePlugin;
|