<?php

namespace App\Listeners;

use App\Events\FeedAddProcessed;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class AddFeedUsers implements ShouldQueue
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  FeedProcessed  $event
     * @return void
     */
    public function handle(FeedAddProcessed $event)
    {
        $feed = $event->feed;
        $userFeed = $feed->user;
        $readers = $userFeed->subscriber_reverse;


        $new_posts = [];

        $new_posts[] = [
            'feed_id' => $feed->id,
            'user_id' => $userFeed->id,
            'home_user_id' => $userFeed->id,
            'times' => $feed->created_at->getTimestamp(),
        ];

        foreach ($readers as $reader) {
            $new_posts[] = [
                'feed_id' => $feed->id,
                'user_id' => $reader->id,
                'home_user_id' => $userFeed->id,
                'times' => $feed->created_at->getTimestamp(),
            ];
        }

        \DB::table('users_live_feeds')->insertOrIgnore($new_posts);


    }
}
