/** * Returns all threads with new messages * * @return array */ public function threadsWithNewMessages() { $threadsWithNewMessages = []; $participants = Participant::where('user_id', $this->id)->lists('last_read', 'thread_id'); if ($participants) { $threads = Thread::whereIn('id', array_keys($participants))->get(); foreach ($threads as $thread) { if ($thread->updated_at > $participants[$thread->id]) { $threadsWithNewMessages[] = $thread->id; } } } return $threadsWithNewMessages; }
/** @test */ public function it_should_get_all_threads_shared_by_specified_users() { $userId = 1; $userId2 = 2; $thread = $this->faktory->create('thread'); $thread2 = $this->faktory->create('thread'); $this->faktory->create('participant', ['user_id' => $userId, 'thread_id' => $thread->id]); $this->faktory->create('participant', ['user_id' => $userId2, 'thread_id' => $thread->id]); $this->faktory->create('participant', ['user_id' => $userId, 'thread_id' => $thread2->id]); $threads = Thread::between([$userId, $userId2])->get(); $this->assertCount(1, $threads); }