UserObserver.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace App\Observers;
  3. use App\Jobs\AvatarPipeline\CreateAvatar;
  4. use App\Follower;
  5. use App\Profile;
  6. use App\User;
  7. use App\UserSetting;
  8. use App\Services\UserFilterService;
  9. use App\Models\DefaultDomainBlock;
  10. use App\Models\UserDomainBlock;
  11. use App\Jobs\FollowPipeline\FollowPipeline;
  12. use DB;
  13. use App\Services\FollowerService;
  14. class UserObserver
  15. {
  16. /**
  17. * Handle the notification "created" event.
  18. *
  19. * @param \App\User $user
  20. * @return void
  21. */
  22. public function created(User $user): void
  23. {
  24. $this->handleUser($user);
  25. }
  26. /**
  27. * Listen to the User saved event.
  28. *
  29. * @param \App\User $user
  30. *
  31. * @return void
  32. */
  33. public function saved(User $user)
  34. {
  35. $this->handleUser($user);
  36. }
  37. /**
  38. * Listen to the User updated event.
  39. *
  40. * @param \App\User $user
  41. *
  42. * @return void
  43. */
  44. public function updated(User $user): void
  45. {
  46. $this->handleUser($user);
  47. if($user->profile) {
  48. $this->applyDefaultDomainBlocks($user);
  49. }
  50. }
  51. /**
  52. * Handle the user "deleted" event.
  53. *
  54. * @param \App\User $user
  55. * @return void
  56. */
  57. public function deleted(User $user)
  58. {
  59. FollowerService::delCache($user->profile_id);
  60. }
  61. protected function handleUser($user)
  62. {
  63. if(in_array($user->status, ['deleted', 'delete'])) {
  64. return;
  65. }
  66. if(Profile::whereUsername($user->username)->exists()) {
  67. return;
  68. }
  69. if (empty($user->profile)) {
  70. $profile = DB::transaction(function() use($user) {
  71. $profile = new Profile();
  72. $profile->user_id = $user->id;
  73. $profile->username = $user->username;
  74. $profile->name = $user->name;
  75. $pkiConfig = [
  76. 'digest_alg' => 'sha512',
  77. 'private_key_bits' => 2048,
  78. 'private_key_type' => OPENSSL_KEYTYPE_RSA,
  79. ];
  80. $pki = openssl_pkey_new($pkiConfig);
  81. openssl_pkey_export($pki, $pki_private);
  82. $pki_public = openssl_pkey_get_details($pki);
  83. $pki_public = $pki_public['key'];
  84. $profile->private_key = $pki_private;
  85. $profile->public_key = $pki_public;
  86. $profile->save();
  87. $this->applyDefaultDomainBlocks($user);
  88. return $profile;
  89. });
  90. DB::transaction(function() use($user, $profile) {
  91. $user = User::findOrFail($user->id);
  92. $user->profile_id = $profile->id;
  93. $user->save();
  94. CreateAvatar::dispatch($profile);
  95. });
  96. if(config_cache('account.autofollow') == true) {
  97. $names = config_cache('account.autofollow_usernames');
  98. $names = explode(',', $names);
  99. if(!$names || !last($names)) {
  100. return;
  101. }
  102. $profiles = Profile::whereIn('username', $names)->get();
  103. if($profiles) {
  104. foreach($profiles as $p) {
  105. $follower = new Follower;
  106. $follower->profile_id = $profile->id;
  107. $follower->following_id = $p->id;
  108. $follower->save();
  109. FollowPipeline::dispatch($follower);
  110. }
  111. }
  112. }
  113. }
  114. if (empty($user->settings)) {
  115. DB::transaction(function() use($user) {
  116. UserSetting::firstOrCreate([
  117. 'user_id' => $user->id
  118. ]);
  119. });
  120. }
  121. }
  122. protected function applyDefaultDomainBlocks($user)
  123. {
  124. if($user->profile_id == null) {
  125. return;
  126. }
  127. $defaultDomainBlocks = DefaultDomainBlock::pluck('domain')->toArray();
  128. if(!$defaultDomainBlocks || !count($defaultDomainBlocks)) {
  129. return;
  130. }
  131. foreach($defaultDomainBlocks as $domain) {
  132. UserDomainBlock::updateOrCreate([
  133. 'profile_id' => $user->profile_id,
  134. 'domain' => strtolower(trim($domain))
  135. ]);
  136. }
  137. }
  138. }