helpers.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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. return News.find().fetch().reverse();
  223. }
  224. });
  225. Template.manageSongs.helpers({
  226. songs: function () {
  227. var noGenres = Session.get("showNoGenres");
  228. var genres = Session.get("showGenres");
  229. if (noGenres === true && genres === true) {
  230. return Songs.find();
  231. } else if (noGenres === true && genres === false) {
  232. return Songs.find({genres: []});
  233. } else {
  234. return Songs.find({$where : "this.genres.length > 0"});
  235. }
  236. },
  237. song_image: function() {
  238. return Session.get("image_url");
  239. }
  240. });
  241. Template.manageStation.helpers({
  242. editingDesc: function() {
  243. return Session.get("editingDesc");
  244. },
  245. description: function() {
  246. var parts = location.href.split('/');
  247. parts.pop();
  248. var id = parts.pop();
  249. var type = id.toLowerCase();
  250. return Rooms.findOne({type: type}).roomDesc;
  251. },
  252. songs: function () {
  253. var parts = location.href.split('/');
  254. parts.pop();
  255. var id = parts.pop();
  256. var type = id.toLowerCase();
  257. var playlist = Playlists.findOne({type: type});
  258. var songs = [];
  259. if (playlist !== undefined && playlist.songs !== undefined) {
  260. playlist.songs.forEach(function(songMid) {
  261. songs.push(Songs.findOne({mid: songMid}));
  262. });
  263. }
  264. return songs;
  265. },
  266. song_image: function() {
  267. return Session.get("image_url");
  268. },
  269. genre: function() {
  270. var parts = location.href.split('/');
  271. parts.pop();
  272. var id = parts.pop();
  273. var type = id.toLowerCase();
  274. return type;
  275. },
  276. reports: function() {
  277. var parts = location.href.split('/');
  278. parts.pop();
  279. var id = parts.pop();
  280. var query = {room: id.toLowerCase()};
  281. var reports = Reports.find(query).fetch();
  282. return reports;
  283. }
  284. });
  285. Template.room.helpers({
  286. currentSongR: function() {
  287. return Session.get("currentSongR");
  288. },
  289. previousSongR: function() {
  290. return Session.get("previousSongR");
  291. },
  292. editingSong: function() {
  293. return Session.get("editingSong");
  294. },
  295. singleVideoResults: function () {
  296. return Session.get("songResults");
  297. },
  298. singleVideoResultsActive: function() {
  299. var songs = Session.get("songResults");
  300. if (songs !== undefined && songs.length > 0) {
  301. return true;
  302. } else {
  303. return false;
  304. }
  305. },
  306. importPlaylistVideos: function () {
  307. return Session.get("songResults");
  308. },
  309. playlistImportVideosActive: function() {
  310. var songs = Session.get("songResults");
  311. if (songs !== undefined && songs.length > 0) {
  312. return true;
  313. } else {
  314. return false;
  315. }
  316. },
  317. singleVideo: function () {
  318. return Session.get("si_or_pl") === "singleVideo";
  319. },
  320. chat: function () {
  321. Meteor.setTimeout(function () {
  322. var elem = document.getElementById('chat');
  323. if (elem !== undefined && elem !== null) {
  324. elem.scrollTop = elem.scrollHeight;
  325. }
  326. }, 100);
  327. return Chat.find({type: Session.get("type")}, {sort: {time: -1}, limit: 50}).fetch().reverse();
  328. },
  329. globalChat: function () {
  330. Meteor.setTimeout(function () {
  331. var elem = document.getElementById('global-chat');
  332. if (elem !== undefined && elem !== null) {
  333. elem.scrollTop = elem.scrollHeight;
  334. }
  335. }, 100);
  336. return Chat.find({type: "global"}, {sort: {time: -1}, limit: 50}).fetch().reverse();
  337. },
  338. likes: function () {
  339. var playlist = Songs.find({"genres": Session.get("type")}).fetch();
  340. var likes = 0;
  341. playlist.forEach(function (song) {
  342. if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
  343. likes = song.likes;
  344. return;
  345. }
  346. });
  347. return likes;
  348. },
  349. dislikes: function () {
  350. var playlist = Songs.find({"genres": Session.get("type")}).fetch();
  351. var dislikes = 0;
  352. playlist.forEach(function (song) {
  353. if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
  354. dislikes = song.dislikes;
  355. return;
  356. }
  357. });
  358. return dislikes;
  359. },
  360. liked: function () {
  361. if (Meteor.userId()) {
  362. var currentSong = Session.get("currentSong");
  363. if (currentSong && Meteor.user().profile.liked.indexOf(currentSong.mid) !== -1) {
  364. return "liked";
  365. } else {
  366. return "";
  367. }
  368. } else {
  369. "";
  370. }
  371. },
  372. disliked: function () {
  373. if (Meteor.userId()) {
  374. var currentSong = Session.get("currentSong");
  375. if (currentSong && Meteor.user().profile.disliked.indexOf(currentSong.mid) !== -1) {
  376. return "disliked";
  377. } else {
  378. return "";
  379. }
  380. } else {
  381. "";
  382. }
  383. },
  384. type: function () {
  385. var parts = location.href.split('/');
  386. var id = parts.pop().toLowerCase();
  387. return Rooms.findOne({type: id}).display;
  388. },
  389. users: function () {
  390. var parts = location.href.split('/');
  391. var id = parts.pop().toLowerCase();
  392. return Rooms.findOne({type: id}).users;
  393. },
  394. title: function () {
  395. return Session.get("title");
  396. },
  397. artist: function () {
  398. return Session.get("artist");
  399. },
  400. loaded: function () {
  401. return Session.get("loaded");
  402. },
  403. paused: function () {
  404. return Session.get("state") === "paused";
  405. },
  406. private: function () {
  407. return 1;
  408. //return Rooms.findOne({type: Session.get("type")}).private === true;
  409. },
  410. currentSong: function(){
  411. return Session.get("currentSong");
  412. },
  413. reportingSong: function() {
  414. if (!Session.get("reportPrevious")) {
  415. return Session.get("currentSongR");
  416. } else {
  417. return Session.get("previousSongR");
  418. }
  419. },
  420. reportSong: function(){
  421. Meteor.setInterval(function(){
  422. if($("#report-song").is(":checked")){
  423. Session.set("reportSong", true)
  424. } else { Session.set("reportSong", false) }
  425. }, 500);
  426. return Session.get("reportSong");
  427. },
  428. reportSongOther: function(){
  429. Meteor.setInterval(function(){
  430. if($("#report-song-other").is(":checked")){
  431. Session.set("reportSongOther", true)
  432. } else { Session.set("reportSongOther", false) }
  433. }, 500);
  434. return Session.get("reportSongOther");
  435. },
  436. reportTitle: function(){
  437. Meteor.setInterval(function(){
  438. if($("#report-title").is(":checked")){
  439. Session.set("reportTitle", true)
  440. } else { Session.set("reportTitle", false) }
  441. }, 500);
  442. return Session.get("reportTitle");
  443. },
  444. reportTitleOther: function(){
  445. Meteor.setInterval(function(){
  446. if($("#report-title-other").is(":checked")){
  447. Session.set("reportTitleOther", true)
  448. } else { Session.set("reportTitleOther", false) }
  449. }, 500);
  450. return Session.get("reportTitleOther");
  451. },
  452. reportArtist: function(){
  453. Meteor.setInterval(function(){
  454. if($("#report-artist").is(":checked")){
  455. Session.set("reportArtist", true)
  456. } else { Session.set("reportArtist", false) }
  457. }, 500);
  458. return Session.get("reportArtist");
  459. },
  460. reportArtistOther: function(){
  461. Meteor.setInterval(function(){
  462. if($("#report-artist-other").is(":checked")){
  463. Session.set("reportArtistOther", true)
  464. } else { Session.set("reportArtistOther", false) }
  465. }, 500);
  466. return Session.get("reportArtistOther");
  467. },
  468. reportDuration: function(){
  469. Meteor.setInterval(function(){
  470. if($("#report-duration").is(":checked")){
  471. Session.set("reportDuration", true)
  472. } else { Session.set("reportDuration", false) }
  473. }, 500);
  474. return Session.get("reportDuration");
  475. },
  476. reportDurationOther: function(){
  477. Meteor.setInterval(function(){
  478. if($("#report-duration-other").is(":checked")){
  479. Session.set("reportDurationOther", true)
  480. } else { Session.set("reportDurationOther", false) }
  481. }, 500);
  482. return Session.get("reportDurationOther");
  483. },
  484. reportAlbumart: function(){
  485. Meteor.setInterval(function(){
  486. if($("#report-albumart").is(":checked")){
  487. Session.set("reportAlbumart", true)
  488. } else { Session.set("reportAlbumart", false) }
  489. }, 500);
  490. return Session.get("reportAlbumart");
  491. },
  492. reportAlbumartOther: function(){
  493. Meteor.setInterval(function(){
  494. if($("#report-albumart-other").is(":checked")){
  495. Session.set("reportAlbumartOther", true)
  496. } else { Session.set("reportAlbumartOther", false) }
  497. }, 500);
  498. return Session.get("reportAlbumartOther");
  499. },
  500. reportOther: function(){
  501. Meteor.setInterval(function(){
  502. if($("#report-other").is(":checked")){
  503. Session.set("reportOther", true)
  504. } else { Session.set("reportOther", false) }
  505. }, 500);
  506. return Session.get("reportOther");
  507. },
  508. votes: function () {
  509. return Rooms.findOne({type: Session.get("type")}).votes;
  510. },
  511. usersInRoom: function(){
  512. var userList = [];
  513. var roomUserList = Rooms.findOne({type: Session.get("type")}).userList;
  514. roomUserList.forEach(function(user){
  515. if(userList.indexOf(user) === -1){
  516. userList.push(user);
  517. }
  518. })
  519. return userList;
  520. }
  521. });
  522. Template.privateRoom.helpers({
  523. privateRoomOwnerName: function() {
  524. var room = PrivateRooms.findOne({name: Session.get("privateRoomName")});
  525. if (room !== undefined) {
  526. return Meteor.users.findOne(room.owner).profile.username;
  527. } else {
  528. return "";
  529. }
  530. },
  531. editingPlaylist: function() {
  532. return PrivatePlaylists.findOne({owner: Meteor.userId(), name: Session.get("editingPlaylistName")});
  533. },
  534. isPlaylistSelected: function(roomName, playlistName) {
  535. return PrivateRooms.findOne({name: roomName}).playlist === playlistName;
  536. },
  537. globalChat: function () {
  538. Meteor.setTimeout(function () {
  539. var elem = document.getElementById('global-chat');
  540. if (elem !== undefined && elem !== null) {
  541. elem.scrollTop = elem.scrollHeight;
  542. }
  543. }, 100);
  544. return Chat.find({type: "global"}, {sort: {time: -1}, limit: 50}).fetch().reverse();
  545. },
  546. privateRoomDisplayName: function () {
  547. var parts = location.href.split('/');
  548. var id = parts.pop().toLowerCase();
  549. return PrivateRooms.findOne({name: id}).displayName;
  550. },
  551. name: function () {
  552. var parts = location.href.split('/');
  553. var id = parts.pop().toLowerCase();
  554. return id;
  555. },
  556. users: function () {
  557. var parts = location.href.split('/');
  558. var id = parts.pop().toLowerCase();
  559. return PrivateRooms.findOne({name: id}).userList.length;
  560. },
  561. allowed: function () {
  562. var parts = location.href.split('/');
  563. var id = parts.pop().toLowerCase();
  564. var arr = [];
  565. PrivateRooms.findOne({name: id}).allowed.forEach(function(allowed) {
  566. arr.push({name: Meteor.users.findOne(allowed).profile.username, id: allowed});
  567. });
  568. return arr;
  569. },
  570. playlists: function () {
  571. return PrivatePlaylists.find({owner: Meteor.userId()});
  572. },
  573. title: function () {
  574. return Session.get("title");
  575. },
  576. loaded: function () {
  577. return Session.get("loaded");
  578. },
  579. paused: function () {
  580. return Session.get("state") === "paused";
  581. },
  582. private: function () {
  583. var room = PrivateRooms.findOne({name: Session.get("privateRoomName")});
  584. if (room !== undefined) {
  585. return room.private;
  586. } else {
  587. return 1;
  588. }
  589. },
  590. playing: function() {
  591. return Session.get("state") === "playing";
  592. },
  593. currentSong: function(){
  594. return Session.get("currentSong");
  595. },
  596. votes: function () {
  597. var room = PrivateRooms.findOne({name: Session.get("privateRoomName")});
  598. if (room !== undefined) {
  599. return room.votes;
  600. } else {
  601. return 0;
  602. }
  603. },
  604. usersInRoom: function(){
  605. var userList = [];
  606. var room = PrivateRooms.findOne({name: Session.get("privateRoomName")});
  607. if (room !== undefined) {
  608. var roomUserList = room.userList;
  609. roomUserList.forEach(function (user) {
  610. if (userList.indexOf(user) === -1) {
  611. userList.push(user);
  612. }
  613. })
  614. }
  615. return userList;
  616. }
  617. });
  618. Template.settings.helpers({
  619. username: function () {
  620. if (Meteor.user() !== undefined) {
  621. return Meteor.user().profile.username;
  622. } else {
  623. return "";
  624. }
  625. }
  626. });