Require valid link url for story test posts.

fork-5.53.8
Cody Henthorne 2022-10-17 15:39:03 -04:00 zatwierdzone przez Greyson Parrelli
rodzic 94f135ac38
commit 2f2711c9a3
3 zmienionych plików z 20 dodań i 7 usunięć

Wyświetl plik

@ -8,11 +8,13 @@ import android.widget.EditText
import androidx.constraintlayout.widget.Group
import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.viewModels
import com.google.android.material.snackbar.Snackbar
import org.thoughtcrime.securesms.R
import org.thoughtcrime.securesms.components.KeyboardEntryDialogFragment
import org.thoughtcrime.securesms.linkpreview.LinkPreviewRepository
import org.thoughtcrime.securesms.linkpreview.LinkPreviewViewModel
import org.thoughtcrime.securesms.stories.StoryLinkPreviewView
import org.thoughtcrime.securesms.util.LinkUtil
import org.thoughtcrime.securesms.util.ViewUtil
import org.thoughtcrime.securesms.util.visible
@ -57,10 +59,18 @@ class TextStoryPostLinkEntryFragment : KeyboardEntryDialogFragment(
val linkPreviewState = linkPreviewViewModel.linkPreviewState.value
if (linkPreviewState != null) {
val url = linkPreviewState.linkPreview.map { it.url }.orElseGet { linkPreviewState.activeUrlForError }
viewModel.setLinkPreview(url)
}
dismissAllowingStateLoss()
if (LinkUtil.isLegalUrl(url, false, true)) {
viewModel.setLinkPreview(url)
dismissAllowingStateLoss()
} else {
val snackbar = Snackbar.make(requireView(), R.string.TextStoryPostSendFragment__please_enter_a_valid_link, Snackbar.LENGTH_SHORT)
snackbar.anchorView = linkPreview
snackbar.show()
}
} else {
dismissAllowingStateLoss()
}
}
linkPreviewViewModel.linkPreviewState.observe(viewLifecycleOwner) { state ->

Wyświetl plik

@ -42,17 +42,17 @@ public final class LinkUtil {
return url != null &&
!TextUtils.isEmpty(url.scheme()) &&
"https".equals(url.scheme()) &&
isLegalUrl(linkUrl, false);
isLegalUrl(linkUrl, false, false);
}
/**
* @return True if URL is valid, mostly useful for linkifying.
*/
public static boolean isLegalUrl(@NonNull String url) {
return isLegalUrl(url, true);
return isLegalUrl(url, true, false);
}
private static boolean isLegalUrl(@NonNull String url, boolean skipTopLevelDomainValidation) {
public static boolean isLegalUrl(@NonNull String url, boolean skipTopLevelDomainValidation, boolean requireTopLevelDomain) {
if (ILLEGAL_CHARACTERS_PATTERN.matcher(url).find()) {
return false;
}
@ -67,7 +67,8 @@ public final class LinkUtil {
boolean validCharacters = ALL_ASCII_PATTERN.matcher(cleanedDomain).matches() ||
ALL_NON_ASCII_PATTERN.matcher(cleanedDomain).matches();
boolean validTopLevelDomain = skipTopLevelDomainValidation || !INVALID_TOP_LEVEL_DOMAINS.contains(topLevelDomain);
boolean validTopLevelDomain = (skipTopLevelDomainValidation || !INVALID_TOP_LEVEL_DOMAINS.contains(topLevelDomain)) &&
(!requireTopLevelDomain || topLevelDomain != null);
return validCharacters && validTopLevelDomain;
} else {

Wyświetl plik

@ -4980,6 +4980,8 @@
<string name="TextStoryPostSendFragment__search">Search</string>
<!-- Toast shown when an unexpected error occurs while sending a text story -->
<string name="TextStoryPostSendFragment__an_unexpected_error_occurred_try_again">An unexpected error occurred</string>
<!-- Toast shown when a trying to add a link preview to a text story post and the link/url is not valid (e.g., missing .com at the end) -->
<string name="TextStoryPostSendFragment__please_enter_a_valid_link">Please enter a valid link.</string>
<!-- Title for screen allowing user to exclude "My Story" entries from specific people -->
<string name="ChangeMyStoryMembershipFragment__all_except">All except…</string>
<!-- Title for screen allowing user to only share "My Story" entries with specific people -->