Validate several ids in constructors (#336)

* Validate ids in constructors.

* Remove redundant `.value` usage in string interpolation.

* Make a distinction between `SessionId` and `UserId` in `TestData`.
This commit is contained in:
Jorge Martin Espinosa 2023-04-18 18:17:13 +02:00 committed by GitHub
parent 638b45930e
commit fae3417181
36 changed files with 193 additions and 199 deletions

View file

@ -24,9 +24,7 @@ import io.element.android.libraries.architecture.bindings
import io.element.android.libraries.core.log.logger.LoggerTag
import io.element.android.libraries.matrix.api.core.RoomId
import io.element.android.libraries.matrix.api.core.SessionId
import io.element.android.libraries.matrix.api.core.asRoomId
import io.element.android.libraries.matrix.api.core.asSessionId
import io.element.android.libraries.matrix.api.core.asThreadId
import io.element.android.libraries.matrix.api.core.ThreadId
import io.element.android.libraries.push.impl.log.notificationLoggerTag
import io.element.android.services.analytics.api.AnalyticsTracker
import io.element.android.services.toolbox.api.systemclock.SystemClock
@ -51,32 +49,27 @@ class NotificationBroadcastReceiver : BroadcastReceiver() {
if (intent == null || context == null) return
context.bindings<NotificationBroadcastReceiverBindings>().inject(this)
Timber.tag(loggerTag.value).v("NotificationBroadcastReceiver received : $intent")
val sessionId = intent.extras?.getString(KEY_SESSION_ID)?.asSessionId() ?: return
val sessionId = intent.extras?.getString(KEY_SESSION_ID)?.let(::SessionId) ?: return
val roomId = intent.getStringExtra(KEY_ROOM_ID)?.let(::RoomId)
when (intent.action) {
actionIds.smartReply ->
handleSmartReply(intent, context)
actionIds.dismissRoom ->
intent.getStringExtra(KEY_ROOM_ID)?.asRoomId()?.let { roomId ->
notificationDrawerManager.clearMessagesForRoom(sessionId, roomId)
}
actionIds.dismissRoom -> if (roomId != null) {
notificationDrawerManager.clearMessagesForRoom(sessionId, roomId)
}
actionIds.dismissSummary ->
notificationDrawerManager.clearAllEvents(sessionId)
actionIds.markRoomRead ->
intent.getStringExtra(KEY_ROOM_ID)?.asRoomId()?.let { roomId ->
notificationDrawerManager.clearMessagesForRoom(sessionId, roomId)
handleMarkAsRead(sessionId, roomId)
}
actionIds.join -> {
intent.getStringExtra(KEY_ROOM_ID)?.asRoomId()?.let { roomId ->
notificationDrawerManager.clearMemberShipNotificationForRoom(sessionId, roomId)
handleJoinRoom(sessionId, roomId)
}
actionIds.markRoomRead -> if (roomId != null) {
notificationDrawerManager.clearMessagesForRoom(sessionId, roomId)
handleMarkAsRead(sessionId, roomId)
}
actionIds.reject -> {
intent.getStringExtra(KEY_ROOM_ID)?.asRoomId()?.let { roomId ->
notificationDrawerManager.clearMemberShipNotificationForRoom(sessionId, roomId)
handleRejectRoom(sessionId, roomId)
}
actionIds.join -> if (roomId != null) {
notificationDrawerManager.clearMemberShipNotificationForRoom(sessionId, roomId)
handleJoinRoom(sessionId, roomId)
}
actionIds.reject -> if (roomId != null) {
notificationDrawerManager.clearMemberShipNotificationForRoom(sessionId, roomId)
handleRejectRoom(sessionId, roomId)
}
}
}
@ -125,9 +118,9 @@ class NotificationBroadcastReceiver : BroadcastReceiver() {
private fun handleSmartReply(intent: Intent, context: Context) {
val message = getReplyMessage(intent)
val sessionId = intent.getStringExtra(KEY_SESSION_ID)?.asSessionId()
val roomId = intent.getStringExtra(KEY_ROOM_ID)?.asRoomId()
val threadId = intent.getStringExtra(KEY_THREAD_ID)?.asThreadId()
val sessionId = intent.getStringExtra(KEY_SESSION_ID)?.let(::SessionId)
val roomId = intent.getStringExtra(KEY_ROOM_ID)?.let(::RoomId)
val threadId = intent.getStringExtra(KEY_THREAD_ID)?.let(::ThreadId)
if (message.isNullOrBlank() || roomId == null) {
// ignore this event

View file

@ -63,7 +63,7 @@ class PendingIntentFactory @Inject constructor(
fun createDismissSummaryPendingIntent(sessionId: SessionId): PendingIntent {
val intent = Intent(context, NotificationBroadcastReceiver::class.java)
intent.action = actionIds.dismissSummary
intent.data = createIgnoredUri("deleteSummary/${sessionId.value}")
intent.data = createIgnoredUri("deleteSummary/$sessionId")
intent.putExtra(NotificationBroadcastReceiver.KEY_SESSION_ID, sessionId.value)
return PendingIntent.getBroadcast(
context,
@ -76,7 +76,7 @@ class PendingIntentFactory @Inject constructor(
fun createDismissRoomPendingIntent(sessionId: SessionId, roomId: RoomId): PendingIntent {
val intent = Intent(context, NotificationBroadcastReceiver::class.java)
intent.action = actionIds.dismissRoom
intent.data = createIgnoredUri("deleteRoom/${sessionId.value}/${roomId.value}")
intent.data = createIgnoredUri("deleteRoom/$sessionId/$roomId")
intent.putExtra(NotificationBroadcastReceiver.KEY_SESSION_ID, sessionId.value)
intent.putExtra(NotificationBroadcastReceiver.KEY_ROOM_ID, roomId.value)
return PendingIntent.getBroadcast(

View file

@ -78,7 +78,7 @@ class QuickReplyActionFactory @Inject constructor(
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val intent = Intent(context, NotificationBroadcastReceiver::class.java)
intent.action = actionIds.smartReply
intent.data = createIgnoredUri("quickReply/${sessionId.value}/${roomId.value}" + threadId?.let { "/${it.value}" }.orEmpty())
intent.data = createIgnoredUri("quickReply/$sessionId/$roomId" + threadId?.let { "/$it" }.orEmpty())
intent.putExtra(NotificationBroadcastReceiver.KEY_SESSION_ID, sessionId.value)
intent.putExtra(NotificationBroadcastReceiver.KEY_ROOM_ID, roomId.value)
threadId?.let {

View file

@ -35,21 +35,21 @@ class NotificationEventQueueTest {
fun `given events when redacting some then marks matching event ids as redacted`() {
val queue = givenQueue(
listOf(
aSimpleNotifiableEvent(eventId = EventId("redacted-id-1")),
aNotifiableMessageEvent(eventId = EventId("redacted-id-2")),
anInviteNotifiableEvent(eventId = EventId("redacted-id-3")),
aSimpleNotifiableEvent(eventId = EventId("kept-id")),
aSimpleNotifiableEvent(eventId = EventId("\$redacted-id-1")),
aNotifiableMessageEvent(eventId = EventId("\$redacted-id-2")),
anInviteNotifiableEvent(eventId = EventId("\$redacted-id-3")),
aSimpleNotifiableEvent(eventId = EventId("\$kept-id")),
)
)
queue.markRedacted(listOf(EventId("redacted-id-1"), EventId("redacted-id-2"), EventId("redacted-id-3")))
queue.markRedacted(listOf(EventId("\$redacted-id-1"), EventId("\$redacted-id-2"), EventId("\$redacted-id-3")))
assertThat(queue.rawEvents()).isEqualTo(
listOf(
aSimpleNotifiableEvent(eventId = EventId("redacted-id-1"), isRedacted = true),
aNotifiableMessageEvent(eventId = EventId("redacted-id-2"), isRedacted = true),
anInviteNotifiableEvent(eventId = EventId("redacted-id-3"), isRedacted = true),
aSimpleNotifiableEvent(eventId = EventId("kept-id"), isRedacted = false),
aSimpleNotifiableEvent(eventId = EventId("\$redacted-id-1"), isRedacted = true),
aNotifiableMessageEvent(eventId = EventId("\$redacted-id-2"), isRedacted = true),
anInviteNotifiableEvent(eventId = EventId("\$redacted-id-3"), isRedacted = true),
aSimpleNotifiableEvent(eventId = EventId("\$kept-id"), isRedacted = false),
)
)
}
@ -179,8 +179,8 @@ class NotificationEventQueueTest {
@Test
fun `given event when adding new event with edited event id matching the existing event id then updates existing event`() {
val editedEvent = aSimpleNotifiableEvent(eventId = EventId("id-to-edit"))
val updatedEvent = editedEvent.copy(eventId = EventId("1"), editedEventId = EventId("id-to-edit"), title = "updated title", isUpdated = true)
val editedEvent = aSimpleNotifiableEvent(eventId = EventId("\$id-to-edit"))
val updatedEvent = editedEvent.copy(eventId = EventId("\$1"), editedEventId = EventId("\$id-to-edit"), title = "updated title", isUpdated = true)
val queue = givenQueue(listOf(editedEvent))
queue.add(updatedEvent)
@ -190,8 +190,8 @@ class NotificationEventQueueTest {
@Test
fun `given event when adding new event with edited event id matching the existing event edited id then updates existing event`() {
val editedEvent = aSimpleNotifiableEvent(eventId = EventId("0"), editedEventId = EventId("id-to-edit"))
val updatedEvent = editedEvent.copy(eventId = EventId("1"), editedEventId = EventId("id-to-edit"), title = "updated title", isUpdated = true)
val editedEvent = aSimpleNotifiableEvent(eventId = EventId("\$0"), editedEventId = EventId("\$id-to-edit"))
val updatedEvent = editedEvent.copy(eventId = EventId("\$1"), editedEventId = EventId("\$id-to-edit"), title = "updated title", isUpdated = true)
val queue = givenQueue(listOf(editedEvent))
queue.add(updatedEvent)

View file

@ -171,10 +171,10 @@ class NotificationFactoryTest {
val roomWithRedactedMessage = mapOf(
A_ROOM_ID to listOf(
ProcessedEvent(ProcessedEvent.Type.KEEP, A_MESSAGE_EVENT.copy(isRedacted = true)),
ProcessedEvent(ProcessedEvent.Type.KEEP, A_MESSAGE_EVENT.copy(eventId = EventId("not-redacted")))
ProcessedEvent(ProcessedEvent.Type.KEEP, A_MESSAGE_EVENT.copy(eventId = EventId("\$not-redacted")))
)
)
val withRedactedRemoved = listOf(A_MESSAGE_EVENT.copy(eventId = EventId("not-redacted")))
val withRedactedRemoved = listOf(A_MESSAGE_EVENT.copy(eventId = EventId("\$not-redacted")))
val expectedNotification = roomGroupMessageCreator.givenCreatesRoomMessageFor(
A_SESSION_ID,
withRedactedRemoved,