UserObserver.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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\Jobs\FollowPipeline\FollowPipeline;
  9. use DB;
  10. class UserObserver
  11. {
  12. /**
  13. * Listen to the User created event.
  14. *
  15. * @param \App\User $user
  16. *
  17. * @return void
  18. */
  19. public function saved(User $user)
  20. {
  21. if($user->status == 'deleted') {
  22. return;
  23. }
  24. if (empty($user->profile)) {
  25. $profile = DB::transaction(function() use($user) {
  26. $profile = new Profile();
  27. $profile->user_id = $user->id;
  28. $profile->username = $user->username;
  29. $profile->name = $user->name;
  30. $pkiConfig = [
  31. 'digest_alg' => 'sha512',
  32. 'private_key_bits' => 2048,
  33. 'private_key_type' => OPENSSL_KEYTYPE_RSA,
  34. ];
  35. $pki = openssl_pkey_new($pkiConfig);
  36. openssl_pkey_export($pki, $pki_private);
  37. $pki_public = openssl_pkey_get_details($pki);
  38. $pki_public = $pki_public['key'];
  39. $profile->private_key = $pki_private;
  40. $profile->public_key = $pki_public;
  41. $profile->save();
  42. return $profile;
  43. });
  44. DB::transaction(function() use($user, $profile) {
  45. $user = User::findOrFail($user->id);
  46. $user->profile_id = $profile->id;
  47. $user->save();
  48. CreateAvatar::dispatch($profile);
  49. });
  50. if(config_cache('account.autofollow') == true) {
  51. $names = config_cache('account.autofollow_usernames');
  52. $names = explode(',', $names);
  53. if(!$names || !last($names)) {
  54. return;
  55. }
  56. $profiles = Profile::whereIn('username', $names)->get();
  57. if($profiles) {
  58. foreach($profiles as $p) {
  59. $follower = new Follower;
  60. $follower->profile_id = $profile->id;
  61. $follower->following_id = $p->id;
  62. $follower->save();
  63. FollowPipeline::dispatch($follower);
  64. }
  65. }
  66. }
  67. }
  68. if (empty($user->settings)) {
  69. DB::transaction(function() use($user) {
  70. UserSetting::firstOrCreate([
  71. 'user_id' => $user->id
  72. ]);
  73. });
  74. }
  75. }
  76. }