helpers.js 11 KB

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