#915 comment - filter with id for avoiding visibility issues with accounts
This commit is contained in:
parent
4381d1d7f6
commit
ed2acd165d
|
@ -953,6 +953,56 @@ public class API {
|
|||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves home timeline from cache the account *synchronously*
|
||||
* @param max_id String id max
|
||||
* @return APIResponse
|
||||
*/
|
||||
public APIResponse getHomeTimelineCache(String max_id) {
|
||||
|
||||
HashMap<String, String> params = new HashMap<>();
|
||||
if (max_id != null)
|
||||
params.put("max_id", max_id);
|
||||
params.put("limit","40");
|
||||
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
||||
statuses = new TimelineCacheDAO(context, db).get(max_id);
|
||||
if( statuses == null){
|
||||
statuses = new ArrayList<>();
|
||||
try {
|
||||
HttpsConnection httpsConnection = new HttpsConnection(context);
|
||||
String response = httpsConnection.get(getAbsoluteUrl("/timelines/home"), 60, params, prefKeyOauthTokenT);
|
||||
apiResponse.setSince_id(httpsConnection.getSince_id());
|
||||
apiResponse.setMax_id(httpsConnection.getMax_id());
|
||||
statuses = parseStatuses(context, new JSONArray(response), true);
|
||||
} 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();
|
||||
}
|
||||
if( apiResponse == null)
|
||||
apiResponse = new APIResponse();
|
||||
apiResponse.setStatuses(statuses);
|
||||
return apiResponse;
|
||||
}else{
|
||||
if( statuses.size() > 0) {
|
||||
apiResponse.setSince_id(String.valueOf(Long.parseLong(statuses.get(0).getId())+1));
|
||||
apiResponse.setMax_id(String.valueOf(Long.parseLong(statuses.get(statuses.size() - 1).getId())-1));
|
||||
}
|
||||
apiResponse.setStatuses(statuses);
|
||||
return apiResponse;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Retrieves home timeline for the account *synchronously*
|
||||
* @param max_id String id max
|
||||
|
@ -978,7 +1028,7 @@ public class API {
|
|||
String response = httpsConnection.get(getAbsoluteUrl("/timelines/home"), 60, params, prefKeyOauthTokenT);
|
||||
apiResponse.setSince_id(httpsConnection.getSince_id());
|
||||
apiResponse.setMax_id(httpsConnection.getMax_id());
|
||||
statuses = parseStatuses(context, new JSONArray(response), instance, true);
|
||||
statuses = parseStatuses(context, new JSONArray(response), true);
|
||||
} catch (HttpsConnection.HttpsConnectionException e) {
|
||||
setError(e.getStatusCode(), e);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
|
@ -3728,7 +3778,7 @@ public class API {
|
|||
* @param jsonArray JSONArray
|
||||
* @return List<Status>
|
||||
*/
|
||||
private static List<Status> parseStatuses(Context context, JSONArray jsonArray, String instance, boolean cached){
|
||||
private static List<Status> parseStatuses(Context context, JSONArray jsonArray, boolean cached){
|
||||
|
||||
List<Status> statuses = new ArrayList<>();
|
||||
try {
|
||||
|
@ -3739,9 +3789,9 @@ public class API {
|
|||
Status status = parseStatuses(context, resobj);
|
||||
if( cached) {
|
||||
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
||||
Status alreadyCached = new TimelineCacheDAO(context, db).getSingle(instance, status.getId());
|
||||
Status alreadyCached = new TimelineCacheDAO(context, db).getSingle(status.getId());
|
||||
if (alreadyCached == null) {
|
||||
new TimelineCacheDAO(context, db).insert(status.getId(), instance, resobj.toString());
|
||||
new TimelineCacheDAO(context, db).insert(status.getId(), resobj.toString());
|
||||
}
|
||||
}
|
||||
i++;
|
||||
|
|
|
@ -287,6 +287,7 @@ public class Sqlite extends SQLiteOpenHelper {
|
|||
+ COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
|
||||
+ COL_STATUS_ID + " TEXT NOT NULL, "
|
||||
+ COL_INSTANCE + " TEXT NOT NULL, "
|
||||
+ COL_USER_ID + " TEXT NOT NULL, "
|
||||
+ COL_CACHE + " TEXT NOT NULL, "
|
||||
+ COL_DATE + " TEXT NOT NULL)";
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@ package fr.gouv.etalab.mastodon.sqlite;
|
|||
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
|
||||
|
@ -52,10 +53,14 @@ public class TimelineCacheDAO {
|
|||
* Insert a status in database
|
||||
* @return boolean
|
||||
*/
|
||||
public long insert(String statusId, String instance, String jsonString) {
|
||||
public long insert(String statusId, String jsonString) {
|
||||
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
||||
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
|
||||
String instance = Helper.getLiveInstance(context);
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(Sqlite.COL_INSTANCE, instance);
|
||||
values.put(Sqlite.COL_STATUS_ID, statusId);
|
||||
values.put(Sqlite.COL_USER_ID, userId);
|
||||
values.put(Sqlite.COL_DATE, Helper.dateToString(new Date()));
|
||||
values.put(Sqlite.COL_CACHE, jsonString);
|
||||
//Inserts cached status
|
||||
|
@ -73,8 +78,11 @@ public class TimelineCacheDAO {
|
|||
* Remove stored status
|
||||
* @return int
|
||||
*/
|
||||
public int remove(String statusId, String instance) {
|
||||
return db.delete(Sqlite.TABLE_TIMELINE_CACHE, Sqlite.COL_STATUS_ID + " = \""+ statusId +"\" AND " + Sqlite.COL_INSTANCE + " = \"" + instance + "\"", null);
|
||||
public int remove(String statusId) {
|
||||
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
||||
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
|
||||
String instance = Helper.getLiveInstance(context);
|
||||
return db.delete(Sqlite.TABLE_TIMELINE_CACHE, Sqlite.COL_STATUS_ID + " = \""+ statusId +"\" AND " + Sqlite.COL_INSTANCE + " = \"" + instance + "\" AND " + Sqlite.COL_USER_ID + " = \"" + userId + "\"", null);
|
||||
}
|
||||
|
||||
/***
|
||||
|
@ -93,13 +101,16 @@ public class TimelineCacheDAO {
|
|||
* Returns all cached Statuses
|
||||
* @return stored Status List<Status>
|
||||
*/
|
||||
public List<Status> get(String instance, String max_id){
|
||||
public List<Status> get(String max_id){
|
||||
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
||||
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
|
||||
String instance = Helper.getLiveInstance(context);
|
||||
try {
|
||||
Cursor c;
|
||||
if( max_id != null)
|
||||
c = db.query(Sqlite.TABLE_TIMELINE_CACHE, null, Sqlite.COL_INSTANCE + " = \"" + instance + "\" AND " + Sqlite.COL_STATUS_ID + " <= " + max_id, null, null, null, Sqlite.COL_STATUS_ID+ " DESC", "40");
|
||||
c = db.query(Sqlite.TABLE_TIMELINE_CACHE, null, Sqlite.COL_INSTANCE + " = \"" + instance + "\" AND " + Sqlite.COL_USER_ID + " = \"" + userId + "\" AND "+ Sqlite.COL_STATUS_ID + " <= " + max_id, null, null, null, Sqlite.COL_STATUS_ID+ " DESC", "40");
|
||||
else
|
||||
c = db.query(Sqlite.TABLE_TIMELINE_CACHE, null, Sqlite.COL_INSTANCE + " = \"" + instance + "\"", null, null, null, Sqlite.COL_STATUS_ID+ " DESC", "40");
|
||||
c = db.query(Sqlite.TABLE_TIMELINE_CACHE, null, Sqlite.COL_INSTANCE + " = \"" + instance + "\" AND " + Sqlite.COL_USER_ID + " = \"" + userId + "\"", null, null, null, Sqlite.COL_STATUS_ID+ " DESC", "40");
|
||||
return cursorToListStatus(c);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
|
@ -110,9 +121,12 @@ public class TimelineCacheDAO {
|
|||
* Returns one cached Statuses
|
||||
* @return stored Status List<Status>
|
||||
*/
|
||||
public Status getSingle(String instance, String statusId){
|
||||
public Status getSingle(String statusId){
|
||||
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
||||
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
|
||||
String instance = Helper.getLiveInstance(context);
|
||||
try {
|
||||
Cursor c = db.query(Sqlite.TABLE_TIMELINE_CACHE, null, Sqlite.COL_INSTANCE + " = \"" + instance + "\" AND " + Sqlite.COL_STATUS_ID + " = " + statusId, null, null, null, null, "1");
|
||||
Cursor c = db.query(Sqlite.TABLE_TIMELINE_CACHE, null, Sqlite.COL_INSTANCE + " = \"" + instance + "\" AND " + Sqlite.COL_USER_ID + " = \"" + userId + "\"", null, null, null, Sqlite.COL_STATUS_ID+ " DESC", "1");
|
||||
return cursorToSingleStatus(c);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
|
|
Loading…
Reference in New Issue