helpers.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. });
  261. Template.room.helpers({
  262. editingSong: function() {
  263. return Session.get("editingSong");
  264. },
  265. singleVideoResults: function () {
  266. return Session.get("songResults");
  267. },
  268. singleVideoResultsActive: function() {
  269. var songs = Session.get("songResults");
  270. if (songs !== undefined && songs.length > 0) {
  271. return true;
  272. } else {
  273. return false;
  274. }
  275. },
  276. importPlaylistVideos: function () {
  277. return Session.get("songResults");
  278. },
  279. playlistImportVideosActive: function() {
  280. var songs = Session.get("songResults");
  281. if (songs !== undefined && songs.length > 0) {
  282. return true;
  283. } else {
  284. return false;
  285. }
  286. },
  287. singleVideo: function () {
  288. return Session.get("si_or_pl") === "singleVideo";
  289. },
  290. chat: function () {
  291. Meteor.setTimeout(function () {
  292. var elem = document.getElementById('chat');
  293. if (elem !== undefined && elem !== null) {
  294. elem.scrollTop = elem.scrollHeight;
  295. }
  296. }, 100);
  297. return Chat.find({type: Session.get("type")}, {sort: {time: -1}, limit: 50}).fetch().reverse();
  298. },
  299. globalChat: function () {
  300. Meteor.setTimeout(function () {
  301. var elem = document.getElementById('global-chat');
  302. if (elem !== undefined && elem !== null) {
  303. elem.scrollTop = elem.scrollHeight;
  304. }
  305. }, 100);
  306. return Chat.find({type: "global"}, {sort: {time: -1}, limit: 50}).fetch().reverse();
  307. },
  308. likes: function () {
  309. var playlist = Playlists.findOne({type: Session.get("type")});
  310. var likes = 0;
  311. playlist.songs.forEach(function (song) {
  312. if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
  313. likes = song.likes;
  314. return;
  315. }
  316. });
  317. return likes;
  318. },
  319. dislikes: function () {
  320. var playlist = Playlists.findOne({type: Session.get("type")});
  321. var dislikes = 0;
  322. playlist.songs.forEach(function (song) {
  323. if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
  324. dislikes = song.dislikes;
  325. return;
  326. }
  327. });
  328. return dislikes;
  329. },
  330. liked: function () {
  331. if (Meteor.userId()) {
  332. var currentSong = Session.get("currentSong");
  333. if (currentSong && Meteor.user().profile.liked.indexOf(currentSong.mid) !== -1) {
  334. return "liked";
  335. } else {
  336. return "";
  337. }
  338. } else {
  339. "";
  340. }
  341. },
  342. disliked: function () {
  343. if (Meteor.userId()) {
  344. var currentSong = Session.get("currentSong");
  345. if (currentSong && Meteor.user().profile.disliked.indexOf(currentSong.mid) !== -1) {
  346. return "disliked";
  347. } else {
  348. return "";
  349. }
  350. } else {
  351. "";
  352. }
  353. },
  354. type: function () {
  355. var parts = location.href.split('/');
  356. var id = parts.pop().toLowerCase();
  357. return Rooms.findOne({type: id}).display;
  358. },
  359. users: function () {
  360. var parts = location.href.split('/');
  361. var id = parts.pop().toLowerCase();
  362. return Rooms.findOne({type: id}).users;
  363. },
  364. title: function () {
  365. return Session.get("title");
  366. },
  367. artist: function () {
  368. return Session.get("artist");
  369. },
  370. loaded: function () {
  371. return Session.get("loaded");
  372. },
  373. paused: function () {
  374. return Session.get("state") === "paused";
  375. },
  376. private: function () {
  377. return Rooms.findOne({type: Session.get("type")}).private === true;
  378. },
  379. currentSong: function(){
  380. return Session.get("currentSong");
  381. },
  382. reportSong: function(){
  383. Meteor.setInterval(function(){
  384. if($("#report-song").is(":checked")){
  385. Session.set("reportSong", true)
  386. } else { Session.set("reportSong", false) }
  387. }, 500);
  388. return Session.get("reportSong");
  389. },
  390. votes: function () {
  391. console.log(Rooms.findOne({type: Session.get("type")}).votes);
  392. return Rooms.findOne({type: Session.get("type")}).votes;
  393. }
  394. });
  395. Template.settings.helpers({
  396. username: function () {
  397. if (Meteor.user() !== undefined) {
  398. return Meteor.user().profile.username;
  399. } else {
  400. return "";
  401. }
  402. }
  403. });