helpers.js 16 KB

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