Skip to content

Commit 61cdfcd

Browse files
committed
improve the Tags feature
1 parent d329de0 commit 61cdfcd

File tree

3 files changed

+20
-10
lines changed

3 files changed

+20
-10
lines changed

src/Conversations/ConversationRepository.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ public function getMessagesByTagId($tagId, $userId)
224224
$query->where('tags.id', $tagId);
225225
},
226226
])
227+
->where(function ($query) use ($userId) {
228+
$query->where('user_one', $userId)
229+
->orWhere('user_two', $userId);
230+
})
227231
->get();
228232
}
229233

src/Tags/Tag.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,11 @@ public function conversations()
2424
return $this->hasMany('Nahid\Talk\Messages\Conversations', 'conversation_id')
2525
->with('sender');
2626
}
27+
28+
public function scopeWithoutSpecialTags($query)
29+
{
30+
return $query->where(function ($query) {
31+
$query->whereNull('is_special_tag')->orWhere('is_special_tag', '!=', 1);
32+
});
33+
}
2734
}

src/Talk.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,6 @@ public function getConversationsAllByUserId($senderId, $offset = 0, $take = 20)
523523
public function getUserTags()
524524
{
525525
return Tags\Tag::where(['user_id' => $this->authUserId])
526-
->where('name', '!=', Talk::STAR_TAG)
527526
->get();
528527
}
529528

@@ -554,13 +553,13 @@ public function createTagForUser($tagName)
554553
*
555554
* @param int $conversationId
556555
* @param string $tagName
557-
* @param bool $makeItASpecialTag when set to true, ensures that only one tag with the specified name should be maintained, thus supporting use of custom "system tags" e.g. for notifications
556+
* @param bool $makeSpecialTag when set to true, ensures that only one tag with the specified name should be maintained, thus supporting use of custom "system tags" e.g. for notifications
558557
*
559558
* @return bool
560559
*/
561-
public function addTagToConversation($conversationId, string $tagName, bool $makeItASpecialTag = null)
560+
public function addTagToConversation($conversationId, string $tagName, bool $makeSpecialTag = null)
562561
{
563-
$makeItASpecialTag = is_bool($makeItASpecialTag) ? $makeItASpecialTag : false;
562+
$makeSpecialTag = is_bool($makeSpecialTag) ? $makeSpecialTag : false;
564563

565564
if (empty($tagName)) {
566565
return false;
@@ -569,16 +568,16 @@ public function addTagToConversation($conversationId, string $tagName, bool $mak
569568
$tag = Tags\Tag::where(['user_id' => $this->authUserId, 'name' => $tagName])->first();
570569

571570
//treat star tag specially
572-
if ($tagName == \Nahid\Talk\Talk::STAR_TAG || $makeItASpecialTag) {
571+
if ($tagName == \Nahid\Talk\Talk::STAR_TAG || $makeSpecialTag) {
573572
//at any time, we want to always have only one star tag, irrespective of who created it
574573
//Therefore, this will ensure that we have only one star tag in our db table
575574
$tag = Tags\Tag::where(['name' => $tagName])->first();
576575
}
577576

578577
if (is_null($tag)) {
579578
//special tags do not have owners
580-
if ($tagName == \Nahid\Talk\Talk::STAR_TAG || $makeItASpecialTag) {
581-
$tag = Tags\Tag::create([
579+
if ($tagName == \Nahid\Talk\Talk::STAR_TAG || $makeSpecialTag) {
580+
$tag = Tags\Tag::forceCreate([
582581
'name' => $tagName,
583582
'is_special_tag' => 1,
584583
]);
@@ -611,14 +610,14 @@ public function starThisConversation($conversationId)
611610
public function removeTagFromConversation($conversationId, $tagId)
612611
{
613612
if (!empty($conversationId) && !empty($tagId)) {
614-
//confirm user owns this tag
615-
$tag = Tags\Tag::where(['user_id' => $this->authUserId, 'id' => $tagId])->firstOrFail();
616613
$conversation = \Nahid\Talk\Conversations\Conversation::with('tags')
617614
->where(function ($query) {
615+
//just to ensure user belongs to the conversation
618616
$query
619617
->where("user_one", $this->authUserId)
620618
->orWhere("user_two", $this->authUserId);
621-
})->findOrFail($conversationId);
619+
})
620+
->findOrFail($conversationId);
622621

623622
$conversation->tags()->detach($tagId);
624623

0 commit comments

Comments
 (0)