helpers.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. });
  110. Template.playlist.helpers({
  111. playlist_songs: function () {
  112. var songIDs = Playlists.find({"type": Session.get("type")}).fetch()[0].songs
  113. var data = [];
  114. songIDs.forEach(function(id){
  115. var song = Songs.findOne({"mid": id});
  116. data.push(song);
  117. })
  118. console.log(data);
  119. if (data !== undefined) {
  120. data.map(function (song) {
  121. if (Session.get("currentSong") !== undefined && song.mid === Session.get("currentSong").mid) {
  122. song.current = true;
  123. } else {
  124. song.current = false;
  125. }
  126. return song;
  127. });
  128. return data;
  129. } else {
  130. return [];
  131. }
  132. },
  133. nextSong: function(){
  134. var song;
  135. var data = Playlists.find({"type": Session.get("type")}).fetch()[0].songs
  136. for(var i = 0; i < data.length; i++){
  137. if(data[i] === Session.get("currentSong").mid){
  138. if(i === data.length - 1){
  139. song = Songs.findOne({"mid": data[0]});
  140. Session.set("nextSong", [song])
  141. } else{
  142. song = Songs.findOne({"mid": data[i+1]});
  143. Session.set("nextSong", [song]);
  144. }
  145. }
  146. };
  147. return Session.get("nextSong");
  148. }
  149. });
  150. Template.profile.helpers({
  151. "real_name": function () {
  152. return Session.get("real_name");
  153. },
  154. "username": function () {
  155. return Session.get("username")
  156. },
  157. "first_joined": function () {
  158. return moment(Session.get("first_joined")).format("DD/MM/YYYY HH:mm:ss");
  159. },
  160. "rank": function () {
  161. return Session.get("rank");
  162. },
  163. "songs_requested": function () {
  164. return Session.get("songs_requested");
  165. },
  166. loaded: function () {
  167. return Session.get("loaded");
  168. },
  169. likedSongs: function () {
  170. var likedArr = [];
  171. Session.get("liked").forEach(function (mid) {
  172. Songs.find().forEach(function (data) {
  173. if (data.mid === mid) {
  174. likedArr.push({title: data.title, artist: data.artist});
  175. }
  176. });
  177. });
  178. return likedArr;
  179. },
  180. dislikedSongs: function () {
  181. var dislikedArr = [];
  182. Session.get("disliked").forEach(function (mid) {
  183. Songs.find().forEach(function (data) {
  184. if (data.mid === mid) {
  185. dislikedArr.push({title: data.title, artist: data.artist});
  186. }
  187. });
  188. });
  189. return dislikedArr;
  190. },
  191. isUser: function () {
  192. var parts = Router.current().url.split('/');
  193. var username = parts.pop();
  194. if (username === Meteor.user().profile.username) {
  195. return true;
  196. }
  197. }
  198. });
  199. Template.queues.helpers({
  200. songs: function () {
  201. return Queues.find({}).fetch();
  202. },
  203. song_image: function() {
  204. return Session.get("image_url");
  205. }
  206. });
  207. Template.news.helpers({
  208. articles: function() {
  209. return News.find().fetch().reverse();
  210. }
  211. });
  212. Template.manageSongs.helpers({
  213. songs: function () {
  214. var noGenres = Session.get("showNoGenres");
  215. var genres = Session.get("showGenres");
  216. if (noGenres === true && genres === true) {
  217. return Songs.find();
  218. } else if (noGenres === true && genres === false) {
  219. return Songs.find({genres: []});
  220. } else {
  221. return Songs.find({$where : "this.genres.length > 0"});
  222. }
  223. },
  224. song_image: function() {
  225. return Session.get("image_url");
  226. }
  227. });
  228. Template.manageStation.helpers({
  229. editingDesc: function() {
  230. return Session.get("editingDesc");
  231. },
  232. description: function() {
  233. var parts = location.href.split('/');
  234. parts.pop();
  235. var id = parts.pop();
  236. var type = id.toLowerCase();
  237. return Rooms.findOne({type: type}).roomDesc;
  238. },
  239. songs: function () {
  240. var parts = location.href.split('/');
  241. parts.pop();
  242. var id = parts.pop();
  243. var type = id.toLowerCase();
  244. var playlist = Playlists.findOne({type: type});
  245. var songs = [];
  246. if (playlist !== undefined && playlist.songs !== undefined) {
  247. playlist.songs.forEach(function(songMid) {
  248. songs.push(Songs.findOne({mid: songMid}));
  249. });
  250. }
  251. return songs;
  252. },
  253. song_image: function() {
  254. return Session.get("image_url");
  255. },
  256. genre: function() {
  257. var parts = location.href.split('/');
  258. parts.pop();
  259. var id = parts.pop();
  260. var type = id.toLowerCase();
  261. return type;
  262. },
  263. reports: function() {
  264. var parts = location.href.split('/');
  265. parts.pop();
  266. var id = parts.pop();
  267. var query = {room: id.toLowerCase()};
  268. var reports = Reports.find(query).fetch();
  269. return reports;
  270. }
  271. });
  272. Template.room.helpers({
  273. currentSongR: function() {
  274. return Session.get("currentSongR");
  275. },
  276. previousSongR: function() {
  277. return Session.get("previousSongR");
  278. },
  279. editingSong: function() {
  280. return Session.get("editingSong");
  281. },
  282. singleVideoResults: function () {
  283. return Session.get("songResults");
  284. },
  285. singleVideoResultsActive: function() {
  286. var songs = Session.get("songResults");
  287. if (songs !== undefined && songs.length > 0) {
  288. return true;
  289. } else {
  290. return false;
  291. }
  292. },
  293. importPlaylistVideos: function () {
  294. return Session.get("songResults");
  295. },
  296. playlistImportVideosActive: function() {
  297. var songs = Session.get("songResults");
  298. if (songs !== undefined && songs.length > 0) {
  299. return true;
  300. } else {
  301. return false;
  302. }
  303. },
  304. singleVideo: function () {
  305. return Session.get("si_or_pl") === "singleVideo";
  306. },
  307. chat: function () {
  308. Meteor.setTimeout(function () {
  309. var elem = document.getElementById('chat');
  310. if (elem !== undefined && elem !== null) {
  311. elem.scrollTop = elem.scrollHeight;
  312. }
  313. }, 100);
  314. return Chat.find({type: Session.get("type")}, {sort: {time: -1}, limit: 50}).fetch().reverse();
  315. },
  316. globalChat: function () {
  317. Meteor.setTimeout(function () {
  318. var elem = document.getElementById('global-chat');
  319. if (elem !== undefined && elem !== null) {
  320. elem.scrollTop = elem.scrollHeight;
  321. }
  322. }, 100);
  323. return Chat.find({type: "global"}, {sort: {time: -1}, limit: 50}).fetch().reverse();
  324. },
  325. likes: function () {
  326. var playlist = Songs.find({"genres": Session.get("type")}).fetch();
  327. var likes = 0;
  328. playlist.forEach(function (song) {
  329. if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
  330. likes = song.likes;
  331. return;
  332. }
  333. });
  334. return likes;
  335. },
  336. dislikes: function () {
  337. var playlist = Songs.find({"genres": Session.get("type")}).fetch();
  338. var dislikes = 0;
  339. playlist.forEach(function (song) {
  340. if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
  341. dislikes = song.dislikes;
  342. return;
  343. }
  344. });
  345. return dislikes;
  346. },
  347. liked: function () {
  348. if (Meteor.userId()) {
  349. var currentSong = Session.get("currentSong");
  350. if (currentSong && Meteor.user().profile.liked.indexOf(currentSong.mid) !== -1) {
  351. return "liked";
  352. } else {
  353. return "";
  354. }
  355. } else {
  356. "";
  357. }
  358. },
  359. disliked: function () {
  360. if (Meteor.userId()) {
  361. var currentSong = Session.get("currentSong");
  362. if (currentSong && Meteor.user().profile.disliked.indexOf(currentSong.mid) !== -1) {
  363. return "disliked";
  364. } else {
  365. return "";
  366. }
  367. } else {
  368. "";
  369. }
  370. },
  371. type: function () {
  372. var parts = location.href.split('/');
  373. var id = parts.pop().toLowerCase();
  374. return Rooms.findOne({type: id}).display;
  375. },
  376. users: function () {
  377. var parts = location.href.split('/');
  378. var id = parts.pop().toLowerCase();
  379. return Rooms.findOne({type: id}).users;
  380. },
  381. title: function () {
  382. return Session.get("title");
  383. },
  384. artist: function () {
  385. return Session.get("artist");
  386. },
  387. loaded: function () {
  388. return Session.get("loaded");
  389. },
  390. paused: function () {
  391. return Session.get("state") === "paused";
  392. },
  393. private: function () {
  394. return Rooms.findOne({type: Session.get("type")}).private === true;
  395. },
  396. currentSong: function(){
  397. return Session.get("currentSong");
  398. },
  399. reportingSong: function() {
  400. if (!Session.get("reportPrevious")) {
  401. return Session.get("currentSongR");
  402. } else {
  403. return Session.get("previousSongR");
  404. }
  405. },
  406. reportSong: function(){
  407. Meteor.setInterval(function(){
  408. if($("#report-song").is(":checked")){
  409. Session.set("reportSong", true)
  410. } else { Session.set("reportSong", false) }
  411. }, 500);
  412. return Session.get("reportSong");
  413. },
  414. reportSongOther: function(){
  415. Meteor.setInterval(function(){
  416. if($("#report-song-other").is(":checked")){
  417. Session.set("reportSongOther", true)
  418. } else { Session.set("reportSongOther", false) }
  419. }, 500);
  420. return Session.get("reportSongOther");
  421. },
  422. reportTitle: function(){
  423. Meteor.setInterval(function(){
  424. if($("#report-title").is(":checked")){
  425. Session.set("reportTitle", true)
  426. } else { Session.set("reportTitle", false) }
  427. }, 500);
  428. return Session.get("reportTitle");
  429. },
  430. reportTitleOther: function(){
  431. Meteor.setInterval(function(){
  432. if($("#report-title-other").is(":checked")){
  433. Session.set("reportTitleOther", true)
  434. } else { Session.set("reportTitleOther", false) }
  435. }, 500);
  436. return Session.get("reportTitleOther");
  437. },
  438. reportArtist: function(){
  439. Meteor.setInterval(function(){
  440. if($("#report-artist").is(":checked")){
  441. Session.set("reportArtist", true)
  442. } else { Session.set("reportArtist", false) }
  443. }, 500);
  444. return Session.get("reportArtist");
  445. },
  446. reportArtistOther: function(){
  447. Meteor.setInterval(function(){
  448. if($("#report-artist-other").is(":checked")){
  449. Session.set("reportArtistOther", true)
  450. } else { Session.set("reportArtistOther", false) }
  451. }, 500);
  452. return Session.get("reportArtistOther");
  453. },
  454. reportDuration: function(){
  455. Meteor.setInterval(function(){
  456. if($("#report-duration").is(":checked")){
  457. Session.set("reportDuration", true)
  458. } else { Session.set("reportDuration", false) }
  459. }, 500);
  460. return Session.get("reportDuration");
  461. },
  462. reportDurationOther: function(){
  463. Meteor.setInterval(function(){
  464. if($("#report-duration-other").is(":checked")){
  465. Session.set("reportDurationOther", true)
  466. } else { Session.set("reportDurationOther", false) }
  467. }, 500);
  468. return Session.get("reportDurationOther");
  469. },
  470. reportAlbumart: function(){
  471. Meteor.setInterval(function(){
  472. if($("#report-albumart").is(":checked")){
  473. Session.set("reportAlbumart", true)
  474. } else { Session.set("reportAlbumart", false) }
  475. }, 500);
  476. return Session.get("reportAlbumart");
  477. },
  478. reportAlbumartOther: function(){
  479. Meteor.setInterval(function(){
  480. if($("#report-albumart-other").is(":checked")){
  481. Session.set("reportAlbumartOther", true)
  482. } else { Session.set("reportAlbumartOther", false) }
  483. }, 500);
  484. return Session.get("reportAlbumartOther");
  485. },
  486. reportOther: function(){
  487. Meteor.setInterval(function(){
  488. if($("#report-other").is(":checked")){
  489. Session.set("reportOther", true)
  490. } else { Session.set("reportOther", false) }
  491. }, 500);
  492. return Session.get("reportOther");
  493. },
  494. votes: function () {
  495. console.log(Rooms.findOne({type: Session.get("type")}).votes);
  496. return Rooms.findOne({type: Session.get("type")}).votes;
  497. },
  498. usersInRoom: function(){
  499. var userList = [];
  500. var roomUserList = Rooms.findOne({type: Session.get("type")}).userList;
  501. roomUserList.forEach(function(user){
  502. if(userList.indexOf(user) === -1){
  503. userList.push(user);
  504. }
  505. })
  506. return userList;
  507. }
  508. });
  509. Template.settings.helpers({
  510. username: function () {
  511. if (Meteor.user() !== undefined) {
  512. return Meteor.user().profile.username;
  513. } else {
  514. return "";
  515. }
  516. }
  517. });