2
0

helpers.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. Template.admin.helpers({
  2. queueCount: function(display) {
  3. var d = display.toLowerCase();
  4. return queues && "songs" in queues ? queues.songs.length : 0;
  5. var queues = Queues.findOne({type: d});
  6. },
  7. queues: function() {
  8. var queues = Queues.find({}).fetch();
  9. return queues;
  10. },
  11. usersOnline: function(){
  12. Meteor.call("getUserNum", function(err, num){
  13. if(err){
  14. console.log(err);
  15. }
  16. Session.set("userNum", num);
  17. });
  18. return Session.get("userNum");
  19. },
  20. roomUserNum: function(){
  21. var type = this.type;
  22. var userNum = Rooms.findOne({type: type}).users;
  23. return userNum;
  24. },
  25. allUsers: function(){
  26. Meteor.call("getTotalUsers", function(err, num){
  27. Session.set("allUsers", num);
  28. })
  29. return Session.get("allUsers");
  30. },
  31. playlists: function() {
  32. var playlists = Playlists.find({}).fetch();
  33. playlists.map(function(playlist) {
  34. if (Rooms.find({type: playlist.type}).count() !== 1) {
  35. return;
  36. } else {
  37. playlist.display = Rooms.findOne({type: playlist.type}).display;
  38. return playlist;
  39. }
  40. });
  41. return playlists;
  42. },
  43. reportsCount: function(room) {
  44. room = room.toLowerCase();
  45. var reports = Reports.findOne({room:room});
  46. return reports && "report" in reports ? reports.report.length : 0;
  47. }
  48. });
  49. Template.alerts.helpers({
  50. alerts: function() {
  51. return Alerts.find({active: true});
  52. }
  53. });
  54. Template.alertsDashboard.helpers({
  55. "activeAlerts": function() {
  56. return Alerts.find({active: true});
  57. },
  58. "inactiveAlerts": function() {
  59. return Alerts.find({active: false});
  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.header.helpers({
  85. userId: function() {
  86. return Meteor.userId();
  87. }
  88. });
  89. Template.home.helpers({
  90. currentSong: function(){
  91. var type = this.type;
  92. var room = Rooms.findOne({type: type});
  93. if(room !== undefined){
  94. return room.currentSong;
  95. } else {
  96. return false;
  97. }
  98. },
  99. userNum: function(){
  100. var type = this.type;
  101. var userNum = Rooms.findOne({type: type}).users;
  102. return userNum;
  103. }
  104. })
  105. Template.playlist.helpers({
  106. playlist_songs: function() {
  107. parts = location.href.split('/');
  108. id = parts.pop();
  109. type = id.toLowerCase();
  110. var data = Playlists.findOne({type: type});
  111. if (data !== undefined) {
  112. data.songs.map(function(song) {
  113. if (Session.get("currentSong") !== undefined && song.mid === Session.get("currentSong").mid) {
  114. song.current = true;
  115. } else {
  116. song.current = false;
  117. }
  118. return song;
  119. });
  120. return data.songs;
  121. } else {
  122. return [];
  123. }
  124. }
  125. });
  126. Template.profile.helpers({
  127. "real_name": function(){
  128. return Session.get("real_name");
  129. },
  130. "username": function() {
  131. return Session.get("username")
  132. },
  133. "first_joined": function() {
  134. return moment(Session.get("first_joined")).format("DD/MM/YYYY HH:mm:ss");
  135. },
  136. "rank": function() {
  137. return Session.get("rank");
  138. },
  139. loaded: function() {
  140. return Session.get("loaded");
  141. },
  142. likedSongs: function(){
  143. var likedArr = [];
  144. Session.get("liked").forEach(function(mid){
  145. Rooms.find().forEach(function(room){
  146. Playlists.find({type: room.type}).forEach(function(pl){
  147. for(var i in pl.songs){
  148. if(pl.songs[i].mid === mid){
  149. likedArr.push({title: pl.songs[i].title, artist: pl.songs[i].artist, room: room.display});
  150. }
  151. }
  152. });
  153. })
  154. });
  155. return likedArr;
  156. },
  157. dislikedSongs: function(){
  158. var dislikedArr = [];
  159. Session.get("disliked").forEach(function(mid){
  160. Rooms.find().forEach(function(room){
  161. Playlists.find({type: room.type}).forEach(function(pl){
  162. for(var i in pl.songs){
  163. if(pl.songs[i].mid === mid){
  164. dislikedArr.push({title: pl.songs[i].title, artist: pl.songs[i].artist, room: room.display});
  165. }
  166. }
  167. });
  168. })
  169. });
  170. return dislikedArr;
  171. },
  172. isUser: function(){
  173. var parts = Router.current().url.split('/');
  174. var username = parts.pop();
  175. if(username === Meteor.user().profile.username){
  176. return true;
  177. }
  178. }
  179. });
  180. Template.queues.helpers({
  181. queues: function() {
  182. var queues = Queues.find({}).fetch();
  183. queues.map(function(queue) {
  184. if (Rooms.find({type: queue.type}).count() !== 1) {
  185. return;
  186. } else {
  187. queue.display = Rooms.findOne({type: queue.type}).display;
  188. return queue;
  189. }
  190. });
  191. return queues;
  192. }
  193. });
  194. Template.room.helpers({
  195. singleVideo: function() {
  196. return true;
  197. },
  198. chat: function() {
  199. Meteor.setTimeout(function() {
  200. var elem = document.getElementById('chat');
  201. if (elem !== undefined && elem !== null) {
  202. elem.scrollTop = elem.scrollHeight;
  203. }
  204. }, 100);
  205. return Chat.find({type: Session.get("type")}, {sort: {time: -1}, limit: 50 }).fetch().reverse();
  206. },
  207. globalChat: function() {
  208. Meteor.setTimeout(function() {
  209. var elem = document.getElementById('global-chat');
  210. if (elem !== undefined && elem !== null) {
  211. elem.scrollTop = elem.scrollHeight;
  212. }
  213. }, 100);
  214. return Chat.find({type: "global"}, {sort: {time: -1}, limit: 50 }).fetch().reverse();
  215. },
  216. likes: function() {
  217. var playlist = Playlists.findOne({type: Session.get("type")});
  218. var likes = 0;
  219. playlist.songs.forEach(function(song) {
  220. if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
  221. likes = song.likes;
  222. return;
  223. }
  224. });
  225. return likes;
  226. },
  227. dislikes: function() {
  228. var playlist = Playlists.findOne({type: Session.get("type")});
  229. var dislikes = 0;
  230. playlist.songs.forEach(function(song) {
  231. if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
  232. dislikes = song.dislikes;
  233. return;
  234. }
  235. });
  236. return dislikes;
  237. },
  238. liked: function() {
  239. if (Meteor.userId()) {
  240. var currentSong = Session.get("currentSong");
  241. if (currentSong && Meteor.user().profile.liked.indexOf(currentSong.mid) !== -1) {
  242. return "active";
  243. } else {
  244. return "";
  245. }
  246. } else {
  247. "";
  248. }
  249. },
  250. disliked: function() {
  251. if (Meteor.userId()) {
  252. var currentSong = Session.get("currentSong");
  253. if (currentSong && Meteor.user().profile.disliked.indexOf(currentSong.mid) !== -1) {
  254. return "active";
  255. } else {
  256. return "";
  257. }
  258. } else {
  259. "";
  260. }
  261. },
  262. type: function() {
  263. var parts = location.href.split('/');
  264. var id = parts.pop().toLowerCase();
  265. return Rooms.findOne({type: id}).display;
  266. },
  267. users: function() {
  268. var parts = location.href.split('/');
  269. var id = parts.pop().toLowerCase();
  270. return Rooms.findOne({type: id}).users;
  271. },
  272. title: function(){
  273. return Session.get("title");
  274. },
  275. artist: function(){
  276. return Session.get("artist");
  277. },
  278. loaded: function() {
  279. return Session.get("loaded");
  280. },
  281. paused: function() {
  282. return Session.get("state") === "paused";
  283. },
  284. private: function() {
  285. return Rooms.findOne({type: Session.get("type")}).private === true;
  286. },
  287. report: function() {
  288. return Session.get("reportObj");
  289. },
  290. reportSong: function() {
  291. return Session.get("reportSong");
  292. },
  293. reportTitle: function() {
  294. return Session.get("reportTitle");
  295. },
  296. reportAuthor: function() {
  297. return Session.get("reportAuthor");
  298. },
  299. reportDuration: function() {
  300. return Session.get("reportDuration");
  301. },
  302. reportAudio: function() {
  303. return Session.get("reportAudio");
  304. },
  305. reportAlbumart: function() {
  306. return Session.get("reportAlbumart");
  307. },
  308. reportOther: function() {
  309. return Session.get("reportOther");
  310. },
  311. currentSong: function() {
  312. return Session.get("currentSong");
  313. },
  314. previousSong: function() {
  315. return Session.get("previousSong");
  316. },
  317. currentSongR: function() {
  318. return Session.get("currentSongR");
  319. },
  320. previousSongR: function() {
  321. return Session.get("previousSongR");
  322. },
  323. reportingSong: function() {
  324. if (Session.get("reportPrevious")) {
  325. return Session.get("previousSongR");
  326. } else {
  327. return Session.get("currentSongR");
  328. }
  329. },
  330. votes: function(){
  331. return Rooms.findOne({type: Session.get("type")}).votes;
  332. }
  333. });
  334. Template.settings.helpers({
  335. username: function() {
  336. if (Meteor.user() !== undefined) {
  337. return Meteor.user().profile.username;
  338. } else {
  339. return "";
  340. }
  341. }
  342. });
  343. Template.stations.helpers({
  344. playlist: function() {
  345. var query = {type: Session.get("playlistToEdit").toLowerCase()};
  346. var playlists = Playlists.find(query).fetch();
  347. return playlists;
  348. },
  349. whichStation: function(){
  350. return Session.get("playlistToEdit");
  351. },
  352. reports: function() {
  353. var query = {room: Session.get("playlistToEdit").toLowerCase()};
  354. var reports = Reports.find(query).fetch();
  355. console.log(reports);
  356. return reports;
  357. }
  358. });