Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.78 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Jobs;
  4.  
  5. use App\EloquentModels\User\User;
  6. use App\Helpers\ConfigHelper;
  7. use App\Models\Notification\Type;
  8. use App\Utils\Iterables;
  9. use Illuminate\Bus\Queueable;
  10. use Illuminate\Contracts\Queue\ShouldQueue;
  11. use Illuminate\Foundation\Bus\Dispatchable;
  12. use Illuminate\Queue\InteractsWithQueue;
  13. use Illuminate\Queue\SerializesModels;
  14. use Illuminate\Support\Facades\DB;
  15.  
  16. /**
  17.  * Class NotifyMentionsInPost
  18.  *
  19.  * Purpose of this job is to send notifications to all users  which were mentioned
  20.  * in the provided post/thread.
  21.  *
  22.  *
  23.  * @package App\Jobs
  24.  */
  25. class NotifyMentionsInPost implements ShouldQueue {
  26.     private $quoteRegex = '/\[quotepost=(.*?)\](.*?)\[\/quotepost\]/si';
  27.     private $mentionRegex = '/\[mention]@(.*?)\[\/mention\]/si';
  28.  
  29.     private $mentionTypeUser = 'user';
  30.  
  31.     private $ignoredNotificationTypes;
  32.     private $content;
  33.     private $postId;
  34.     private $userId;
  35.  
  36.     use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  37.  
  38.     /**
  39.      * NotifyMentionsInPost constructor.
  40.      *
  41.      * @param $content
  42.      * @param $postId
  43.      * @param $userId
  44.      */
  45.     public function __construct($content, $postId, $userId) {
  46.         $this->content = $content;
  47.         $this->postId = $postId;
  48.         $this->userId = $userId;
  49.         $this->ignoredNotificationTypes = ConfigHelper::getIgnoredNotificationsConfig();
  50.     }
  51.  
  52.     /**
  53.      * Executes the job
  54.      */
  55.     public function handle() {
  56.         $quotedPostIds = $this->getQuotedUserIds($this->content);
  57.         $content = preg_replace($this->quoteRegex, '', $this->content);
  58.         $mentionedIds = $this->getMentionedIds($content, $this->mentionTypeUser);
  59.  
  60.         $quotedPosts = DB::table('posts')->select('userId')->whereIn('postId', $quotedPostIds)->get()->toArray();
  61.         $quotedUserIds = array_map(function ($post) {
  62.             return $post->userId;
  63.         }, $quotedPosts);
  64.  
  65.         $mentionType = Type::getType(Type::MENTION);
  66.         $quoteType = Type::getType(Type::QUOTE);
  67.         $notifiedUsers = DB::table('notifications')
  68.             ->select('userId', 'type')
  69.             ->where('contentId', $this->postId)
  70.             ->whereIn('type', [$mentionType, $quoteType])
  71.             ->get()->toArray();
  72.  
  73.         $mentionedFromEarlier = array_map(function ($notification) {
  74.             return $notification->userId;
  75.         }, Iterables::filter($notifiedUsers, function ($notification) use ($mentionType) {
  76.             return $notification->type == $mentionType;
  77.         }));
  78.  
  79.         $quotedFromEarlier = array_map(function ($notification) {
  80.             return $notification->userId;
  81.         }, Iterables::filter($notifiedUsers, function ($notification) use ($quoteType) {
  82.             return $notification->type == $quoteType;
  83.         }));
  84.  
  85.         $quoteInserts = $this->createNotificationInserts($quotedUserIds, $this->userId, $quoteType, $this->postId, $quotedFromEarlier);
  86.         $mentionInserts = $this->createNotificationInserts($mentionedIds, $this->userId, $mentionType, $this->postId, $mentionedFromEarlier);
  87.         $inserts = array_merge($quoteInserts, $mentionInserts);
  88.  
  89.         DB::table('notifications')->insert($inserts);
  90.     }
  91.  
  92.     private function createNotificationInserts($userIds, $senderId, $type, $postId, $earlier) {
  93.         $inserts = [];
  94.         foreach ($userIds as $mentionedId) {
  95.             $ignoredNotifications = $this->isUserIgnoringNotification($mentionedId, $type);
  96.             if (!in_array($mentionedId, $earlier) && $mentionedId != $senderId && !$ignoredNotifications) {
  97.                 $inserts[] = [
  98.                     'userId' => $mentionedId,
  99.                     'senderId' => $senderId,
  100.                     'type' => $type,
  101.                     'contentId' => $postId,
  102.                     'createdAt' => time()
  103.                 ];
  104.             }
  105.         }
  106.         return $inserts;
  107.     }
  108.  
  109.     private function isUserIgnoringNotification($userId, $type) {
  110.         $notificationType = $type == Type::getType(Type::MENTION) ?
  111.             $this->ignoredNotificationTypes->MENTION_NOTIFICATIONS :
  112.             $this->ignoredNotificationTypes->QUOTE_NOTIFICATIONS;
  113.         return User::where('userId', $userId)
  114.                 ->whereRaw('(ignoredNotifications & ' . $notificationType . ')')->count('userId') > 0;
  115.     }
  116.  
  117.     private function getQuotedUserIds($content) {
  118.         if (preg_match_all($this->quoteRegex, $content, $matches)) {
  119.             return $matches[1];
  120.         }
  121.         return [];
  122.     }
  123.  
  124.     private function getMentionedIds($content, $mentionType) {
  125.         if ($mentionType == $this->mentionTypeUser && preg_match_all($this->mentionRegex, $content, $matches)) {
  126.             return User::whereIn('nickname', str_replace('_', ' ', $matches[1]))->pluck('userId');
  127.         }
  128.         return [];
  129.     }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement