mirror of
				https://github.com/KevinMidboe/zoff.git
				synced 2025-10-29 18:00:23 +00:00 
			
		
		
		
	More intermediary work
This commit is contained in:
		| @@ -1,9 +1,12 @@ | ||||
| var path = require("path"); | ||||
| var db = require(pathThumbnails + "/handlers/db.js"); | ||||
| var aggregate = require(pathThumbnails + "/handlers/dbFunctions/aggregate.js"); | ||||
| var update = require(pathThumbnails + "/handlers/dbFunctions/update.js"); | ||||
| var remove = require(pathThumbnails + "/handlers/dbFunctions/remove.js"); | ||||
|  | ||||
| var Helpers = require(pathThumbnails + "/handlers/helpers.js"); | ||||
| var frontpage = require(pathThumbnails + | ||||
|   "/handlers/dbFunctions/frontpageUpdates.js"); | ||||
|   "/handlers/dbFunctions/advancedFunctions/frontpageUpdates.js"); | ||||
| var findAggregate = [ | ||||
|   { | ||||
|     $match: { | ||||
| @@ -49,12 +52,14 @@ var verifyAggregate = [ | ||||
|   } | ||||
| ]; | ||||
|  | ||||
| var sIO = require(pathThumbnails + "/apps/client.js").socketIO; | ||||
| async function changeSong(coll, error, id, conf, socket) { | ||||
|   console.log("hello"); | ||||
|   return pre(coll, error, id, conf, socket); | ||||
| } | ||||
|  | ||||
| async function pre(coll, error, id, conf, socket) { | ||||
|   return new Promise((resolve, reject) => { | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     var startTime = conf[0].startTime; | ||||
|     if (conf === null || conf.length == 0) { | ||||
|       return; | ||||
| @@ -72,13 +77,12 @@ async function pre(coll, error, id, conf, socket) { | ||||
|           next_song = now_playing_doc[1].id; | ||||
|         } | ||||
|         await post(coll, next_song, conf, socket, error); | ||||
|         /*if (!callback) { | ||||
|           io.to(coll).emit("channel", { | ||||
|             type: "deleted", | ||||
|             value: now_playing_doc[0].id, | ||||
|             removed: true | ||||
|           }); | ||||
|         }*/ | ||||
|  | ||||
|         io.to(coll).emit("channel", { | ||||
|           type: "deleted", | ||||
|           value: now_playing_doc[0].id, | ||||
|           removed: true | ||||
|         }); | ||||
|         if (docs.deletedCount == 1) { | ||||
|           frontpage.incrementList("frontpage_lists", -1); | ||||
|         } | ||||
| @@ -95,9 +99,10 @@ async function pre(coll, error, id, conf, socket) { | ||||
|         resolve(); | ||||
|         return; | ||||
|       } else { | ||||
|         console.log("here 2"); | ||||
|         if ( | ||||
|           (conf[0].skipped_time != undefined && | ||||
|             conf[0].skipped_time != Functions.get_time()) || | ||||
|             conf[0].skipped_time != Helpers.get_time()) || | ||||
|           conf[0].skipped_time == undefined | ||||
|         ) { | ||||
|           var docs = await update( | ||||
| @@ -112,6 +117,7 @@ async function pre(coll, error, id, conf, socket) { | ||||
|             }, | ||||
|             { multi: true } | ||||
|           ); | ||||
|           console.log("update", docs); | ||||
|           var next_song; | ||||
|           if (now_playing_doc.length == 2) next_song = now_playing_doc[1].id; | ||||
|           await post(coll, next_song, conf, socket, error); | ||||
| @@ -140,7 +146,7 @@ async function pre(coll, error, id, conf, socket) { | ||||
| } | ||||
|  | ||||
| async function post(coll, next_song, conf, socket, removed) { | ||||
|   return new Promise((resolve, reject) => { | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     var docs = await aggregate(coll, verifyAggregate); | ||||
|     if (docs === null || docs.length == 0) { | ||||
|       reject(); | ||||
| @@ -163,7 +169,7 @@ async function post(coll, next_song, conf, socket, removed) { | ||||
|           now_playing: true, | ||||
|           votes: 0, | ||||
|           guids: [], | ||||
|           added: Functions.get_time() | ||||
|           added: Helpers.get_time() | ||||
|         } | ||||
|       }, | ||||
|       {} | ||||
| @@ -175,12 +181,18 @@ async function post(coll, next_song, conf, socket, removed) { | ||||
|       resolve(); | ||||
|       return; | ||||
|     } | ||||
|     io.to(coll).emit("channel", { | ||||
|       type: "song_change", | ||||
|       time: Helpers.get_time(), | ||||
|       remove: conf[0].removeplay || removed, | ||||
|       id: id | ||||
|     }); | ||||
|     returnDocs = await update( | ||||
|       coll + "_settings", | ||||
|       { id: "config" }, | ||||
|       { | ||||
|         $set: { | ||||
|           startTime: Functions.get_time(), | ||||
|           startTime: Helpers.get_time(), | ||||
|           skips: [] | ||||
|         } | ||||
|       }, | ||||
| @@ -190,6 +202,6 @@ async function post(coll, next_song, conf, socket, removed) { | ||||
|   }); | ||||
| } | ||||
|  | ||||
| module.exports.changeSong = changeSong; | ||||
| module.exports = changeSong; | ||||
| module.exports.pre = pre; | ||||
| module.exports.post = post; | ||||
|   | ||||
							
								
								
									
										402
									
								
								server/handlers/dbFunctions/advancedFunctions/chat.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										402
									
								
								server/handlers/dbFunctions/advancedFunctions/chat.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,402 @@ | ||||
| var path = require("path"); | ||||
| var mongojs = require("mongojs"); | ||||
| var db = require(pathThumbnails + "/handlers/db.js"); | ||||
| var find = require(pathThumbnails + "/handlers/dbFunctions/find.js"); | ||||
| var update = require(pathThumbnails + "/handlers/dbFunctions/update.js"); | ||||
| var findAndModify = require(pathThumbnails + | ||||
|   "/handlers/dbFunctions/findAndModify.js"); | ||||
|  | ||||
| var Helpers = require(pathThumbnails + "/handlers/helpers.js"); | ||||
| var SessionHandler = require(pathThumbnails + | ||||
|   "/handlers/dbFunctions/advancedFunctions/sessionHandler.js"); | ||||
| async function checkIfChatEnabled(channel, socket, callback) { | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     if (channel == "" || channel == undefined) resolve(); | ||||
|     else { | ||||
|       var docs = await find(channel + "_settings"); | ||||
|       if ( | ||||
|         docs.length > 0 && | ||||
|         (docs[0].hasOwnProperty("toggleChat") && !docs[0].toggleChat) | ||||
|       ) { | ||||
|         socket.emit("chat", { | ||||
|           from: "System", | ||||
|           msg: ": Chat for this channel has been disabled.", | ||||
|           icon: "https://zoff.me/assets/images/favicon-32x32.png" | ||||
|         }); | ||||
|         resolve(false); | ||||
|       } else { | ||||
|         resolve(true); | ||||
|       } | ||||
|     } | ||||
|   }); | ||||
| } | ||||
|  | ||||
| async function checkIfUserIsBanned(channel, socket, guid) { | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     var connection_id = Helpers.hash_pass( | ||||
|       socket.handshake.headers["user-agent"] + | ||||
|         socket.handshake.address + | ||||
|         socket.handshake.headers["accept-language"] | ||||
|     ); | ||||
|     var docs = await find(channel + "_banned_chat", { | ||||
|       $or: [{ connection_id: connection_id }, { connection_id: guid }] | ||||
|     }); | ||||
|     if (docs.length == 0) resolve(); | ||||
|     else { | ||||
|       var d = await findAndModify("user_names", { | ||||
|         query: { guid, guid }, | ||||
|         update: { $addToSet: { channels: channel } } | ||||
|       }); | ||||
|       socket.emit("chat", { | ||||
|         from: "System", | ||||
|         msg: | ||||
|           ": You can't chat in this channel, you are banned. The reason is: " + | ||||
|           docs[0].reason, | ||||
|         icon: "https://zoff.me/assets/images/favicon-32x32.png" | ||||
|       }); | ||||
|       reject(); | ||||
|     } | ||||
|   }); | ||||
| } | ||||
|  | ||||
| async function namechange(data, guid, socket, tried) { | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     var enabled = await checkIfChatEnabled(data.channel, socket); | ||||
|     if (!enabled) { | ||||
|       resolve(false); | ||||
|       return; | ||||
|     } | ||||
|     try { | ||||
|       await checkIfUserIsBanned(data.channel, socket, guid); | ||||
|     } catch (e) { | ||||
|       return; | ||||
|     } | ||||
|     var pw = ""; | ||||
|     var new_password; | ||||
|     var first = false; | ||||
|     var sessionObject = await SessionHandler.getSessionChatPass( | ||||
|       Helpers.getSession(socket) | ||||
|     ); | ||||
|     var name = sessionObject.name; | ||||
|     var pass = sessionObject.pass; | ||||
|     var fetched = false; | ||||
|     if (data.hasOwnProperty("first") && data.first) { | ||||
|       pw = pass; | ||||
|       name = name; | ||||
|       data.name = name; | ||||
|       data.password = pass; | ||||
|       new_password = false; | ||||
|       if (name == "" || pass == "") { | ||||
|         resolve(true); | ||||
|         return; | ||||
|       } | ||||
|       fetched = true; | ||||
|       password = pw; | ||||
|     } else { | ||||
|       var name = data.name; | ||||
|       if (data.hasOwnProperty("first")) { | ||||
|         first = data.first; | ||||
|       } | ||||
|       if (data.hasOwnProperty("password")) { | ||||
|         pw = data.password; | ||||
|         new_password = false; | ||||
|       } else if ( | ||||
|         data.hasOwnProperty("new_password") && | ||||
|         data.hasOwnProperty("old_password") | ||||
|       ) { | ||||
|         pw = data.old_password; | ||||
|         new_password = Helpers.decrypt_string(data.new_password); | ||||
|       } | ||||
|       password = Helpers.decrypt_string(pw); | ||||
|       password = Helpers.hash_pass(password); | ||||
|       doubled = true; | ||||
|     } | ||||
|  | ||||
|     if (name == "") { | ||||
|       resolve(true); | ||||
|       return; | ||||
|     } | ||||
|  | ||||
|     var docs = await find("registered_users", { _id: name.toLowerCase() }); | ||||
|     var accepted_password = false; | ||||
|     var icon = false; | ||||
|     if (docs.length == 0) { | ||||
|       if (new_password) { | ||||
|         resolve(true); | ||||
|         return; | ||||
|       } | ||||
|       accepted_password = true; | ||||
|       await SessionHandler.setSessionChatPass( | ||||
|         Functions.getSession(socket), | ||||
|         name.toLowerCase(), | ||||
|         data.password | ||||
|       ); | ||||
|       update( | ||||
|         "registered_users", | ||||
|         { _id: name.toLowerCase() }, | ||||
|         { $set: { password: password } }, | ||||
|         { upsert: true } | ||||
|       ); | ||||
|     } else if (docs[0].password == password) { | ||||
|       if (docs[0].icon) { | ||||
|         icon = docs[0].icon; | ||||
|       } | ||||
|       accepted_password = true; | ||||
|       if (new_password) { | ||||
|         await SessionHandler.setSessionChatPass( | ||||
|           Helpers.getSession(socket), | ||||
|           name.toLowerCase(), | ||||
|           data.new_password | ||||
|         ); | ||||
|         update( | ||||
|           "registered_users", | ||||
|           { _id: name.toLowerCase(), password: password }, | ||||
|           { | ||||
|             $set: { password: Helpers.hash_pass(new_password) } | ||||
|           } | ||||
|         ); | ||||
|       } else { | ||||
|         await SessionHandler.setSessionChatPass( | ||||
|           Helpers.getSession(socket), | ||||
|           name.toLowerCase(), | ||||
|           fetched | ||||
|             ? data.password | ||||
|             : Helpers.hash_pass(Helpers.decrypt_string(data.password)) | ||||
|         ); | ||||
|       } | ||||
|     } | ||||
|     if (accepted_password) { | ||||
|       var names = await find("user_names", { guid: guid }); | ||||
|       if ( | ||||
|         names.length > 0 || | ||||
|         (docs.length != 0 && docs[0].password == password) | ||||
|       ) { | ||||
|         var no_name = false; | ||||
|         if (names.length == 0) no_name = true; | ||||
|         if (!no_name) { | ||||
|           var old_name = names[0].name; | ||||
|           update( | ||||
|             "user_names", | ||||
|             { _id: "all_names" }, | ||||
|             { $pull: { names: old_name } } | ||||
|           ); | ||||
|         } | ||||
|         var connection_id = Helpers.hash_pass( | ||||
|           socket.handshake.headers["user-agent"] + | ||||
|             socket.handshake.address + | ||||
|             socket.handshake.headers["accept-language"] | ||||
|         ); | ||||
|         var updateElement = { | ||||
|           $set: { | ||||
|             name: name, | ||||
|             icon: icon, | ||||
|             connection_id: connection_id | ||||
|           } | ||||
|         }; | ||||
|         if (data.hasOwnProperty("channel") && data.channel != "") { | ||||
|           updateElement["$addToSet"] = { channels: data.channel }; | ||||
|         } | ||||
|         await update("user_names", { guid: guid }, updateElement, { | ||||
|           upsert: true | ||||
|         }); | ||||
|         await update( | ||||
|           "user_names", | ||||
|           { _id: "all_names" }, | ||||
|           { $addToSet: { names: name } } | ||||
|         ); | ||||
|         //socket.emit('name', {type: "name", accepted: true}); | ||||
|         if (old_name != name && !first && !no_name) { | ||||
|           if ( | ||||
|             data.hasOwnProperty("channel") && | ||||
|             typeof data.channel == "string" | ||||
|           ) { | ||||
|             io.to(data.channel).emit("chat", { | ||||
|               from: old_name, | ||||
|               msg: " changed name to " + name | ||||
|             }); | ||||
|             io.sockets.emit("chat.all", { | ||||
|               from: old_name, | ||||
|               msg: " changed name to " + name, | ||||
|               channel: data.channel | ||||
|             }); | ||||
|           } | ||||
|         } | ||||
|  | ||||
|         resolve(true); | ||||
|       } else { | ||||
|         if (tried < 3 || tried == undefined) { | ||||
|           if (tried == undefined) { | ||||
|             tried = 1; | ||||
|           } | ||||
|           namechange(data, guid, socket, tried + 1); | ||||
|         } | ||||
|       } | ||||
|     } else { | ||||
|       await SessionHandler.removeSessionChatPass(Functions.Helpers(socket)); | ||||
|       socket.emit("name", { type: "name", accepted: false }); | ||||
|     } | ||||
|   }); | ||||
| } | ||||
|  | ||||
| async function get_name(guid, announce_payload, first) { | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     if ( | ||||
|       !announce_payload.announce && | ||||
|       announce_payload.hasOwnProperty("socket") | ||||
|     ) { | ||||
|       var sessionObject = await SessionHandler.getSessionChatPass( | ||||
|         Helpers.getSession(announce_payload.socket) | ||||
|       ); | ||||
|       var name = sessionObject.name; | ||||
|       var pass = sessionObject.pass; | ||||
|       if (name == "" || pass == "") { | ||||
|         get_name_generate( | ||||
|           guid, | ||||
|           announce_payload, | ||||
|           first, | ||||
|           announce_payload.channel | ||||
|         ); | ||||
|         return; | ||||
|       } | ||||
|       var docs = find("registered_users", { _id: name.toLowerCase() }); | ||||
|       if (docs[0].password == Helpers.hash_pass(Helpers.decrypt_string(pass))) { | ||||
|         var icon = false; | ||||
|         if (docs[0].icon) { | ||||
|           icon = docs[0].icon; | ||||
|         } | ||||
|         SessionHandler.setSessionChatPass( | ||||
|           Functions.getSession(announce_payload.socket), | ||||
|           name.toLowerCase(), | ||||
|           pass | ||||
|         ); | ||||
|         var connection_id = Helpers.hash_pass( | ||||
|           announce_payload.socket.handshake.headers["user-agent"] + | ||||
|             announce_payload.socket.handshake.address + | ||||
|             announce_payload.socket.handshake.headers["accept-language"] | ||||
|         ); | ||||
|         var updateElement = { | ||||
|           $set: { name: name, icon: icon, connection_id: connection_id } | ||||
|         }; | ||||
|         if ( | ||||
|           announce_payload.hasOwnProperty("channel") && | ||||
|           announce_payload.channel != "" | ||||
|         ) | ||||
|           updateElement["$addToSet"] = { | ||||
|             channel: announce_payload.channel | ||||
|           }; | ||||
|         await update("user_names", { guid: guid }, updateElement, { | ||||
|           upsert: true | ||||
|         }); | ||||
|         await update( | ||||
|           "user_names", | ||||
|           { _id: "all_names" }, | ||||
|           { $addToSet: { names: name } } | ||||
|         ); | ||||
|         name = name; | ||||
|         resolve(); | ||||
|       } | ||||
|     } else { | ||||
|       get_name_generate( | ||||
|         guid, | ||||
|         announce_payload, | ||||
|         first, | ||||
|         announce_payload.channel | ||||
|       ); | ||||
|       resolve(); | ||||
|     } | ||||
|   }); | ||||
| } | ||||
|  | ||||
| async function get_name_generate(guid, announce_payload, first, channel) { | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     var docs = await find("user_names", { guid: guid }); | ||||
|     if (docs.length == 0) { | ||||
|       generate_name(guid, announce_payload, undefined); | ||||
|     } else { | ||||
|       name = docs[0].name; | ||||
|     } | ||||
|   }); | ||||
| } | ||||
|  | ||||
| async function generate_name(guid, announce_payload, second, round, channel) { | ||||
|   if (round == undefined) round = 0; | ||||
|   var tmp_name = Helpers.rndName(second ? second : guid, Math.floor(8 + round)); | ||||
|   var docs = await find("registered_users", { _id: tmp_name }); | ||||
|   if (docs.length == 0) { | ||||
|     var updated = await update( | ||||
|       "user_names", | ||||
|       { _id: "all_names" }, | ||||
|       { $addToSet: { names: tmp_name } }, | ||||
|       { upsert: true } | ||||
|     ); | ||||
|     if ( | ||||
|       updated.nModified == 1 || | ||||
|       (updated.hasOwnProperty("upserted") && | ||||
|         updated.hasOwnProperty("n") && | ||||
|         updated.n == 1) | ||||
|     ) { | ||||
|       var connection_id = Helpers.hash_pass( | ||||
|         announce_payload.socket.handshake.headers["user-agent"] + | ||||
|           announce_payload.socket.handshake.address + | ||||
|           announce_payload.socket.handshake.headers["accept-language"] | ||||
|       ); | ||||
|       var updateElement = { | ||||
|         $set: { | ||||
|           name: tmp_name, | ||||
|           icon: false, | ||||
|           connection_id: connection_id | ||||
|         } | ||||
|       }; | ||||
|       if (channel != undefined && channel != "") { | ||||
|         updateElement["$addToSet"] = { channels: channel }; | ||||
|       } | ||||
|       if ( | ||||
|         announce_payload.hasOwnProperty("channel") && | ||||
|         announce_payload.channel != "" | ||||
|       ) { | ||||
|         updateElement["$addToSet"] = { | ||||
|           channels: announce_payload.channel | ||||
|         }; | ||||
|       } | ||||
|       var updateElement = await update( | ||||
|         "user_names", | ||||
|         { guid: guid }, | ||||
|         updateElement, | ||||
|         { upsert: true } | ||||
|       ); | ||||
|       name = tmp_name; | ||||
|       if (announce_payload.announce) { | ||||
|         io.to(announce_payload.channel).emit("chat", { | ||||
|           from: announce_payload.old_name, | ||||
|           msg: " changed name to " + name | ||||
|         }); | ||||
|         io.sockets.emit("chat.all", { | ||||
|           from: announce_payload.old_name, | ||||
|           msg: " changed name to " + name, | ||||
|           channel: announce_payload.channel | ||||
|         }); | ||||
|       } else if (announce_payload.message && !announce_payload.all) { | ||||
|         io.to(announce_payload.channel).emit("chat", { | ||||
|           from: name, | ||||
|           msg: ": " + announce_payload.message | ||||
|         }); | ||||
|       } else if (announce_payload.message && announce_payload.all) { | ||||
|         io.sockets.emit("chat.all", { | ||||
|           from: name, | ||||
|           msg: ": " + announce_payload.message, | ||||
|           channel: announce_payload.channel | ||||
|         }); | ||||
|       } | ||||
|     } else { | ||||
|       generate_name(guid, announce_payload, tmp_name, round + 0.25, channel); | ||||
|     } | ||||
|   } else { | ||||
|     generate_name(guid, announce_payload, tmp_name, round + 0.25, channel); | ||||
|   } | ||||
| } | ||||
|  | ||||
| module.exports.get_name = get_name; | ||||
| module.exports.checkIfChatEnabled = checkIfChatEnabled; | ||||
| module.exports.checkIfUserIsBanned = checkIfUserIsBanned; | ||||
| module.exports.namechange = namechange; | ||||
| module.exports.get_name_generate = get_name_generate; | ||||
| @@ -1,3 +1,12 @@ | ||||
| var path = require("path"); | ||||
| var mongojs = require("mongojs"); | ||||
| var db = require(pathThumbnails + "/handlers/db.js"); | ||||
| var find = require(pathThumbnails + "/handlers/dbFunctions/find.js"); | ||||
|  | ||||
| var SessionHandler = require(pathThumbnails + | ||||
|   "/handlers/dbFunctions/advancedFunctions/sessionHandler.js"); | ||||
| var Helpers = require(pathThumbnails + "/handlers/helpers.js"); | ||||
|  | ||||
| async function end(obj, coll, guid, offline, socket) { | ||||
|   var socketid = socket.zoff_id; | ||||
|   if (typeof obj !== "object") { | ||||
| @@ -45,9 +54,12 @@ async function end(obj, coll, guid, offline, socket) { | ||||
|       docs.length > 0 && | ||||
|       (docs[0].userpass != undefined && docs[0].userpass != "") | ||||
|     ) { | ||||
|       callback_function = Functions.getSessionAdminUser; | ||||
|       callback_function = SessionHandler.getSessionAdminUser; | ||||
|       authentication_needed = true; | ||||
|       var sessionAdminUser = await Functions.getSessionAdminUser(Functions.getSession(socket), coll); | ||||
|       var sessionAdminUser = await SessionHandler.getSessionAdminUser( | ||||
|         Helpers.getSession(socket), | ||||
|         coll | ||||
|       ); | ||||
|       obj.userpass = sessionAdminUser.userpass; | ||||
|     } | ||||
|  | ||||
| @@ -55,52 +67,44 @@ async function end(obj, coll, guid, offline, socket) { | ||||
|       obj.pass = userpass; | ||||
|     } else { | ||||
|       obj.pass = crypto | ||||
|       .createHash("sha256") | ||||
|       .update(Functions.decrypt_string(obj.pass)) | ||||
|       .digest("base64"); | ||||
|         .createHash("sha256") | ||||
|         .update(Helpers.decrypt_string(obj.pass)) | ||||
|         .digest("base64"); | ||||
|     } | ||||
|     if ( | ||||
|       !authentication_needed || | ||||
|       (authentication_needed && | ||||
|         obj.hasOwnProperty("pass") && | ||||
|         docs[0].userpass == obj.pass) | ||||
|       ) { | ||||
|         Functions.check_inlist( | ||||
|           coll, | ||||
|           guid, | ||||
|           socket, | ||||
|           offline, | ||||
|           undefined, | ||||
|           "place 13" | ||||
|         ); | ||||
|         var np = await find(coll, { now_playing: true }); | ||||
|         if (err !== null) console.log(err); | ||||
|         if ( | ||||
|           np !== null && | ||||
|           np !== undefined && | ||||
|           np.length == 1 && | ||||
|           np[0].id == id | ||||
|         ) { | ||||
|           var startTime = docs[0].startTime; | ||||
|           if ( | ||||
|             startTime + parseInt(np[0].duration) <= | ||||
|             Functions.get_time() + 5 | ||||
|           ) { | ||||
|             changeSong(coll, false, id, docs); | ||||
|           } | ||||
|     ) { | ||||
|       Functions.check_inlist( | ||||
|         coll, | ||||
|         guid, | ||||
|         socket, | ||||
|         offline, | ||||
|         undefined, | ||||
|         "place 13" | ||||
|       ); | ||||
|       var np = await find(coll, { now_playing: true }); | ||||
|       if (err !== null) console.log(err); | ||||
|       if (np !== null && np !== undefined && np.length == 1 && np[0].id == id) { | ||||
|         var startTime = docs[0].startTime; | ||||
|         if (startTime + parseInt(np[0].duration) <= Helpers.get_time() + 5) { | ||||
|           changeSong(coll, false, id, docs); | ||||
|         } | ||||
|       } else { | ||||
|         socket.emit("auth_required"); | ||||
|       } | ||||
|     } else { | ||||
|       var result = { | ||||
|         msg: { | ||||
|           expected: "object", | ||||
|           got: typeof obj | ||||
|         } | ||||
|       }; | ||||
|       socket.emit("update_required", result); | ||||
|       socket.emit("auth_required"); | ||||
|     } | ||||
|   } else { | ||||
|     var result = { | ||||
|       msg: { | ||||
|         expected: "object", | ||||
|         got: typeof obj | ||||
|       } | ||||
|     }; | ||||
|     socket.emit("update_required", result); | ||||
|   } | ||||
| } | ||||
|  | ||||
|   module.exports.end = end; | ||||
| module.exports.end = end; | ||||
|   | ||||
							
								
								
									
										112
									
								
								server/handlers/dbFunctions/advancedFunctions/frontpageLists.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										112
									
								
								server/handlers/dbFunctions/advancedFunctions/frontpageLists.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,112 @@ | ||||
| var path = require("path"); | ||||
| var mongojs = require("mongojs"); | ||||
| var db = require(pathThumbnails + "/handlers/db.js"); | ||||
| var find = require(pathThumbnails + "/handlers/dbFunctions/find.js"); | ||||
| var aggregate = require(pathThumbnails + "/handlers/dbFunctions/aggregate.js"); | ||||
|  | ||||
| async function frontpageLists(msg, socket) { | ||||
|   if ( | ||||
|     msg == undefined || | ||||
|     !msg.hasOwnProperty("version") || | ||||
|     msg.version != VERSION || | ||||
|     msg.version == undefined | ||||
|   ) { | ||||
|     var result = { | ||||
|       version: { | ||||
|         expected: VERSION, | ||||
|         got: msg.hasOwnProperty("version") ? msg.version : undefined | ||||
|       } | ||||
|     }; | ||||
|     socket.emit("update_required", result); | ||||
|     return; | ||||
|   } | ||||
|  | ||||
|   var docs = await find("frontpage_lists", { frontpage: true }); | ||||
|   var tot = await find("connected_users", { _id: "total_users" }); | ||||
|   socket.compress(true).emit("playlists", { | ||||
|     channels: docs, | ||||
|     viewers: tot[0].total_users.length | ||||
|   }); | ||||
| } | ||||
|  | ||||
| async function getFrontpageLists(callback) { | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     var project_object = { | ||||
|       _id: 1, | ||||
|       count: 1, | ||||
|       frontpage: 1, | ||||
|       id: 1, | ||||
|       title: 1, | ||||
|       viewers: 1, | ||||
|       accessed: 1, | ||||
|       pinned: { $ifNull: ["$pinned", 0] }, | ||||
|       description: { | ||||
|         $ifNull: [ | ||||
|           { | ||||
|             $cond: { | ||||
|               if: { | ||||
|                 $or: [ | ||||
|                   { $eq: ["$description", ""] }, | ||||
|                   { $eq: ["$description", null] }, | ||||
|                   { $eq: ["$description", undefined] } | ||||
|                 ] | ||||
|               }, | ||||
|               then: "This list has no description", | ||||
|               else: "$description" | ||||
|             } | ||||
|           }, | ||||
|           "This list has no description" | ||||
|         ] | ||||
|       }, | ||||
|       thumbnail: { | ||||
|         $ifNull: [ | ||||
|           { | ||||
|             $cond: { | ||||
|               if: { | ||||
|                 $or: [ | ||||
|                   { $eq: ["$thumbnail", ""] }, | ||||
|                   { $eq: ["$thumbnail", null] }, | ||||
|                   { $eq: ["$thumbnail", undefined] } | ||||
|                 ] | ||||
|               }, | ||||
|               then: { | ||||
|                 $concat: [ | ||||
|                   "https://img.youtube.com/vi/", | ||||
|                   "$id", | ||||
|                   "/mqdefault.jpg" | ||||
|                 ] | ||||
|               }, | ||||
|               else: "$thumbnail" | ||||
|             } | ||||
|           }, | ||||
|           { $concat: ["https://img.youtube.com/vi/", "$id", "/mqdefault.jpg"] } | ||||
|         ] | ||||
|       } | ||||
|     }; | ||||
|     var docs = await aggregate("frontpage_lists", [ | ||||
|       { | ||||
|         $match: { | ||||
|           frontpage: true, | ||||
|           count: { $gt: 3 } | ||||
|         } | ||||
|       }, | ||||
|       { | ||||
|         $project: project_object | ||||
|       }, | ||||
|       { | ||||
|         $sort: { | ||||
|           pinned: -1, | ||||
|           viewers: -1, | ||||
|           accessed: -1, | ||||
|           count: -1, | ||||
|           title: 1 | ||||
|         } | ||||
|       } | ||||
|     ]); | ||||
|  | ||||
|     resolve(docs); | ||||
|   }); | ||||
| } | ||||
|  | ||||
| module.exports.frontpageLists = frontpageLists; | ||||
| module.exports.getFrontpageLists = getFrontpageLists; | ||||
| @@ -3,16 +3,49 @@ var mongojs = require("mongojs"); | ||||
| var db = require(pathThumbnails + "/handlers/db.js"); | ||||
| var update = require(pathThumbnails + "/handlers/dbFunctions/update.js"); | ||||
|  | ||||
| var Helpers = require(pathThumbnails + "/handlers/helpers.js"); | ||||
| async function incrementList(collection, way) { | ||||
|   return update( | ||||
|     collection, | ||||
|     { _id: coll, count: { $gt: 0 } }, | ||||
|     { | ||||
|       $inc: { count: way }, | ||||
|       $set: { accessed: Functions.get_time() } | ||||
|       $set: { accessed: Helpers.get_time() } | ||||
|     }, | ||||
|     { upsert: true } | ||||
|   ); | ||||
| } | ||||
|  | ||||
| async function updateFrontpage(coll, id, title, thumbnail, source) { | ||||
|   //coll = coll.replace(/ /g,''); | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     var doc = await find("frontpage_lists", { _id: coll }); | ||||
|     var updateObject = { | ||||
|       id: id, | ||||
|       title: title, | ||||
|       accessed: Helpers.get_time() | ||||
|     }; | ||||
|     if ( | ||||
|       doc.length > 0 && | ||||
|       ((doc[0].thumbnail != "" && | ||||
|         doc[0].thumbnail != undefined && | ||||
|         (doc[0].thumbnail.indexOf("https://i1.sndcdn.com") > -1 || | ||||
|           doc[0].thumbnail.indexOf("https://w1.sndcdn.com") > -1 || | ||||
|           doc[0].thumbnail.indexOf("https://img.youtube.com") > -1)) || | ||||
|         (doc[0].thumbnail == "" || doc[0].thumbnail == undefined)) | ||||
|     ) { | ||||
|       updateObject.thumbnail = thumbnail; | ||||
|       if (thumbnail == undefined) updateObject.thumbnail = ""; | ||||
|     } | ||||
|     await update( | ||||
|       "frontpage_lists", | ||||
|       { _id: coll }, | ||||
|       { $set: updateObject }, | ||||
|       { upsert: true } | ||||
|     ); | ||||
|     resolve(); | ||||
|   }); | ||||
| } | ||||
|  | ||||
| module.exports.updateFrontpage = updateFrontpage; | ||||
| module.exports.incrementList = incrementList; | ||||
|   | ||||
							
								
								
									
										111
									
								
								server/handlers/dbFunctions/advancedFunctions/inlistCheck.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										111
									
								
								server/handlers/dbFunctions/advancedFunctions/inlistCheck.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,111 @@ | ||||
| var find = require(pathThumbnails + "/handlers/dbFunctions/find.js"); | ||||
| var update = require(pathThumbnails + "/handlers/dbFunctions/update.js"); | ||||
|  | ||||
| var SessionHandler = require(pathThumbnails + | ||||
|   "/handlers/dbFunctions/advancedFunctions/sessionHandler.js"); | ||||
| var Helpers = require(pathThumbnails + "/handlers/helpers.js"); | ||||
| var Chat = require(pathThumbnails + | ||||
|   "/handlers/dbFunctions/advancedFunctions/chat.js"); | ||||
| var sIO = require(pathThumbnails + "/apps/client.js").socketIO; | ||||
|  | ||||
| async function check(coll, guid, socket, offline, callback, double_check) { | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     if (coll == undefined) { | ||||
|       resolve(); | ||||
|       return; | ||||
|     } | ||||
|     //coll = coll.replace(/ /g,''); | ||||
|     if (!offline && coll != undefined) { | ||||
|       var updated = await update( | ||||
|         "connected_users", | ||||
|         { _id: coll }, | ||||
|         { $addToSet: { users: guid } }, | ||||
|         { upsert: true } | ||||
|       ); | ||||
|       if (updated.nModified > 0 || updated.upserted != undefined) { | ||||
|         var new_doc = await find("connected_users", { _id: coll }); | ||||
|         await update( | ||||
|           "frontpage_lists", | ||||
|           { _id: coll }, | ||||
|           { $set: { viewers: new_doc[0].users.length } } | ||||
|         ); | ||||
|         if ( | ||||
|           new_doc[0].users == undefined || | ||||
|           new_doc[0].users.length == undefined | ||||
|         ) { | ||||
|           io.to(coll).emit("viewers", 1); | ||||
|         } else { | ||||
|           io.to(coll).emit("viewers", new_doc[0].users.length); | ||||
|         } | ||||
|         var enabled = await Chat.namechange( | ||||
|           { initial: true, first: true, channel: coll }, | ||||
|           guid, | ||||
|           socket, | ||||
|           false | ||||
|         ); | ||||
|         var docs = await find("user_names", { guid: guid }); | ||||
|         if (docs.length == 1) { | ||||
|           var icon = ""; | ||||
|           if (docs[0].icon != undefined) icon = docs[0].icon; | ||||
|           update( | ||||
|             "user_names", | ||||
|             { guid: guid }, | ||||
|             { $addToSet: { channels: coll } } | ||||
|           ); | ||||
|           if (enabled) { | ||||
|             socket.broadcast.to(coll).emit("chat", { | ||||
|               from: docs[0].name, | ||||
|               icon: icon, | ||||
|               msg: " joined" | ||||
|             }); | ||||
|           } | ||||
|         } else if (docs.length == 0) { | ||||
|           //console.log("User doesn't have a name for some reason."); | ||||
|           //console.log("guid", guid); | ||||
|           //console.log("channel", coll); | ||||
|           //console.log("Trying to get a chat-name"); | ||||
|           Chat.get_name(guid, { | ||||
|             announce: false, | ||||
|             socket: socket, | ||||
|             channel: coll | ||||
|           }); | ||||
|         } | ||||
|         update( | ||||
|           "connected_users", | ||||
|           { _id: "total_users" }, | ||||
|           { $addToSet: { total_users: guid + coll } } | ||||
|         ); | ||||
|         resolve(); | ||||
|       } else { | ||||
|         var new_doc = await find("connected_users", { _id: coll }); | ||||
|         io.to(coll).emit("viewers", new_doc[0].users.length); | ||||
|         resolve(); | ||||
|       } | ||||
|     } else { | ||||
|       if (offline) { | ||||
|         update( | ||||
|           "connected_users", | ||||
|           { _id: "offline_users" }, | ||||
|           { $addToSet: { users: guid } } | ||||
|         ); | ||||
|       } else { | ||||
|         update( | ||||
|           "connected_users", | ||||
|           { _id: coll }, | ||||
|           { $addToSet: { users: guid } } | ||||
|         ); | ||||
|       } | ||||
|       // | ||||
|       if (coll != undefined && coll != "") { | ||||
|         update( | ||||
|           "connected_users", | ||||
|           { _id: "total_users" }, | ||||
|           { $addToSet: { total_users: guid + coll } } | ||||
|         ); | ||||
|       } | ||||
|       resolve(); | ||||
|     } | ||||
|   }); | ||||
| } | ||||
|  | ||||
| module.exports.check = check; | ||||
| @@ -5,6 +5,19 @@ var find = require(pathThumbnails + "/handlers/dbFunctions/find.js"); | ||||
| var create = require(pathThumbnails + "/handlers/dbFunctions/create.js"); | ||||
| var insert = require(pathThumbnails + "/handlers/dbFunctions/insert.js"); | ||||
|  | ||||
| var sendList = require(pathThumbnails + | ||||
|   "/handlers/dbFunctions/advancedFunctions/sendList.js"); | ||||
|  | ||||
| var crypto = require("crypto"); | ||||
| var SessionHandler = require(pathThumbnails + | ||||
|   "/handlers/dbFunctions/advancedFunctions/sessionHandler.js"); | ||||
| var Inlist = require(pathThumbnails + | ||||
|   "/handlers/dbFunctions/advancedFunctions/inlistCheck.js"); | ||||
| var Play = require(pathThumbnails + | ||||
|   "/handlers/dbFunctions/advancedFunctions/play.js"); | ||||
| var Helpers = require(pathThumbnails + "/handlers/helpers.js"); | ||||
|  | ||||
| var sIO = require(pathThumbnails + "/apps/client.js").socketIO; | ||||
| async function joinSilent(msg, socket) { | ||||
|   if (typeof msg === "object" && msg !== undefined && msg !== null) { | ||||
|     var channelName = msg.channel; | ||||
| @@ -12,7 +25,7 @@ async function joinSilent(msg, socket) { | ||||
|     var password = ""; | ||||
|     if (msg.password != "") { | ||||
|       tryingPassword = true; | ||||
|       password = Functions.decrypt_string(msg.password); | ||||
|       password = Helpers.decrypt_string(msg.password); | ||||
|       password = crypto | ||||
|         .createHash("sha256") | ||||
|         .update(password) | ||||
| @@ -20,7 +33,7 @@ async function joinSilent(msg, socket) { | ||||
|     } | ||||
|  | ||||
|     channelName = channelName.toLowerCase(); //.replace(/ /g,''); | ||||
|     channelName = Functions.removeEmojis(channelName).toLowerCase(); | ||||
|     channelName = Helpers.removeEmojis(channelName).toLowerCase(); | ||||
|     var docs = await find(channelName + "_settings"); | ||||
|     if (docs.length == 0) { | ||||
|       socket.emit("join_silent_declined", ""); | ||||
| @@ -34,7 +47,7 @@ async function joinSilent(msg, socket) { | ||||
|       socket.join(channelName); | ||||
|       socket.emit("join_silent_accepted", ""); | ||||
|  | ||||
|       send_play(channelName, socket); | ||||
|       Play.sendPlay(channelName, socket); | ||||
|     } else { | ||||
|       socket.emit("join_silent_declined", ""); | ||||
|     } | ||||
| @@ -46,8 +59,8 @@ async function joinSilent(msg, socket) { | ||||
| async function joinList(msg, guid, coll, offline, socket) { | ||||
|   var socketid = socket.zoff_id; | ||||
|   if (typeof msg === "object" && msg !== undefined && msg !== null) { | ||||
|     var sessionAdminUser = await Functions.getSessionAdminUser( | ||||
|       Functions.getSession(socket), | ||||
|     var sessionAdminUser = await SessionHandler.getSessionAdminUser( | ||||
|       Helpers.getSession(socket), | ||||
|       coll | ||||
|     ); | ||||
|     var userpass = sessionAdminUser.userpass; | ||||
| @@ -58,10 +71,10 @@ async function joinList(msg, guid, coll, offline, socket) { | ||||
|     } else { | ||||
|       msg.pass = crypto | ||||
|         .createHash("sha256") | ||||
|         .update(Functions.decrypt_string(msg.pass)) | ||||
|         .update(Helpers.decrypt_string(msg.pass)) | ||||
|         .digest("base64"); | ||||
|     } | ||||
|     adminpass = Functions.hash_pass(adminpass); | ||||
|     adminpass = Helpers.hash_pass(adminpass); | ||||
|     if ( | ||||
|       !msg.hasOwnProperty("version") || | ||||
|       !msg.hasOwnProperty("channel") || | ||||
| @@ -87,7 +100,7 @@ async function joinList(msg, guid, coll, offline, socket) { | ||||
|       return; | ||||
|     } | ||||
|     coll = msg.channel.toLowerCase(); //.replace(/ /g,''); | ||||
|     coll = Functions.removeEmojis(coll).toLowerCase(); | ||||
|     coll = Helpers.removeEmojis(coll).toLowerCase(); | ||||
|     //coll = filter.clean(coll); | ||||
|     var pass = msg.pass; | ||||
|     var frontpage_lists = await find("frontpage_lists", { _id: coll }); | ||||
| @@ -106,15 +119,19 @@ async function joinList(msg, guid, coll, offline, socket) { | ||||
|           docs[0].userpass != "" && | ||||
|           docs[0].userpass == pass | ||||
|         ) { | ||||
|           Functions.setSessionUserPass( | ||||
|             Functions.getSession(socket), | ||||
|           SessionHandler.setSessionUserPass( | ||||
|             Helpers.getSession(socket), | ||||
|             msg.pass, | ||||
|             coll | ||||
|           ); | ||||
|           socket.emit("auth_accepted", { value: true }); | ||||
|         } | ||||
|         if (docs.length > 0 && docs[0].userpass != pass) { | ||||
|           Functions.setSessionUserPass(Functions.getSession(socket), "", coll); | ||||
|           SessionHandler.setSessionUserPass( | ||||
|             Helpers.getSession(socket), | ||||
|             "", | ||||
|             coll | ||||
|           ); | ||||
|         } | ||||
|         if ( | ||||
|           docs.length > 0 && | ||||
| @@ -126,14 +143,7 @@ async function joinList(msg, guid, coll, offline, socket) { | ||||
|         } | ||||
|         in_list = true; | ||||
|         socket.join(coll); | ||||
|         Functions.check_inlist( | ||||
|           coll, | ||||
|           guid, | ||||
|           socket, | ||||
|           offline, | ||||
|           undefined, | ||||
|           "place 10" | ||||
|         ); | ||||
|         Inlist.check(coll, guid, socket, offline, undefined, "place 10"); | ||||
|  | ||||
|         if (frontpage_lists[0].viewers != undefined) { | ||||
|           io.to(coll).emit("viewers", frontpage_lists[0].viewers); | ||||
| @@ -141,7 +151,7 @@ async function joinList(msg, guid, coll, offline, socket) { | ||||
|           io.to(coll).emit("viewers", 1); | ||||
|         } | ||||
|  | ||||
|         send_list(coll, socket, true, false, true); | ||||
|         sendList(coll, socket, true, false, true); | ||||
|       } else { | ||||
|         socket.emit("auth_required"); | ||||
|       } | ||||
| @@ -159,7 +169,7 @@ async function joinList(msg, guid, coll, offline, socket) { | ||||
|         shuffle: true, | ||||
|         skip: false, | ||||
|         skips: [], | ||||
|         startTime: Functions.get_time(), | ||||
|         startTime: Helpers.get_time(), | ||||
|         views: [], | ||||
|         vote: false, | ||||
|         description: "", | ||||
| @@ -171,22 +181,15 @@ async function joinList(msg, guid, coll, offline, socket) { | ||||
|       }; | ||||
|       await insert(coll + "_settings", configs); | ||||
|       socket.join(coll); | ||||
|       send_list(coll, socket, true, false, true); | ||||
|       sendList(coll, socket, true, false, true); | ||||
|       insert("frontpage_lists", { | ||||
|         _id: coll, | ||||
|         count: 0, | ||||
|         frontpage: true, | ||||
|         accessed: Functions.get_time(), | ||||
|         accessed: Helpers.get_time(), | ||||
|         viewers: 1 | ||||
|       }); | ||||
|       Functions.check_inlist( | ||||
|         coll, | ||||
|         guid, | ||||
|         socket, | ||||
|         offline, | ||||
|         undefined, | ||||
|         "place 11" | ||||
|       ); | ||||
|       Inlist.check(coll, guid, socket, offline, undefined, "place 11"); | ||||
|     } | ||||
|   } else { | ||||
|     var result = { | ||||
|   | ||||
| @@ -2,127 +2,130 @@ var path = require("path"); | ||||
| var mongojs = require("mongojs"); | ||||
| var db = require(pathThumbnails + "/handlers/db.js"); | ||||
| var find = require(pathThumbnails + "/handlers/dbFunctions/find.js"); | ||||
| var aggregate = require(pathThumbnails + "/handlers/dbFunctions/aggregate.js"); | ||||
|  | ||||
| var Helpers = require(pathThumbnails + "/handlers/helpers.js"); | ||||
| var sIO = require(pathThumbnails + "/apps/client.js").socketIO; | ||||
| var nextSongAggregate = [ | ||||
|   { | ||||
|     $match: { | ||||
|       views: { | ||||
|         $exists: false | ||||
|       }, | ||||
|       type: { | ||||
|         $ne: "suggested" | ||||
|       } | ||||
|     } | ||||
|   }, | ||||
|   { | ||||
|     $sort: { | ||||
|       now_playing: 1, | ||||
|       votes: -1, | ||||
|       added: 1, | ||||
|       title: 1 | ||||
|     } | ||||
|   }, | ||||
|   { | ||||
|     $limit: 1 | ||||
|   } | ||||
| ]; | ||||
|  | ||||
| async function getNowPlaying(list, socket) { | ||||
|     return new Promise((resolve, reject) => { | ||||
|         if (typeof list !== "string" || typeof fn !== "function") { | ||||
|             socket.emit("update_required"); | ||||
|             return; | ||||
|         } | ||||
|         var docs = await find(list, { now_playing: true }); | ||||
|         if (docs.length === 0) { | ||||
|             resolve("No song currently playing"); | ||||
|             return; | ||||
|         } | ||||
|         var title = docs[0].title; | ||||
|         if (title === undefined) resolve("No song currently playing"); | ||||
|         else resolve(title); | ||||
|     }); | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     if (typeof list !== "string" || typeof fn !== "function") { | ||||
|       socket.emit("update_required"); | ||||
|       return; | ||||
|     } | ||||
|     var docs = await find(list, { now_playing: true }); | ||||
|     if (docs.length === 0) { | ||||
|       resolve("No song currently playing"); | ||||
|       return; | ||||
|     } | ||||
|     var title = docs[0].title; | ||||
|     if (title === undefined) resolve("No song currently playing"); | ||||
|     else resolve(title); | ||||
|   }); | ||||
| } | ||||
|  | ||||
| async function sendPlay(coll, socket, broadcast) { | ||||
|     //coll = coll.replace(/ /g,''); | ||||
|     var np = await find(coll, { now_playing: true }); | ||||
|     var conf = await find(coll + "_settings"); | ||||
|     if (err !== null) console.log(err); | ||||
|     try { | ||||
|         if (Functions.get_time() - conf[0].startTime > np[0].duration) { | ||||
|             changeSong(coll, false, np[0].id, conf); | ||||
|         } else if (conf !== null && conf !== undefined && conf.length !== 0) { | ||||
|             if (conf[0].adminpass !== "") conf[0].adminpass = true; | ||||
|             if (conf[0].hasOwnProperty("userpass") && conf[0].userpass != "") | ||||
|             conf[0].userpass = true; | ||||
|             else conf[0].userpass = false; | ||||
|             if (!np.hasOwnProperty("start")) np.start = 0; | ||||
|             if (!np.hasOwnProperty("end")) np.end = np.duration; | ||||
|             toSend = { np: np, conf: conf, time: Functions.get_time() }; | ||||
|             if (socket === undefined) { | ||||
|                 io.to(coll).emit("np", toSend); | ||||
|                 // | ||||
|                 getNextSong(coll, undefined); | ||||
|                 var url = | ||||
|                 "https://img.youtube.com/vi/" + np[0].id + "/mqdefault.jpg"; | ||||
|                 if (np[0].source == "soundcloud") url = np[0].thumbnail; | ||||
|                 sendColor(coll, false, url); | ||||
|             } else { | ||||
|                 var url = | ||||
|                 "https://img.youtube.com/vi/" + np[0].id + "/mqdefault.jpg"; | ||||
|                 if (np[0].source == "soundcloud") url = np[0].thumbnail; | ||||
|                 sendColor(coll, socket, url); | ||||
|                 if (broadcast) { | ||||
|                     socket.to(coll).emit("np", toSend); | ||||
|                     return; | ||||
|                 } | ||||
|                 socket.emit("np", toSend); | ||||
|             } | ||||
|         } | ||||
|     } catch (e) { | ||||
|         if (socket) { | ||||
|             if (broadcast) { | ||||
|                 socket.to(coll).emit("np", {}); | ||||
|                 return; | ||||
|             } | ||||
|             socket.emit("np", {}); | ||||
|         } else { | ||||
|             io.to(coll).emit("np", {}); | ||||
|   //coll = coll.replace(/ /g,''); | ||||
|   var np = await find(coll, { now_playing: true }); | ||||
|   var conf = await find(coll + "_settings"); | ||||
|   console.log(np); | ||||
|   try { | ||||
|     if (Helpers.get_time() - conf[0].startTime > np[0].duration) { | ||||
|       changeSong(coll, false, np[0].id, conf); | ||||
|     } else if (conf !== null && conf !== undefined && conf.length !== 0) { | ||||
|       if (conf[0].adminpass !== "") conf[0].adminpass = true; | ||||
|       if (conf[0].hasOwnProperty("userpass") && conf[0].userpass != "") | ||||
|         conf[0].userpass = true; | ||||
|       else conf[0].userpass = false; | ||||
|       if (!np.hasOwnProperty("start")) np.start = 0; | ||||
|       if (!np.hasOwnProperty("end")) np.end = np.duration; | ||||
|       toSend = { np: np, conf: conf, time: Helpers.get_time() }; | ||||
|       if (socket === undefined) { | ||||
|         io.to(coll).emit("np", toSend); | ||||
|         // | ||||
|         getNextSong(coll, undefined); | ||||
|         var url = "https://img.youtube.com/vi/" + np[0].id + "/mqdefault.jpg"; | ||||
|         if (np[0].source == "soundcloud") url = np[0].thumbnail; | ||||
|         Helpers.sendColor(coll, false, url); | ||||
|       } else { | ||||
|         var url = "https://img.youtube.com/vi/" + np[0].id + "/mqdefault.jpg"; | ||||
|         if (np[0].source == "soundcloud") url = np[0].thumbnail; | ||||
|         Helpers.sendColor(coll, socket, url); | ||||
|         if (broadcast) { | ||||
|           socket.to(coll).emit("np", toSend); | ||||
|           return; | ||||
|         } | ||||
|         socket.emit("np", toSend); | ||||
|       } | ||||
|     } | ||||
|   } catch (e) { | ||||
|     console.log(e); | ||||
|     if (socket) { | ||||
|       if (broadcast) { | ||||
|         socket.to(coll).emit("np", {}); | ||||
|         return; | ||||
|       } | ||||
|       socket.emit("np", {}); | ||||
|     } else { | ||||
|       io.to(coll).emit("np", {}); | ||||
|     } | ||||
|   } | ||||
| } | ||||
|  | ||||
| async function getNextSong(coll, socket) { | ||||
|     //coll = coll.replace(/ /g,''); | ||||
|     return new Promise((resolve, reject) => { | ||||
|         var docs = await aggregate(coll, | ||||
|             [ | ||||
|                 { | ||||
|                     $match: { | ||||
|                         views: { | ||||
|                             $exists: false | ||||
|                         }, | ||||
|                         type: { | ||||
|                             $ne: "suggested" | ||||
|                         } | ||||
|                     } | ||||
|                 }, | ||||
|                 { | ||||
|                     $sort: { | ||||
|                         now_playing: 1, | ||||
|                         votes: -1, | ||||
|                         added: 1, | ||||
|                         title: 1 | ||||
|                     } | ||||
|                 }, | ||||
|                 { | ||||
|                     $limit: 1 | ||||
|                 } | ||||
|             ]); | ||||
|             if (doc.length == 1) { | ||||
|                 var thumbnail = ""; | ||||
|                 var source = "youtube"; | ||||
|                 if (doc[0].source && doc[0].source == "soundcloud") { | ||||
|                     source = "soundcloud"; | ||||
|                     thumbnail = doc[0].thumbnail; | ||||
|                 } | ||||
|                 if (socket != undefined) { | ||||
|                     socket.emit("next_song", { | ||||
|                         videoId: doc[0].id, | ||||
|                         title: doc[0].title, | ||||
|                         source: source, | ||||
|                         thumbnail: thumbnail | ||||
|                     }); | ||||
|                 } else { | ||||
|                     io.to(coll).emit("next_song", { | ||||
|                         videoId: doc[0].id, | ||||
|                         title: doc[0].title, | ||||
|                         source: source, | ||||
|                         thumbnail: thumbnail | ||||
|                     }); | ||||
|                 } | ||||
|             } | ||||
|             resolve(); | ||||
|   //coll = coll.replace(/ /g,''); | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     var doc = await aggregate(coll, nextSongAggregate); | ||||
|     if (doc.length == 1) { | ||||
|       var thumbnail = ""; | ||||
|       var source = "youtube"; | ||||
|       if (doc[0].source && doc[0].source == "soundcloud") { | ||||
|         source = "soundcloud"; | ||||
|         thumbnail = doc[0].thumbnail; | ||||
|       } | ||||
|       if (socket != undefined) { | ||||
|         socket.emit("next_song", { | ||||
|           videoId: doc[0].id, | ||||
|           title: doc[0].title, | ||||
|           source: source, | ||||
|           thumbnail: thumbnail | ||||
|         }); | ||||
|       } else { | ||||
|         io.to(coll).emit("next_song", { | ||||
|           videoId: doc[0].id, | ||||
|           title: doc[0].title, | ||||
|           source: source, | ||||
|           thumbnail: thumbnail | ||||
|         }); | ||||
|       } | ||||
|     } | ||||
|     resolve(); | ||||
|   }); | ||||
| } | ||||
|  | ||||
|     module.exports.getNowPlaying = getNowPlaying; | ||||
|     module.exports.getNextSong = getNextSong; | ||||
|     module.exports.sendPlay = sendPlay; | ||||
| module.exports.getNowPlaying = getNowPlaying; | ||||
| module.exports.getNextSong = getNextSong; | ||||
| module.exports.sendPlay = sendPlay; | ||||
|   | ||||
| @@ -7,9 +7,16 @@ var update = require(pathThumbnails + "/handlers/dbFunctions/update.js"); | ||||
| var aggregate = require(pathThumbnails + "/handlers/dbFunctions/aggregate.js"); | ||||
| var sort = require(pathThumbnails + "/handlers/dbFunctions/sort.js"); | ||||
|  | ||||
| var projects = require(pathThumbnails + "/handlers/aggregates.js"); | ||||
| var Play = require(pathThumbnails + | ||||
|   "/handlers/dbFunctions/advancedFunctions/play.js"); | ||||
| var changeSong = require(pathThumbnails + | ||||
|   "/handlers/dbFunctions/advancedFunctions/changeSong.js"); | ||||
| var Helpers = require(pathThumbnails + "/handlers/helpers.js"); | ||||
| var sIO = require(pathThumbnails + "/apps/client.js").socketIO; | ||||
| async function sendList(coll, socket, send, list_send, configs, shuffled) { | ||||
|   //coll = coll.replace(/ /g,''); | ||||
|   var conf = await aggregate(coll + "_settings", [ | ||||
|   var _conf = await aggregate(coll + "_settings", [ | ||||
|     { | ||||
|       $match: { | ||||
|         id: "config" | ||||
| @@ -32,7 +39,7 @@ async function sendList(coll, socket, send, list_send, configs, shuffled) { | ||||
|       shuffle: true, | ||||
|       skip: false, | ||||
|       skips: [], | ||||
|       startTime: Functions.get_time(), | ||||
|       startTime: Helpers.get_time(), | ||||
|       views: [], | ||||
|       vote: false, | ||||
|       description: "", | ||||
| @@ -88,7 +95,7 @@ async function sendList(coll, socket, send, list_send, configs, shuffled) { | ||||
|                 now_playing: true, | ||||
|                 votes: 0, | ||||
|                 guids: [], | ||||
|                 added: Functions.get_time() | ||||
|                 added: Helpers.get_time() | ||||
|               } | ||||
|             } | ||||
|           ); | ||||
| @@ -97,7 +104,7 @@ async function sendList(coll, socket, send, list_send, configs, shuffled) { | ||||
|             { id: "config" }, | ||||
|             { | ||||
|               $set: { | ||||
|                 startTime: Functions.get_time(), | ||||
|                 startTime: Helpers.get_time(), | ||||
|                 skips: [] | ||||
|               } | ||||
|             } | ||||
| @@ -136,7 +143,7 @@ async function sendList(coll, socket, send, list_send, configs, shuffled) { | ||||
|         ); | ||||
|         sendList(coll, socket, send, list_send, configs, shuffled); | ||||
|       } else { | ||||
|         if (Functions.get_time() - conf[0].startTime > np_docs[0].duration) { | ||||
|         if (Helpers.get_time() - conf[0].startTime > np_docs[0].duration) { | ||||
|           await changeSong(coll, false, np_docs[0].id, conf, socket); | ||||
|           sendList(coll, socket, send, list_send, configs, shuffled); | ||||
|         } else { | ||||
| @@ -154,9 +161,9 @@ async function sendList(coll, socket, send, list_send, configs, shuffled) { | ||||
|             }); | ||||
|           } | ||||
|           if (socket === undefined && send) { | ||||
|             sendPlay(coll); | ||||
|             Play.sendPlay(coll); | ||||
|           } else if (send) { | ||||
|             sendPlay(coll, socket); | ||||
|             Play.sendPlay(coll, socket); | ||||
|           } | ||||
|         } | ||||
|       } | ||||
| @@ -175,9 +182,9 @@ async function sendList(coll, socket, send, list_send, configs, shuffled) { | ||||
|         }); | ||||
|       } | ||||
|       if (socket === undefined && send) { | ||||
|         sendPlay(coll); | ||||
|         Play.sendPlay(coll); | ||||
|       } else if (send) { | ||||
|         sendPlay(coll, socket); | ||||
|         Play.sendPlay(coll, socket); | ||||
|       } | ||||
|     } | ||||
|     if (configs) { | ||||
| @@ -199,7 +206,7 @@ async function sendList(coll, socket, send, list_send, configs, shuffled) { | ||||
|           shuffle: true, | ||||
|           skip: false, | ||||
|           skips: [], | ||||
|           startTime: Functions.get_time(), | ||||
|           startTime: Helpers.get_time(), | ||||
|           views: [], | ||||
|           vote: false, | ||||
|           desc: "", | ||||
| @@ -218,4 +225,4 @@ async function sendList(coll, socket, send, list_send, configs, shuffled) { | ||||
|   } | ||||
| } | ||||
|  | ||||
| module.exports.sendList = sendList; | ||||
| module.exports = sendList; | ||||
|   | ||||
| @@ -1,12 +1,9 @@ | ||||
| var path = require("path"); | ||||
| var mongojs = require("mongojs"); | ||||
| var db = require(pathThumbnails + "/handlers/db.js"); | ||||
| var find = require(pathThumbnails + "/handlers/dbFunctions/find.js"); | ||||
| var remove = require(pathThumbnails + "/handlers/dbFunctions/remove.js"); | ||||
| var update = require(pathThumbnails + "/handlers/dbFunctions/update.js"); | ||||
| 
 | ||||
| async function setSessionAdminPass(id, adminpass, list) { | ||||
|   return new Promise((resolve, reject) => { | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     try { | ||||
|       if (id == "empty" || id == undefined) { | ||||
|         resolve(); | ||||
| @@ -27,7 +24,7 @@ async function setSessionAdminPass(id, adminpass, list) { | ||||
| } | ||||
| 
 | ||||
| async function setSessionChatPass(id, name, pass) { | ||||
|   return new Promise((resolve, reject) => { | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     try { | ||||
|       if (id == "empty" || id == undefined) { | ||||
|         resolve(); | ||||
| @@ -48,7 +45,7 @@ async function setSessionChatPass(id, name, pass) { | ||||
| } | ||||
| 
 | ||||
| async function getSessionChatPass(id) { | ||||
|   return new Promise((resolve, reject) => { | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     try { | ||||
|       if (id == "empty" || id == undefined) { | ||||
|         resolve({ name: "", pass: "", gotten: false }); | ||||
| @@ -75,7 +72,7 @@ async function getSessionChatPass(id) { | ||||
| } | ||||
| 
 | ||||
| async function setChromecastHost(id, other_id, list) { | ||||
|   return new Promise((resolve, reject) => { | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     try { | ||||
|       if ( | ||||
|         id == "empty" || | ||||
| @@ -101,7 +98,7 @@ async function setChromecastHost(id, other_id, list) { | ||||
| } | ||||
| 
 | ||||
| async function setSessionUserPass(id, userpass, list) { | ||||
|   return new Promise((resolve, reject) => { | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     try { | ||||
|       if (id == "empty" || id == undefined || userpass == undefined) { | ||||
|         reject(); | ||||
| @@ -123,7 +120,7 @@ async function setSessionUserPass(id, userpass, list) { | ||||
| } | ||||
| 
 | ||||
| async function getSessionAdminUser(id, list) { | ||||
|   return new Promise((resolve, reject) => { | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     try { | ||||
|       if (id == "empty" || id == undefined) { | ||||
|         resolve({ userpass: "", adminpass: "", gotten: false }); | ||||
| @@ -150,7 +147,7 @@ async function getSessionAdminUser(id, list) { | ||||
| } | ||||
| 
 | ||||
| async function removeSessionChatPass(id) { | ||||
|   return new Promise((resolve, reject) => { | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     if (id == "empty" || id == undefined) { | ||||
|       resolve(); | ||||
|       return; | ||||
| @@ -162,7 +159,7 @@ async function removeSessionChatPass(id) { | ||||
| } | ||||
| 
 | ||||
| async function removeSessionAdminPass(id, channel) { | ||||
|   return new Promise((resolve, reject) => { | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     if (id == "empty" || id == undefined) { | ||||
|       resolve(); | ||||
|       return; | ||||
| @@ -172,40 +169,6 @@ async function removeSessionAdminPass(id, channel) { | ||||
|   }); | ||||
| } | ||||
| 
 | ||||
| function sendColor(coll, socket, url, ajax, res) { | ||||
|   if (coll != undefined && typeof coll == "string") { | ||||
|     //coll = coll.replace(/ /g,'');
 | ||||
|   } | ||||
|   if (url.indexOf("://") == -1) | ||||
|     url = "https://img.youtube.com/vi/" + url + "/mqdefault.jpg"; | ||||
|   //var url = 'https://img.youtube.com/vi/'+id+'/mqdefault.jpg';
 | ||||
| 
 | ||||
|   Jimp.read(url) | ||||
|     .then(function(image) { | ||||
|       var c = ColorThief.getColor(image); | ||||
|       if (ajax) { | ||||
|         res.header({ "Content-Type": "application/json" }); | ||||
|         res.status(200).send(c); | ||||
|         return; | ||||
|       } else { | ||||
|         if (socket) { | ||||
|           socket.emit("color", { color: c, only: true }); | ||||
|         } else { | ||||
|           io.to(coll).emit("color", { color: c, only: false }); | ||||
|         } | ||||
|       } | ||||
|     }) | ||||
|     .catch(function(err) { | ||||
|       console.log("Crashed on fetching image, url is " + url); | ||||
|       console.log("Is ajax: " + ajax); | ||||
|       if (ajax) { | ||||
|         res.header({ "Content-Type": "application/json" }); | ||||
|         res.status(404); | ||||
|         return; | ||||
|       } | ||||
|     }); | ||||
| } | ||||
| 
 | ||||
| module.exports.setSessionAdminPass = setSessionAdminPass; | ||||
| module.exports.setSessionChatPass = setSessionChatPass; | ||||
| module.exports.getSessionChatPass = getSessionChatPass; | ||||
| @@ -214,4 +177,3 @@ module.exports.setSessionUserPass = setSessionUserPass; | ||||
| module.exports.getSessionAdminUser = getSessionAdminUser; | ||||
| module.exports.removeSessionChatPass = removeSessionChatPass; | ||||
| module.exports.removeSessionAdminPass = removeSessionAdminPass; | ||||
| module.exports.sendColor = sendColor; | ||||
| @@ -4,6 +4,19 @@ var db = require(pathThumbnails + "/handlers/db.js"); | ||||
| var find = require(pathThumbnails + "/handlers/dbFunctions/find.js"); | ||||
| var update = require(pathThumbnails + "/handlers/dbFunctions/update.js"); | ||||
|  | ||||
| var Helpers = require(pathThumbnails + "/handlers/helpers.js"); | ||||
| var SessionHandler = require(pathThumbnails + | ||||
|   "/handlers/dbFunctions/advancedFunctions/sessionHandler.js"); | ||||
| var Inlist = require(pathThumbnails + | ||||
|   "/handlers/dbFunctions/advancedFunctions/inlistCheck.js"); | ||||
| var Timeout = require(pathThumbnails + | ||||
|   "/handlers/dbFunctions/advancedFunctions/timeout.js"); | ||||
| var changeSong = require(pathThumbnails + | ||||
|   "/handlers/dbFunctions/advancedFunctions/changeSong.js"); | ||||
| var Play = require(pathThumbnails + | ||||
|   "/handlers/dbFunctions/advancedFunctions/play.js"); | ||||
| var sIO = require(pathThumbnails + "/apps/client.js").socketIO; | ||||
|  | ||||
| async function skip(list, guid, coll, offline, socket, callback) { | ||||
|   var socketid = socket.zoff_id; | ||||
|  | ||||
| @@ -23,7 +36,7 @@ async function skip(list, guid, coll, offline, socket, callback) { | ||||
|     try { | ||||
|       coll = list.channel.toLowerCase(); //.replace(/ /g,''); | ||||
|       if (coll.length == 0) return; | ||||
|       coll = Functions.removeEmojis(coll).toLowerCase(); | ||||
|       coll = Helpers.removeEmojis(coll).toLowerCase(); | ||||
|       //coll = coll.replace(/_/g, ""); | ||||
|  | ||||
|       //coll = filter.clean(coll); | ||||
| @@ -63,18 +76,18 @@ async function skip(list, guid, coll, offline, socket, callback) { | ||||
|     return; | ||||
|   } | ||||
|   list.id = list.id + ""; | ||||
|   var sessionAdminUser = await Functions.getSessionAdminUser( | ||||
|     Functions.getSession(socket), | ||||
|   var sessionAdminUser = await SessionHandler.getSessionAdminUser( | ||||
|     Helpers.getSession(socket), | ||||
|     coll | ||||
|   ); | ||||
|   var userpass = sessionAdminUser.userpass; | ||||
|   var adminpass = sessionAdminUser.adminpass; | ||||
|   var gotten = sessionAdminUser.gotten; | ||||
|   if (adminpass != "" || list.pass == undefined) { | ||||
|     list.pass = Functions.hash_pass(adminpass); | ||||
|     list.pass = Helpers.hash_pass(adminpass); | ||||
|   } else if (list.pass != "") { | ||||
|     list.pass = Functions.hash_pass( | ||||
|       Functions.hash_pass(Functions.decrypt_string(list.pass), true) | ||||
|     list.pass = Helpers.hash_pass( | ||||
|       Helpers.hash_pass(Helpers.decrypt_string(list.pass), true) | ||||
|     ); | ||||
|   } else { | ||||
|     list.pass = ""; | ||||
| @@ -84,7 +97,7 @@ async function skip(list, guid, coll, offline, socket, callback) { | ||||
|   } else { | ||||
|     list.userpass = crypto | ||||
|       .createHash("sha256") | ||||
|       .update(Functions.decrypt_string(list.userpass)) | ||||
|       .update(Helpers.decrypt_string(list.userpass)) | ||||
|       .digest("base64"); | ||||
|   } | ||||
|  | ||||
| @@ -95,24 +108,27 @@ async function skip(list, guid, coll, offline, socket, callback) { | ||||
|       docs[0].userpass == "" || | ||||
|       (list.hasOwnProperty("userpass") && docs[0].userpass == list.userpass)) | ||||
|   ) { | ||||
|     Functions.check_inlist(coll, guid, socket, offline, undefined, "place 12"); | ||||
|     Inlist.check(coll, guid, socket, offline, undefined, "place 12"); | ||||
|  | ||||
|     var video_id; | ||||
|     adminpass = ""; | ||||
|     video_id = list.id; | ||||
|     var err = list.error; | ||||
|     var trueError = await Search.check_if_error_or_blocked( | ||||
|       video_id, | ||||
|       coll, | ||||
|       err == "5" || | ||||
|         err == "100" || | ||||
|         err == "101" || | ||||
|         err == "150" || | ||||
|         err == 5 || | ||||
|         err == 100 || | ||||
|         err == 101 || | ||||
|         err == 150 | ||||
|     ); | ||||
|     var trueError = false; | ||||
|     if (err) { | ||||
|       trueError = await Search.check_if_error_or_blocked( | ||||
|         video_id, | ||||
|         coll, | ||||
|         err == "5" || | ||||
|           err == "100" || | ||||
|           err == "101" || | ||||
|           err == "150" || | ||||
|           err == 5 || | ||||
|           err == 100 || | ||||
|           err == 101 || | ||||
|           err == 150 | ||||
|       ); | ||||
|     } | ||||
|     var error = false; | ||||
|     if (!trueError) { | ||||
|       adminpass = list.pass; | ||||
| @@ -139,16 +155,17 @@ async function skip(list, guid, coll, offline, socket, callback) { | ||||
|               docs[0].skips.length + 1 >= strictSkipNumber)) || | ||||
|             (!strictSkip && | ||||
|               ((frontpage_viewers[0].viewers / 2 <= docs[0].skips.length + 1 && | ||||
|                 !Functions.contains(docs[0].skips, guid) && | ||||
|                 !Helpers.contains(docs[0].skips, guid) && | ||||
|                 frontpage_viewers[0].viewers != 2) || | ||||
|                 (frontpage_viewers[0].viewers == 2 && | ||||
|                   docs[0].skips.length + 1 == 2 && | ||||
|                   !Functions.contains(docs[0].skips, guid)) || | ||||
|                   !Helpers.contains(docs[0].skips, guid)) || | ||||
|                 (docs[0].adminpass == hash && | ||||
|                   docs[0].adminpass !== "" && | ||||
|                   docs[0].skip)))) | ||||
|         ) { | ||||
|           var canContinue = await Functions.checkTimeout( | ||||
|           console.log("here"); | ||||
|           var canContinue = await Timeout.check( | ||||
|             "skip", | ||||
|             1, | ||||
|             coll, | ||||
| @@ -158,10 +175,19 @@ async function skip(list, guid, coll, offline, socket, callback) { | ||||
|             socket, | ||||
|             "The channel is skipping too often, please wait " | ||||
|           ); | ||||
|           console.log("canSkip", canContinue); | ||||
|           if (!canContinue) { | ||||
|             return; | ||||
|           } | ||||
|           change_song(coll, error, video_id, docs); | ||||
|  | ||||
|           try { | ||||
|             await changeSong(coll, error, video_id, docs); | ||||
|           } catch (e) { | ||||
|             socket.emit("toast", "Something went wrong.. Please try again"); | ||||
|             return; | ||||
|           } | ||||
|  | ||||
|           Play.sendPlay(coll); | ||||
|           socket.emit("toast", "skip"); | ||||
|           var docs = await find("user_names", { guid: guid }); | ||||
|           if (docs.length == 1) { | ||||
| @@ -176,7 +202,7 @@ async function skip(list, guid, coll, offline, socket, callback) { | ||||
|               msg: " skipped" | ||||
|             }); | ||||
|           } | ||||
|         } else if (!Functions.contains(docs[0].skips, guid)) { | ||||
|         } else if (!Helpers.contains(docs[0].skips, guid)) { | ||||
|           await update( | ||||
|             coll + "_settings", | ||||
|             { id: "config" }, | ||||
|   | ||||
| @@ -4,8 +4,9 @@ var db = require(pathThumbnails + "/handlers/db.js"); | ||||
| var find = require(pathThumbnails + "/handlers/dbFunctions/find.js"); | ||||
| var create = require(pathThumbnails + "/handlers/dbFunctions/create.js"); | ||||
| var insert = require(pathThumbnails + "/handlers/dbFunctions/insert.js"); | ||||
| var update = require(pathThumbnails + "/handlers/dbFunctions/update.js"); | ||||
|  | ||||
| function checkTimeout( | ||||
| function check( | ||||
|   type, | ||||
|   timeout, | ||||
|   channel, | ||||
| @@ -15,13 +16,12 @@ function checkTimeout( | ||||
|   socket, | ||||
|   error_message | ||||
| ) { | ||||
|   return new Promise((resolve, reject) => { | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     if (conf_pass != "" && conf_pass == this_pass) { | ||||
|       resolve(); | ||||
|       resolve(true); | ||||
|       return; | ||||
|     } | ||||
|     var docs = await find("timeout_api", | ||||
|     { | ||||
|     var docs = await find("timeout_api", { | ||||
|       type: type, | ||||
|       guid: guid | ||||
|     }); | ||||
| @@ -32,12 +32,12 @@ function checkTimeout( | ||||
|  | ||||
|       var retry_in = (date.getTime() - now.getTime()) / 1000; | ||||
|       if (retry_in > 0) { | ||||
|         if (typeof error_callback == "function") { | ||||
|           reject(); | ||||
|         if (!error_message) { | ||||
|           resolve(false); | ||||
|           return; | ||||
|         } else if (error_message) { | ||||
|           var sOrNot = | ||||
|           Math.ceil(retry_in) > 1 || Math.ceil(retry_in) == 0 ? "s" : ""; | ||||
|             Math.ceil(retry_in) > 1 || Math.ceil(retry_in) == 0 ? "s" : ""; | ||||
|           socket.emit( | ||||
|             "toast", | ||||
|             error_message + Math.ceil(retry_in) + " second" + sOrNot + "." | ||||
| @@ -49,18 +49,20 @@ function checkTimeout( | ||||
|       } | ||||
|     } | ||||
|     var now_date = new Date(); | ||||
|     await update("timeout_api", | ||||
|     { type: type, guid: guid }, | ||||
|     { | ||||
|       $set: { | ||||
|         createdAt: now_date, | ||||
|         type: type, | ||||
|         guid: guid | ||||
|       } | ||||
|     }, | ||||
|     { upsert: true }); | ||||
|     resolve(); | ||||
|     await update( | ||||
|       "timeout_api", | ||||
|       { type: type, guid: guid }, | ||||
|       { | ||||
|         $set: { | ||||
|           createdAt: now_date, | ||||
|           type: type, | ||||
|           guid: guid | ||||
|         } | ||||
|       }, | ||||
|       { upsert: true } | ||||
|     ); | ||||
|     resolve(true); | ||||
|   }); | ||||
| } | ||||
|  | ||||
| module.exports.checkTimeout = checkTimeout; | ||||
| module.exports.check = check; | ||||
|   | ||||
| @@ -3,8 +3,11 @@ var mongojs = require("mongojs"); | ||||
| var db = require(pathThumbnails + "/handlers/db.js"); | ||||
|  | ||||
| function aggregate(collection, aggregateObject) { | ||||
|   return new Promise((resolve, reject) => { | ||||
|     db.collection(coll).aggregate(aggregateObject, function(error, results) { | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     db.collection(collection).aggregate(aggregateObject, function( | ||||
|       error, | ||||
|       results | ||||
|     ) { | ||||
|       if (error) { | ||||
|         reject(error); | ||||
|         return; | ||||
| @@ -14,4 +17,4 @@ function aggregate(collection, aggregateObject) { | ||||
|   }); | ||||
| } | ||||
|  | ||||
| module.exports.aggregate = aggregate; | ||||
| module.exports = aggregate; | ||||
|   | ||||
| @@ -3,7 +3,7 @@ var mongojs = require("mongojs"); | ||||
| var db = require(pathThumbnails + "/handlers/db.js"); | ||||
|  | ||||
| async function collection(collection) { | ||||
|   return new Promise((resolve, reject) => { | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     db.createCollection(coll, function(err, docs) { | ||||
|       if (err) { | ||||
|         reject(err); | ||||
| @@ -15,7 +15,7 @@ async function collection(collection) { | ||||
| } | ||||
|  | ||||
| async function index(collection, indexObject, extraObject) { | ||||
|   return new Promise((resolve, reject) => { | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     db.collection(collection).createIndex(indexObject, extraObject, function( | ||||
|       err, | ||||
|       docs | ||||
|   | ||||
| @@ -3,7 +3,7 @@ var mongojs = require("mongojs"); | ||||
| var db = require(pathThumbnails + "/handlers/db.js"); | ||||
|  | ||||
| async function find(collection, searchObject) { | ||||
|   return new Promise((resolve, reject) => { | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     db.collection(collection).find(searchObject, (error, result) => { | ||||
|       if (error) { | ||||
|         reject(error); | ||||
| @@ -14,4 +14,4 @@ async function find(collection, searchObject) { | ||||
|   }); | ||||
| } | ||||
|  | ||||
| module.exports.find = find; | ||||
| module.exports = find; | ||||
|   | ||||
							
								
								
									
										17
									
								
								server/handlers/dbFunctions/findAndModify.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								server/handlers/dbFunctions/findAndModify.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,17 @@ | ||||
| var path = require("path"); | ||||
| var mongojs = require("mongojs"); | ||||
| var db = require(pathThumbnails + "/handlers/db.js"); | ||||
|  | ||||
| async function findAndModify(collection, query) { | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     db.collection(collection).findAndModify(query, function(err, docs) { | ||||
|       if (err) { | ||||
|         reject(err); | ||||
|         return; | ||||
|       } | ||||
|       resolve(docs); | ||||
|     }); | ||||
|   }); | ||||
| } | ||||
|  | ||||
| module.exports = findAndModify; | ||||
| @@ -3,7 +3,7 @@ var mongojs = require("mongojs"); | ||||
| var db = require(pathThumbnails + "/handlers/db.js"); | ||||
|  | ||||
| async function insert(collection, insertObject) { | ||||
|   return new Promise((resolve, reject) => { | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     db.collection(collection).insert(insertObject, function(err, docs) { | ||||
|       if (err) { | ||||
|         reject(err); | ||||
| @@ -14,4 +14,4 @@ async function insert(collection, insertObject) { | ||||
|   }); | ||||
| } | ||||
|  | ||||
| module.exports.insert = insert; | ||||
| module.exports = insert; | ||||
|   | ||||
| @@ -3,7 +3,7 @@ var mongojs = require("mongojs"); | ||||
| var db = require(pathThumbnails + "/handlers/db.js"); | ||||
|  | ||||
| function remove(collection, removeObject) { | ||||
|   return new Promise((resolve, reject) => { | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     db.collection(collection).remove(removeObject, (error, result) => { | ||||
|       if (error) { | ||||
|         reject(error); | ||||
| @@ -14,4 +14,4 @@ function remove(collection, removeObject) { | ||||
|   }); | ||||
| } | ||||
|  | ||||
| module.exports.remove = remove; | ||||
| module.exports = remove; | ||||
|   | ||||
| @@ -2,8 +2,8 @@ var path = require("path"); | ||||
| var mongojs = require("mongojs"); | ||||
| var db = require(pathThumbnails + "/handlers/db.js"); | ||||
|  | ||||
| async function find(collection, searchObject, sortObject) { | ||||
|   return new Promise((resolve, reject) => { | ||||
| async function sort(collection, searchObject, sortObject) { | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     db.collection(collection) | ||||
|       .find(searchObject) | ||||
|       .sort(sortObject, (error, result) => { | ||||
| @@ -16,4 +16,4 @@ async function find(collection, searchObject, sortObject) { | ||||
|   }); | ||||
| } | ||||
|  | ||||
| module.exports.find = find; | ||||
| module.exports = sort; | ||||
|   | ||||
| @@ -2,8 +2,8 @@ var path = require("path"); | ||||
| var mongojs = require("mongojs"); | ||||
| var db = require(pathThumbnails + "/handlers/db.js"); | ||||
|  | ||||
| function update(collection, searchObject, updateObject, extraObject) { | ||||
|   return new Promise((resolve, reject) => { | ||||
| async function update(collection, searchObject, updateObject, extraObject) { | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     db.collection(collection).update( | ||||
|       searchObject, | ||||
|       updateObject, | ||||
| @@ -19,4 +19,4 @@ function update(collection, searchObject, updateObject, extraObject) { | ||||
|   }); | ||||
| } | ||||
|  | ||||
| module.exports.update = update; | ||||
| module.exports = update; | ||||
|   | ||||
							
								
								
									
										174
									
								
								server/handlers/helpers.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										174
									
								
								server/handlers/helpers.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,174 @@ | ||||
| var crypto = require("crypto"); | ||||
| var ColorThief = require("color-thief-jimp"); | ||||
| var Jimp = require("jimp"); | ||||
| var sIO = require(pathThumbnails + "/apps/client.js").socketIO; | ||||
|  | ||||
| function encodeChannelName(str) { | ||||
|   var _fn = encodeURIComponent; | ||||
|   str = filter.clean(str); | ||||
|   var toReturn = _fn(str); | ||||
|   toReturn = toReturn.replace(/_/g, "%5F"); | ||||
|   toReturn = toReturn.replace(/'/g, "%27"); | ||||
|   toReturn = toReturn.replace(/%26amp%3B/g, "%26").replace(/%26amp%3b/g, "%26"); | ||||
|   toReturn = toReturn.toLowerCase(); | ||||
|   return toReturn; | ||||
| } | ||||
|  | ||||
| function decodeChannelName(str) { | ||||
|   var _fn = decodeURIComponent; | ||||
|   str = str.toUpperCase(); | ||||
|   var toReturn = _fn(str.replace(/%5F/g, "_").replace(/%27/g, "'")); | ||||
|   toReturn = filter.clean(toReturn); | ||||
|   return toReturn.toLowerCase(); | ||||
| } | ||||
|  | ||||
| function contains(a, obj) { | ||||
|   try { | ||||
|     var i = a.length; | ||||
|     while (i--) { | ||||
|       if (a[i] === obj) { | ||||
|         return true; | ||||
|       } | ||||
|     } | ||||
|     return false; | ||||
|   } catch (e) { | ||||
|     return false; | ||||
|   } | ||||
| } | ||||
|  | ||||
| function getSession(socket) { | ||||
|   try { | ||||
|     /*var cookieParser = require("cookie-parser"); | ||||
|         var cookie = require("cookie"); | ||||
|         var parsedCookies = cookie.parse(socket.handshake.headers.cookie); | ||||
|         return parsedCookies["_uI"];*/ | ||||
|     if (socket.cookie_id == undefined) throw "Undefined error"; | ||||
|     return socket.cookie_id; | ||||
|   } catch (e) { | ||||
|     // Returning "sessiong"-based on place of connection | ||||
|     return hash_pass( | ||||
|       socket.handshake.headers["user-agent"] + | ||||
|         socket.handshake.address + | ||||
|         socket.handshake.headers["accept-language"] | ||||
|     ); | ||||
|     //return "empty"; | ||||
|   } | ||||
| } | ||||
|  | ||||
| function hash_pass(adminpass, hex) { | ||||
|   if (adminpass == undefined || adminpass == "") return ""; | ||||
|   if (hex) | ||||
|     return crypto | ||||
|       .createHash("sha256") | ||||
|       .update(adminpass) | ||||
|       .digest("hex"); | ||||
|   return crypto | ||||
|     .createHash("sha256") | ||||
|     .update(adminpass) | ||||
|     .digest("base64"); | ||||
| } | ||||
|  | ||||
| function decrypt_string(pw) { | ||||
|   try { | ||||
|     return Buffer.from(pw, "base64").toString("ascii"); | ||||
|   } catch (e) { | ||||
|     return ""; | ||||
|   } | ||||
| } | ||||
|  | ||||
| function removeEmojis(string) { | ||||
|   //https://stackoverflow.com/a/41164278/4266467 | ||||
|   var regex = /(?:[\u2700-\u27bf]|(?:\ud83c[\udde6-\uddff]){2}|[\ud800-\udbff][\udc00-\udfff]|[\u0023-\u0039]\ufe0f?\u20e3|\u3299|\u3297|\u303d|\u3030|\u24c2|\ud83c[\udd70-\udd71]|\ud83c[\udd7e-\udd7f]|\ud83c\udd8e|\ud83c[\udd91-\udd9a]|\ud83c[\udde6-\uddff]|\ud83c[\ude01-\ude02]|\ud83c\ude1a|\ud83c\ude2f|\ud83c[\ude32-\ude3a]|\ud83c[\ude50-\ude51]|\u203c|\u2049|[\u25aa-\u25ab]|\u25b6|\u25c0|[\u25fb-\u25fe]|\u00a9|\u00ae|\u2122|\u2139|\ud83c\udc04|[\u2600-\u26FF]|\u2b05|\u2b06|\u2b07|\u2b1b|\u2b1c|\u2b50|\u2b55|\u231a|\u231b|\u2328|\u23cf|[\u23e9-\u23f3]|[\u23f8-\u23fa]|\ud83c\udccf|\u2934|\u2935|[\u2190-\u21ff])/g; | ||||
|   return string.replace(regex, ""); | ||||
| } | ||||
|  | ||||
| function get_time() { | ||||
|   var d = new Date(); | ||||
|   var time = Math.floor(d.getTime() / 1000); | ||||
|   return time; | ||||
| } | ||||
|  | ||||
| function sendColor(coll, socket, url, ajax, res) { | ||||
|   if (coll != undefined && typeof coll == "string") { | ||||
|     //coll = coll.replace(/ /g,''); | ||||
|   } | ||||
|   if (url.indexOf("://") == -1) | ||||
|     url = "https://img.youtube.com/vi/" + url + "/mqdefault.jpg"; | ||||
|   //var url = 'https://img.youtube.com/vi/'+id+'/mqdefault.jpg'; | ||||
|  | ||||
|   Jimp.read(url) | ||||
|     .then(function(image) { | ||||
|       var c = ColorThief.getColor(image); | ||||
|       if (ajax) { | ||||
|         res.header({ "Content-Type": "application/json" }); | ||||
|         res.status(200).send(c); | ||||
|         return; | ||||
|       } else { | ||||
|         if (socket) { | ||||
|           socket.emit("color", { color: c, only: true }); | ||||
|         } else { | ||||
|           io.to(coll).emit("color", { color: c, only: false }); | ||||
|         } | ||||
|       } | ||||
|     }) | ||||
|     .catch(function(err) { | ||||
|       console.log("Crashed on fetching image, url is " + url); | ||||
|       console.log("Is ajax: " + ajax); | ||||
|       if (ajax) { | ||||
|         res.header({ "Content-Type": "application/json" }); | ||||
|         res.status(404); | ||||
|         return; | ||||
|       } | ||||
|     }); | ||||
| } | ||||
|  | ||||
| function rndName(seed, len) { | ||||
|   var vowels = ["a", "e", "i", "o", "u"]; | ||||
|   consts = [ | ||||
|     "b", | ||||
|     "c", | ||||
|     "d", | ||||
|     "f", | ||||
|     "g", | ||||
|     "h", | ||||
|     "j", | ||||
|     "k", | ||||
|     "l", | ||||
|     "m", | ||||
|     "n", | ||||
|     "p", | ||||
|     "r", | ||||
|     "s", | ||||
|     "t", | ||||
|     "v", | ||||
|     "w", | ||||
|     "x", | ||||
|     "y" | ||||
|   ]; | ||||
|   len = Math.floor(len); | ||||
|   word = ""; | ||||
|   is_vowel = false; | ||||
|   var arr; | ||||
|   try { | ||||
|     for (var i = 0; i < len; i++) { | ||||
|       if (is_vowel) arr = vowels; | ||||
|       else arr = consts; | ||||
|       is_vowel = !is_vowel; | ||||
|       word += arr[(seed[i % seed.length].charCodeAt() + i) % (arr.length - 1)]; | ||||
|     } | ||||
|   } catch (e) { | ||||
|     return rndName(uniqid.time().toLowerCase(), len); | ||||
|   } | ||||
|   return word; | ||||
| } | ||||
|  | ||||
| module.exports.rndName = rndName; | ||||
| module.exports.contains = contains; | ||||
| module.exports.sendColor = sendColor; | ||||
| module.exports.encodeChannelName = encodeChannelName; | ||||
| module.exports.decodeChannelName = decodeChannelName; | ||||
| module.exports.getSession = getSession; | ||||
| module.exports.hash_pass = hash_pass; | ||||
| module.exports.decrypt_string = decrypt_string; | ||||
| module.exports.removeEmojis = removeEmojis; | ||||
| module.exports.get_time = get_time; | ||||
| @@ -9,6 +9,10 @@ var Suggestions = require(pathThumbnails + "/handlers/suggestions.js"); | ||||
| var ListSettings = require(pathThumbnails + "/handlers/list_settings.js"); | ||||
| var Frontpage = require(pathThumbnails + "/handlers/frontpage.js"); | ||||
| var Search = require(pathThumbnails + "/handlers/search.js"); | ||||
| var Join = require(pathThumbnails + | ||||
|   "/handlers/dbFunctions/advancedFunctions/joinList.js"); | ||||
| var Skip = require(pathThumbnails + | ||||
|   "/handlers/dbFunctions/advancedFunctions/skip.js"); | ||||
| var crypto = require("crypto"); | ||||
| var Filter = require("bad-words"); | ||||
| var filter = new Filter({ placeHolder: "x" }); | ||||
| @@ -187,24 +191,8 @@ module.exports = function() { | ||||
|     }); | ||||
|  | ||||
|     socket.on("error_video", function(msg) { | ||||
|       try { | ||||
|         msg.channel = Functions.encodeChannelName(msg.channel); | ||||
|         var _list = msg.channel; //.replace(/ /g,''); | ||||
|         if (_list.length == 0) return; | ||||
|         if (_list.indexOf("?") > -1) { | ||||
|           _list = _list.substring(0, _list.indexOf("?")); | ||||
|           msg.channel = _list; | ||||
|         } | ||||
|         coll = Functions.removeEmojis(_list).toLowerCase(); | ||||
|         //coll = coll.replace(/_/g, ""); | ||||
|  | ||||
|         //coll = filter.clean(coll); | ||||
|       } catch (e) { | ||||
|         return; | ||||
|       } | ||||
|       if (msg.hasOwnProperty("channel")) { | ||||
|         msg.channel = Functions.encodeChannelName(msg.channel); | ||||
|       } | ||||
|       msg = middleware(msg); | ||||
|       coll = msg.channel; | ||||
|       Search.check_error_video(msg, coll); | ||||
|     }); | ||||
|  | ||||
| @@ -240,57 +228,32 @@ module.exports = function() { | ||||
|     }); | ||||
|  | ||||
|     socket.on("suggest_thumbnail", function(msg) { | ||||
|       if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { | ||||
|         var _list = msg.channel.substring(0, msg.channel.indexOf("?")); | ||||
|         msg.channel = _list; | ||||
|       } | ||||
|       if (msg.hasOwnProperty("channel")) { | ||||
|         msg.channel = Functions.encodeChannelName(msg.channel); | ||||
|       } | ||||
|       msg = middleware(msg); | ||||
|       coll = msg.channel; | ||||
|       Suggestions.thumbnail(msg, coll, guid, offline, socket); | ||||
|     }); | ||||
|  | ||||
|     socket.on("suggest_description", function(msg) { | ||||
|       if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { | ||||
|         var _list = msg.channel.substring(0, msg.channel.indexOf("?")); | ||||
|         msg.channel = _list; | ||||
|       } | ||||
|       if (msg.hasOwnProperty("channel")) { | ||||
|         msg.channel = Functions.encodeChannelName(msg.channel); | ||||
|       } | ||||
|       msg = middleware(msg); | ||||
|       coll = msg.channel; | ||||
|       Suggestions.description(msg, coll, guid, offline, socket); | ||||
|     }); | ||||
|  | ||||
|     socket.on("suggest_rules", function(msg) { | ||||
|       if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { | ||||
|         var _list = msg.channel.substring(0, msg.channel.indexOf("?")); | ||||
|         msg.channel = _list; | ||||
|       } | ||||
|       if (msg.hasOwnProperty("channel")) { | ||||
|         msg.channel = Functions.encodeChannelName(msg.channel); | ||||
|       } | ||||
|       msg = middleware(msg); | ||||
|       coll = msg.channel; | ||||
|       Suggestions.rules(msg, coll, guid, offline, socket); | ||||
|     }); | ||||
|  | ||||
|     socket.on("namechange", function(msg) { | ||||
|       if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { | ||||
|         var _list = msg.channel.substring(0, msg.channel.indexOf("?")); | ||||
|         msg.channel = _list; | ||||
|       } | ||||
|       if (msg.hasOwnProperty("channel")) { | ||||
|         msg.channel = Functions.encodeChannelName(msg.channel); | ||||
|       } | ||||
|       msg = middleware(msg); | ||||
|       coll = msg.channel; | ||||
|       Chat.namechange(msg, guid, socket); | ||||
|     }); | ||||
|  | ||||
|     socket.on("removename", function(msg) { | ||||
|       if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { | ||||
|         var _list = msg.channel.substring(0, msg.channel.indexOf("?")); | ||||
|         msg.channel = _list; | ||||
|       } | ||||
|       if (msg.hasOwnProperty("channel")) { | ||||
|         msg.channel = Functions.encodeChannelName(msg.channel); | ||||
|       } | ||||
|       msg = middleware(msg); | ||||
|       coll = msg.channel; | ||||
|       if (typeof msg != "object" || !msg.hasOwnProperty("channel")) { | ||||
|         var result = { | ||||
|           channel: { | ||||
| @@ -305,13 +268,8 @@ module.exports = function() { | ||||
|     }); | ||||
|  | ||||
|     socket.on("offline", function(msg) { | ||||
|       if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { | ||||
|         var _list = msg.channel.substring(0, msg.channel.indexOf("?")); | ||||
|         msg.channel = _list; | ||||
|       } | ||||
|       if (msg.hasOwnProperty("channel")) { | ||||
|         msg.channel = Functions.encodeChannelName(msg.channel); | ||||
|       } | ||||
|       msg = middleware(msg); | ||||
|       coll = msg.channel; | ||||
|       if ( | ||||
|         !msg.hasOwnProperty("status") || | ||||
|         !msg.hasOwnProperty("channel") || | ||||
| @@ -408,13 +366,8 @@ module.exports = function() { | ||||
|     }); | ||||
|  | ||||
|     socket.on("get_history", function(msg) { | ||||
|       if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { | ||||
|         var _list = msg.channel.substring(0, msg.channel.indexOf("?")); | ||||
|         msg.channel = _list; | ||||
|       } | ||||
|       if (msg.hasOwnProperty("channel")) { | ||||
|         msg.channel = Functions.encodeChannelName(msg.channel); | ||||
|       } | ||||
|       msg = middleware(msg); | ||||
|       coll = msg.channel; | ||||
|       if ( | ||||
|         !msg.hasOwnProperty("channel") || | ||||
|         !msg.hasOwnProperty("all") || | ||||
| @@ -442,46 +395,26 @@ module.exports = function() { | ||||
|     }); | ||||
|  | ||||
|     socket.on("chat", function(msg) { | ||||
|       if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { | ||||
|         var _list = msg.channel.substring(0, msg.channel.indexOf("?")); | ||||
|         msg.channel = _list; | ||||
|       } | ||||
|       if (msg.hasOwnProperty("channel")) { | ||||
|         msg.channel = Functions.encodeChannelName(msg.channel); | ||||
|       } | ||||
|       msg = middleware(msg); | ||||
|       coll = msg.channel; | ||||
|       Chat.chat(msg, guid, offline, socket); | ||||
|     }); | ||||
|  | ||||
|     socket.on("all,chat", function(data) { | ||||
|       if (data.hasOwnProperty("channel") && data.channel.indexOf("?") > -1) { | ||||
|         var _list = data.channel.substring(0, data.channel.indexOf("?")); | ||||
|         data.channel = _list; | ||||
|       } | ||||
|       if (data.hasOwnProperty("channel")) { | ||||
|         data.channel = Functions.encodeChannelName(data.channel); | ||||
|       } | ||||
|       Chat.all_chat(data, guid, offline, socket); | ||||
|     socket.on("all,chat", function(msg) { | ||||
|       msg = middleware(msg); | ||||
|       coll = msg.channel; | ||||
|       Chat.all_chat(msg, guid, offline, socket); | ||||
|     }); | ||||
|  | ||||
|     socket.on("frontpage_lists", function(msg) { | ||||
|       if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { | ||||
|         var _list = msg.channel.substring(0, msg.channel.indexOf("?")); | ||||
|         msg.channel = _list; | ||||
|       } | ||||
|       if (msg.hasOwnProperty("channel")) { | ||||
|         msg.channel = Functions.encodeChannelName(msg.channel); | ||||
|       } | ||||
|       msg = middleware(msg); | ||||
|       coll = msg.channel; | ||||
|       Frontpage.frontpage_lists(msg, socket); | ||||
|     }); | ||||
|  | ||||
|     socket.on("import_zoff", function(msg) { | ||||
|       if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { | ||||
|         var _list = msg.channel.substring(0, msg.channel.indexOf("?")); | ||||
|         msg.channel = _list; | ||||
|       } | ||||
|       if (msg.hasOwnProperty("channel")) { | ||||
|         msg.channel = Functions.encodeChannelName(msg.channel); | ||||
|       } | ||||
|       msg = middleware(msg); | ||||
|       coll = msg.channel; | ||||
|       ListChange.addFromOtherList(msg, guid, offline, socket); | ||||
|     }); | ||||
|  | ||||
| @@ -489,42 +422,25 @@ module.exports = function() { | ||||
|       List.now_playing(list, fn, socket); | ||||
|     }); | ||||
|  | ||||
|     socket.on("id", function(arr) { | ||||
|       if (arr.hasOwnProperty("channel") && arr.channel.indexOf("?") > -1) { | ||||
|         var _list = arr.channel.substring(0, arr.channel.indexOf("?")); | ||||
|         arr.channel = _list; | ||||
|       } | ||||
|       if (arr.hasOwnProperty("channel")) { | ||||
|         arr.channel = Functions.encodeChannelName(arr.channel); | ||||
|       } | ||||
|       if (typeof arr == "object") | ||||
|         io.to(arr.id).emit(arr.id.toLowerCase(), { | ||||
|           type: arr.type, | ||||
|           value: arr.value | ||||
|     socket.on("id", function(msg) { | ||||
|       msg = middleware(msg); | ||||
|       coll = msg.channel; | ||||
|       if (typeof msg == "object") | ||||
|         io.to(msg.id).emit(msg.id.toLowerCase(), { | ||||
|           type: msg.type, | ||||
|           value: msg.value | ||||
|         }); | ||||
|     }); | ||||
|  | ||||
|     socket.on("join_silent", function(msg) { | ||||
|         if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { | ||||
|           var _list = msg.channel.substring(0, msg.channel.indexOf("?")); | ||||
|           msg.channel = _list; | ||||
|         } | ||||
|         if (msg.hasOwnProperty("channel")) { | ||||
|           msg.channel = Functions.encodeChannelName(msg.channel); | ||||
|       } else { | ||||
|           return; | ||||
|       } | ||||
|       msg = middleware(msg); | ||||
|       coll = msg.channel; | ||||
|       List.join_silent(msg, socket); | ||||
|     }); | ||||
|  | ||||
|     socket.on("list", function(msg) { | ||||
|       if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { | ||||
|         var _list = msg.channel.substring(0, msg.channel.indexOf("?")); | ||||
|         msg.channel = _list; | ||||
|       } | ||||
|       if (msg.hasOwnProperty("channel")) { | ||||
|         msg.channel = Functions.encodeChannelName(msg.channel); | ||||
|       } | ||||
|       msg = middleware(msg); | ||||
|       coll = msg.channel; | ||||
|       try { | ||||
|         //var _list = msg.channel.replace(/ /g,''); | ||||
|         var _list = msg.channel; | ||||
| @@ -544,7 +460,7 @@ module.exports = function() { | ||||
|       if (msg.hasOwnProperty("offline") && msg.offline) { | ||||
|         offline = true; | ||||
|       } | ||||
|       List.list(msg, guid, coll, offline, socket); | ||||
|       Join.joinList(msg, guid, coll, offline, socket); | ||||
|       Functions.get_short_id(socket); | ||||
|     }); | ||||
|  | ||||
| @@ -570,15 +486,10 @@ module.exports = function() { | ||||
|       List.end(obj, coll, guid, offline, socket); | ||||
|     }); | ||||
|  | ||||
|     socket.on("addPlaylist", function(arr) { | ||||
|       if (arr.hasOwnProperty("channel") && arr.channel.indexOf("?") > -1) { | ||||
|         var _list = arr.channel.substring(0, arr.channel.indexOf("?")); | ||||
|         arr.channel = _list; | ||||
|       } | ||||
|       if (arr.hasOwnProperty("channel")) { | ||||
|         arr.channel = Functions.encodeChannelName(arr.channel); | ||||
|       } | ||||
|       ListChange.addPlaylist(arr, guid, offline, socket); | ||||
|     socket.on("addPlaylist", function(msg) { | ||||
|       msg = middleware(msg); | ||||
|       coll = msg.channel; | ||||
|       ListChange.addPlaylist(msg, guid, offline, socket); | ||||
|     }); | ||||
|  | ||||
|     socket.on("add", function(arr) { | ||||
| @@ -612,13 +523,8 @@ module.exports = function() { | ||||
|  | ||||
|     socket.on("delete_all", function(msg) { | ||||
|       try { | ||||
|         if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { | ||||
|           var _list = msg.channel.substring(0, msg.channel.indexOf("?")); | ||||
|           msg.channel = _list; | ||||
|         } | ||||
|         if (msg.hasOwnProperty("channel")) { | ||||
|           msg.channel = Functions.encodeChannelName(msg.channel); | ||||
|         } | ||||
|         msg = middleware(msg); | ||||
|         coll = msg.channel; | ||||
|         coll = msg.channel.toLowerCase(); //.replace(/ /g,''); | ||||
|         if (coll.length == 0) return; | ||||
|         coll = Functions.removeEmojis(coll).toLowerCase(); | ||||
| @@ -633,13 +539,8 @@ module.exports = function() { | ||||
|     }); | ||||
|  | ||||
|     socket.on("vote", function(msg) { | ||||
|       if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { | ||||
|         var _list = msg.channel.substring(0, msg.channel.indexOf("?")); | ||||
|         msg.channel = _list; | ||||
|       } | ||||
|       if (msg.hasOwnProperty("channel")) { | ||||
|         msg.channel = Functions.encodeChannelName(msg.channel); | ||||
|       } | ||||
|       msg = middleware(msg); | ||||
|       coll = msg.channel; | ||||
|       if (coll !== undefined) { | ||||
|         try { | ||||
|           coll = msg.channel.toLowerCase(); //.replace(/ /g,''); | ||||
| @@ -655,53 +556,30 @@ module.exports = function() { | ||||
|       ListChange.voteUndecided(msg, coll, guid, offline, socket); | ||||
|     }); | ||||
|  | ||||
|     socket.on("password", function(inp) { | ||||
|       if (inp.hasOwnProperty("channel") && inp.channel.indexOf("?") > -1) { | ||||
|         var _list = inp.channel.substring(0, inp.channel.indexOf("?")); | ||||
|         inp.channel = _list; | ||||
|       } | ||||
|       if (inp.hasOwnProperty("channel")) { | ||||
|         inp.channel = Functions.encodeChannelName(inp.channel); | ||||
|       } | ||||
|     socket.on("password", function(msg) { | ||||
|       msg = middleware(msg); | ||||
|       coll = msg.channel; | ||||
|       //if(coll != undefined) coll.replace(/ /g,''); | ||||
|       ListSettings.password(inp, coll, guid, offline, socket); | ||||
|       ListSettings.password(msg, coll, guid, offline, socket); | ||||
|     }); | ||||
|  | ||||
|     socket.on("skip", function(list) { | ||||
|       if (list.hasOwnProperty("channel") && list.channel.indexOf("?") > -1) { | ||||
|         var _list = list.channel.substring(0, list.channel.indexOf("?")); | ||||
|         list.channel = _list; | ||||
|         coll = list.channel; | ||||
|       } | ||||
|       if (list.hasOwnProperty("channel")) { | ||||
|         list.channel = Functions.encodeChannelName(list.channel); | ||||
|       } | ||||
|     socket.on("skip", function(msg) { | ||||
|       msg = middleware(msg); | ||||
|       coll = msg.channel; | ||||
|       //if(coll != undefined) coll.replace(/ /g,''); | ||||
|       List.skip(list, guid, coll, offline, socket); | ||||
|       Skip.skip(msg, guid, coll, offline, socket); | ||||
|     }); | ||||
|  | ||||
|     socket.on("conf", function(conf) { | ||||
|       if (conf.hasOwnProperty("channel") && conf.channel.indexOf("?") > -1) { | ||||
|         var _list = conf.channel.substring(0, conf.channel.indexOf("?")); | ||||
|         conf.channel = _list; | ||||
|         coll = conf.channel; | ||||
|       } | ||||
|       if (conf.hasOwnProperty("channel")) { | ||||
|         conf.channel = Functions.encodeChannelName(conf.channel); | ||||
|         coll = conf.channel; | ||||
|       } | ||||
|     socket.on("conf", function(msg) { | ||||
|       msg = middleware(msg); | ||||
|       coll = msg.channel; | ||||
|       //if(coll != undefined) coll.replace(/ /g,''); | ||||
|       ListSettings.conf_function(conf, coll, guid, offline, socket); | ||||
|     }); | ||||
|  | ||||
|     socket.on("shuffle", function(msg) { | ||||
|       if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { | ||||
|         var _list = msg.channel.substring(0, msg.channel.indexOf("?")); | ||||
|         msg.channel = _list; | ||||
|       } | ||||
|       if (msg.hasOwnProperty("channel")) { | ||||
|         msg.channel = Functions.encodeChannelName(msg.channel); | ||||
|       } | ||||
|       msg = middleware(msg); | ||||
|       coll = msg.channel; | ||||
|       try { | ||||
|         coll = msg.channel.toLowerCase(); //.replace(/ /g,''); | ||||
|         if (coll.length == 0) return; | ||||
| @@ -933,6 +811,17 @@ module.exports = function() { | ||||
|   //send_ping(); | ||||
| }; | ||||
|  | ||||
| function middleware(msg) { | ||||
|   if (msg.hasOwnProperty("channel") && msg.channel.indexOf("?") > -1) { | ||||
|     var _list = list.channel.substring(0, msg.channel.indexOf("?")); | ||||
|     msg.channel = _list; | ||||
|   } | ||||
|   if (msg.hasOwnProperty("channel")) { | ||||
|     msg.channel = Functions.encodeChannelName(msg.channel); | ||||
|   } | ||||
|   return msg; | ||||
| } | ||||
|  | ||||
| /* | ||||
| function send_ping() { | ||||
| db.collection("connected_users").update({users: {$exists: true}}, {$set: {users: []}}, {multi: true}, function(err, docs){ | ||||
|   | ||||
| @@ -1,6 +1,9 @@ | ||||
| var ColorThief = require("color-thief-jimp"); | ||||
| var Jimp = require("jimp"); | ||||
| var Functions = require(pathThumbnails + "/handlers/functions.js"); | ||||
|  | ||||
| var AdvancedFunctions = require(pathThumbnails + | ||||
|   "/handlers/dbFunctions/advancedFunctions/joinList.js"); | ||||
| var Frontpage = require(pathThumbnails + "/handlers/frontpage.js"); | ||||
| var projects = require(pathThumbnails + "/handlers/aggregates.js"); | ||||
| var crypto = require("crypto"); | ||||
| @@ -33,216 +36,46 @@ function now_playing(list, fn, socket) { | ||||
| } | ||||
|  | ||||
| function join_silent(msg, socket) { | ||||
|     if (typeof msg === "object" && msg !== undefined && msg !== null) { | ||||
|       var channelName = msg.channel; | ||||
|       var tryingPassword = false; | ||||
|       var password = ""; | ||||
|       if(msg.password != "") { | ||||
|         tryingPassword = true; | ||||
|         password = Functions.decrypt_string(msg.password); | ||||
|         password = crypto | ||||
|                  .createHash("sha256") | ||||
|                  .update(password) | ||||
|                  .digest("base64"); | ||||
|       } | ||||
|  | ||||
|       channelName = channelName.toLowerCase(); //.replace(/ /g,''); | ||||
|       channelName = Functions.removeEmojis(channelName).toLowerCase(); | ||||
|       db.collection(channelName + "_settings").find(function(err, docs) { | ||||
|         if(docs.length == 0) { | ||||
|           socket.emit("join_silent_declined", ""); | ||||
|           return; | ||||
|         } | ||||
|         if(docs[0].userpass == "" || docs[0].userpass == undefined || docs[0].userpass == password) { | ||||
|           socket.join(channelName); | ||||
|           socket.emit("join_silent_accepted", ""); | ||||
|  | ||||
|           send_play(channelName, socket); | ||||
|         } else { | ||||
|           socket.emit("join_silent_declined", ""); | ||||
|         } | ||||
|       }); | ||||
|     } else { | ||||
|         return; | ||||
|   if (typeof msg === "object" && msg !== undefined && msg !== null) { | ||||
|     var channelName = msg.channel; | ||||
|     var tryingPassword = false; | ||||
|     var password = ""; | ||||
|     if (msg.password != "") { | ||||
|       tryingPassword = true; | ||||
|       password = Functions.decrypt_string(msg.password); | ||||
|       password = crypto | ||||
|         .createHash("sha256") | ||||
|         .update(password) | ||||
|         .digest("base64"); | ||||
|     } | ||||
|  | ||||
|     channelName = channelName.toLowerCase(); //.replace(/ /g,''); | ||||
|     channelName = Functions.removeEmojis(channelName).toLowerCase(); | ||||
|     db.collection(channelName + "_settings").find(function(err, docs) { | ||||
|       if (docs.length == 0) { | ||||
|         socket.emit("join_silent_declined", ""); | ||||
|         return; | ||||
|       } | ||||
|       if ( | ||||
|         docs[0].userpass == "" || | ||||
|         docs[0].userpass == undefined || | ||||
|         docs[0].userpass == password | ||||
|       ) { | ||||
|         socket.join(channelName); | ||||
|         socket.emit("join_silent_accepted", ""); | ||||
|  | ||||
|         send_play(channelName, socket); | ||||
|       } else { | ||||
|         socket.emit("join_silent_declined", ""); | ||||
|       } | ||||
|     }); | ||||
|   } else { | ||||
|     return; | ||||
|   } | ||||
| } | ||||
|  | ||||
| function list(msg, guid, coll, offline, socket) { | ||||
|   var socketid = socket.zoff_id; | ||||
|   if (typeof msg === "object" && msg !== undefined && msg !== null) { | ||||
|     Functions.getSessionAdminUser(Functions.getSession(socket), coll, function( | ||||
|       userpass, | ||||
|       adminpass, | ||||
|       gotten | ||||
|     ) { | ||||
|       if (gotten && userpass != "" && !msg.hasOwnProperty("pass")) { | ||||
|         msg.pass = userpass; | ||||
|       } else { | ||||
|         msg.pass = crypto | ||||
|           .createHash("sha256") | ||||
|           .update(Functions.decrypt_string(msg.pass)) | ||||
|           .digest("base64"); | ||||
|       } | ||||
|       adminpass = Functions.hash_pass(adminpass); | ||||
|       if ( | ||||
|         !msg.hasOwnProperty("version") || | ||||
|         !msg.hasOwnProperty("channel") || | ||||
|         msg.version != VERSION || | ||||
|         msg.version == undefined || | ||||
|         typeof msg.channel != "string" | ||||
|       ) { | ||||
|         var result = { | ||||
|           channel: { | ||||
|             expected: "string", | ||||
|             got: msg.hasOwnProperty("channel") ? typeof msg.channel : undefined | ||||
|           }, | ||||
|           version: { | ||||
|             expected: VERSION, | ||||
|             got: msg.version | ||||
|           }, | ||||
|           pass: { | ||||
|             expected: "string", | ||||
|             got: msg.hasOwnProperty("pass") ? typeof msg.pass : undefined | ||||
|           } | ||||
|         }; | ||||
|         socket.emit("update_required", result); | ||||
|         return; | ||||
|       } | ||||
|       coll = msg.channel.toLowerCase(); //.replace(/ /g,''); | ||||
|       coll = Functions.removeEmojis(coll).toLowerCase(); | ||||
|       //coll = filter.clean(coll); | ||||
|       var pass = msg.pass; | ||||
|       db.collection("frontpage_lists").find({ _id: coll }, function( | ||||
|         err, | ||||
|         frontpage_lists | ||||
|       ) { | ||||
|         if (frontpage_lists.length == 1) { | ||||
|           db.collection(coll + "_settings").find(function(err, docs) { | ||||
|             if ( | ||||
|               docs.length == 0 || | ||||
|               (docs.length > 0 && | ||||
|                 (docs[0].userpass == undefined || | ||||
|                   docs[0].userpass == "" || | ||||
|                   docs[0].userpass == pass)) | ||||
|             ) { | ||||
|               if ( | ||||
|                 docs.length > 0 && | ||||
|                 docs[0].hasOwnProperty("userpass") && | ||||
|                 docs[0].userpass != "" && | ||||
|                 docs[0].userpass == pass | ||||
|               ) { | ||||
|                 Functions.setSessionUserPass( | ||||
|                   Functions.getSession(socket), | ||||
|                   msg.pass, | ||||
|                   coll, | ||||
|                   function() {} | ||||
|                 ); | ||||
|                 socket.emit("auth_accepted", { value: true }); | ||||
|               } | ||||
|               if (docs.length > 0 && docs[0].userpass != pass) { | ||||
|                 Functions.setSessionUserPass( | ||||
|                   Functions.getSession(socket), | ||||
|                   "", | ||||
|                   coll, | ||||
|                   function() {} | ||||
|                 ); | ||||
|               } | ||||
|               if ( | ||||
|                 docs.length > 0 && | ||||
|                 docs[0].hasOwnProperty("adminpass") && | ||||
|                 docs[0].adminpass != "" && | ||||
|                 docs[0].adminpass == adminpass | ||||
|               ) { | ||||
|                 socket.emit("pw", true); | ||||
|               } | ||||
|               in_list = true; | ||||
|               socket.join(coll); | ||||
|               Functions.check_inlist( | ||||
|                 coll, | ||||
|                 guid, | ||||
|                 socket, | ||||
|                 offline, | ||||
|                 undefined, | ||||
|                 "place 10" | ||||
|               ); | ||||
|  | ||||
|               if (frontpage_lists[0].viewers != undefined) { | ||||
|                 io.to(coll).emit("viewers", frontpage_lists[0].viewers); | ||||
|               } else { | ||||
|                 io.to(coll).emit("viewers", 1); | ||||
|               } | ||||
|  | ||||
|               send_list(coll, socket, true, false, true); | ||||
|             } else { | ||||
|               socket.emit("auth_required"); | ||||
|             } | ||||
|           }); | ||||
|         } else { | ||||
|           db.createCollection(coll, function(err, docs) { | ||||
|             db.collection(coll).createIndex( | ||||
|               { id: 1 }, | ||||
|               { unique: true }, | ||||
|               function(e, d) { | ||||
|                 var configs = { | ||||
|                   addsongs: false, | ||||
|                   adminpass: "", | ||||
|                   allvideos: true, | ||||
|                   frontpage: true, | ||||
|                   longsongs: false, | ||||
|                   removeplay: false, | ||||
|                   shuffle: true, | ||||
|                   skip: false, | ||||
|                   skips: [], | ||||
|                   startTime: Functions.get_time(), | ||||
|                   views: [], | ||||
|                   vote: false, | ||||
|                   description: "", | ||||
|                   thumbnail: "", | ||||
|                   rules: "", | ||||
|                   userpass: "", | ||||
|                   id: "config", | ||||
|                   toggleChat: true | ||||
|                 }; | ||||
|                 db.collection(coll + "_settings").insert(configs, function( | ||||
|                   err, | ||||
|                   docs | ||||
|                 ) { | ||||
|                   socket.join(coll); | ||||
|                   send_list(coll, socket, true, false, true); | ||||
|                   db.collection("frontpage_lists").insert( | ||||
|                     { | ||||
|                       _id: coll, | ||||
|                       count: 0, | ||||
|                       frontpage: true, | ||||
|                       accessed: Functions.get_time(), | ||||
|                       viewers: 1 | ||||
|                     }, | ||||
|                     function(e, d) {} | ||||
|                   ); | ||||
|                   Functions.check_inlist( | ||||
|                     coll, | ||||
|                     guid, | ||||
|                     socket, | ||||
|                     offline, | ||||
|                     undefined, | ||||
|                     "place 11" | ||||
|                   ); | ||||
|                 }); | ||||
|               } | ||||
|             ); | ||||
|           }); | ||||
|         } | ||||
|       }); | ||||
|     }); | ||||
|   } else { | ||||
|     var result = { | ||||
|       msg: { | ||||
|         expected: "object", | ||||
|         got: typeof msg | ||||
|       } | ||||
|     }; | ||||
|     socket.emit("update_required", result); | ||||
|   } | ||||
|   AdvancedFunctions.joinList(msg, guid, coll, offline, socket); | ||||
| } | ||||
|  | ||||
| function skip(list, guid, coll, offline, socket, callback) { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user