2020-09-25 18:58:04 +02:00
|
|
|
package app.fedilab.fedilabtube.client;
|
|
|
|
/* Copyright 2020 Thomas Schneider
|
|
|
|
*
|
|
|
|
* This file is a part of TubeLab
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* TubeLab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with TubeLab; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
2020-09-26 16:46:51 +02:00
|
|
|
import android.app.Activity;
|
2020-09-25 18:58:04 +02:00
|
|
|
import android.content.Context;
|
2020-09-26 16:46:51 +02:00
|
|
|
import android.content.Intent;
|
2020-09-25 18:58:04 +02:00
|
|
|
import android.content.SharedPreferences;
|
2020-09-26 16:46:51 +02:00
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Looper;
|
2020-09-25 18:58:04 +02:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2020-09-26 16:46:51 +02:00
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
|
import java.net.URLDecoder;
|
2020-09-25 18:58:04 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.LinkedHashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2020-09-26 16:46:51 +02:00
|
|
|
import app.fedilab.fedilabtube.MainActivity;
|
2020-09-25 18:58:04 +02:00
|
|
|
import app.fedilab.fedilabtube.R;
|
|
|
|
import app.fedilab.fedilabtube.client.data.AccountData;
|
|
|
|
import app.fedilab.fedilabtube.client.data.CaptionData;
|
|
|
|
import app.fedilab.fedilabtube.client.data.ChannelData;
|
|
|
|
import app.fedilab.fedilabtube.client.data.CommentData;
|
|
|
|
import app.fedilab.fedilabtube.client.data.InstanceData;
|
|
|
|
import app.fedilab.fedilabtube.client.data.NotificationData;
|
2020-09-27 11:37:48 +02:00
|
|
|
import app.fedilab.fedilabtube.client.entities.OverviewVideo;
|
2020-09-25 18:58:04 +02:00
|
|
|
import app.fedilab.fedilabtube.client.data.PlaylistData;
|
|
|
|
import app.fedilab.fedilabtube.client.data.VideoData;
|
|
|
|
import app.fedilab.fedilabtube.client.entities.AccountCreation;
|
|
|
|
import app.fedilab.fedilabtube.client.entities.ChannelParams;
|
|
|
|
import app.fedilab.fedilabtube.client.entities.Error;
|
|
|
|
import app.fedilab.fedilabtube.client.entities.InstanceParams;
|
2020-09-26 16:46:51 +02:00
|
|
|
import app.fedilab.fedilabtube.client.entities.Oauth;
|
|
|
|
import app.fedilab.fedilabtube.client.entities.OauthParams;
|
2020-09-25 18:58:04 +02:00
|
|
|
import app.fedilab.fedilabtube.client.entities.PeertubeInformation;
|
|
|
|
import app.fedilab.fedilabtube.client.entities.PlaylistParams;
|
|
|
|
import app.fedilab.fedilabtube.client.entities.Rating;
|
|
|
|
import app.fedilab.fedilabtube.client.entities.RefreshToken;
|
|
|
|
import app.fedilab.fedilabtube.client.entities.Report;
|
2020-09-26 10:22:11 +02:00
|
|
|
import app.fedilab.fedilabtube.client.entities.VideoParams;
|
2020-09-26 16:46:51 +02:00
|
|
|
import app.fedilab.fedilabtube.client.entities.WellKnownNodeinfo;
|
2020-09-25 18:58:04 +02:00
|
|
|
import app.fedilab.fedilabtube.helper.Helper;
|
2020-09-26 16:46:51 +02:00
|
|
|
import app.fedilab.fedilabtube.sqlite.AccountDAO;
|
|
|
|
import app.fedilab.fedilabtube.sqlite.Sqlite;
|
2020-09-25 18:58:04 +02:00
|
|
|
import app.fedilab.fedilabtube.viewmodel.ChannelsVM;
|
|
|
|
import app.fedilab.fedilabtube.viewmodel.CommentVM;
|
|
|
|
import app.fedilab.fedilabtube.viewmodel.PlaylistsVM;
|
2020-09-26 10:22:11 +02:00
|
|
|
import app.fedilab.fedilabtube.viewmodel.TimelineVM;
|
2020-09-25 18:58:04 +02:00
|
|
|
import okhttp3.MediaType;
|
|
|
|
import okhttp3.MultipartBody;
|
|
|
|
import okhttp3.RequestBody;
|
|
|
|
import retrofit2.Call;
|
|
|
|
import retrofit2.Response;
|
|
|
|
import retrofit2.Retrofit;
|
|
|
|
import retrofit2.converter.gson.GsonConverterFactory;
|
|
|
|
|
|
|
|
public class RetrofitPeertubeAPI {
|
|
|
|
|
|
|
|
private String finalUrl;
|
|
|
|
private Context _context;
|
|
|
|
private String instance;
|
|
|
|
private String token;
|
|
|
|
private Set<String> selection;
|
|
|
|
|
|
|
|
public RetrofitPeertubeAPI(Context context) {
|
|
|
|
_context = context;
|
|
|
|
instance = Helper.getLiveInstance(context);
|
|
|
|
finalUrl = "https://" + Helper.getLiveInstance(context) + "/api/v1/";
|
|
|
|
}
|
|
|
|
|
|
|
|
public RetrofitPeertubeAPI(Context context, String instance, String token) {
|
|
|
|
_context = context;
|
|
|
|
this.instance = instance;
|
|
|
|
this.token = token;
|
|
|
|
finalUrl = "https://" + instance + "/api/v1/";
|
|
|
|
}
|
|
|
|
|
|
|
|
private PeertubeService init() {
|
|
|
|
Retrofit retrofit = new Retrofit.Builder()
|
|
|
|
.baseUrl(finalUrl)
|
|
|
|
.addConverterFactory(GsonConverterFactory.create())
|
|
|
|
.build();
|
|
|
|
SharedPreferences sharedpreferences = _context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null);
|
|
|
|
selection = sharedpreferences.getStringSet(_context.getString(R.string.set_video_language_choice), null);
|
|
|
|
return retrofit.create(PeertubeService.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
private PeertubeService initTranslation() {
|
|
|
|
Retrofit retrofit = new Retrofit.Builder()
|
|
|
|
.baseUrl("https://" + instance)
|
|
|
|
.addConverterFactory(GsonConverterFactory.create())
|
|
|
|
.build();
|
|
|
|
return retrofit.create(PeertubeService.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Verifiy credential of the authenticated user *synchronously*
|
|
|
|
* @return Account
|
|
|
|
*/
|
2020-09-26 16:46:51 +02:00
|
|
|
public RefreshToken manageToken(OauthParams oauthParams) {
|
2020-09-25 18:58:04 +02:00
|
|
|
PeertubeService peertubeService = init();
|
2020-09-26 16:46:51 +02:00
|
|
|
Call<RefreshToken> refreshTokenCall = peertubeService.manageToken(oauthParams);
|
2020-09-25 18:58:04 +02:00
|
|
|
try {
|
|
|
|
Response<RefreshToken> response = refreshTokenCall.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
return response.body();
|
|
|
|
} else {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setStatusCode(response.code());
|
|
|
|
if (response.errorBody() != null) {
|
|
|
|
error.setError(response.message());
|
|
|
|
} else {
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve notifications
|
|
|
|
*
|
|
|
|
* @param max_id String pagination
|
|
|
|
* @param since_id String pagination
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getNotifications(String max_id, String since_id) {
|
|
|
|
APIResponse apiResponse = new APIResponse();
|
|
|
|
PeertubeService peertubeService = init();
|
|
|
|
Call<List<NotificationData.Notification>> notificationsCall = peertubeService.getNotifications(token, max_id, since_id);
|
|
|
|
try {
|
|
|
|
Response<List<NotificationData.Notification>> response = notificationsCall.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
apiResponse.setPeertubeNotifications(response.body());
|
|
|
|
} else {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setStatusCode(response.code());
|
|
|
|
if (response.errorBody() != null) {
|
|
|
|
error.setError(response.message());
|
|
|
|
} else {
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
}
|
|
|
|
apiResponse.setError(error);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
apiResponse.setError(error);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
2020-09-26 16:46:51 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get caption content
|
|
|
|
* @param path String path to caption
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getCaptionContent(String path) {
|
|
|
|
APIResponse apiResponse = new APIResponse();
|
|
|
|
PeertubeService peertubeService = init();
|
|
|
|
Call<String> captionContentCall = peertubeService.getCaptionContent(path);
|
|
|
|
try {
|
|
|
|
Response<String> response = captionContentCall.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
apiResponse.setCaptionText(response.body());
|
|
|
|
} else {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setStatusCode(response.code());
|
|
|
|
if (response.errorBody() != null) {
|
|
|
|
error.setError(response.message());
|
|
|
|
} else {
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
}
|
|
|
|
apiResponse.setError(error);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
apiResponse.setError(error);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get videos in a channel
|
|
|
|
* @param channelId String id of the channel
|
|
|
|
* @param max_id String pagination
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getVideosForChannel(String channelId, String max_id) {
|
|
|
|
APIResponse apiResponse = new APIResponse();
|
|
|
|
PeertubeService peertubeService = init();
|
2020-09-27 14:12:06 +02:00
|
|
|
Call<VideoData> videoCall = peertubeService.getChannelVideos(channelId, max_id);
|
2020-09-26 16:46:51 +02:00
|
|
|
if (videoCall != null) {
|
|
|
|
try {
|
2020-09-27 14:12:06 +02:00
|
|
|
Response<VideoData> response = videoCall.execute();
|
|
|
|
if (response.isSuccessful() && response.body() != null) {
|
|
|
|
apiResponse.setPeertubes(response.body().data);
|
2020-09-26 16:46:51 +02:00
|
|
|
} else {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setStatusCode(response.code());
|
|
|
|
if (response.errorBody() != null) {
|
|
|
|
error.setError(response.message());
|
|
|
|
} else {
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
}
|
|
|
|
apiResponse.setError(error);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
apiResponse.setError(error);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-09-26 10:22:11 +02:00
|
|
|
public APIResponse getTL(TimelineVM.TimelineType timelineType, String max_id) {
|
2020-09-25 18:58:04 +02:00
|
|
|
APIResponse apiResponse = new APIResponse();
|
|
|
|
PeertubeService peertubeService = init();
|
2020-09-26 19:31:38 +02:00
|
|
|
Call<VideoData> videoCall = null;
|
|
|
|
ArrayList<String> filter = selection!=null?new ArrayList<>(selection):null;
|
2020-09-25 18:58:04 +02:00
|
|
|
switch (timelineType) {
|
2020-09-26 10:22:11 +02:00
|
|
|
case MY_VIDEOS:
|
|
|
|
videoCall = peertubeService.getMyVideo(token, max_id);
|
|
|
|
break;
|
2020-09-25 18:58:04 +02:00
|
|
|
case SUBSCRIBTIONS:
|
2020-09-26 19:31:38 +02:00
|
|
|
videoCall = peertubeService.getSubscriptionVideos(max_id, filter);
|
2020-09-25 18:58:04 +02:00
|
|
|
break;
|
|
|
|
case MOST_LIKED:
|
2020-09-26 19:31:38 +02:00
|
|
|
videoCall = peertubeService.getMostLikedVideos(max_id, filter);
|
2020-09-25 18:58:04 +02:00
|
|
|
break;
|
|
|
|
case LOCAL:
|
2020-09-26 19:31:38 +02:00
|
|
|
videoCall = peertubeService.getLocalVideos(max_id, filter);
|
2020-09-25 18:58:04 +02:00
|
|
|
break;
|
|
|
|
case TRENDING:
|
2020-09-26 19:31:38 +02:00
|
|
|
videoCall = peertubeService.getTrendingVideos(max_id, filter);
|
2020-09-25 18:58:04 +02:00
|
|
|
break;
|
|
|
|
case HISTORY:
|
|
|
|
videoCall = peertubeService.getHistory(max_id);
|
|
|
|
break;
|
|
|
|
case RECENT:
|
2020-09-26 19:31:38 +02:00
|
|
|
videoCall = peertubeService.getRecentlyAddedVideos(max_id, filter);
|
2020-09-25 18:58:04 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (videoCall != null) {
|
|
|
|
try {
|
2020-09-26 19:31:38 +02:00
|
|
|
Response<VideoData> response = videoCall.execute();
|
|
|
|
if (response.isSuccessful() && response.body() != null) {
|
|
|
|
apiResponse.setPeertubes(response.body().data);
|
2020-09-25 18:58:04 +02:00
|
|
|
} else {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setStatusCode(response.code());
|
|
|
|
if (response.errorBody() != null) {
|
|
|
|
error.setError(response.message());
|
|
|
|
} else {
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
}
|
|
|
|
apiResponse.setError(error);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
apiResponse.setError(error);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-27 11:11:39 +02:00
|
|
|
* Retrieves overview videos *synchronously*
|
2020-09-25 18:58:04 +02:00
|
|
|
*
|
2020-09-27 11:11:39 +02:00
|
|
|
* @param page String id pagination
|
2020-09-25 18:58:04 +02:00
|
|
|
* @return APIResponse
|
|
|
|
*/
|
2020-09-27 11:11:39 +02:00
|
|
|
public APIResponse getOverviewVideo(String page) {
|
2020-09-25 18:58:04 +02:00
|
|
|
APIResponse apiResponse = new APIResponse();
|
|
|
|
PeertubeService peertubeService = init();
|
2020-09-26 19:31:38 +02:00
|
|
|
ArrayList<String> filter = selection!=null?new ArrayList<>(selection):null;
|
2020-09-27 11:37:48 +02:00
|
|
|
Call<OverviewVideo> overviewVideoCall = peertubeService.getOverviewVideos(page, filter);
|
2020-09-25 18:58:04 +02:00
|
|
|
try {
|
2020-09-27 11:37:48 +02:00
|
|
|
Response<OverviewVideo> response = overviewVideoCall.execute();
|
2020-09-26 19:31:38 +02:00
|
|
|
if (response.isSuccessful() && response.body() != null) {
|
2020-09-27 11:37:48 +02:00
|
|
|
apiResponse.setOverviewVideo(response.body());
|
2020-09-25 18:58:04 +02:00
|
|
|
} else {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setStatusCode(response.code());
|
|
|
|
if (response.errorBody() != null) {
|
|
|
|
error.setError(response.message());
|
|
|
|
} else {
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
}
|
|
|
|
apiResponse.setError(error);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
apiResponse.setError(error);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if users via their uris are following the authenticated user
|
|
|
|
*
|
|
|
|
* @param uris List<String>
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse isFollowing(List<String> uris) {
|
|
|
|
APIResponse apiResponse = new APIResponse();
|
|
|
|
PeertubeService peertubeService = init();
|
|
|
|
Call<List<Map<String, Boolean>>> followingCall = peertubeService.getSubscriptionsExist(token, uris);
|
|
|
|
try {
|
|
|
|
Response<List<Map<String, Boolean>>> response = followingCall.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
apiResponse.setRelationships(response.body());
|
|
|
|
} else {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setStatusCode(response.code());
|
|
|
|
if (response.errorBody() != null) {
|
|
|
|
error.setError(response.message());
|
|
|
|
} else {
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find captions for a video
|
|
|
|
*
|
|
|
|
* @param videoId String id of the video
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getCaptions(String videoId) {
|
|
|
|
|
|
|
|
APIResponse apiResponse = new APIResponse();
|
|
|
|
PeertubeService peertubeService = init();
|
2020-09-27 14:12:06 +02:00
|
|
|
Call<CaptionData> captions = peertubeService.getCaptions(videoId);
|
2020-09-25 18:58:04 +02:00
|
|
|
try {
|
2020-09-27 14:12:06 +02:00
|
|
|
Response<CaptionData> response = captions.execute();
|
|
|
|
if (response.isSuccessful() && response.body() !=null) {
|
|
|
|
apiResponse.setCaptions(response.body().data);
|
2020-09-25 18:58:04 +02:00
|
|
|
} else {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setStatusCode(response.code());
|
|
|
|
if (response.errorBody() != null) {
|
|
|
|
error.setError(response.message());
|
|
|
|
} else {
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns informations about Peertube such privacies, licenses, etc.
|
|
|
|
*
|
|
|
|
* @return PeertubeInformation information about peertube
|
|
|
|
*/
|
|
|
|
public PeertubeInformation getPeertubeInformation() {
|
|
|
|
PeertubeInformation peertubeInformation = new PeertubeInformation();
|
|
|
|
PeertubeService peertubeService = init();
|
|
|
|
Call<Map<Integer, String>> categories = peertubeService.getCategories();
|
|
|
|
try {
|
|
|
|
Response<Map<Integer, String>> response = categories.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
peertubeInformation.setCategories(response.body());
|
|
|
|
} else {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setStatusCode(response.code());
|
|
|
|
if (response.errorBody() != null) {
|
|
|
|
error.setError(response.message());
|
|
|
|
} else {
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
Call<Map<String, String>> languages = peertubeService.getLanguages();
|
|
|
|
try {
|
|
|
|
Response<Map<String, String>> response = languages.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
peertubeInformation.setLanguages(response.body());
|
|
|
|
} else {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setStatusCode(response.code());
|
|
|
|
if (response.errorBody() != null) {
|
|
|
|
error.setError(response.message());
|
|
|
|
} else {
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
Call<Map<Integer, String>> privacies = peertubeService.getPrivacies();
|
|
|
|
try {
|
|
|
|
Response<Map<Integer, String>> response = privacies.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
peertubeInformation.setPrivacies(response.body());
|
|
|
|
} else {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setStatusCode(response.code());
|
|
|
|
if (response.errorBody() != null) {
|
|
|
|
error.setError(response.message());
|
|
|
|
} else {
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
Call<Map<Integer, String>> playlistsPrivacies = peertubeService.getPlaylistsPrivacies();
|
|
|
|
try {
|
|
|
|
Response<Map<Integer, String>> response = playlistsPrivacies.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
peertubeInformation.setPlaylistPrivacies(response.body());
|
|
|
|
} else {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setStatusCode(response.code());
|
|
|
|
if (response.errorBody() != null) {
|
|
|
|
error.setError(response.message());
|
|
|
|
} else {
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
Call<Map<Integer, String>> licenses = peertubeService.getLicences();
|
|
|
|
try {
|
|
|
|
Response<Map<Integer, String>> response = licenses.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
peertubeInformation.setLicences(response.body());
|
|
|
|
} else {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setStatusCode(response.code());
|
|
|
|
if (response.errorBody() != null) {
|
|
|
|
error.setError(response.message());
|
|
|
|
} else {
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
String lang = null;
|
|
|
|
if (PeertubeInformation.langueMapped.containsKey(Locale.getDefault().getLanguage()))
|
|
|
|
lang = PeertubeInformation.langueMapped.get(Locale.getDefault().getLanguage());
|
|
|
|
|
|
|
|
Call<Map<String, String>> translations = initTranslation().getTranslations(lang);
|
|
|
|
try {
|
|
|
|
Response<Map<String, String>> response = translations.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
peertubeInformation.setTranslations(response.body());
|
|
|
|
} else {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setStatusCode(response.code());
|
|
|
|
if (response.errorBody() != null) {
|
|
|
|
error.setError(response.message());
|
|
|
|
} else {
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return peertubeInformation;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get instances
|
|
|
|
*
|
|
|
|
* @param instanceParams InstanceParams
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getInstances(InstanceParams instanceParams) {
|
|
|
|
PeertubeService peertubeService = init();
|
|
|
|
LinkedHashMap<String, String> params = new LinkedHashMap<>();
|
|
|
|
params.put("start", "0");
|
|
|
|
params.put("count", "250");
|
|
|
|
params.put("healthy", "true");
|
|
|
|
params.put("signup", "true");
|
|
|
|
params.put("minUserQuota", instanceParams.getMinUserQuota());
|
|
|
|
Call<InstanceData> instancesCall = peertubeService.getInstances(params, instanceParams.getNsfwPolicy(), instanceParams.getCategoriesOr(), instanceParams.getLanguagesOr());
|
|
|
|
APIResponse apiResponse = new APIResponse();
|
|
|
|
try {
|
|
|
|
Response<InstanceData> response = instancesCall.execute();
|
|
|
|
if (!response.isSuccessful()) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setStatusCode(response.code());
|
|
|
|
if (response.errorBody() != null) {
|
|
|
|
error.setError(response.message());
|
|
|
|
} else {
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
}
|
|
|
|
apiResponse.setError(error);
|
|
|
|
} else {
|
|
|
|
InstanceData instanceData = response.body();
|
|
|
|
if (instanceData != null) {
|
|
|
|
apiResponse.setInstances(instanceData.data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
apiResponse.setError(error);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves peertube search *synchronously*
|
|
|
|
*
|
|
|
|
* @param query String search
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse searchPeertube(String query, String max_id) {
|
|
|
|
PeertubeService peertubeService = init();
|
|
|
|
Call<List<VideoData.Video>> searchVideosCall = peertubeService.searchVideos(query, max_id);
|
|
|
|
APIResponse apiResponse = new APIResponse();
|
|
|
|
try {
|
|
|
|
Response<List<VideoData.Video>> response = searchVideosCall.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
apiResponse.setPeertubes(response.body());
|
|
|
|
} else {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setStatusCode(response.code());
|
|
|
|
if (response.errorBody() != null) {
|
|
|
|
error.setError(response.message());
|
|
|
|
} else {
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
}
|
|
|
|
apiResponse.setError(error);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
apiResponse.setError(error);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Verifiy credential of the authenticated user *synchronously*
|
|
|
|
* @return Account
|
|
|
|
*/
|
|
|
|
public AccountData.Account verifyCredentials(String token, String instance) {
|
|
|
|
PeertubeService peertubeService = init();
|
|
|
|
Call<AccountData.Account> accountCall = peertubeService.verifyCredentials(token);
|
|
|
|
APIResponse apiResponse = new APIResponse();
|
|
|
|
try {
|
|
|
|
Response<AccountData.Account> response = accountCall.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
return response.body();
|
|
|
|
} else {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setStatusCode(response.code());
|
|
|
|
if (response.errorBody() != null) {
|
|
|
|
error.setError(response.message());
|
|
|
|
} else {
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
}
|
|
|
|
apiResponse.setError(error);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
apiResponse.setError(error);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public APIResponse report(Report report) {
|
|
|
|
PeertubeService peertubeService = init();
|
|
|
|
Call<String> report1 = peertubeService.report(token, report);
|
|
|
|
APIResponse apiResponse = new APIResponse();
|
|
|
|
try {
|
|
|
|
Response<String> response = report1.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
apiResponse.setActionReturn(response.body());
|
|
|
|
} else {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setStatusCode(response.code());
|
|
|
|
if (response.errorBody() != null) {
|
|
|
|
error.setError(response.message());
|
|
|
|
} else {
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
}
|
|
|
|
apiResponse.setError(error);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
apiResponse.setError(error);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
2020-09-26 10:22:11 +02:00
|
|
|
/***
|
|
|
|
* Update a video
|
|
|
|
* @param videoId String id of the video
|
|
|
|
* @param videoParams VideoParams params for the video
|
|
|
|
* @param thumbnail File thumbnail
|
|
|
|
* @param previewfile File preview
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse updateVideo(String videoId, VideoParams videoParams, File thumbnail, File previewfile) {
|
|
|
|
PeertubeService peertubeService = init();
|
|
|
|
MultipartBody.Part bodyThumbnail = null;
|
|
|
|
MultipartBody.Part bodyPreviewfile= null;
|
|
|
|
if (thumbnail != null) {
|
|
|
|
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), thumbnail);
|
|
|
|
bodyThumbnail = MultipartBody.Part.createFormData("image", thumbnail.getName(), requestFile);
|
|
|
|
}
|
|
|
|
if (previewfile != null) {
|
|
|
|
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), previewfile);
|
|
|
|
bodyPreviewfile = MultipartBody.Part.createFormData("image", previewfile.getName(), requestFile);
|
|
|
|
}
|
|
|
|
Call<String> upload = peertubeService.updateVideo(token, videoId, videoParams, bodyThumbnail, bodyPreviewfile);
|
|
|
|
APIResponse apiResponse = new APIResponse();
|
|
|
|
try {
|
|
|
|
Response<String> response = upload.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
apiResponse.setActionReturn(response.body());
|
|
|
|
} else {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setStatusCode(response.code());
|
|
|
|
if (response.errorBody() != null) {
|
|
|
|
error.setError(response.message());
|
|
|
|
} else {
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
}
|
|
|
|
apiResponse.setError(error);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
apiResponse.setError(error);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
2020-09-25 18:58:04 +02:00
|
|
|
public APIResponse createAccount(AccountCreation accountCreation) {
|
|
|
|
PeertubeService peertubeService = init();
|
|
|
|
Call<String> report1 = peertubeService.register(accountCreation.getEmail(), accountCreation.getPassword(), accountCreation.getUsername(), accountCreation.getDisplayName());
|
|
|
|
APIResponse apiResponse = new APIResponse();
|
|
|
|
try {
|
|
|
|
Response<String> response = report1.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
apiResponse.setActionReturn(accountCreation.getEmail());
|
|
|
|
} else {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setStatusCode(response.code());
|
|
|
|
if (response.errorBody() != null) {
|
|
|
|
error.setError(response.message());
|
|
|
|
} else {
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
}
|
|
|
|
apiResponse.setError(error);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
apiResponse.setError(error);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
public APIResponse post(ActionType actionType, String id, String element) {
|
|
|
|
PeertubeService peertubeService = init();
|
|
|
|
Call<String> postCall = null;
|
|
|
|
APIResponse apiResponse = new APIResponse();
|
|
|
|
switch (actionType) {
|
|
|
|
case FOLLOW:
|
|
|
|
postCall = peertubeService.follow(token, id);
|
|
|
|
break;
|
|
|
|
case UNFOLLOW:
|
|
|
|
postCall = peertubeService.unfollow(token, id);
|
|
|
|
break;
|
|
|
|
case MUTE:
|
|
|
|
postCall = peertubeService.mute(token, id);
|
|
|
|
break;
|
|
|
|
case UNMUTE:
|
|
|
|
postCall = peertubeService.unmute(token, id);
|
|
|
|
break;
|
|
|
|
case RATEVIDEO:
|
|
|
|
postCall = peertubeService.rate(token, id, element);
|
|
|
|
break;
|
|
|
|
case PEERTUBEDELETEVIDEO:
|
|
|
|
postCall = peertubeService.deleteVideo(token, id);
|
|
|
|
break;
|
|
|
|
case DELETE_CHANNEL:
|
|
|
|
postCall = peertubeService.deleteChannel(token, id);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (postCall != null) {
|
|
|
|
try {
|
|
|
|
Response<String> response = postCall.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
apiResponse.setActionReturn(response.body());
|
|
|
|
} else {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setStatusCode(response.code());
|
|
|
|
if (response.errorBody() != null) {
|
|
|
|
error.setError(response.message());
|
|
|
|
} else {
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
}
|
|
|
|
apiResponse.setError(error);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
apiResponse.setError(error);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get accounts data
|
|
|
|
*
|
|
|
|
* @param accountDataType AccountDataType (type of requested data)
|
|
|
|
* @param maxId String (pagination)
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getAccountData(DataType accountDataType, String maxId) {
|
|
|
|
PeertubeService peertubeService = init();
|
|
|
|
Call<AccountData> accountDataCall = null;
|
|
|
|
switch (accountDataType) {
|
|
|
|
case SUBSCRIBER:
|
|
|
|
accountDataCall = peertubeService.getSubscription("Bearer " + token, maxId);
|
|
|
|
break;
|
|
|
|
case MUTED:
|
|
|
|
accountDataCall = peertubeService.getMuted("Bearer " + token, maxId);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
APIResponse apiResponse = new APIResponse();
|
|
|
|
if (accountDataCall != null) {
|
|
|
|
try {
|
|
|
|
Response<AccountData> response = accountDataCall.execute();
|
|
|
|
if (!response.isSuccessful()) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setStatusCode(response.code());
|
|
|
|
if (response.errorBody() != null) {
|
|
|
|
error.setError(response.message());
|
|
|
|
} else {
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
}
|
|
|
|
apiResponse.setError(error);
|
|
|
|
} else {
|
|
|
|
AccountData accountData = response.body();
|
|
|
|
if (accountData != null) {
|
|
|
|
apiResponse.setAccounts(accountData.data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
apiResponse.setError(error);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create or update a channel
|
|
|
|
*
|
|
|
|
* @param apiAction ChannelsVM.action
|
|
|
|
* @param channelId String
|
|
|
|
* @param channelParams PlaylistParams
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
2020-09-26 19:31:38 +02:00
|
|
|
public APIResponse createOrUpdateChannel(ChannelsVM.action apiAction, String channelId, ChannelParams channelParams) {
|
2020-09-25 18:58:04 +02:00
|
|
|
PeertubeService peertubeService = init();
|
|
|
|
|
|
|
|
APIResponse apiResponse = new APIResponse();
|
|
|
|
try {
|
|
|
|
if (apiAction == ChannelsVM.action.CREATE_CHANNEL) {
|
|
|
|
Call<String> stringCall = peertubeService.addChannel(token, channelParams);
|
|
|
|
Response<String> response = stringCall.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
apiResponse.setActionReturn(response.body());
|
|
|
|
} else {
|
|
|
|
Error error = generateError(response.code(), response.message());
|
|
|
|
apiResponse.setError(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (apiAction == ChannelsVM.action.UPDATE_CHANNEL) {
|
|
|
|
Call<String> stringCall = peertubeService.updateChannel(token, channelId, channelParams);
|
|
|
|
Response<String> response = stringCall.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
apiResponse.setActionReturn(response.body());
|
|
|
|
} else {
|
|
|
|
Error error = generateError(response.code(), response.message());
|
|
|
|
apiResponse.setError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
apiResponse.setError(error);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
2020-09-26 10:22:11 +02:00
|
|
|
|
2020-09-26 16:46:51 +02:00
|
|
|
/**
|
|
|
|
* Get Oauth
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public Oauth oauthClient() {
|
|
|
|
PeertubeService peertubeService = init();
|
|
|
|
try {
|
|
|
|
Call<Oauth> oauth = peertubeService.getOauth();
|
|
|
|
Response<Oauth> response = oauth.execute();
|
|
|
|
if( response.isSuccessful() && response.body() != null) {
|
|
|
|
return response.body();
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get NodeInfo
|
|
|
|
* @param instance String
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public WellKnownNodeinfo.NodeInfo getNodeInfo(String instance) {
|
|
|
|
PeertubeService peertubeService = init();
|
|
|
|
try {
|
|
|
|
Call<WellKnownNodeinfo> wellKnownNodeinfoCall = peertubeService.getWellKnownNodeinfo();
|
|
|
|
Response<WellKnownNodeinfo> response = wellKnownNodeinfoCall.execute();
|
|
|
|
if( response.isSuccessful() && response.body() != null) {
|
|
|
|
if (response.body().getHref() != null) {
|
|
|
|
Call<WellKnownNodeinfo.NodeInfo> nodeinfo = peertubeService.getNodeinfo(response.body().getHref());
|
|
|
|
Response<WellKnownNodeinfo.NodeInfo> responseNodeInfo = nodeinfo.execute();
|
|
|
|
return responseNodeInfo.body();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-09-25 18:58:04 +02:00
|
|
|
/**
|
|
|
|
* Get channel data
|
|
|
|
*
|
|
|
|
* @param accountDataType AccountDataType (type of requested data)
|
|
|
|
* @param element String (pagination or name for the channel)
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getChannelData(DataType accountDataType, String element) {
|
|
|
|
PeertubeService peertubeService = init();
|
|
|
|
|
|
|
|
APIResponse apiResponse = new APIResponse();
|
|
|
|
switch (accountDataType) {
|
2020-09-26 10:22:11 +02:00
|
|
|
case MY_CHANNELS:
|
2020-09-25 18:58:04 +02:00
|
|
|
case CHANNELS_FOR_ACCOUNT:
|
2020-09-26 10:22:11 +02:00
|
|
|
Call<List<ChannelData.Channel>> channelDataCall;
|
|
|
|
if( accountDataType == DataType.MY_CHANNELS) {
|
|
|
|
channelDataCall = peertubeService.getMyChannels(token);
|
|
|
|
} else {
|
|
|
|
channelDataCall = peertubeService.getChannelsForAccount(element);
|
|
|
|
}
|
2020-09-25 18:58:04 +02:00
|
|
|
try {
|
2020-09-26 10:22:11 +02:00
|
|
|
Response<List<ChannelData.Channel>> response = channelDataCall.execute();
|
2020-09-25 18:58:04 +02:00
|
|
|
if (!response.isSuccessful()) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setStatusCode(response.code());
|
|
|
|
if (response.errorBody() != null) {
|
|
|
|
error.setError(response.message());
|
|
|
|
} else {
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
}
|
|
|
|
apiResponse.setError(error);
|
|
|
|
} else {
|
2020-09-26 10:22:11 +02:00
|
|
|
apiResponse.setChannels( response.body());
|
2020-09-25 18:58:04 +02:00
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
apiResponse.setError(error);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case CHANNEL:
|
|
|
|
Call<ChannelData.Channel> channelCall = peertubeService.getChannel(element);
|
|
|
|
try {
|
|
|
|
Response<ChannelData.Channel> response = channelCall.execute();
|
|
|
|
if (!response.isSuccessful()) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setStatusCode(response.code());
|
|
|
|
if (response.errorBody() != null) {
|
|
|
|
error.setError(response.message());
|
|
|
|
} else {
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
}
|
|
|
|
apiResponse.setError(error);
|
|
|
|
} else {
|
|
|
|
ChannelData.Channel channelData = response.body();
|
|
|
|
if (channelData != null) {
|
|
|
|
List<ChannelData.Channel> channelList = new ArrayList<>();
|
|
|
|
channelList.add(channelData);
|
|
|
|
apiResponse.setChannels(channelList);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
apiResponse.setError(error);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create or update a playlist
|
|
|
|
*
|
|
|
|
* @param apiAction PlaylistsVM.action
|
|
|
|
* @param playlistId String
|
|
|
|
* @param playlistParams PlaylistParams
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse createOrUpdatePlaylist(PlaylistsVM.action apiAction, String playlistId, PlaylistParams playlistParams, File thumbnail) {
|
|
|
|
PeertubeService peertubeService = init();
|
|
|
|
|
|
|
|
APIResponse apiResponse = new APIResponse();
|
|
|
|
MultipartBody.Part body = null;
|
|
|
|
if (thumbnail != null) {
|
|
|
|
RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), thumbnail);
|
|
|
|
body = MultipartBody.Part.createFormData("image", thumbnail.getName(), requestFile);
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
if (apiAction == PlaylistsVM.action.CREATE_PLAYLIST) {
|
|
|
|
Call<String> stringCall = peertubeService.addPlaylist(token, playlistParams, body);
|
|
|
|
Response<String> response = stringCall.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
apiResponse.setActionReturn(response.body());
|
|
|
|
} else {
|
|
|
|
Error error = generateError(response.code(), response.message());
|
|
|
|
apiResponse.setError(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (apiAction == PlaylistsVM.action.UPDATE_PLAYLIST) {
|
|
|
|
Call<String> stringCall = peertubeService.updatePlaylist(token, playlistId, playlistParams, body);
|
|
|
|
Response<String> response = stringCall.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
apiResponse.setActionReturn(response.body());
|
|
|
|
} else {
|
|
|
|
Error error = generateError(response.code(), response.message());
|
|
|
|
apiResponse.setError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
apiResponse.setError(error);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves playlist *synchronously*
|
|
|
|
*
|
|
|
|
* @param type PlaylistsVM.action
|
|
|
|
* @param playlistId String id of the playlist
|
|
|
|
* @param videoId String id of the video
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse playlistAction(PlaylistsVM.action type, String playlistId, String videoId) {
|
|
|
|
PeertubeService peertubeService = init();
|
|
|
|
|
|
|
|
APIResponse apiResponse = new APIResponse();
|
|
|
|
try {
|
|
|
|
|
|
|
|
if (type == PlaylistsVM.action.GET_PLAYLIST_INFO) {
|
|
|
|
Call<PlaylistData.Playlist> playlistCall = peertubeService.getPlaylist(playlistId);
|
|
|
|
Response<PlaylistData.Playlist> response = playlistCall.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
List<PlaylistData.Playlist> playlists = new ArrayList<>();
|
|
|
|
playlists.add(response.body());
|
|
|
|
apiResponse.setPlaylists(playlists);
|
|
|
|
} else {
|
|
|
|
Error error = generateError(response.code(), response.message());
|
|
|
|
apiResponse.setError(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (type == PlaylistsVM.action.GET_PLAYLISTS) {
|
|
|
|
Call<List<PlaylistData.Playlist>> playlistsCall = peertubeService.getPlaylists();
|
|
|
|
Response<List<PlaylistData.Playlist>> response = playlistsCall.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
apiResponse.setPlaylists(response.body());
|
|
|
|
} else {
|
|
|
|
Error error = generateError(response.code(), response.message());
|
|
|
|
apiResponse.setError(error);
|
|
|
|
}
|
|
|
|
} else if (type == PlaylistsVM.action.GET_LIST_VIDEOS) {
|
|
|
|
Call<List<VideoData.Video>> videosPlayList = peertubeService.getVideosPlayList(playlistId);
|
|
|
|
Response<List<VideoData.Video>> response = videosPlayList.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
apiResponse.setPeertubes(response.body());
|
|
|
|
} else {
|
|
|
|
Error error = generateError(response.code(), response.message());
|
|
|
|
apiResponse.setError(error);
|
|
|
|
}
|
|
|
|
} else if (type == PlaylistsVM.action.DELETE_PLAYLIST) {
|
|
|
|
Call<String> stringCall = peertubeService.deletePlaylist(token, playlistId);
|
|
|
|
Response<String> response = stringCall.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
apiResponse.setActionReturn(response.body());
|
|
|
|
} else {
|
|
|
|
Error error = generateError(response.code(), response.message());
|
|
|
|
apiResponse.setError(error);
|
|
|
|
}
|
|
|
|
} else if (type == PlaylistsVM.action.ADD_VIDEOS) {
|
|
|
|
Call<String> stringCall = peertubeService.addVideoInPlaylist(token, playlistId, videoId);
|
|
|
|
Response<String> response = stringCall.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
apiResponse.setActionReturn(response.body());
|
|
|
|
} else {
|
|
|
|
Error error = generateError(response.code(), response.message());
|
|
|
|
apiResponse.setError(error);
|
|
|
|
}
|
|
|
|
} else if (type == PlaylistsVM.action.DELETE_VIDEOS) {
|
|
|
|
Call<String> stringCall = peertubeService.deleteVideoInPlaylist(token, playlistId, videoId);
|
|
|
|
Response<String> response = stringCall.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
apiResponse.setActionReturn(response.body());
|
|
|
|
} else {
|
|
|
|
Error error = generateError(response.code(), response.message());
|
|
|
|
apiResponse.setError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
apiResponse.setError(error);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
public APIResponse getComments(CommentVM.action type, String videoId, String forCommentId) {
|
|
|
|
PeertubeService peertubeService = init();
|
|
|
|
|
|
|
|
APIResponse apiResponse = new APIResponse();
|
|
|
|
try {
|
|
|
|
if (type == CommentVM.action.GET_THREAD) {
|
2020-09-27 14:12:06 +02:00
|
|
|
Call<CommentData> commentsCall = peertubeService.getComments(videoId);
|
|
|
|
Response<CommentData> response = commentsCall.execute();
|
|
|
|
if (response.isSuccessful() && response.body() != null) {
|
|
|
|
apiResponse.setComments(response.body().data);
|
2020-09-25 18:58:04 +02:00
|
|
|
} else {
|
|
|
|
Error error = generateError(response.code(), response.message());
|
|
|
|
apiResponse.setError(error);
|
|
|
|
}
|
|
|
|
} else if (type == CommentVM.action.GET_REPLIES) {
|
2020-09-27 14:12:06 +02:00
|
|
|
Call<CommentData> commentsCall = peertubeService.getReplies(videoId, forCommentId);
|
|
|
|
Response<CommentData> response = commentsCall.execute();
|
|
|
|
if (response.isSuccessful() && response.body() != null) {
|
|
|
|
apiResponse.setComments(response.body().data);
|
2020-09-25 18:58:04 +02:00
|
|
|
} else {
|
|
|
|
Error error = generateError(response.code(), response.message());
|
|
|
|
apiResponse.setError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
apiResponse.setError(error);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manage comments *synchronously*
|
|
|
|
*
|
|
|
|
* @param type (CommentVM.action
|
|
|
|
* @param videoId String id of the video
|
|
|
|
* @param toCommentId String id of the comment for replies
|
|
|
|
* @param text String text
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
2020-09-26 16:46:51 +02:00
|
|
|
public APIResponse commentAction(RetrofitPeertubeAPI.ActionType type, String videoId, String toCommentId, String text) {
|
2020-09-25 18:58:04 +02:00
|
|
|
PeertubeService peertubeService = init();
|
|
|
|
|
|
|
|
APIResponse apiResponse = new APIResponse();
|
|
|
|
try {
|
2020-09-26 16:46:51 +02:00
|
|
|
if (type == ActionType.ADD_COMMENT) {
|
2020-09-25 18:58:04 +02:00
|
|
|
Call<String> stringCall = peertubeService.postComment(token, videoId, text);
|
|
|
|
Response<String> response = stringCall.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
apiResponse.setActionReturn(response.body());
|
|
|
|
} else {
|
|
|
|
Error error = generateError(response.code(), response.message());
|
|
|
|
apiResponse.setError(error);
|
|
|
|
}
|
2020-09-26 16:46:51 +02:00
|
|
|
} else if (type == ActionType.REPLY) {
|
2020-09-25 18:58:04 +02:00
|
|
|
Call<String> stringCall = peertubeService.postReply(token, videoId, toCommentId, text);
|
|
|
|
Response<String> response = stringCall.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
apiResponse.setActionReturn(response.body());
|
|
|
|
} else {
|
|
|
|
Error error = generateError(response.code(), response.message());
|
|
|
|
apiResponse.setError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
apiResponse.setError(error);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves playlist *synchronously*
|
|
|
|
*
|
|
|
|
* @param id String id
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getPlayist(String id) {
|
|
|
|
PeertubeService peertubeService = init();
|
|
|
|
Call<PlaylistData.Playlist> playlistCall;
|
|
|
|
playlistCall = peertubeService.getPlaylist(id);
|
|
|
|
APIResponse apiResponse = new APIResponse();
|
|
|
|
try {
|
|
|
|
Response<PlaylistData.Playlist> response = playlistCall.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
List<PlaylistData.Playlist> playlists = new ArrayList<>();
|
|
|
|
playlists.add(response.body());
|
|
|
|
apiResponse.setPlaylists(playlists);
|
|
|
|
} else {
|
|
|
|
Error error = generateError(response.code(), response.message());
|
|
|
|
apiResponse.setError(error);
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
apiResponse.setError(error);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Error generateError(int responseCode, String message) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setStatusCode(responseCode);
|
|
|
|
if (message != null) {
|
|
|
|
error.setError(message);
|
|
|
|
} else {
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
}
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves rating of user on a video *synchronously*
|
|
|
|
*
|
|
|
|
* @param id String id
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getRating(String id) {
|
|
|
|
PeertubeService peertubeService = init();
|
|
|
|
Call<Rating> rating = peertubeService.getRating(token, id);
|
|
|
|
APIResponse apiResponse = new APIResponse();
|
|
|
|
try {
|
|
|
|
Response<Rating> response = rating.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
apiResponse.setRating(response.body());
|
|
|
|
} else {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setStatusCode(response.code());
|
|
|
|
if (response.errorBody() != null) {
|
|
|
|
error.setError(response.message());
|
|
|
|
} else {
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
apiResponse.setError(error);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return apiResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves videos *synchronously*
|
|
|
|
*
|
|
|
|
* @param id String id
|
|
|
|
* @return APIResponse
|
|
|
|
*/
|
|
|
|
public APIResponse getVideos(String id) {
|
|
|
|
PeertubeService peertubeService = init();
|
|
|
|
Call<VideoData.Video> video = peertubeService.getVideo(id);
|
|
|
|
APIResponse apiResponse = new APIResponse();
|
|
|
|
try {
|
|
|
|
Response<VideoData.Video> response = video.execute();
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
List<VideoData.Video> videos = new ArrayList<>();
|
|
|
|
videos.add(response.body());
|
|
|
|
apiResponse.setPeertubes(videos);
|
|
|
|
} else {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setStatusCode(response.code());
|
|
|
|
if (response.errorBody() != null) {
|
|
|
|
error.setError(response.message());
|
|
|
|
} else {
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
Error error = new Error();
|
|
|
|
error.setError(_context.getString(R.string.toast_error));
|
|
|
|
apiResponse.setError(error);
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return apiResponse;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-09-26 16:46:51 +02:00
|
|
|
public static void updateCredential(Context _mContext, String token, String client_id, String client_secret, String refresh_token, String host) {
|
|
|
|
new Thread(() -> {
|
|
|
|
AccountData.Account account;
|
|
|
|
String instance;
|
|
|
|
if (host.startsWith("tube")) {
|
|
|
|
instance = host;
|
|
|
|
} else {
|
|
|
|
instance = Helper.getPeertubeUrl(host);
|
|
|
|
}
|
|
|
|
account = new RetrofitPeertubeAPI(_mContext).verifyCredentials(token, instance);
|
|
|
|
if (account == null)
|
|
|
|
return;
|
|
|
|
try {
|
|
|
|
//At the state the instance can be encoded
|
|
|
|
instance = URLDecoder.decode(instance, "utf-8");
|
|
|
|
} catch (UnsupportedEncodingException ignored) {
|
|
|
|
}
|
|
|
|
SharedPreferences sharedpreferences = _mContext.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
account.setToken(token);
|
|
|
|
account.setClient_id(client_id);
|
|
|
|
account.setClient_secret(client_secret);
|
|
|
|
account.setRefresh_token(refresh_token);
|
|
|
|
account.setHost(instance);
|
|
|
|
SQLiteDatabase db = Sqlite.getInstance(_mContext.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
|
|
|
boolean userExists = new AccountDAO(_mContext, db).userExist(account);
|
|
|
|
SharedPreferences.Editor editor = sharedpreferences.edit();
|
|
|
|
editor.putString(Helper.PREF_KEY_ID, account.getId());
|
|
|
|
if (!host.startsWith("tube")) {
|
|
|
|
editor.putString(Helper.PREF_INSTANCE, host);
|
|
|
|
}
|
|
|
|
editor.apply();
|
|
|
|
if (userExists)
|
|
|
|
new AccountDAO(_mContext, db).updateAccountCredential(account);
|
|
|
|
else {
|
|
|
|
if (account.getName() != null && account.getCreatedAt() != null)
|
|
|
|
new AccountDAO(_mContext, db).insertAccount(account);
|
|
|
|
}
|
|
|
|
Handler mainHandler = new Handler(Looper.getMainLooper());
|
|
|
|
Runnable myRunnable = () -> {
|
|
|
|
Intent mainActivity = new Intent(_mContext, MainActivity.class);
|
|
|
|
mainActivity.putExtra(Helper.INTENT_ACTION, Helper.ADD_USER_INTENT);
|
|
|
|
_mContext.startActivity(mainActivity);
|
|
|
|
((Activity) _mContext).finish();
|
|
|
|
};
|
|
|
|
mainHandler.post(myRunnable);
|
|
|
|
}).start();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-09-25 18:58:04 +02:00
|
|
|
|
|
|
|
public enum DataType {
|
|
|
|
SUBSCRIBER,
|
|
|
|
MUTED,
|
|
|
|
CHANNELS_FOR_ACCOUNT,
|
2020-09-26 10:22:11 +02:00
|
|
|
CHANNEL,
|
|
|
|
MY_CHANNELS
|
2020-09-25 18:58:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public enum ActionType {
|
|
|
|
FOLLOW,
|
|
|
|
UNFOLLOW,
|
|
|
|
MUTE,
|
|
|
|
UNMUTE,
|
|
|
|
RATEVIDEO,
|
|
|
|
PEERTUBEDELETECOMMENT,
|
|
|
|
PEERTUBEDELETEVIDEO,
|
|
|
|
REPORT_VIDEO,
|
2020-09-26 10:22:11 +02:00
|
|
|
REPORT_ACCOUNT,
|
2020-09-26 16:46:51 +02:00
|
|
|
DELETE_CHANNEL,
|
|
|
|
ADD_COMMENT,
|
|
|
|
REPLY,
|
2020-09-25 18:58:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|