ModLogService.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. namespace App\Services;
  3. use Auth;
  4. use App\ModLog;
  5. use App\Notification;
  6. use App\User;
  7. class ModLogService {
  8. protected $log;
  9. public function __construct()
  10. {
  11. $this->log = new \StdClass;
  12. }
  13. public static function boot()
  14. {
  15. return new self;
  16. }
  17. public function user(User $user)
  18. {
  19. $this->log->user = $user;
  20. return $this;
  21. }
  22. public function objectUid($val = null)
  23. {
  24. $this->log->object_uid = $val;
  25. return $this;
  26. }
  27. public function objectId($val = null)
  28. {
  29. $this->log->object_id = $val;
  30. return $this;
  31. }
  32. public function objectType($val = null)
  33. {
  34. $this->log->object_type = $val;
  35. return $this;
  36. }
  37. public function action($val = null)
  38. {
  39. $this->log->action = $val;
  40. return $this;
  41. }
  42. public function message($val = null)
  43. {
  44. $this->log->message = $val;
  45. return $this;
  46. }
  47. public function metadata(array $val = null)
  48. {
  49. $this->log->metadata = json_encode($val);
  50. return $this;
  51. }
  52. public function accessLevel($val = null)
  53. {
  54. if(!in_array($val, ['admin', 'mod'])) {
  55. return $this;
  56. }
  57. $this->log->access_level = $val;
  58. return $this;
  59. }
  60. public function save($res = false)
  61. {
  62. $log = $this->log;
  63. if(!isset($log->user)) {
  64. throw new \Exception('Invalid ModLog attribute.');
  65. }
  66. $ml = new ModLog();
  67. $ml->user_id = $log->user->id;
  68. $ml->user_username = $log->user->username;
  69. $ml->object_uid = $log->object_uid ?? null;
  70. $ml->object_id = $log->object_id ?? null;
  71. $ml->object_type = $log->object_type ?? null;
  72. $ml->action = $log->action ?? null;
  73. $ml->message = $log->message ?? null;
  74. $ml->metadata = $log->metadata ?? null;
  75. $ml->access_level = $log->access_level ?? 'admin';
  76. $ml->save();
  77. if($res == true) {
  78. return $ml;
  79. } else {
  80. return;
  81. }
  82. }
  83. public function load($modLog)
  84. {
  85. $this->log = $modLog;
  86. return $this;
  87. }
  88. public function fanout()
  89. {
  90. $log = $this->log;
  91. $msg = "{$log->user_username} commented on a modlog";
  92. $rendered = "<span class='font-weight-bold'>{$log->user_username}</span> commented on a <a href='/i/admin/users/modlogs/{$log->user_id}}' class='font-weight-bold text-decoration-none'>modlog</a>";
  93. $item_id = $log->id;
  94. $item_type = 'App\ModLog';
  95. $action = 'admin.user.modlog.comment';
  96. $admins = User::whereNull('status')
  97. ->whereNotIn('id', [$log->user_id])
  98. ->whereIsAdmin(true)
  99. ->pluck('profile_id')
  100. ->toArray();
  101. foreach($admins as $user) {
  102. $n = new Notification;
  103. $n->profile_id = $user;
  104. $n->actor_id = $log->admin->profile_id;
  105. $n->item_id = $item_id;
  106. $n->item_type = $item_type;
  107. $n->action = $action;
  108. $n->message = $msg;
  109. $n->rendered = $rendered;
  110. $n->save();
  111. }
  112. }
  113. public function unfanout()
  114. {
  115. Notification::whereItemType('App\ModLog')
  116. ->whereItemId($this->log->id)
  117. ->delete();
  118. }
  119. }