From 46c7fc06a5c3cb9d0ce8799ae3258a80b61e59c7 Mon Sep 17 00:00:00 2001 From: Jacocococo Date: Tue, 14 Nov 2023 21:54:59 +0100 Subject: [PATCH] Translations for Akkoma --- .../statuses/AkkomaTranslateStatus.java | 11 +++ .../fragments/BaseStatusListFragment.java | 86 +++++++++++++------ .../android/model/AkkomaTranslation.java | 6 ++ .../joinmastodon/android/model/Instance.java | 6 +- .../joinmastodon/android/model/Status.java | 3 +- .../displayitems/TextStatusDisplayItem.java | 2 +- 6 files changed, 86 insertions(+), 28 deletions(-) create mode 100644 mastodon/src/main/java/org/joinmastodon/android/api/requests/statuses/AkkomaTranslateStatus.java create mode 100644 mastodon/src/main/java/org/joinmastodon/android/model/AkkomaTranslation.java diff --git a/mastodon/src/main/java/org/joinmastodon/android/api/requests/statuses/AkkomaTranslateStatus.java b/mastodon/src/main/java/org/joinmastodon/android/api/requests/statuses/AkkomaTranslateStatus.java new file mode 100644 index 0000000000..f47f7c2c1b --- /dev/null +++ b/mastodon/src/main/java/org/joinmastodon/android/api/requests/statuses/AkkomaTranslateStatus.java @@ -0,0 +1,11 @@ +package org.joinmastodon.android.api.requests.statuses; + +import org.joinmastodon.android.api.MastodonAPIRequest; +import org.joinmastodon.android.model.AkkomaTranslation; + +public class AkkomaTranslateStatus extends MastodonAPIRequest{ + public AkkomaTranslateStatus(String id, String lang){ + super(HttpMethod.GET, "/statuses/"+id+"/translations/"+lang.toUpperCase(), AkkomaTranslation.class); + } +} + diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/BaseStatusListFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/BaseStatusListFragment.java index d4c1a870d4..32dd3e73ea 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/BaseStatusListFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/BaseStatusListFragment.java @@ -21,10 +21,12 @@ import org.joinmastodon.android.R; import org.joinmastodon.android.api.requests.accounts.GetAccountRelationships; import org.joinmastodon.android.api.requests.polls.SubmitPollVote; +import org.joinmastodon.android.api.requests.statuses.AkkomaTranslateStatus; import org.joinmastodon.android.api.requests.statuses.TranslateStatus; import org.joinmastodon.android.api.session.AccountSessionManager; import org.joinmastodon.android.events.PollUpdatedEvent; import org.joinmastodon.android.model.Account; +import org.joinmastodon.android.model.AkkomaTranslation; import org.joinmastodon.android.model.DisplayItemsParent; import org.joinmastodon.android.model.Poll; import org.joinmastodon.android.model.Relationship; @@ -855,38 +857,69 @@ public void togglePostTranslation(Status status, String itemID){ status.translationState=Status.TranslationState.SHOWN; }else{ status.translationState=Status.TranslationState.LOADING; - new TranslateStatus(status.getContentStatus().id, Locale.getDefault().getLanguage()) - .setCallback(new Callback<>(){ - @Override - public void onSuccess(Translation result){ - if(getActivity()==null) - return; - status.translation=result; - status.translationState=Status.TranslationState.SHOWN; - updateTranslation(itemID); - } + if(!isInstanceAkkoma()){ + new TranslateStatus(status.getContentStatus().id, Locale.getDefault().getLanguage()) + .setCallback(new Callback<>(){ + @Override + public void onSuccess(Translation result){ + if(getActivity()==null) + return; + status.translation=result; + status.translationState=Status.TranslationState.SHOWN; + updateTranslation(itemID); + } + + @Override + public void onError(ErrorResponse error){ + if(getActivity()==null) + return; + translationCallbackError(status, itemID); + } + }) + .setTimeout(60000) // 1 minute + .exec(accountID); + }else{ + new AkkomaTranslateStatus(status.getContentStatus().id, Locale.getDefault().getLanguage()) + .setCallback(new Callback<>(){ + @Override + public void onSuccess(AkkomaTranslation result){ + if(getActivity()==null) + return; + status.translation=new Translation(); + status.translation.content=result.text; + status.translation.detectedSourceLanguage=result.detectedLanguage; + status.translation.provider=""; + status.translationState=Status.TranslationState.SHOWN; + updateTranslation(itemID); + } + + @Override + public void onError(ErrorResponse error){ + if(getActivity()==null) + return; + translationCallbackError(status, itemID); + } + }) + .setTimeout(60000) // 1 minute + .exec(accountID); + } - @Override - public void onError(ErrorResponse error){ - if(getActivity()==null) - return; - status.translationState=Status.TranslationState.HIDDEN; - updateTranslation(itemID); - new M3AlertDialogBuilder(getActivity()) - .setTitle(R.string.error) - .setMessage(R.string.translation_failed) - .setPositiveButton(R.string.ok, null) - .show(); - } - }) - .setTimeout(60000) // 1 minute - .exec(accountID); } } } updateTranslation(itemID); } + private void translationCallbackError(Status status, String itemID) { + status.translationState=Status.TranslationState.HIDDEN; + updateTranslation(itemID); + new M3AlertDialogBuilder(getActivity()) + .setTitle(R.string.error) + .setMessage(R.string.translation_failed) + .setPositiveButton(R.string.ok, null) + .show(); + } + private void updateTranslation(String itemID) { TextStatusDisplayItem.Holder text=findHolderOfType(itemID, TextStatusDisplayItem.Holder.class); if(text!=null){ @@ -896,6 +929,9 @@ private void updateTranslation(String itemID) { notifyItemChanged(itemID, TextStatusDisplayItem.class); } + if(isInstanceAkkoma()) + return; + SpoilerStatusDisplayItem.Holder spoiler=findHolderOfType(itemID, SpoilerStatusDisplayItem.Holder.class); if(spoiler!=null){ spoiler.rebind(); diff --git a/mastodon/src/main/java/org/joinmastodon/android/model/AkkomaTranslation.java b/mastodon/src/main/java/org/joinmastodon/android/model/AkkomaTranslation.java new file mode 100644 index 0000000000..a8f94681df --- /dev/null +++ b/mastodon/src/main/java/org/joinmastodon/android/model/AkkomaTranslation.java @@ -0,0 +1,6 @@ +package org.joinmastodon.android.model; + +public class AkkomaTranslation extends BaseModel{ + public String detectedLanguage; + public String text; +} diff --git a/mastodon/src/main/java/org/joinmastodon/android/model/Instance.java b/mastodon/src/main/java/org/joinmastodon/android/model/Instance.java index f90dfd4bde..aacf41b9a9 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/model/Instance.java +++ b/mastodon/src/main/java/org/joinmastodon/android/model/Instance.java @@ -161,11 +161,15 @@ public boolean hasFeature(Feature feature) { case BUBBLE_TIMELINE -> pleromaFeatures .map(f -> f.contains("bubble_timeline")) .orElse(false); + case MACHINE_TRANSLATION -> pleromaFeatures + .map(f -> f.contains("akkoma:machine_translation")) + .orElse(false); }; } public enum Feature { - BUBBLE_TIMELINE + BUBBLE_TIMELINE, + MACHINE_TRANSLATION } @Parcel diff --git a/mastodon/src/main/java/org/joinmastodon/android/model/Status.java b/mastodon/src/main/java/org/joinmastodon/android/model/Status.java index 6498cb0f74..382f252db6 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/model/Status.java +++ b/mastodon/src/main/java/org/joinmastodon/android/model/Status.java @@ -223,7 +223,8 @@ public boolean isEligibleForTranslation(AccountSession session){ Instance instanceInfo = AccountSessionManager.getInstance().getInstanceInfo(session.domain); boolean translateEnabled = instanceInfo != null && instanceInfo.v2 != null && instanceInfo.v2.configuration.translation != null && - instanceInfo.v2.configuration.translation.enabled; + instanceInfo.v2.configuration.translation.enabled || + (instanceInfo != null && instanceInfo.isAkkoma() && instanceInfo.hasFeature(Instance.Feature.MACHINE_TRANSLATION)); try { Pair> decoded=BOTTOM_TEXT_PATTERN.matcher(getStrippedText()).find() diff --git a/mastodon/src/main/java/org/joinmastodon/android/ui/displayitems/TextStatusDisplayItem.java b/mastodon/src/main/java/org/joinmastodon/android/ui/displayitems/TextStatusDisplayItem.java index 9235e6d99b..ecbf2f3d5e 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/ui/displayitems/TextStatusDisplayItem.java +++ b/mastodon/src/main/java/org/joinmastodon/android/ui/displayitems/TextStatusDisplayItem.java @@ -191,7 +191,7 @@ private CustomEmojiHelper getEmojiHelper(){ public void updateTranslation(boolean updateText){ if(item.status==null) return; - boolean translateEnabled=!item.disableTranslate && item.status.isEligibleForTranslation(item.parentFragment.getSession()); + boolean translateEnabled=!item.disableTranslate && item.status.isEligibleForTranslation(item.parentFragment.getSession()) && !item.isForQuote; if(translationFooter==null && translateEnabled){ translationFooter=translationFooterStub.inflate(); translationInfo=findViewById(R.id.translation_info_text);