Fix backup for Pleroma

This commit is contained in:
tom79 2019-08-07 10:24:35 +02:00
parent dd7f3813d1
commit 8adb9d0fcc
3 changed files with 30 additions and 5 deletions

View File

@ -121,7 +121,7 @@ public class BackupStatusesSyncJob extends Job {
boolean autobackup = sharedpreferences.getBoolean(Helper.SET_AUTO_BACKUP_STATUSES + account.getId() + account.getInstance(), false);
if( autobackup) {
Intent backupIntent = new Intent(getContext(), BackupStatusInDataBaseService.class);
backupIntent.putExtra("userId", account.getId());
backupIntent.putExtra("userid", account.getId());
backupIntent.putExtra("instance", account.getInstance());
getContext().startService(backupIntent);
}

View File

@ -27,6 +27,7 @@ import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import app.fedilab.android.client.API;
@ -77,8 +78,9 @@ public class BackupStatusInDataBaseService extends IntentService {
boolean toastMessage = true;
String userId = null;
String instance = null;
if( intent != null){
userId = intent.getStringExtra("userId");
if( intent != null && intent.hasExtra("userid") && intent.hasExtra("instance")){
userId = intent.getStringExtra("userid");
instance = intent.getStringExtra("instance");
toastMessage = false;
}
@ -115,7 +117,7 @@ public class BackupStatusInDataBaseService extends IntentService {
API api = new API(getApplicationContext(), account.getInstance(), account.getToken());
try {
//Starts from the last recorded ID
String since_id = new StatusCacheDAO(BackupStatusInDataBaseService.this, db).getLastTootIDCache(StatusCacheDAO.ARCHIVE_CACHE);
Date sinceDate = new StatusCacheDAO(BackupStatusInDataBaseService.this, db).getLastTootDateCache(StatusCacheDAO.ARCHIVE_CACHE);
String max_id = null;
List<Status> backupStatus = new ArrayList<>();
boolean canContinue = true;
@ -124,7 +126,7 @@ public class BackupStatusInDataBaseService extends IntentService {
max_id = apiResponse.getMax_id();
List<Status> statuses = apiResponse.getStatuses();
for(Status tmpStatus : statuses) {
if(since_id != null && max_id != null && Long.parseLong(tmpStatus.getId()) <= Long.parseLong(since_id)){
if(sinceDate != null && max_id != null && tmpStatus.getCreated_at().before(sinceDate)){
canContinue = false;
break;
}

View File

@ -420,6 +420,29 @@ public class StatusCacheDAO {
}
}
/**
* Returns the last date of backup for a user depending of the type of cache
* @return Date
*/
public Date getLastTootDateCache(int cacheType){
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_STATUSES_CACHE, null, Sqlite.COL_CACHED_ACTION + " = '" + cacheType+ "' AND " + Sqlite.COL_INSTANCE + " = '" + instance+ "' AND " + Sqlite.COL_USER_ID + " = '" + userId+ "'", null, null, null, Sqlite.COL_CREATED_AT + " DESC", "1");
//No element found
if (c.getCount() == 0)
return null;
//Take the first element
c.moveToFirst();
Date last_id = Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_CREATED_AT)));
c.close();
return last_id;
} catch (Exception e) {
return null;
}
}
/**
* Returns a cached status by id in db
* @return stored status StoredStatus