Skip to content

Commit cda5c16

Browse files
committed
wip
1 parent 8b741ba commit cda5c16

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

app/Jobs/CreateThread.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ public function handle(): Thread
6565
$thread->syncTags($this->tags);
6666
$thread->save();
6767

68+
// Subscribe author to the thread.
69+
$this->author->subscriptionAble()->create([
70+
'subscriptionable_id' => $thread->id(),
71+
'subscriptionable_type' => Thread::TABLE,
72+
]);
73+
6874
return $thread;
6975
}
7076
}

app/User.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Models\Reply;
66
use App\Models\Thread;
7+
use App\Models\Subscription;
78
use App\Helpers\ModelHelpers;
89
use App\Helpers\HasTimestamps;
910
use Illuminate\Notifications\Notifiable;
@@ -238,4 +239,17 @@ public function delete()
238239

239240
parent::delete();
240241
}
242+
243+
public function isSubscribedTo($subscriptionAble): bool
244+
{
245+
return $this->subscriptionAble()
246+
->where('subscriptionable_id', $subscriptionAble->id())
247+
->where('subscriptionable_type', $subscriptionAble::TABLE)
248+
->exists();
249+
}
250+
251+
public function subscriptionAble(): HasMany
252+
{
253+
return $this->hasMany(Subscription::class, 'user_id');
254+
}
241255
}

tests/Components/Jobs/CreateThreadTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ public function we_can_create_a_thread()
1818
$thread = $this->dispatch(new CreateThread('Subject', 'Body', '', $user));
1919

2020
$this->assertEquals('Subject', $thread->subject());
21+
$this->assertTrue($user->isSubscribedTo($thread));
2122
}
2223
}

tests/Components/Models/UserTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Tests\TestCase;
77
use App\Models\Reply;
88
use App\Models\Thread;
9+
use App\Models\Subscription;
910
use Illuminate\Foundation\Testing\DatabaseMigrations;
1011

1112
class UserTest extends TestCase
@@ -47,4 +48,14 @@ private function createTwoSolutionReplies(User $user)
4748
$reply = factory(Reply::class)->create(['replyable_id' => $thread->id(), 'author_id' => $user->id()]);
4849
$thread->markSolution($reply);
4950
}
51+
52+
/** @test */
53+
public function it_can_check_if_its_subscribed_to_a_thread()
54+
{
55+
$user = $this->createUser();
56+
$thread = factory(Thread::class)->create();
57+
factory(Subscription::class)->create(['user_id' => $user->id(), 'subscriptionable_id' => $thread->id()]);
58+
59+
$this->assertTrue($user->isSubscribedTo($thread));
60+
}
5061
}

0 commit comments

Comments
 (0)