FixDuplicateProfiles.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\{
  5. Avatar,
  6. Bookmark,
  7. Collection,
  8. DirectMessage,
  9. FollowRequest,
  10. Follower,
  11. HashtagFollow,
  12. Like,
  13. Media,
  14. MediaTag,
  15. Mention,
  16. Profile,
  17. Report,
  18. ReportComment,
  19. ReportLog,
  20. StatusArchived,
  21. StatusHashtag,
  22. StatusView,
  23. Status,
  24. Story,
  25. StoryView,
  26. User,
  27. UserFilter
  28. };
  29. use App\Models\{
  30. Conversation,
  31. Portfolio,
  32. UserPronoun
  33. };
  34. use DB, Cache;
  35. class FixDuplicateProfiles extends Command
  36. {
  37. /**
  38. * The name and signature of the console command.
  39. *
  40. * @var string
  41. */
  42. protected $signature = 'fix:profile:duplicates';
  43. /**
  44. * The console command description.
  45. *
  46. * @var string
  47. */
  48. protected $description = 'Fix duplicate profiles';
  49. /**
  50. * Create a new command instance.
  51. *
  52. * @return void
  53. */
  54. public function __construct()
  55. {
  56. parent::__construct();
  57. }
  58. /**
  59. * Execute the console command.
  60. *
  61. * @return mixed
  62. */
  63. public function handle()
  64. {
  65. $duplicates = DB::table('profiles')
  66. ->whereNull('domain')
  67. ->select('username', DB::raw('COUNT(*) as "count"'))
  68. ->groupBy('username')
  69. ->havingRaw('COUNT(*) > 1')
  70. ->pluck('username');
  71. foreach($duplicates as $dupe) {
  72. $ids = Profile::whereNull('domain')->whereUsername($dupe)->pluck('id');
  73. if(!$ids || $ids->count() != 2) {
  74. continue;
  75. }
  76. $id = $ids->first();
  77. $oid = $ids->last();
  78. $user = User::whereUsername($dupe)->first();
  79. if($user) {
  80. $user->profile_id = $id;
  81. $user->save();
  82. } else {
  83. continue;
  84. }
  85. $this->checkAvatar($id, $oid);
  86. $this->checkBookmarks($id, $oid);
  87. $this->checkCollections($id, $oid);
  88. $this->checkConversations($id, $oid);
  89. $this->checkDirectMessages($id, $oid);
  90. $this->checkFollowRequest($id, $oid);
  91. $this->checkFollowers($id, $oid);
  92. $this->checkHashtagFollow($id, $oid);
  93. $this->checkLikes($id, $oid);
  94. $this->checkMedia($id, $oid);
  95. $this->checkMediaTag($id, $oid);
  96. $this->checkMention($id, $oid);
  97. $this->checkPortfolio($id, $oid);
  98. $this->checkReport($id, $oid);
  99. $this->checkStatusArchived($id, $oid);
  100. $this->checkStatusHashtag($id, $oid);
  101. $this->checkStatusView($id, $oid);
  102. $this->checkStatus($id, $oid);
  103. $this->checkStory($id, $oid);
  104. $this->checkStoryView($id, $oid);
  105. $this->checkUserFilter($id, $oid);
  106. $this->checkUserPronoun($id, $oid);
  107. Profile::find($oid)->forceDelete();
  108. }
  109. Cache::clear();
  110. }
  111. protected function checkAvatar($id, $oid)
  112. {
  113. Avatar::whereProfileId($oid)->forceDelete();
  114. }
  115. protected function checkBookmarks($id, $oid)
  116. {
  117. Bookmark::whereProfileId($oid)->update(['profile_id' => $id]);
  118. }
  119. protected function checkCollections($id, $oid)
  120. {
  121. Collection::whereProfileId($oid)->update(['profile_id' => $id]);
  122. }
  123. protected function checkConversations($id, $oid)
  124. {
  125. Conversation::whereToId($oid)->update(['to_id' => $id]);
  126. Conversation::whereFromId($oid)->update(['from_id' => $id]);
  127. }
  128. protected function checkDirectMessages($id, $oid)
  129. {
  130. DirectMessage::whereToId($oid)->update(['to_id' => $id]);
  131. DirectMessage::whereFromId($oid)->update(['from_id' => $id]);
  132. }
  133. protected function checkFollowRequest($id, $oid)
  134. {
  135. FollowRequest::whereFollowerId($oid)->update(['follower_id' => $id]);
  136. FollowRequest::whereFollowingId($oid)->update(['following_id' => $id]);
  137. }
  138. protected function checkFollowers($id, $oid)
  139. {
  140. $f = Follower::whereProfileId($oid)->pluck('following_id');
  141. foreach($f as $fo) {
  142. Follower::updateOrCreate([
  143. 'profile_id' => $id,
  144. 'following_id' => $fo
  145. ]);
  146. }
  147. $f = Follower::whereFollowingId($oid)->pluck('profile_id');
  148. foreach($f as $fo) {
  149. Follower::updateOrCreate([
  150. 'profile_id' => $fo,
  151. 'following_id' => $id
  152. ]);
  153. }
  154. }
  155. protected function checkHashtagFollow($id, $oid)
  156. {
  157. HashtagFollow::whereProfileId($oid)->update(['profile_id' => $id]);
  158. }
  159. protected function checkLikes($id, $oid)
  160. {
  161. Like::whereStatusProfileId($oid)->update(['status_profile_id' => $id]);
  162. Like::whereProfileId($oid)->update(['profile_id' => $id]);
  163. }
  164. protected function checkMedia($id, $oid)
  165. {
  166. Media::whereProfileId($oid)->update(['profile_id' => $id]);
  167. }
  168. protected function checkMediaTag($id, $oid)
  169. {
  170. MediaTag::whereProfileId($oid)->update(['profile_id' => $id]);
  171. }
  172. protected function checkMention($id, $oid)
  173. {
  174. Mention::whereProfileId($oid)->update(['profile_id' => $id]);
  175. }
  176. protected function checkPortfolio($id, $oid)
  177. {
  178. Portfolio::whereProfileId($oid)->update(['profile_id' => $id]);
  179. }
  180. protected function checkReport($id, $oid)
  181. {
  182. ReportComment::whereProfileId($oid)->update(['profile_id' => $id]);
  183. ReportLog::whereProfileId($oid)->update(['profile_id' => $id]);
  184. Report::whereProfileId($oid)->update(['profile_id' => $id]);
  185. }
  186. protected function checkStatusArchived($id, $oid)
  187. {
  188. StatusArchived::whereProfileId($oid)->update(['profile_id' => $id]);
  189. }
  190. protected function checkStatusHashtag($id, $oid)
  191. {
  192. StatusHashtag::whereProfileId($oid)->update(['profile_id' => $id]);
  193. }
  194. protected function checkStatusView($id, $oid)
  195. {
  196. StatusView::whereStatusProfileId($oid)->update(['profile_id' => $id]);
  197. StatusView::whereProfileId($oid)->update(['profile_id' => $id]);
  198. }
  199. protected function checkStatus($id, $oid)
  200. {
  201. Status::whereProfileId($oid)->update(['profile_id' => $id]);
  202. }
  203. protected function checkStory($id, $oid)
  204. {
  205. Story::whereProfileId($oid)->update(['profile_id' => $id]);
  206. }
  207. protected function checkStoryView($id, $oid)
  208. {
  209. StoryView::whereProfileId($oid)->update(['profile_id' => $id]);
  210. }
  211. protected function checkUserFilter($id, $oid)
  212. {
  213. UserFilter::whereUserId($oid)->update(['user_id' => $id]);
  214. UserFilter::whereFilterableType('App\Profile')->whereFilterableId($oid)->update(['filterable_id' => $id]);
  215. }
  216. protected function checkUserPronoun($id, $oid)
  217. {
  218. UserPronoun::whereProfileId($oid)->update(['profile_id' => $id]);
  219. }
  220. }