2023_04_20_092740_fix_account_blocks.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. use Illuminate\Database\Migrations\Migration;
  3. use Illuminate\Database\Schema\Blueprint;
  4. use Illuminate\Support\Facades\Schema;
  5. use App\Follower;
  6. use App\Notification;
  7. use App\Profile;
  8. use App\UserFilter;
  9. use App\Services\AccountService;
  10. use App\Services\FollowerService;
  11. use App\Services\NotificationService;
  12. use App\Services\RelationshipService;
  13. use App\Services\UserFilterService;
  14. return new class extends Migration
  15. {
  16. /**
  17. * Run the migrations.
  18. */
  19. public function up(): void
  20. {
  21. UserFilter::whereFilterType('block')
  22. ->whereFilterableType('App\Profile')
  23. ->chunk(10, function($filters) {
  24. foreach($filters as $filter) {
  25. $actor = Profile::whereNull('status')->find($filter->user_id);
  26. if(!$actor) {
  27. continue;
  28. }
  29. $target = Profile::whereNull('status')->find($filter->filterable_id);
  30. if(!$target) {
  31. continue;
  32. }
  33. $followed = Follower::whereProfileId($target->id)->whereFollowingId($actor->id)->first();
  34. if($followed) {
  35. $followed->delete();
  36. $target->following_count = Follower::whereProfileId($target->id)->count();
  37. $target->save();
  38. $actor->followers_count = Follower::whereFollowingId($actor->id)->count();
  39. $actor->save();
  40. FollowerService::remove($target->id, $actor->id);
  41. AccountService::del($actor->id);
  42. AccountService::del($target->id);
  43. }
  44. $following = Follower::whereProfileId($actor->id)->whereFollowingId($target->id)->first();
  45. if($following) {
  46. $following->delete();
  47. $actor->followers_count = Follower::whereFollowingId($target->id)->count();
  48. $actor->save();
  49. $target->following_count = Follower::whereProfileId($actor->id)->count();
  50. $target->save();
  51. FollowerService::remove($actor->id, $target->id);
  52. AccountService::del($actor->id);
  53. AccountService::del($target->id);
  54. }
  55. Notification::whereProfileId($actor->id)
  56. ->whereActorId($target->id)
  57. ->get()
  58. ->map(function($n) use($actor) {
  59. NotificationService::del($actor->id, $n['id']);
  60. $n->forceDelete();
  61. });
  62. UserFilterService::block($actor->id, $target->id);
  63. RelationshipService::refresh($actor->id, $target->id);
  64. }
  65. });
  66. }
  67. /**
  68. * Reverse the migrations.
  69. */
  70. public function down(): void
  71. {
  72. //
  73. }
  74. };