Jelajahi Sumber

Added bandwidth limit check

Will Browning 6 tahun lalu
induk
melakukan
e1554b141c

+ 17 - 10
app/Console/Commands/ReceiveEmail.php

@@ -115,6 +115,8 @@ class ReceiveEmail extends Command
                     continue;
                 }
 
+                $this->checkBandwidthLimit($user);
+
                 $this->checkRateLimit($user);
 
                 // Check whether this email is a reply or a new email to be forwarded.
@@ -158,10 +160,6 @@ class ReceiveEmail extends Command
 
                 $user->bandwidth += $this->size;
                 $user->save();
-
-                if ($user->nearBandwidthLimit()) {
-                    $user->notify(new NearBandwidthLimit());
-                }
             }
         }
     }
@@ -178,7 +176,7 @@ class ReceiveEmail extends Command
         if (!isset($alias->id)) {
             // This is a new alias.
             if ($user->hasExceededNewAliasLimit()) {
-                $this->error('4.2.1 New aliases per hour limit exceeded for user ' . $user->username . '.');
+                $this->error('4.2.1 New aliases per hour limit exceeded for user.');
 
                 exit(1);
             }
@@ -221,10 +219,19 @@ class ReceiveEmail extends Command
 
             $user->bandwidth += $this->size;
             $user->save();
+        }
+    }
 
-            if ($user->nearBandwidthLimit()) {
-                $user->notify(new NearBandwidthLimit());
-            }
+    protected function checkBandwidthLimit($user)
+    {
+        if ($user->hasReachedBandwidthLimit()) {
+            $this->error('5.2.1 Bandwidth limit exceeded for user. Please try again later.');
+
+            exit(1);
+        }
+
+        if ($user->nearBandwidthLimit()) {
+            $user->notify(new NearBandwidthLimit());
         }
     }
 
@@ -236,8 +243,8 @@ class ReceiveEmail extends Command
             ->then(
                 function () {
                 },
-                function () use ($user) {
-                    $this->error('4.2.1 Rate limit exceeded for user ' . $user->username . '. Please try again later.');
+                function () {
+                    $this->error('4.2.1 Rate limit exceeded for user. Please try again later.');
 
                     exit(1);
                 }

+ 8 - 3
app/User.php

@@ -186,17 +186,17 @@ class User extends Authenticatable implements MustVerifyEmail
 
     public function totalEmailsForwarded()
     {
-        return $this->aliases()->sum('emails_forwarded');
+        return $this->aliases()->withTrashed()->sum('emails_forwarded');
     }
 
     public function totalEmailsBlocked()
     {
-        return $this->aliases()->sum('emails_blocked');
+        return $this->aliases()->withTrashed()->sum('emails_blocked');
     }
 
     public function totalEmailsReplied()
     {
-        return $this->aliases()->sum('emails_replied');
+        return $this->aliases()->withTrashed()->sum('emails_replied');
     }
 
     public function getBandwidthLimit()
@@ -215,6 +215,11 @@ class User extends Authenticatable implements MustVerifyEmail
         return ($this->bandwidth / $this->getBandwidthLimit()) > 0.9;
     }
 
+    public function hasReachedBandwidthLimit()
+    {
+        return $this->bandwidth >= $this->getBandwidthLimit();
+    }
+
     public function hasExceededNewAliasLimit()
     {
         return $this

+ 1 - 1
tests/Feature/ReplyToEmailTest.php

@@ -68,7 +68,7 @@ class ReplyToEmailTest extends TestCase
     }
 
     /** @test */
-    public function it_can_reply_to__multiple_emails_from_file()
+    public function it_can_reply_to_multiple_emails_from_file()
     {
         Mail::fake();
 

+ 3 - 3
tests/Unit/UserTest.php

@@ -65,7 +65,7 @@ class UserTest extends TestCase
             'active' => false
         ]);
 
-        $this->assertEquals(7, $this->user->totalEmailsForwarded());
+        $this->assertEquals(10, $this->user->totalEmailsForwarded());
     }
 
     /** @test */
@@ -88,7 +88,7 @@ class UserTest extends TestCase
             'active' => false
         ]);
 
-        $this->assertEquals(4, $this->user->totalEmailsBlocked());
+        $this->assertEquals(8, $this->user->totalEmailsBlocked());
     }
 
     /** @test */
@@ -111,7 +111,7 @@ class UserTest extends TestCase
             'active' => false
         ]);
 
-        $this->assertEquals(3, $this->user->totalEmailsReplied());
+        $this->assertEquals(7, $this->user->totalEmailsReplied());
     }
 
     /** @test */