diff --git a/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java b/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java index 769cb8d..d3b5ad8 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/MainActivity.java @@ -172,6 +172,7 @@ public class MainActivity extends AppCompatActivity { oauthParams.setClient_id(account.getClient_id()); oauthParams.setClient_secret(account.getClient_secret()); oauthParams.setRefresh_token(account.getRefresh_token()); + oauthParams.setAccess_token(account.getToken()); new Thread(() -> { try { new RetrofitPeertubeAPI(MainActivity.this).manageToken(oauthParams); diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeService.java b/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeService.java index e944885..2796e05 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeService.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeService.java @@ -96,6 +96,16 @@ public interface PeertubeService { @Field("username") String username, @Field("password") String password); + //TOKEN + //Refresh + @FormUrlEncoded + @POST("users/token") + Call refreshToken( + @Field("client_id") String client_id, + @Field("client_secret") String client_secret, + @Field("refresh_token") String refresh_token, + @Field("grant_type") String grant_type); + @GET("users/me") Call verifyCredentials(@Header("Authorization") String credentials); @@ -188,7 +198,11 @@ public interface PeertubeService { //Get/Post/Update/Delete playlist @GET("video-playlists") - Call> getPlaylists(); + Call getPlaylists(); + + //Get/Post/Update/Delete playlist + @GET("accounts/{accountHandle}/video-playlists") + Call getPlaylistsForAccount(@Header("Authorization") String credentials, @Path("accountHandle") String accountHandle); @GET("video-playlists/{id}") Call getPlaylist(@Path("id") String id); 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 a0c7d02..fae1a4d 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitPeertubeAPI.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/RetrofitPeertubeAPI.java @@ -176,12 +176,23 @@ public class RetrofitPeertubeAPI { Call refreshTokenCall = null; if (oauthParams.getGrant_type().compareTo("password") == 0) { refreshTokenCall = peertubeService.createToken(oauthParams.getClient_id(), oauthParams.getClient_secret(), oauthParams.getGrant_type(), oauthParams.getUsername(), oauthParams.getPassword()); + }else if (oauthParams.getGrant_type().compareTo("refresh_token") == 0) { + refreshTokenCall = peertubeService.refreshToken(oauthParams.getClient_id(), oauthParams.getClient_secret(), oauthParams.getRefresh_token(), oauthParams.getGrant_type()); } if (refreshTokenCall != null) { try { Response response = refreshTokenCall.execute(); if (response.isSuccessful()) { - return response.body(); + Token tokenReply = response.body(); + if (oauthParams.getGrant_type().compareTo("refresh_token") == 0 && tokenReply != null) { + SharedPreferences sharedpreferences = _context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedpreferences.edit(); + editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, tokenReply.getAccess_token()); + editor.apply(); + SQLiteDatabase db = Sqlite.getInstance(_context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); + new AccountDAO(_context, db).updateAccountToken(oauthParams.getExternalAuthToken(), tokenReply); + } + return tokenReply; } else { Error error = new Error(); error.setStatusCode(response.code()); @@ -1082,7 +1093,7 @@ public class RetrofitPeertubeAPI { * @param videoId String id of the video * @return APIResponse */ - public APIResponse playlistAction(PlaylistsVM.action type, String playlistId, String videoId) { + public APIResponse playlistAction(PlaylistsVM.action type, String playlistId, String videoId, String acct) { PeertubeService peertubeService = init(); APIResponse apiResponse = new APIResponse(); @@ -1101,12 +1112,13 @@ public class RetrofitPeertubeAPI { } } else if (type == PlaylistsVM.action.GET_PLAYLISTS) { - Call> playlistsCall = peertubeService.getPlaylists(); - Response> response = playlistsCall.execute(); - if (response.isSuccessful()) { - apiResponse.setPlaylists(response.body()); + Call playlistsCall = peertubeService.getPlaylistsForAccount(token, acct); + Response response = playlistsCall.execute(); + if (response.isSuccessful() && response.body() != null) { + apiResponse.setPlaylists(response.body().data); } else { - Error error = generateError(response.code(), response.message()); + assert response.errorBody() != null; + Error error = generateError(response.code(), response.errorBody().string()); apiResponse.setError(error); } } else if (type == PlaylistsVM.action.GET_LIST_VIDEOS) { diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/entities/OauthParams.java b/app/src/main/java/app/fedilab/fedilabtube/client/entities/OauthParams.java index 18d2fd5..238687a 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/entities/OauthParams.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/entities/OauthParams.java @@ -35,6 +35,8 @@ public class OauthParams { private String externalAuthToken; @SerializedName("refresh_token") private String refresh_token; + @SerializedName("access_token") + private String access_token; public String getClient_secret() { @@ -100,4 +102,12 @@ public class OauthParams { public void setRefresh_token(String refresh_token) { this.refresh_token = refresh_token; } + + public String getAccess_token() { + return access_token; + } + + public void setAccess_token(String access_token) { + this.access_token = access_token; + } } diff --git a/app/src/main/java/app/fedilab/fedilabtube/sqlite/AccountDAO.java b/app/src/main/java/app/fedilab/fedilabtube/sqlite/AccountDAO.java index 80d7d7f..bad9a75 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/sqlite/AccountDAO.java +++ b/app/src/main/java/app/fedilab/fedilabtube/sqlite/AccountDAO.java @@ -25,6 +25,7 @@ import java.util.List; import app.fedilab.fedilabtube.client.data.AccountData.Account; import app.fedilab.fedilabtube.client.entities.Avatar; +import app.fedilab.fedilabtube.client.entities.Token; import app.fedilab.fedilabtube.helper.Helper; @@ -128,6 +129,28 @@ public class AccountDAO { } + /** + * Update an Account token in database + * + * @param oldToken String + * @param token Token + * @return boolean + */ + public int updateAccountToken(String oldToken, Token token) { + ContentValues values = new ContentValues(); + + + if (token.getRefresh_token() != null) { + values.put(Sqlite.COL_REFRESH_TOKEN, token.getRefresh_token()); + } + if (token.getAccess_token() != null) + values.put(Sqlite.COL_OAUTHTOKEN, token.getAccess_token()); + return db.update(Sqlite.TABLE_USER_ACCOUNT, + values, Sqlite.COL_OAUTHTOKEN + " = ? ", + new String[]{oldToken}); + } + + /** * Update an Account in database * diff --git a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/PlaylistsVM.java b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/PlaylistsVM.java index 671e2f1..6af2f2c 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/viewmodel/PlaylistsVM.java +++ b/app/src/main/java/app/fedilab/fedilabtube/viewmodel/PlaylistsVM.java @@ -87,7 +87,7 @@ public class PlaylistsVM extends AndroidViewModel { apiResponse = new APIResponse(); apiResponse.setPlaylists(new ArrayList<>()); } else { - apiResponse = new RetrofitPeertubeAPI(_mContext).playlistAction(apiAction, playlist.getId(), videoId); + apiResponse = new RetrofitPeertubeAPI(_mContext).playlistAction(apiAction, playlist!=null?playlist.getId():null, videoId, account.getAcct()); } Handler mainHandler = new Handler(Looper.getMainLooper()); if (apiResponse != null) {