瀏覽代碼

Merge remote-tracking branch 'origin/master'

KrisVos130 9 年之前
父節點
當前提交
5afc8a54a6
共有 3 個文件被更改,包括 112 次插入31 次删除
  1. 2 0
      .gitignore
  2. 99 26
      app/app.js
  3. 11 5
      app/templates/room.html

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+app/settings.json
+app/mup.json

+ 99 - 26
app/app.js

@@ -214,6 +214,12 @@ if (Meteor.isClient) {
     });
 
     Template.room.events({
+        "click #like": function(e) {
+            Meteor.call("likeSong", Session.get("currentSong").mid);
+        },
+        "click #dislike": function(e) {
+            Meteor.call("dislikeSong", Session.get("currentSong").mid);
+        },
         "click #report-prev": function(e) {
             if (Session.get("previousSong") !== undefined) {
                 Session.set("reportPrevious", true);
@@ -246,30 +252,6 @@ if (Meteor.isClient) {
             });
             $("#close-modal-a").click();
         },
-        "click #smile-modal": function(e){
-            e.preventDefault();
-            if (Session.get("smileClicked")) {
-                $("#smile-modal").removeClass("active");
-                Session.set("smileClicked", false);
-            } else {
-				$("#meh-modal").removeClass("active");
-				Session.set("mehClicked", false);
-                $("#smile-modal").addClass("active");
-                Session.set("smileClicked", true);
-            }
-        },
-        "click #meh-modal": function(e){
-            e.preventDefault();
-            if (Session.get("mehClicked")) {
-                $("#meh-modal").removeClass("active");
-                Session.set("mehClicked", false);
-            } else {
-				$("#smile-modal").removeClass("active");
-				Session.set("smileClicked", false);
-                $("#meh-modal").addClass("active");
-                Session.set("mehClicked", true);
-            }
-        },
         "click #toggle-video": function(e){
             e.preventDefault();
             if (Session.get("mediaHidden")) {
@@ -525,6 +507,52 @@ if (Meteor.isClient) {
     });
 
     Template.room.helpers({
+        likes: function() {
+            var playlist = Playlists.findOne({type: Session.get("type")});
+            var likes = 0;
+            playlist.songs.forEach(function(song) {
+                if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
+                    likes = song.likes;
+                    return;
+                }
+            });
+            return likes;
+        },
+        dislikes: function() {
+            var playlist = Playlists.findOne({type: Session.get("type")});
+            var dislikes = 0;
+            playlist.songs.forEach(function(song) {
+                if (Session.get("currentSong") && song.mid === Session.get("currentSong").mid) {
+                    dislikes = song.dislikes;
+                    return;
+                }
+            });
+            return dislikes;
+        },
+        liked: function() {
+            if (Meteor.userId()) {
+                var currentSong = Session.get("currentSong");
+                if (currentSong && Meteor.user().profile.liked.indexOf(currentSong.mid) !== -1) {
+                    return "active";
+                } else {
+                    return "";
+                }
+            } else {
+                "";
+            }
+        },
+        disliked: function() {
+            if (Meteor.userId()) {
+                var currentSong = Session.get("currentSong");
+                if (currentSong && Meteor.user().profile.disliked.indexOf(currentSong.mid) !== -1) {
+                    return "active";
+                } else {
+                    return "";
+                }
+            } else {
+                "";
+            }
+        },
         type: function() {
             var parts = location.href.split('/');
             var id = parts.pop().toLowerCase();
@@ -1312,7 +1340,7 @@ if (Meteor.isServer) {
                 username = user.username;
             }
         }
-        user.profile = {username: username, usernameL: username.toLowerCase(), rank: "default"};
+        user.profile = {username: username, usernameL: username.toLowerCase(), rank: "default", liked: [], disliked: []};
         return user;
     });
 
@@ -1370,6 +1398,46 @@ if (Meteor.isServer) {
     }
 
     Meteor.methods({
+        likeSong: function(mid) {
+            if (Meteor.userId()) {
+                var user = Meteor.user();
+                if (user.profile.liked.indexOf(mid) === -1) {
+                    Meteor.users.update({"profile.username": user.profile.username}, {$push: {"profile.liked": mid}});
+                    Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.likes": 1}})
+                } else {
+                    Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.liked": mid}});
+                    Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.likes": -1}})
+                }
+                
+                if (user.profile.disliked.indexOf(mid) !== -1) {
+                    Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.disliked": mid}});
+                    Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.dislikes": -1}})
+                }
+                return true;
+            } else {
+                throw new Meteor.Error(403, "Invalid permissions.");
+            }
+        },
+        dislikeSong: function(mid) {
+            if (Meteor.userId()) {
+                var user = Meteor.user();
+                if (user.profile.disliked.indexOf(mid) === -1) {
+                    Meteor.users.update({"profile.username": user.profile.username}, {$push: {"profile.disliked": mid}});
+                    Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.dislikes": 1}});
+                } else {
+                    Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.disliked": mid}});
+                    Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.dislikes": -1}});
+                }
+
+                if (user.profile.liked.indexOf(mid) !== -1) {
+                    Meteor.users.update({"profile.username": user.profile.username}, {$pull: {"profile.liked": mid}});
+                    Playlists.update({"songs.mid": mid}, {$inc: {"songs.$.likes": -1}});
+                }
+                return true;
+            } else {
+                throw new Meteor.Error(403, "Invalid permissions.");
+            }
+        },
         submitReport: function(report, id) {
             var obj = report;
             obj.id = id;
@@ -1521,6 +1589,9 @@ if (Meteor.isServer) {
                         Playlists.insert({type: type, songs: []});
                     }
                     if (songData !== undefined && Object.keys(songData).length === 7 && songData.type !== undefined && songData.mid !== undefined && songData.title !== undefined && songData.title !== undefined && songData.artist !== undefined && songData.duration !== undefined && songData.img !== undefined) {
+                        songData.likes = 0;
+                        songData.dislikes = 0
+
                         Playlists.update({type: type}, {
                             $push: {
                                 songs: {
@@ -1530,7 +1601,9 @@ if (Meteor.isServer) {
                                     artist: songData.artist,
                                     duration: songData.duration,
                                     img: songData.img,
-                                    type: songData.type
+                                    type: songData.type,
+                                    likes: songData.likes,
+                                    dislikes: songData.dislikes
                                 }
                             }
                         });

+ 11 - 5
app/templates/room.html

@@ -52,7 +52,14 @@
                             <button type="button" id="toggle-video" class="button">Hide video</button>
                         </div>
                         <div class="col-md-3">
-                            <button title="Report this song!" type="button" id="report-modal" class="btn btn-warning btn-lg report-button" data-toggle="modal" data-target="#reportModal"><i class="fa fa-flag"></i></button>
+                        {{#if currentUser}}
+                            <button title="Smile to this song." type="button" id="like" class="btn btn-success btn-lg {{liked}}"><i class="fa fa-smile-o"> {{likes}}</i></button>
+                            <button title="I dislike this song." type="button" id="dislike" class="btn btn-danger btn-lg {{disliked}}"><i class="fa fa-meh-o"> {{dislikes}}</i></button>
+                        {{else}}
+                            <button title="You need to be logged to smile this song." type="button" class="btn btn-success btn-lg" disabled><i class="fa fa-smile-o"> {{likes}}</i></button> 
+                            <button title="You need to be logged to dislike this song." type="button" class="btn btn-danger btn-lg" disabled><i class="fa fa-meh-o"> {{dislikes}}</i></button>
+                        {{/if}}
+                        <button title="Report this song!" type="button" id="report-modal" class="btn btn-warning btn-lg report-button" data-toggle="modal" data-target="#reportModal"><i class="fa fa-flag"></i></button>
                         </div>
                     </div>
                 </div>
@@ -112,10 +119,9 @@
 
                     <!-- Modal content-->
                     <div class="modal-content">
-                        <div class="modal-header">
-                            <button type="button" class="close" data-dismiss="modal">&times;</button>
-                            <h4 class="modal-title">Report this song</h4>
-                        </div>
+                         <div class="col-md-3">
+                             <button title="Report this song!" type="button" id="report-modal" class="btn btn-warning btn-lg report-button" data-toggle="modal" data-target="#reportModal"><i class="fa fa-flag"></i></button>
+                         </div>
                         <div class="modal-body">
 
                             <button type="button" id="report-prev" class="btn btn-warning btn-lg btn-block">Report previous song ({{previousSongR.title}})</button>