improve API calls

This commit is contained in:
stom79 2018-10-07 10:45:34 +02:00
parent 923e89984f
commit 07a35b1f68
8 changed files with 133 additions and 21 deletions

View File

@ -85,6 +85,7 @@ import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Attachment;
import fr.gouv.etalab.mastodon.client.Entities.Error;
import fr.gouv.etalab.mastodon.client.Entities.RemoteInstance;
import fr.gouv.etalab.mastodon.client.Entities.Status;
import fr.gouv.etalab.mastodon.client.HttpsConnection;
import fr.gouv.etalab.mastodon.drawers.StatusListAdapter;
@ -359,8 +360,8 @@ public class ShowAccountActivity extends BaseActivity implements OnPostActionInt
case R.id.action_follow_instance:
String finalInstanceName = splitAcct[1];
final SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
List<String> instance = new InstancesDAO(ShowAccountActivity.this, db).getInstanceByName(finalInstanceName);
if( instance != null && instance.size() > 0 ){
List<RemoteInstance> remoteInstances = new InstancesDAO(ShowAccountActivity.this, db).getInstanceByName(finalInstanceName);
if( remoteInstances != null && remoteInstances.size() > 0 ){
Toast.makeText(getApplicationContext(), R.string.toast_instance_already_added,Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), InstanceFederatedActivity.class);
Bundle bundle = new Bundle();
@ -378,7 +379,7 @@ public class ShowAccountActivity extends BaseActivity implements OnPostActionInt
runOnUiThread(new Runnable() {
public void run() {
final SQLiteDatabase db = Sqlite.getInstance(getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
new InstancesDAO(ShowAccountActivity.this, db).insertInstance(finalInstanceName);
new InstancesDAO(ShowAccountActivity.this, db).insertInstance(finalInstanceName, "MASTODON");
Toast.makeText(getApplicationContext(), R.string.toast_instance_followed,Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), InstanceFederatedActivity.class);
Bundle bundle = new Bundle();

View File

@ -24,10 +24,13 @@ import java.util.List;
import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.Entities.Peertube;
import fr.gouv.etalab.mastodon.client.Entities.RemoteInstance;
import fr.gouv.etalab.mastodon.client.Entities.Status;
import fr.gouv.etalab.mastodon.helper.FilterToots;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveFeedsInterface;
import fr.gouv.etalab.mastodon.sqlite.InstancesDAO;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import fr.gouv.etalab.mastodon.sqlite.StatusCacheDAO;
@ -136,12 +139,18 @@ public class RetrieveFeedsAsyncTask extends AsyncTask<Void, Void, Void> {
apiResponse = api.getPublicTimeline(false, max_id);
break;
case REMOTE_INSTANCE:
apiResponse = api.getPublicTimeline(this.instanceName,false, max_id);
List<fr.gouv.etalab.mastodon.client.Entities.Status> statusesTemp = apiResponse.getStatuses();
if( statusesTemp != null){
for(fr.gouv.etalab.mastodon.client.Entities.Status status: statusesTemp){
status.setType(action);
SQLiteDatabase db = Sqlite.getInstance(this.contextReference.get(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
List<RemoteInstance> remoteInstanceObj = new InstancesDAO(this.contextReference.get(), db).getInstanceByName(this.instanceName);
if( remoteInstanceObj != null && remoteInstanceObj.size() > 0 && remoteInstanceObj.get(0).getType().equals("MASTODON")) {
apiResponse = api.getPublicTimeline(this.instanceName, false, max_id);
List<fr.gouv.etalab.mastodon.client.Entities.Status> statusesTemp = apiResponse.getStatuses();
if( statusesTemp != null){
for(fr.gouv.etalab.mastodon.client.Entities.Status status: statusesTemp){
status.setType(action);
}
}
}else {
apiResponse = api.getPeertube(this.instanceName, max_id);
}
break;
case FAVOURITES:
@ -163,7 +172,7 @@ public class RetrieveFeedsAsyncTask extends AsyncTask<Void, Void, Void> {
break;
case CACHE_BOOKMARKS:
apiResponse = new APIResponse();
SQLiteDatabase db = Sqlite.getInstance(contextReference.get(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
db = Sqlite.getInstance(contextReference.get(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
List<fr.gouv.etalab.mastodon.client.Entities.Status> statuses = new StatusCacheDAO(contextReference.get(), db).getAllStatus(StatusCacheDAO.BOOKMARK_CACHE);
apiResponse.setStatuses(statuses);
break;

View File

@ -549,6 +549,37 @@ public class API {
}
/**
* Retrieves Peertube videos from an instance *synchronously*
* @return APIResponse
*/
public APIResponse getPeertube(String instance, String max_id) {
List<Peertube> peertubes = new ArrayList<>();
HashMap<String, String> params = new HashMap<>();
if( max_id == null)
max_id = "0";
params.put("start", max_id);
params.put("count", "50");
try {
HttpsConnection httpsConnection = new HttpsConnection(context);
String response = httpsConnection.get("https://"+instance+"/api/v1/videos", 60, params, null);
JSONArray jsonArray = new JSONObject(response).getJSONArray("data");
peertubes = parsePeertube(jsonArray);
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
apiResponse.setPeertubes(peertubes);
return apiResponse;
}
/**
* Retrieves home timeline for the account *synchronously*
@ -559,7 +590,7 @@ public class API {
List<HowToVideo> howToVideos = new ArrayList<>();
try {
HttpsConnection httpsConnection = new HttpsConnection(context);
String response = httpsConnection.get("https://peertube.social/api/v1/video-channels/bb32394a-a6d2-4f41-9b8e-ad9514a66009/videos", 60, null, null);
String response = httpsConnection.get("https://peertube.social/api/v1/video-channels/mastalab_channel/videos", 60, null, null);
JSONArray jsonArray = new JSONObject(response).getJSONArray("data");
howToVideos = parseHowTos(jsonArray);
} catch (HttpsConnection.HttpsConnectionException e) {
@ -2185,6 +2216,53 @@ public class API {
return howToVideos;
}
/**
* Parse json response for several howto
* @param jsonArray JSONArray
* @return List<Peertube>
*/
private List<Peertube> parsePeertube(JSONArray jsonArray){
List<Peertube> peertubes = new ArrayList<>();
try {
int i = 0;
while (i < jsonArray.length() ){
JSONObject resobj = jsonArray.getJSONObject(i);
Peertube peertube = parsePeertube(context, resobj);
i++;
peertubes.add(peertube);
}
} catch (JSONException e) {
setDefaultError(e);
}
return peertubes;
}
/**
* Parse json response for unique how to
* @param resobj JSONObject
* @return Peertube
*/
private static Peertube parsePeertube(Context context, JSONObject resobj){
Peertube peertube = new Peertube();
try {
peertube.setId(resobj.get("id").toString());
peertube.setUuid(resobj.get("uuid").toString());
peertube.setName(resobj.get("name").toString());
peertube.setDescription(resobj.get("description").toString());
peertube.setEmbedPath(resobj.get("embedPath").toString());
peertube.setPreviewPath(resobj.get("previewPath").toString());
peertube.setThumbnailPath(resobj.get("thumbnailPath").toString());
} catch (JSONException e) {
e.printStackTrace();
}
return peertube;
}
/**
* Parse json response for unique how to
* @param resobj JSONObject

View File

@ -31,6 +31,7 @@ public class APIResponse {
private List<Notification> notifications = null;
private List<Relationship> relationships = null;
private List<HowToVideo> howToVideos = null;
private List<Peertube> peertubes = null;
private List<Filters> filters = null;
private List<String> domains = null;
private List<fr.gouv.etalab.mastodon.client.Entities.List> lists = null;
@ -150,4 +151,12 @@ public class APIResponse {
public void setHowToVideos(List<HowToVideo> howToVideos) {
this.howToVideos = howToVideos;
}
public List<Peertube> getPeertubes() {
return peertubes;
}
public void setPeertubes(List<Peertube> peertubes) {
this.peertubes = peertubes;
}
}

View File

@ -56,11 +56,13 @@ public class PeertubeAdapter extends RecyclerView.Adapter implements OnListActio
private List<Peertube> peertubes;
private LayoutInflater layoutInflater;
private Context context;
private String instance;
public PeertubeAdapter(Context context, List<Peertube> peertubes){
public PeertubeAdapter(Context context, String instance, List<Peertube> peertubes){
this.peertubes = peertubes;
layoutInflater = LayoutInflater.from(context);
this.context = context;
this.instance = instance;
}
@ -100,14 +102,14 @@ public class PeertubeAdapter extends RecyclerView.Adapter implements OnListActio
next.setBounds(0,0,(int) (30 * scale + 0.5f),(int) (30 * scale + 0.5f));
holder.how_to_description.setCompoundDrawables(null, null, next, null);
Glide.with(holder.how_to_image.getContext())
.load("https://peertube.social" + peertube.getThumbnailPath())
.load("https://" + instance + peertube.getThumbnailPath())
.into(holder.how_to_image);
holder.how_to_container.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, WebviewActivity.class);
Bundle b = new Bundle();
String finalUrl = "https://peertube.social" + peertube.getEmbedPath();
String finalUrl = "https://" + instance + peertube.getEmbedPath();
b.putString("url", finalUrl);
b.putBoolean("peertubeLink", true);
Pattern link = Pattern.compile("(https?:\\/\\/[\\da-z\\.-]+\\.[a-z\\.]{2,10})\\/videos\\/embed\\/(\\w{8}-\\w{4}-\\w{4}-\\w{4}-\\w{12})$");
@ -130,7 +132,7 @@ public class PeertubeAdapter extends RecyclerView.Adapter implements OnListActio
@Override
public int getItemCount() {
return 0;
return peertubes.size();
}

View File

@ -146,7 +146,7 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
statusListAdapter = new StatusListAdapter(context, type, targetedId, isOnWifi, behaviorWithAttachments, positionSpinnerTrans, this.statuses);
lv_status.setAdapter(statusListAdapter);
}else {
peertubeAdapater = new PeertubeAdapter(context, this.peertubes);
peertubeAdapater = new PeertubeAdapter(context, remoteInstance, this.peertubes);
lv_status.setAdapter(peertubeAdapater);
}
mLayoutManager = new LinearLayoutManager(context);
@ -309,6 +309,17 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn
flag_loading = false;
return;
}
if( apiResponse.getHowToVideos() != null){
int previousPosition = this.peertubes.size();
if( max_id == null)
max_id = "0";
max_id = String.valueOf(Integer.valueOf(max_id) + 50);
this.peertubes.addAll(apiResponse.getPeertubes());
statusListAdapter.notifyItemRangeInserted(previousPosition, apiResponse.getHowToVideos().size());
swipeRefreshLayout.setRefreshing(false);
firstLoad = false;
return;
}
int previousPosition = this.statuses.size();
List<Status> statuses = Helper.filterToots(context, apiResponse.getStatuses(), mutedAccount, type);

View File

@ -157,7 +157,9 @@ import fr.gouv.etalab.mastodon.client.Entities.Application;
import fr.gouv.etalab.mastodon.client.Entities.Attachment;
import fr.gouv.etalab.mastodon.client.Entities.Emojis;
import fr.gouv.etalab.mastodon.client.Entities.Filters;
import fr.gouv.etalab.mastodon.client.Entities.Instance;
import fr.gouv.etalab.mastodon.client.Entities.Mention;
import fr.gouv.etalab.mastodon.client.Entities.RemoteInstance;
import fr.gouv.etalab.mastodon.client.Entities.Results;
import fr.gouv.etalab.mastodon.client.Entities.Status;
import fr.gouv.etalab.mastodon.client.Entities.Tag;
@ -2265,17 +2267,17 @@ public class Helper {
public static void refreshInstanceTab(Context context, TabLayout tableLayout, InstanceFederatedActivity.PagerAdapter pagerAdapter){
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
new InstancesDAO(context, db).cleanDoublon();
List<String> instances = new InstancesDAO(context, db).getAllInstances();
List<RemoteInstance> remoteInstances = new InstancesDAO(context, db).getAllInstances();
int allTabCount = tableLayout.getTabCount();
while(allTabCount > 0){
removeTab(tableLayout, pagerAdapter, allTabCount-1);
allTabCount -=1;
}
if( instances != null) {
for (String instance : instances) {
addTab(tableLayout, pagerAdapter, instance);
if( remoteInstances != null) {
for (RemoteInstance remoteInstance : remoteInstances) {
addTab(tableLayout, pagerAdapter, remoteInstance.getHost());
}
if( instances.size() > 0 ){
if( remoteInstances.size() > 0 ){
tableLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tableLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
}

View File

@ -134,7 +134,7 @@ public class InstancesDAO {
while (c.moveToNext() ) {
RemoteInstance remoteInstance = new RemoteInstance();
remoteInstance.setHost(c.getString(c.getColumnIndex(Sqlite.COL_INSTANCE)));
remoteInstance.setType(c.getString(c.getColumnIndex(Sqlite.COL_INSTANCE)) == null?"MASTODON":c.getString(c.getColumnIndex(Sqlite.COL_INSTANCE)));
remoteInstance.setType(c.getString(c.getColumnIndex(Sqlite.COL_INSTANCE_TYPE)) == null?"MASTODON":c.getString(c.getColumnIndex(Sqlite.COL_INSTANCE_TYPE)));
}
//Close the cursor
c.close();