1 var ChannelModule = require("./module");
2 var Flags = require("../flags");
3 var util = require("../utilities");
4 var InfoGetter = require("../get-info");
5 var db = require("../database");
6 import { Counter, Summary } from 'prom-client';
7 const LOGGER = require('@calzoneman/jsli')('channel/library');
13 const TYPE_SEARCH_MEDIA = {
14 source: "string,optional",
18 function LibraryModule(_channel) {
19 ChannelModule.apply(this, arguments);
22 LibraryModule.prototype = Object.create(ChannelModule.prototype);
24 LibraryModule.prototype.onUserPostJoin = function (user) {
25 user.socket.typecheckedOn("uncache", TYPE_UNCACHE, this.handleUncache.bind(this, user));
26 user.socket.typecheckedOn("searchMedia", TYPE_SEARCH_MEDIA, this.handleSearchMedia.bind(this, user));
29 LibraryModule.prototype.cacheMedia = function (media) {
30 if (this.channel.is(Flags.C_REGISTERED) && !util.isLive(media.type)) {
31 db.channels.addToLibrary(this.channel.name, media);
35 LibraryModule.prototype.cacheMediaList = function (list) {
36 if (this.channel.is(Flags.C_REGISTERED)) {
38 'Saving %d items to library for %s',
42 db.channels.addListToLibrary(this.channel.name, list).catch(error => {
43 LOGGER.error('Failed to add list to library: %s', error.stack);
48 LibraryModule.prototype.handleUncache = function (user, data) {
49 if (!this.channel.is(Flags.C_REGISTERED)) {
53 if (!this.channel.modules.permissions.canUncache(user)) {
57 const chan = this.channel;
58 chan.refCounter.ref("LibraryModule::handleUncache");
59 db.channels.deleteFromLibrary(chan.name, data.id, function (err, _res) {
63 chan.refCounter.unref("LibraryModule::handleUncache");
67 chan.logger.log("[library] " + user.getName() + " deleted " + data.id +
69 chan.refCounter.unref("LibraryModule::handleUncache");
73 const librarySearchQueryCount = new Counter({
74 name: 'cytube_library_search_query_count',
75 help: 'Counter for number of channel library searches',
76 labelNames: ['source']
78 const librarySearchResultSize = new Summary({
79 name: 'cytube_library_search_results_size',
80 help: 'Summary for number of channel library results returned',
81 labelNames: ['source']
83 LibraryModule.prototype.handleSearchMedia = function (user, data) {
84 var query = data.query.substring(0, 100);
85 var searchYT = function () {
86 librarySearchQueryCount.labels('yt').inc(1, new Date());
87 InfoGetter.Getters.ytSearch(query, function (e, vids) {
89 librarySearchResultSize.labels('yt')
90 .observe(vids.length, new Date());
91 user.socket.emit("searchResults", {
99 if (data.source === "yt" || !this.channel.is(Flags.C_REGISTERED) ||
100 !this.channel.modules.permissions.canSeePlaylist(user)) {
103 librarySearchQueryCount.labels('library').inc(1, new Date());
105 db.channels.searchLibrary(this.channel.name, query, function (err, res) {
110 librarySearchResultSize.labels('library')
111 .observe(res.length, new Date());
113 res.sort(function (a, b) {
114 var x = a.title.toLowerCase();
115 var y = b.title.toLowerCase();
116 return (x === y) ? 0 : (x < y ? -1 : 1);
119 res.forEach(function (r) {
120 r.duration = util.formatTime(r.seconds);
123 user.socket.emit("searchResults", {
131 module.exports = LibraryModule;