Skip to content

Commit

Permalink
Merge pull request #1893 from OneSignal/add-public-getTags-method
Browse files Browse the repository at this point in the history
Add public get tags method
  • Loading branch information
jinliu9508 committed Feb 6, 2024
2 parents 11b402d + fea61e2 commit 613c6d1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
2 changes: 1 addition & 1 deletion MIGRATION_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ The user name space is accessible via `OneSignal.User` (in Kotlin) or `OneSignal
| `fun addTags(tags: Map<String, String>)` | `void addTags(Map<String, String> tags)` | *Add multiple tags for the current user. Tags are key:value pairs used as building blocks for targeting specific users and/or personalizing messages. If the tag key already exists, it will be replaced with the value provided here.* |
| `fun removeTag(key: String)` | `void removeTag(String key)` | *Remove the data tag with the provided key from the current user.* |
| `fun removeTags(keys: Collection<String>)` | `void removeTags(Collection<String> keys)` | *Remove multiple tags from the current user.* |

| `fun getTags()` | `Map<String, String> getTags()` | *Return a copy of all local tags from the current user.*

**Session Namespace**
The session namespace is accessible via `OneSignal.Session` (in Kotlin) or `OneSignal.getSession()` (in Java) and provides access to session-scoped functionality.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,9 @@ interface IUserManager {
* @param keys The collection of keys, all of which will be removed from the current user.
*/
fun removeTags(keys: Collection<String>)

/**
* Return a copy of all local tags from the current user.
*/
fun getTags(): Map<String, String>
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ internal open class UserManager(
val externalId: String?
get() = _identityModel.externalId

val tags: Map<String, String>
get() = _propertiesModel.tags

val aliases: Map<String, String>
get() = _identityModel.filter { it.key != IdentityModel::id.name }.toMap()

Expand Down Expand Up @@ -218,4 +215,8 @@ internal open class UserManager(
_propertiesModel.tags.remove(it)
}
}

override fun getTags(): Map<String, String> {
return _propertiesModel.tags.toMap()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.onesignal.user.internal.subscriptions.ISubscriptionManager
import com.onesignal.user.internal.subscriptions.SubscriptionList
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import io.kotest.runner.junit4.KotestTestRunner
import io.mockk.every
import io.mockk.just
Expand Down Expand Up @@ -107,8 +108,8 @@ class UserManagerTests : FunSpec({
UserManager(mockSubscriptionManager, MockHelper.identityModelStore(), propertiesModelStore, MockHelper.languageContext())

// When
val tag1 = userManager.tags["my-tag-key1"]
val tag2 = userManager.tags["my-tag-key2"]
val tag1 = propertiesModelStore.model.tags["my-tag-key1"]
val tag2 = propertiesModelStore.model.tags["my-tag-key2"]

// add
userManager.addTag("my-tag-key5", "my-tag-value5")
Expand All @@ -135,6 +136,31 @@ class UserManagerTests : FunSpec({
propertiesModelStore.model.tags["my-tag-key3"] shouldBe null
}

test("getTags returns a copy of tags") {
// Given
val mockSubscriptionManager = mockk<ISubscriptionManager>()
val propertiesModelStore =
MockHelper.propertiesModelStore {
it.tags["my-tag-key1"] = "my-tag-value1"
}

val userManager = UserManager(mockSubscriptionManager, MockHelper.identityModelStore(), propertiesModelStore, MockHelper.languageContext())

// When
val tagSnapshot1 = userManager.getTags()

// Then
tagSnapshot1.size shouldBe propertiesModelStore.model.tags.size
tagSnapshot1["my-tag-key1"] shouldBe propertiesModelStore.model.tags["my-tag-key1"]

// Modify
userManager.addTag("my-tag-key2", "my-tag-value2")
userManager.getTags().size shouldBe 2

// Then
tagSnapshot1.size shouldNotBe userManager.getTags().size
}

test("subscriptions are backed by the subscriptions manager") {
// Given
val subscriptionList = SubscriptionList(listOf(), UninitializedPushSubscription())
Expand Down

0 comments on commit 613c6d1

Please sign in to comment.