-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(message): fix in-message embedded link validator
- Loading branch information
Showing
10 changed files
with
210 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
app/src/main/kotlin/com/wire/android/ui/common/dialogs/VisitLinkDialog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
|
||
package com.wire.android.ui.common.dialogs | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.res.stringResource | ||
import com.wire.android.R | ||
import com.wire.android.ui.common.WireDialog | ||
import com.wire.android.ui.common.WireDialogButtonProperties | ||
import com.wire.android.ui.common.WireDialogButtonType | ||
import com.wire.android.ui.home.conversations.VisitLinkDialogState | ||
|
||
@Composable | ||
fun VisitLinkDialog(dialogState: VisitLinkDialogState, hideDialog: () -> Unit) { | ||
if (dialogState is VisitLinkDialogState.Visible) { | ||
WireDialog( | ||
title = stringResource(R.string.label_visit_link_title), | ||
text = stringResource(R.string.visit_link_dialog_body, dialogState.link), | ||
buttonsHorizontalAlignment = false, | ||
onDismiss = hideDialog, | ||
optionButton1Properties = WireDialogButtonProperties( | ||
text = stringResource(R.string.label_open), | ||
type = WireDialogButtonType.Primary, | ||
onClick = dialogState.openLink | ||
), | ||
optionButton2Properties = WireDialogButtonProperties( | ||
text = stringResource(R.string.label_cancel), | ||
type = WireDialogButtonType.Primary, | ||
onClick = hideDialog | ||
) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.android.util | ||
|
||
import java.net.URLDecoder | ||
|
||
fun containsSchema(url: String): Boolean { | ||
val regexPattern = Regex(".+://.+") | ||
|
||
return regexPattern.matches(url) | ||
} | ||
|
||
fun normalizeLink(url: String): String { | ||
val normalizedUrl = URLDecoder.decode(url, "UTF-8") // Decode URL to human-readable format | ||
|
||
return if (containsSchema(normalizedUrl)) { | ||
normalizedUrl | ||
} else { | ||
"https://$normalizedUrl" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.android | ||
|
||
import kotlin.random.Random | ||
|
||
val charPool: List<Char> = ('a'..'z') + ('A'..'Z') + ('0'..'9') | ||
|
||
fun Random.string(length: Int) = (1..length) | ||
.map { Random.nextInt(0, charPool.size).let { charPool[it] } } | ||
.joinToString("") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Wire | ||
* Copyright (C) 2023 Wire Swiss GmbH | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses/. | ||
*/ | ||
package com.wire.android.util | ||
|
||
import com.wire.android.string | ||
import org.junit.jupiter.api.Test | ||
import kotlin.random.Random | ||
|
||
class UriUtilTest { | ||
@Test | ||
fun givenLink_whenTheLinkStartsWithHttps_thenReturnsTheSameLink() { | ||
val input = "https://google.com" | ||
val expected = "https://google.com" | ||
val actual = normalizeLink(input) | ||
assert(expected == actual) | ||
} | ||
|
||
@Test | ||
fun givenLink_whenTheLinkStartsWithHttp_thenReturnsTheSameLink() { | ||
val input = "http://google.com" | ||
val expected = "http://google.com" | ||
val actual = normalizeLink(input) | ||
assert(expected == actual) | ||
} | ||
|
||
@Test | ||
fun givenLink_whenTheLinkStartsWithRandomSchema_thenReturnsTheSameLink() { | ||
val randomString = Random.string(Random.nextInt(5)) | ||
val input = "$randomString://google.com" | ||
val expected = "$randomString://google.com" | ||
val actual = normalizeLink(input) | ||
assert(expected == actual) | ||
} | ||
|
||
@Test | ||
fun givenLink_whenTheLinkWithoutSchema_thenReturnsTheLinkWithHttps() { | ||
val input = Random.string(Random.nextInt(20)) | ||
val expected = "https://$input" | ||
val actual = normalizeLink(input) | ||
assert(expected == actual) | ||
} | ||
} |