Browse Source

Added ban method, added ban view.

KrisVos130 9 năm trước cách đây
mục cha
commit
2bdfdbefcd
5 tập tin đã thay đổi với 129 bổ sung6 xóa
  1. 11 0
      app/client/app.css
  2. 39 4
      app/client/client.js
  3. 15 2
      app/client/routes.js
  4. 26 0
      app/client/templates/banned.html
  5. 38 0
      app/server/server.js

+ 11 - 0
app/client/app.css

@@ -1353,3 +1353,14 @@ nav form input[type="image"]{
 #header-dropdown li:hover > a {
     color: #1C39B2 !important;
 }
+
+#ban-icon {
+    font-size: 20em;
+    text-align: center;
+    width: 100%;
+    z-index: 3;
+}
+
+#banned-container {
+    color: white;
+}

+ 39 - 4
app/client/client.js

@@ -637,6 +637,41 @@ Template.room.events({
     }
 });
 
+Template.banned.helpers({
+    bannedAt: function() {
+        if (Session.get("ban") !== undefined) {
+            return Session.get("ban").bannedAt;
+        }
+    },
+    bannedBy: function() {
+        if (Session.get("ban") !== undefined) {
+            return Session.get("ban").bannedBy;
+        }
+    },
+    bannedUntil: function() {
+        if (Session.get("ban") !== undefined) {
+            return Session.get("ban").bannedUntil;
+        }
+    },
+    bannedReason: function() {
+        if (Session.get("ban") !== undefined) {
+            return Session.get("ban").bannedReason;
+        }
+    }
+});
+
+Template.banned.onCreated(function() {
+    if (rTimeInterval !== undefined) {
+        Meteor.clearInterval(rTimeInterval)
+    }
+    rTimeInterval = Meteor.setInterval(function() {
+        Session.set("time", new Date().getTime());
+    }, 10000);
+    Meteor.subscribe("ownBan", Meteor.userId(), function() {
+        Session.set("ban", Meteor.user().punishments.ban);
+    });
+});
+
 Template.registerHelper("rtime", function(date) {
     Session.get("time");
     if (date) {
@@ -644,13 +679,13 @@ Template.registerHelper("rtime", function(date) {
     }
 });
 
-var chatTimeInterval = undefined;
+var rTimeInterval = undefined;
 
 Template.room.onRendered(function() {
-    if (chatTimeInterval !== undefined) {
-        Meteor.clearInterval(chatTimeInterval)
+    if (rTimeInterval !== undefined) {
+        Meteor.clearInterval(rTimeInterval)
     }
-    chatTimeInterval = Meteor.setInterval(function() {
+    rTimeInterval = Meteor.setInterval(function() {
         Session.set("time", new Date().getTime());
     }, 10000);
     $(document).ready(function() {

+ 15 - 2
app/client/routes.js

@@ -1,9 +1,22 @@
-Router.onBeforeAction('loading');
-
 Router.configure({
     loadingTemplate: 'loading'
 });
 
+Router.onBeforeAction(function() {
+    var self = this;
+    var next = self.next;
+    if (Meteor.userId()) {
+        Meteor.call("isBanned", function(err, res) {
+            if (res) {
+                self.render('banned');
+                pause();
+            } else {
+                next();
+            }
+        });
+    }
+});
+
 Router.route("/", {
     template: "home"
 });

+ 26 - 0
app/client/templates/banned.html

@@ -0,0 +1,26 @@
+<template name="banned">
+    {{> alerts}}
+
+    <div class="row">
+        <div class="col-md-4 col-md-offset-4" id="banned-container">
+            <i class="fa fa-gavel fa-5x" id="ban-icon"></i>
+            <hr/>
+            <h3 class="text text-center">You were banned by {{bannedBy}} <span title="{{bannedAt}}">{{rtime bannedAt}}</span>.</h3>
+            <h3 class="text text-center">You will be unbanned <span title="{{bannedAt}}">{{rtime bannedUntil}}</span>.</h3>
+            <h3 class="text text-center">Reason: {{bannedReason}}</h3>
+        </div>
+    </div>
+
+    <ul class="bg-bubbles">
+        <li></li>
+        <li></li>
+        <li></li>
+        <li></li>
+        <li></li>
+        <li></li>
+        <li></li>
+        <li></li>
+        <li></li>
+        <li></li>
+    </ul>
+</template>

+ 38 - 0
app/server/server.js

@@ -394,6 +394,10 @@ Meteor.publish("chat", function() {
     return Chat.find({});
 });
 
+Meteor.publish("ownBan", function(userId) {
+    return Meteor.users.find(userId, {"punishments.ban": 1, "profile": 1});
+});
+
 Meteor.publish("userProfiles", function(username) {
     var settings = Meteor.users.findOne({"profile.usernameL": username}, {fields: {"profile.settings": 1}});
     if (settings !== undefined && settings.profile.settings) {
@@ -419,6 +423,40 @@ function isAdmin() {
 }
 
 Meteor.methods({
+    banUser: function(username, period, reason) {
+        if (isAdmin()) {
+            var user = Meteor.user();
+            var bannedUser = Meteor.users.findOne({"profile.usernameL": username.toLowerCase()});
+            var bannedUntil = (new Date).getTime() + (period * 1000);
+            if (bannedUntil > 8640000000000000) {
+                bannedUntil = 8640000000000000;
+            }
+            bannedUntil = new Date(bannedUntil);
+            var banObject = {bannedBy: user.profile.usernameL, bannedAt: new Date(Date.now()), bannedReason: reason, bannedUntil: bannedUntil};
+            Meteor.users.update({"profile.usernameL": bannedUser.profile.usernameL}, {$set: {"punishments.ban": banObject}});
+            Meteor.users.update({"profile.usernameL": bannedUser.profile.usernameL}, {$push: {"punishments.bans": banObject}});
+        } else {
+            throw new Meteor.Error(403, "Invalid permissions.");
+        }
+    },
+    isBanned: function() {
+        if (Meteor.userId()) {
+            var user = Meteor.user();
+            if (user.punishments && user.punishments.ban) {
+                var ban = user.punishments.ban;
+                if (new Date(ban.bannedUntil).getTime() <= new Date().getTime()) {
+                    Meteor.users.update({"profile.usernameL": user.profile.usernameL}, {$unset: {"punishments.ban": ""}});
+                    return false;
+                } else {
+                    return true;
+                }
+            } else {
+                return false;
+            }
+        } else {
+            return false;
+        }
+    },
     updateSettings: function(showRating) {
         if (Meteor.userId()) {
             var user = Meteor.user();