<?php

namespace App\Domain\Feeds\Service;

use DB;

class LiveFeed
{
    public static function addBySub($user)
    {
        $userID = $user->id;
        $userFeeds = $user->feeds()->pluck('created_at', 'id')->transform(function ($item)  {
            return ['times' => $item->getTimestamp()];
        })->toArray();
        $add_posts = [];

        foreach ($userFeeds as $feedID => $userFeed) {
            $add_posts[] = [
                'feed_id' => $feedID,
                'user_id' => auth()->user()->id,
                'home_user_id' => $userID,
                'times' => $userFeed['times'],
            ];
        }
        DB::table('users_live_feeds')->insertOrIgnore($add_posts);


    }

    public static function removeBySub($user)
    {
        $userID = $user->id;
        DB::table('users_live_feeds')
            ->where('home_user_id', $userID)
            ->where('user_id',  auth()->user()->id)
            ->delete();
    }
}
