helpers.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. Template.admin.helpers({
  2. queueCount: function () {
  3. return Queues.find().count();
  4. },
  5. genreQueue: function(type) {
  6. if (type === "none") {
  7. return Queues.find({genres: []}).count();
  8. } else {
  9. return Queues.find({genres: type}).count();
  10. }
  11. },
  12. alertsList: function() {
  13. return Alerts.find({});
  14. },
  15. queues: function () {
  16. var queues = Queues.find({}).fetch();
  17. return queues;
  18. },
  19. usersOnline: function () {
  20. Meteor.call("getUserNum", function (err, num) {
  21. if (err) {
  22. console.log(err);
  23. }
  24. Session.set("userNum", num);
  25. });
  26. return Session.get("userNum");
  27. },
  28. roomUserNum: function () {
  29. var type = this.type;
  30. var userNum = Rooms.findOne({type: type}).users;
  31. return userNum;
  32. },
  33. allUsers: function () {
  34. Meteor.call("getTotalUsers", function (err, num) {
  35. Session.set("allUsers", num);
  36. })
  37. return Session.get("allUsers");
  38. },
  39. playlists: function () {
  40. var playlists = Playlists.find({}).fetch();
  41. playlists.map(function (playlist) {
  42. if (Rooms.find({type: playlist.type}).count() !== 1) {
  43. return;
  44. } else {
  45. playlist.display = Rooms.findOne({type: playlist.type}).display;
  46. return playlist;
  47. }
  48. });
  49. return playlists;
  50. },
  51. reportsCount: function (room) {
  52. room = room.toLowerCase();
  53. var reports = Reports.findOne({room: room});
  54. return reports && "report" in reports ? reports.report.length : 0;
  55. }
  56. });
  57. Template.alerts.helpers({
  58. alerts: function () {
  59. return Alerts.find({active: true});
  60. }
  61. });
  62. Template.banned.helpers({
  63. bannedAt: function () {
  64. if (Session.get("ban") !== undefined) {
  65. return Session.get("ban").bannedAt;
  66. }
  67. },
  68. bannedBy: function () {
  69. if (Session.get("ban") !== undefined) {
  70. return Session.get("ban").bannedBy;
  71. }
  72. },
  73. bannedUntil: function () {
  74. if (Session.get("ban") !== undefined) {
  75. return Session.get("ban").bannedUntil;
  76. }
  77. },
  78. bannedReason: function () {
  79. if (Session.get("ban") !== undefined) {
  80. return Session.get("ban").bannedReason;
  81. }
  82. }
  83. });
  84. Template.feedback.helpers({
  85. feedback: function () {
  86. return Feedback.find().fetch().reverse();
  87. }
  88. })
  89. Template.header.helpers({
  90. userId: function () {
  91. return Meteor.userId();
  92. }
  93. });
  94. Template.home.helpers({
  95. currentSong: function () {
  96. var type = this.type;
  97. var room = Rooms.findOne({type: type});
  98. if (room !== undefined) {
  99. return room.currentSong;
  100. } else {
  101. return false;
  102. }
  103. },
  104. userNum: function () {
  105. var type = this.type;
  106. var userNum = Rooms.findOne({type: type}).users;
  107. return userNum;
  108. },
  109. currentPrivateSong: function () {
  110. var name = this.name;
  111. var room = PrivateRooms.findOne({name: name});
  112. if (room !== undefined) {
  113. return room.currentSong;
  114. } else {
  115. return false;
  116. }
  117. },
  118. userPrivateNum: function () {
  119. var name = this.name;
  120. var userNum = PrivateRooms.findOne({name: name}).users;
  121. return userNum;
  122. }
  123. });
  124. Template.playlist.helpers({
  125. playlist_songs: function () {
  126. var songIDs = Playlists.find({"type": Session.get("type")}).fetch()[0].songs
  127. var data = [];
  128. songIDs.forEach(function(id){
  129. var song = Songs.findOne({"mid": id});
  130. data.push(song);
  131. })
  132. if (data !== undefined) {
  133. data.map(function (song) {
  134. if (Session.get("currentSong") !== undefined && song.mid === Session.get("currentSong").mid) {
  135. song.current = true;
  136. } else {
  137. song.current = false;
  138. }
  139. return song;
  140. });
  141. return data;
  142. } else {
  143. return [];
  144. }
  145. },
  146. nextSong: function(){
  147. var song;
  148. var data = Playlists.find({"type": Session.get("type")}).fetch()[0].songs;
  149. for(var i = 0; i < data.length; i++){
  150. if(Session.get("currentSong") !== undefined && data[i] === Session.get("currentSong").mid){
  151. if(i === data.length - 1){
  152. song = Songs.findOne({"mid": data[0]});
  153. Session.set("nextSong", [song])
  154. } else{
  155. song = Songs.findOne({"mid": data[i+1]});
  156. Session.set("nextSong", [song]);
  157. }
  158. }
  159. }
  160. return Session.get("nextSong");
  161. }
  162. });
  163. Template.profile.helpers({
  164. "real_name": function () {
  165. return Session.get("real_name");
  166. },
  167. "username": function () {
  168. return Session.get("username")
  169. },
  170. "first_joined": function () {
  171. return moment(Session.get("first_joined")).format("DD/MM/YYYY HH:mm:ss");
  172. },
  173. "rank": function () {
  174. return Session.get("rank");
  175. },
  176. "songs_requested": function () {
  177. return Session.get("songs_requested");
  178. },
  179. loaded: function () {
  180. return Session.get("loaded");
  181. },
  182. likedSongs: function () {
  183. var likedArr = [];
  184. Session.get("liked").forEach(function (mid) {
  185. Songs.find().forEach(function (data) {
  186. if (data.mid === mid) {
  187. likedArr.push({title: data.title, artist: data.artist});
  188. }
  189. });
  190. });
  191. return likedArr;
  192. },
  193. dislikedSongs: function () {
  194. var dislikedArr = [];
  195. Session.get("disliked").forEach(function (mid) {
  196. Songs.find().forEach(function (data) {
  197. if (data.mid === mid) {
  198. dislikedArr.push({title: data.title, artist: data.artist});
  199. }
  200. });
  201. });
  202. return dislikedArr;
  203. },
  204. isUser: function () {
  205. var parts = Router.current().url.split('/');
  206. var username = parts.pop();
  207. if (username === Meteor.user().profile.username) {
  208. return true;
  209. }
  210. }
  211. });
  212. Template.queues.helpers({
  213. songs: function () {
  214. return Queues.find({}).fetch();
  215. },
  216. song_image: function() {
  217. return Session.get("image_url");
  218. }
  219. });
  220. Template.news.helpers({
  221. articles: function() {
  222. var articles = News.find().fetch().reverse();
  223. articles = articles.map(function(article) {
  224. article.content = replaceURLWithHTMLLinks(article.content);
  225. return article;
  226. });
  227. return articles;
  228. }
  229. });
  230. Template.manageSongs.helpers({
  231. songs: function () {
  232. var noGenres = Session.get("showNoGenres");
  233. var genres = Session.get("showGenres");
  234. if (noGenres === true && genres === true) {
  235. return Songs.find();
  236. } else if (noGenres === true && genres === false) {
  237. return Songs.find({genres: []});
  238. } else {
  239. return Songs.find({$where : "this.genres.length > 0"});
  240. }
  241. },
  242. song_image: function() {
  243. return Session.get("image_url");
  244. }
  245. });
  246. Template.manageStation.helpers({
  247. editingDesc: function() {
  248. return Session.get("editingDesc");
  249. },
  250. description: function() {
  251. var parts = location.href.split('/');
  252. parts.pop();
  253. var id = parts.pop();
  254. var type = id.toLowerCase();
  255. return Rooms.findOne({type: type}).roomDesc;
  256. },
  257. songs: function () {
  258. var parts = location.href.split('/');
  259. parts.pop();
  260. var id = parts.pop();
  261. var type = id.toLowerCase();
  262. var playlist = Playlists.findOne({type: type});
  263. var songs = [];
  264. if (playlist !== undefined && playlist.songs !== undefined) {
  265. playlist.songs.forEach(function(songMid) {
  266. songs.push(Songs.findOne({mid: songMid}));
  267. });
  268. }
  269. return songs;
  270. },
  271. song_image: function() {
  272. return Session.get("image_url");
  273. },
  274. genre: function() {
  275. var parts = location.href.split('/');
  276. parts.pop();
  277. var id = parts.pop();
  278. var type = id.toLowerCase();
  279. return type;
  280. },
  281. reports: function() {
  282. var parts = location.href.split('/');
  283. parts.pop();
  284. var id = parts.pop();
  285. var query = {room: id.toLowerCase()};
  286. var reports = Reports.find(query).fetch();
  287. return reports;
  288. }
  289. });
  290. Template.room.helpers({
  291. currentSongR: function() {
  292. return Session.get("currentSongR");
  293. },
  294. previousSongR: function() {
  295. return Session.get("previousSongR");
  296. },
  297. editingSong: function() {
  298. return Session.get("editingSong");
  299. },
  300. singleVideoResults: function () {
  301. return Session.get("songResults");
  302. },
  303. singleVideoResultsActive: function() {
  304. var songs = Session.get("songResults");
  305. if (songs !== undefined && songs.length > 0) {
  306. return true;
  307. } else {
  308. return false;
  309. }
  310. },
  311. importPlaylistVideos: function () {
  312. return Session.get("songResults");
  313. },
  314. playlistImportVideosActive: function() {
  315. var songs = Session.get("songResults");
  316. if (songs !== undefined && songs.length > 0) {
  317. return true;
  318. } else {
  319. return false;
  320. }
  321. },
  322. singleVideo: function () {
  323. return Session.get("si_or_pl") === "singleVideo";
  324. },
  325. chat: function () {
  326. Meteor.setTimeout(function () {
  327. var elem = document.getElementById('chat');
  328. if (elem !== undefined && elem !== null) {
  329. elem.scrollTop = elem.scrollHeight;
  330. }
  331. }, 100);
  332. return Chat.find({type: Session.get("type")}, {sort: {time: -1}, limit: 50}).fetch().reverse();
  333. },
  334. globalChat: function () {
  335. Meteor.setTimeout(function () {
  336. var elem = document.getElementById('global-chat');
  337. if (elem !== undefined && elem !== null) {
  338. elem.scrollTop = elem.scrollHeight;
  339. }
  340. }, 100);
  341. var messages = Chat.find({type: "global"}, {sort: {time: -1}, limit: 50}).fetch().reverse();
  342. messages = messages.map(function(message) {
  343. message.message = replaceURLWithHTMLLinks(message.message);
  344. return message;
  345. });
  346. return messages;
  347. },
  348. likes: function () {
  349. var playlist = Songs.find({"genres": Session.get("type")}).fetch();
  350. var likes = 0;
  351. playlist.forEach(function (song) {
  352. if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
  353. likes = song.likes;
  354. return;
  355. }
  356. });
  357. return likes;
  358. },
  359. dislikes: function () {
  360. var playlist = Songs.find({"genres": Session.get("type")}).fetch();
  361. var dislikes = 0;
  362. playlist.forEach(function (song) {
  363. if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
  364. dislikes = song.dislikes;
  365. return;
  366. }
  367. });
  368. return dislikes;
  369. },
  370. liked: function () {
  371. if (Meteor.userId()) {
  372. var currentSong = Session.get("currentSong");
  373. if (currentSong && Meteor.user().profile.liked.indexOf(currentSong.mid) !== -1) {
  374. return "liked";
  375. } else {
  376. return "";
  377. }
  378. } else {
  379. "";
  380. }
  381. },
  382. disliked: function () {
  383. if (Meteor.userId()) {
  384. var currentSong = Session.get("currentSong");
  385. if (currentSong && Meteor.user().profile.disliked.indexOf(currentSong.mid) !== -1) {
  386. return "disliked";
  387. } else {
  388. return "";
  389. }
  390. } else {
  391. "";
  392. }
  393. },
  394. type: function () {
  395. var parts = location.href.split('/');
  396. var id = parts.pop().toLowerCase();
  397. return Rooms.findOne({type: id}).display;
  398. },
  399. users: function () {
  400. var parts = location.href.split('/');
  401. var id = parts.pop().toLowerCase();
  402. return Rooms.findOne({type: id}).users;
  403. },
  404. title: function () {
  405. return Session.get("title");
  406. },
  407. artist: function () {
  408. return Session.get("artist");
  409. },
  410. loaded: function () {
  411. return Session.get("loaded");
  412. },
  413. paused: function () {
  414. return Session.get("state") === "paused";
  415. },
  416. private: function () {
  417. return 1;
  418. //return Rooms.findOne({type: Session.get("type")}).private === true;
  419. },
  420. currentSong: function(){
  421. return Session.get("currentSong");
  422. },
  423. reportingSong: function() {
  424. if (!Session.get("reportPrevious")) {
  425. return Session.get("currentSongR");
  426. } else {
  427. return Session.get("previousSongR");
  428. }
  429. },
  430. reportSong: function(){
  431. Meteor.setInterval(function(){
  432. if($("#report-song").is(":checked")){
  433. Session.set("reportSong", true)
  434. } else { Session.set("reportSong", false) }
  435. }, 500);
  436. return Session.get("reportSong");
  437. },
  438. reportSongOther: function(){
  439. Meteor.setInterval(function(){
  440. if($("#report-song-other").is(":checked")){
  441. Session.set("reportSongOther", true)
  442. } else { Session.set("reportSongOther", false) }
  443. }, 500);
  444. return Session.get("reportSongOther");
  445. },
  446. reportTitle: function(){
  447. Meteor.setInterval(function(){
  448. if($("#report-title").is(":checked")){
  449. Session.set("reportTitle", true)
  450. } else { Session.set("reportTitle", false) }
  451. }, 500);
  452. return Session.get("reportTitle");
  453. },
  454. reportTitleOther: function(){
  455. Meteor.setInterval(function(){
  456. if($("#report-title-other").is(":checked")){
  457. Session.set("reportTitleOther", true)
  458. } else { Session.set("reportTitleOther", false) }
  459. }, 500);
  460. return Session.get("reportTitleOther");
  461. },
  462. reportArtist: function(){
  463. Meteor.setInterval(function(){
  464. if($("#report-artist").is(":checked")){
  465. Session.set("reportArtist", true)
  466. } else { Session.set("reportArtist", false) }
  467. }, 500);
  468. return Session.get("reportArtist");
  469. },
  470. reportArtistOther: function(){
  471. Meteor.setInterval(function(){
  472. if($("#report-artist-other").is(":checked")){
  473. Session.set("reportArtistOther", true)
  474. } else { Session.set("reportArtistOther", false) }
  475. }, 500);
  476. return Session.get("reportArtistOther");
  477. },
  478. reportDuration: function(){
  479. Meteor.setInterval(function(){
  480. if($("#report-duration").is(":checked")){
  481. Session.set("reportDuration", true)
  482. } else { Session.set("reportDuration", false) }
  483. }, 500);
  484. return Session.get("reportDuration");
  485. },
  486. reportDurationOther: function(){
  487. Meteor.setInterval(function(){
  488. if($("#report-duration-other").is(":checked")){
  489. Session.set("reportDurationOther", true)
  490. } else { Session.set("reportDurationOther", false) }
  491. }, 500);
  492. return Session.get("reportDurationOther");
  493. },
  494. reportAlbumart: function(){
  495. Meteor.setInterval(function(){
  496. if($("#report-albumart").is(":checked")){
  497. Session.set("reportAlbumart", true)
  498. } else { Session.set("reportAlbumart", false) }
  499. }, 500);
  500. return Session.get("reportAlbumart");
  501. },
  502. reportAlbumartOther: function(){
  503. Meteor.setInterval(function(){
  504. if($("#report-albumart-other").is(":checked")){
  505. Session.set("reportAlbumartOther", true)
  506. } else { Session.set("reportAlbumartOther", false) }
  507. }, 500);
  508. return Session.get("reportAlbumartOther");
  509. },
  510. reportOther: function(){
  511. Meteor.setInterval(function(){
  512. if($("#report-other").is(":checked")){
  513. Session.set("reportOther", true)
  514. } else { Session.set("reportOther", false) }
  515. }, 500);
  516. return Session.get("reportOther");
  517. },
  518. votes: function () {
  519. return Rooms.findOne({type: Session.get("type")}).votes;
  520. },
  521. usersInRoom: function(){
  522. var userList = [];
  523. var roomUserList = Rooms.findOne({type: Session.get("type")}).userList;
  524. roomUserList.forEach(function(user){
  525. if(userList.indexOf(user) === -1){
  526. userList.push(user);
  527. }
  528. })
  529. return userList;
  530. }
  531. });
  532. Template.privateRoom.helpers({
  533. privateRoomOwnerName: function() {
  534. var room = PrivateRooms.findOne({name: Session.get("privateRoomName")});
  535. if (room !== undefined) {
  536. return Meteor.users.findOne(room.owner).profile.username;
  537. } else {
  538. return "";
  539. }
  540. },
  541. editingPlaylist: function() {
  542. return PrivatePlaylists.findOne({owner: Meteor.userId(), name: Session.get("editingPlaylistName")});
  543. },
  544. isPlaylistSelected: function(roomName, playlistName) {
  545. return PrivateRooms.findOne({name: roomName}).playlist === playlistName;
  546. },
  547. globalChat: function () {
  548. Meteor.setTimeout(function () {
  549. var elem = document.getElementById('global-chat');
  550. if (elem !== undefined && elem !== null) {
  551. elem.scrollTop = elem.scrollHeight;
  552. }
  553. }, 100);
  554. var messages = Chat.find({type: "global"}, {sort: {time: -1}, limit: 50}).fetch().reverse();
  555. messages = messages.map(function(message) {
  556. message.message = replaceURLWithHTMLLinks(message.message);
  557. return message;
  558. });
  559. return messages;
  560. },
  561. privateRoomDisplayName: function () {
  562. var parts = location.href.split('/');
  563. var id = parts.pop().toLowerCase();
  564. return PrivateRooms.findOne({name: id}).displayName;
  565. },
  566. name: function () {
  567. var parts = location.href.split('/');
  568. var id = parts.pop().toLowerCase();
  569. return id;
  570. },
  571. users: function () {
  572. var parts = location.href.split('/');
  573. var id = parts.pop().toLowerCase();
  574. return PrivateRooms.findOne({name: id}).userList.length;
  575. },
  576. allowed: function () {
  577. var parts = location.href.split('/');
  578. var id = parts.pop().toLowerCase();deploy
  579. var arr = [];
  580. PrivateRooms.findOne({name: id}).allowed.forEach(function(allowed) {
  581. arr.push({name: Meteor.users.findOne(allowed).profile.username, id: allowed});
  582. });
  583. return arr;
  584. },
  585. playlists: function () {
  586. return PrivatePlaylists.find({owner: Meteor.userId()});
  587. },
  588. title: function () {
  589. return Session.get("title");
  590. },
  591. loaded: function () {
  592. return Session.get("loaded");
  593. },
  594. paused: function () {
  595. return Session.get("state") === "paused";
  596. },
  597. private: function () {
  598. var room = PrivateRooms.findOne({name: Session.get("privateRoomName")});
  599. if (room !== undefined) {
  600. return room.private;
  601. } else {
  602. return 1;
  603. }
  604. },
  605. playing: function() {
  606. return Session.get("state") === "playing";
  607. },
  608. currentSong: function(){
  609. return Session.get("currentSong");
  610. },
  611. votes: function () {
  612. var room = PrivateRooms.findOne({name: Session.get("privateRoomName")});
  613. if (room !== undefined) {
  614. return room.votes;
  615. } else {
  616. return 0;
  617. }
  618. },
  619. usersInRoom: function(){
  620. var userList = [];
  621. var room = PrivateRooms.findOne({name: Session.get("privateRoomName")});
  622. if (room !== undefined) {
  623. var roomUserList = room.userList;
  624. roomUserList.forEach(function (user) {
  625. if (userList.indexOf(user) === -1) {
  626. userList.push(user);
  627. }
  628. })
  629. }
  630. return userList;
  631. }
  632. });
  633. Template.settings.helpers({
  634. username: function () {
  635. if (Meteor.user() !== undefined) {
  636. return Meteor.user().profile.username;
  637. } else {
  638. return "";
  639. }
  640. }
  641. });
  642. function replaceURLWithHTMLLinks(text) {
  643. var re = /(\(.*?)?\b((?:https?|ftp|file):\/\/[-a-z0-9+&@#\/%?=~_()|!:,.;]*[-a-z0-9+&@#\/%=~_()|])/ig;
  644. return text.replace(re, function(match, lParens, url) {
  645. var rParens = '';
  646. lParens = lParens || '';
  647. // Try to strip the same number of right parens from url
  648. // as there are left parens. Here, lParenCounter must be
  649. // a RegExp object. You cannot use a literal
  650. // while (/\(/g.exec(lParens)) { ... }
  651. // because an object is needed to store the lastIndex state.
  652. var lParenCounter = /\(/g;
  653. while (lParenCounter.exec(lParens)) {
  654. var m;
  655. // We want m[1] to be greedy, unless a period precedes the
  656. // right parenthesis. These tests cannot be simplified as
  657. // /(.*)(\.?\).*)/.exec(url)
  658. // because if (.*) is greedy then \.? never gets a chance.
  659. if (m = /(.*)(\.\).*)/.exec(url) ||
  660. /(.*)(\).*)/.exec(url)) {
  661. url = m[1];
  662. rParens = m[2] + rParens;
  663. }
  664. }
  665. return lParens + "<a style='font-size: 18px; padding: 0; display: inline;' target='_blank' href='" + url + "'>" + url + "</a>" + rParens;
  666. });
  667. }