Forráskód Böngészése

Add limits to Following

Daniel Supernault 6 éve
szülő
commit
d1c0e9aae9

+ 3 - 0
app/Follower.php

@@ -9,6 +9,9 @@ class Follower extends Model
 
     protected $fillable = ['profile_id', 'following_id', 'local_profile'];
 
+    const MAX_FOLLOWING = 7500;
+    const FOLLOW_PER_HOUR = 20;
+
     public function actor()
     {
         return $this->belongsTo(Profile::class, 'profile_id', 'id');

+ 10 - 1
app/Http/Controllers/FollowerController.php

@@ -37,6 +37,8 @@ class FollowerController extends Controller
     protected function handleFollowRequest($item)
     {
         $user = Auth::user()->profile;
+
+
         $target = Profile::where('id', '!=', $user->id)->whereNull('status')->findOrFail($item);
         $private = (bool) $target->is_private;
         $remote = (bool) $target->domain;
@@ -47,7 +49,7 @@ class FollowerController extends Controller
                 ->exists();
 
         if($blocked == true) {
-            return redirect()->back()->with('error', 'You cannot follow this user.');
+            abort(400, 'You cannot follow this user.');
         }
 
         $isFollowing = Follower::whereProfileId($user->id)->whereFollowingId($target->id)->count();
@@ -61,6 +63,13 @@ class FollowerController extends Controller
                 
             }
         } elseif ($isFollowing == 0) {
+            if($user->following()->count() >= Follower::MAX_FOLLOWING) {
+                abort(400, 'You cannot follow more than ' . Follower::MAX_FOLLOWING . ' accounts');
+            }
+
+            if($user->following()->where('followers.created_at', '>', now()->subHour())->count() >= Follower::FOLLOW_PER_HOUR) {
+                abort(400, 'You can only follow ' . Follower::FOLLOW_PER_HOUR . ' users per hour');
+            }
             $follower = new Follower();
             $follower->profile_id = $user->id;
             $follower->following_id = $target->id;

+ 4 - 0
resources/assets/js/components/Activity.vue

@@ -211,6 +211,10 @@ export default {
 						notification.relationship.following = true;
 					}
 				});
+			}).catch(err => {
+				if(err.response.data.message) {
+					swal('Error', err.response.data.message, 'error');
+				}
 			});
 		},
 

+ 3 - 5
resources/assets/js/components/DiscoverComponent.vue

@@ -73,11 +73,9 @@ export default {
         el.addClass('btn-outline-secondary').removeClass('btn-primary');
         el.text('Unfollow');
       }).catch(err => {
-        swal(
-          'Whoops! Something went wrong…',
-          'An error occurred, please try again later.',
-          'error'
-        );
+        if(err.response.data.message) {
+          swal('Error', err.response.data.message, 'error');
+        }
       });
     },
 

+ 9 - 1
resources/assets/js/components/Profile.vue

@@ -950,6 +950,10 @@ export default {
 					this.profile.followers_count++;
 				}
 				this.relationship.following = !this.relationship.following;
+			}).catch(err => {
+				if(err.response.data.message) {
+					swal('Error', err.response.data.message, 'error');
+				}
 			});
 		},
 
@@ -1064,7 +1068,11 @@ export default {
 					this.following.splice(index, 1);
 					this.profile.following_count--;
 				}
-			})
+			}).catch(err => {
+				if(err.response.data.message) {
+					swal('Error', err.response.data.message, 'error');
+				}
+			});
 		},
 
 		momentBackground() {

+ 4 - 0
resources/assets/js/components/SearchResults.vue

@@ -148,6 +148,10 @@ export default {
 				item: id
 			}).then(res => {
 				window.location.href = window.location.href;
+			}).catch(err => {
+				if(err.response.data.message) {
+					swal('Error', err.response.data.message, 'error');
+				}
 			});
 		},
 	}

+ 10 - 2
resources/assets/js/components/Timeline.vue

@@ -1083,7 +1083,11 @@
 						item: id
 				}).then(res => {
 					this.suggestions.splice(index, 1);
-				})
+				}).catch(err => {
+					if(err.response.data.message) {
+						swal('Error', err.response.data.message, 'error');
+					}
+				});
 			},
 
 			followModalAction(id, index, type = 'following') {
@@ -1093,7 +1097,11 @@
 					if(type == 'following') {
 						this.following.splice(index, 1);
 					}
-				})
+				}).catch(err => {
+					if(err.response.data.message) {
+						swal('Error', err.response.data.message, 'error');
+					}
+				});
 			},
 
 			owner(status) {