From 5540c8caccfe1b13189a27af97d2e81a8f0b886d Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 22 Nov 2020 10:59:30 +0100 Subject: [PATCH] Fix issue #109 --- .../fedilab/fedilabtube/PeertubeActivity.java | 12 +++++++++--- .../fedilabtube/client/RetrofitPeertubeAPI.java | 16 ++++++++++++++-- .../fedilabtube/client/data/VideoData.java | 9 +++++++++ .../fedilabtube/viewmodel/TimelineVM.java | 7 ++++++- 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java b/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java index 450be58..ce279d4 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/PeertubeActivity.java @@ -727,7 +727,7 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd @SuppressLint("ClickableViewAccessibility") public void manageVIewVideo(APIResponse apiResponse) { - if (!isRemote && apiResponse != null && apiResponse.getPeertubes().get(0).getErrorCode() == 1 && apiResponse.getPeertubes().get(0).getOriginUrl() != null) { + if (!isRemote && apiResponse != null && apiResponse.getPeertubes() != null && apiResponse.getPeertubes().get(0).getErrorCode() == 1 && apiResponse.getPeertubes().get(0).getOriginUrl() != null) { String url = apiResponse.getPeertubes().get(0).getOriginUrl(); Pattern link = Pattern.compile("(https?://[\\da-z.-]+\\.[a-z.]{2,10})/videos/watch/(\\w{8}-\\w{4}-\\w{4}-\\w{4}-\\w{12})(\\?start=(\\d+[hH])?(\\d+[mM])?(\\d+[sS])?)?$"); Matcher matcherLink = link.matcher(url); @@ -771,6 +771,11 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd } return; } + if (apiResponse != null && apiResponse.getPeertubes() != null && apiResponse.getPeertubes().size() > 0 && apiResponse.getPeertubes().get(0).getErrorMessage() != null) { + Toasty.error(PeertubeActivity.this, apiResponse.getPeertubes().get(0).getErrorMessage(), Toast.LENGTH_LONG).show(); + binding.loader.setVisibility(View.GONE); + return; + } if (apiResponse == null || (apiResponse.getError() != null) || apiResponse.getPeertubes() == null || apiResponse.getPeertubes().size() == 0) { Toasty.error(PeertubeActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show(); binding.loader.setVisibility(View.GONE); @@ -782,11 +787,12 @@ public class PeertubeActivity extends AppCompatActivity implements CommentListAd return; } long position = -1; + + peertube = apiResponse.getPeertubes().get(0); + if (peertube.getUserHistory() != null) { position = peertube.getUserHistory().getCurrentTime() * 1000; } - - peertube = apiResponse.getPeertubes().get(0); if (peertube.getTags() != null && peertube.getTags().size() > 0) { SearchVM searchViewModel = new ViewModelProvider(PeertubeActivity.this).get(SearchVM.class); searchViewModel.searchNextVideos(peertube.getTags()).observe(PeertubeActivity.this, this::manageNextVideos); diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitPeertubeAPI.java b/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitPeertubeAPI.java index 274a0d7..2aaca53 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitPeertubeAPI.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitPeertubeAPI.java @@ -1664,10 +1664,10 @@ public class RetrofitPeertubeAPI { * @param id String id * @return APIResponse */ - public APIResponse getVideos(String id, boolean myVideo) { + public APIResponse getVideos(String id, boolean myVideo, boolean canUseToken) { PeertubeService peertubeService = init(); Call video; - if (myVideo) { + if (myVideo || canUseToken) { video = peertubeService.getMyVideo(getToken(), id); } else { video = peertubeService.getVideo(id); @@ -1682,6 +1682,7 @@ public class RetrofitPeertubeAPI { } else { if (response.errorBody() != null) { + String error = response.errorBody().string(); if (error.contains("originUrl")) { try { @@ -1695,6 +1696,17 @@ public class RetrofitPeertubeAPI { } catch (JSONException e) { e.printStackTrace(); } + } else if (error.contains("error")) { + try { + JSONObject jsonObject = new JSONObject(error); + List videos = new ArrayList<>(); + VideoData.Video videoErrorMessage = new VideoData.Video(); + videoErrorMessage.setErrorMessage(jsonObject.getString("error")); + videos.add(videoErrorMessage); + apiResponse.setPeertubes(videos); + } catch (JSONException e) { + e.printStackTrace(); + } } } else { Error error = new Error(); diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/data/VideoData.java b/app/src/main/java/app/fedilab/fedilabtube/client/data/VideoData.java index 7f95808..559300a 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/data/VideoData.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/data/VideoData.java @@ -133,6 +133,7 @@ public class VideoData { private String myRating; private String originUrl; private int errorCode; + private String errorMessage; //Dedicated to overview videos to reuse the logic of videos private boolean hasTitle = false; private String title; @@ -600,6 +601,14 @@ public class VideoData { this.playlistExists = playlistExists; } + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + public UserHistory getUserHistory() { return userHistory; } diff --git a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/TimelineVM.java b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/TimelineVM.java index 7c5c1d2..82e08b2 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/TimelineVM.java +++ b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/TimelineVM.java @@ -122,6 +122,11 @@ public class TimelineVM extends AndroidViewModel { private void getSingle(String instance, String videoId, boolean myVideo) { Context _mContext = getApplication().getApplicationContext(); + boolean canUseToken = false; + if (instance == null || instance.compareTo(Helper.getLiveInstance(_mContext)) == 0) { + canUseToken = true; + } + boolean finalCanUseToken = canUseToken; new Thread(() -> { try { RetrofitPeertubeAPI retrofitPeertubeAPI; @@ -130,7 +135,7 @@ public class TimelineVM extends AndroidViewModel { } else { retrofitPeertubeAPI = new RetrofitPeertubeAPI(_mContext, instance, null); } - APIResponse apiResponse = retrofitPeertubeAPI.getVideos(videoId, myVideo); + APIResponse apiResponse = retrofitPeertubeAPI.getVideos(videoId, myVideo, finalCanUseToken); if (Helper.isLoggedIn(_mContext) && instance == null) { if (apiResponse.getPeertubes() != null && apiResponse.getPeertubes().size() > 0 && apiResponse.getPeertubes().get(0) != null) { APIResponse response = new RetrofitPeertubeAPI(_mContext).getRating(videoId);