Merge branch '3.0' of https://github.com/calzoneman/sync into 3.0
[KisSync.git] / src / channel / library.js
blob31b34316f2dbc782690a9e3710e2f351fab25310
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');
9 const TYPE_UNCACHE = {
10     id: "string"
13 const TYPE_SEARCH_MEDIA = {
14     source: "string,optional",
15     query: "string"
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);
32     }
35 LibraryModule.prototype.cacheMediaList = function (list) {
36     if (this.channel.is(Flags.C_REGISTERED)) {
37         LOGGER.info(
38             'Saving %d items to library for %s',
39             list.length,
40             this.channel.name
41         );
42         db.channels.addListToLibrary(this.channel.name, list).catch(error => {
43             LOGGER.error('Failed to add list to library: %s', error.stack);
44         });
45     }
48 LibraryModule.prototype.handleUncache = function (user, data) {
49     if (!this.channel.is(Flags.C_REGISTERED)) {
50         return;
51     }
53     if (!this.channel.modules.permissions.canUncache(user)) {
54         return;
55     }
57     const chan = this.channel;
58     chan.refCounter.ref("LibraryModule::handleUncache");
59     db.channels.deleteFromLibrary(chan.name, data.id, function (err, _res) {
60         if (chan.dead) {
61             return;
62         } else if (err) {
63             chan.refCounter.unref("LibraryModule::handleUncache");
64             return;
65         }
67         chan.logger.log("[library] " + user.getName() + " deleted " + data.id +
68                         "from the library");
69         chan.refCounter.unref("LibraryModule::handleUncache");
70     });
73 const librarySearchQueryCount = new Counter({
74     name: 'cytube_library_search_query_count',
75     help: 'Counter for number of channel library searches',
76     labelNames: ['source']
77 });
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']
82 });
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) {
88             if (!e) {
89                 librarySearchResultSize.labels('yt')
90                         .observe(vids.length, new Date());
91                 user.socket.emit("searchResults", {
92                     source: "yt",
93                     results: vids
94                 });
95             }
96         });
97     };
99     if (data.source === "yt" || !this.channel.is(Flags.C_REGISTERED) ||
100         !this.channel.modules.permissions.canSeePlaylist(user)) {
101         searchYT();
102     } else {
103         librarySearchQueryCount.labels('library').inc(1, new Date());
105         db.channels.searchLibrary(this.channel.name, query, function (err, res) {
106             if (err) {
107                 res = [];
108             }
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);
117             });
119             res.forEach(function (r) {
120                 r.duration = util.formatTime(r.seconds);
121             });
123             user.socket.emit("searchResults", {
124                 source: "library",
125                 results: res
126             });
127         });
128     }
131 module.exports = LibraryModule;