Fix invite link detection

This commit is contained in:
Grishka 2024-02-26 23:54:35 +03:00
parent ce427d2a0b
commit b51cccf1d7
3 changed files with 11 additions and 7 deletions

View File

@ -260,9 +260,9 @@ public class SplashFragment extends AppKitFragment{
private void loadAndChooseDefaultServer(){ private void loadAndChooseDefaultServer(){
ClipData clipData=getActivity().getSystemService(ClipboardManager.class).getPrimaryClip(); ClipData clipData=getActivity().getSystemService(ClipboardManager.class).getPrimaryClip();
if(clipData!=null && clipData.getItemCount()>0){ if(clipData!=null && clipData.getItemCount()>0){
CharSequence clipText=clipData.getItemAt(0).coerceToText(getActivity()); String clipText=clipData.getItemAt(0).coerceToText(getActivity()).toString();
if(HtmlParser.INVITE_LINK_PATTERN.matcher(clipText).find()){ if(HtmlParser.isValidInviteUrl(clipText)){
currentInviteLink=Uri.parse(clipText.toString()); currentInviteLink=Uri.parse(clipText);
defaultServerButton.setText(getString(R.string.join_server_x_with_invite, currentInviteLink.getHost())); defaultServerButton.setText(getString(R.string.join_server_x_with_invite, currentInviteLink.getHost()));
} }
}else{ }else{

View File

@ -370,7 +370,7 @@ public class InstanceCatalogSignupFragment extends InstanceCatalogFragment imple
protected void proceedWithAuthOrSignup(Instance instance){ protected void proceedWithAuthOrSignup(Instance instance){
if(currentInviteLinkAlert!=null){ if(currentInviteLinkAlert!=null){
currentInviteLinkAlert.dismiss(); currentInviteLinkAlert.dismiss();
}else if(!TextUtils.isEmpty(currentSearchQuery) && HtmlParser.INVITE_LINK_PATTERN.matcher(currentSearchQueryButWithCasePreserved).find()){ }else if(!TextUtils.isEmpty(currentSearchQuery) && HtmlParser.isValidInviteUrl(currentSearchQueryButWithCasePreserved)){
if(TextUtils.isEmpty(inviteCode) || !Objects.equals(instance.uri, inviteCodeHost)){ if(TextUtils.isEmpty(inviteCode) || !Objects.equals(instance.uri, inviteCodeHost)){
Uri inviteLink=Uri.parse(currentSearchQueryButWithCasePreserved); Uri inviteLink=Uri.parse(currentSearchQueryButWithCasePreserved);
new CheckInviteLink(inviteLink.getPath()) new CheckInviteLink(inviteLink.getPath())
@ -543,8 +543,8 @@ public class InstanceCatalogSignupFragment extends InstanceCatalogFragment imple
ClipData clipData=getActivity().getSystemService(ClipboardManager.class).getPrimaryClip(); ClipData clipData=getActivity().getSystemService(ClipboardManager.class).getPrimaryClip();
if(clipData!=null && clipData.getItemCount()>0){ if(clipData!=null && clipData.getItemCount()>0){
CharSequence clipText=clipData.getItemAt(0).coerceToText(getActivity()); String clipText=clipData.getItemAt(0).coerceToText(getActivity()).toString();
if(HtmlParser.INVITE_LINK_PATTERN.matcher(clipText).find()){ if(HtmlParser.isValidInviteUrl(clipText)){
edit.setText(clipText); edit.setText(clipText);
supportingText.setText(R.string.invite_link_pasted); supportingText.setText(R.string.invite_link_pasted);
} }

View File

@ -52,7 +52,7 @@ public class HtmlParser{
")" + ")" +
")"; ")";
public static final Pattern URL_PATTERN=Pattern.compile(VALID_URL_PATTERN_STRING, Pattern.CASE_INSENSITIVE); public static final Pattern URL_PATTERN=Pattern.compile(VALID_URL_PATTERN_STRING, Pattern.CASE_INSENSITIVE);
public static final Pattern INVITE_LINK_PATTERN=Pattern.compile("^https://"+Regex.URL_VALID_DOMAIN+"/invite/[a-z\\d]+$", Pattern.CASE_INSENSITIVE); private static final Pattern INVITE_LINK_PATH=Pattern.compile("/invite/[a-z\\d]+$", Pattern.CASE_INSENSITIVE);
private static Pattern EMOJI_CODE_PATTERN=Pattern.compile(":([\\w]+):"); private static Pattern EMOJI_CODE_PATTERN=Pattern.compile(":([\\w]+):");
private HtmlParser(){} private HtmlParser(){}
@ -259,4 +259,8 @@ public class HtmlParser{
} }
} }
} }
public static boolean isValidInviteUrl(String url){
return url.startsWith("https://") && INVITE_LINK_PATH.matcher(url).find();
}
} }