Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 20b4163e authored by Stefan Niedermann's avatar Stefan Niedermann
Browse files

#831 Fix pruneBefore

parent f4f64fb5
Loading
Loading
Loading
Loading
+11 −8
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
@@ -90,13 +91,14 @@ public abstract class NotesClient {

    /**
     * Gets the list of notes from the server.
     *
     * @param ssoAccount   Account to be used
     * @param lastModified Last modified time of a former response (Unix timestamp in seconds!). All notes older than this time will be skipped.
     * @param lastETag     ETag of a former response. If nothing changed, the response will be 304 NOT MODIFIED.
     * @return list of notes
     * @throws Exception
     */
    abstract NotesResponse getNotes(SingleSignOnAccount ssoAccount, long lastModified, String lastETag) throws Exception;
    abstract NotesResponse getNotes(SingleSignOnAccount ssoAccount, Calendar lastModified, String lastETag) throws Exception;

    abstract NoteResponse createNote(SingleSignOnAccount ssoAccount, Note note) throws Exception;

@@ -111,9 +113,9 @@ public abstract class NotesClient {
        private final String content;
        private final String etag;
        private final String supportedApiVersions;
        private final long lastModified;
        private final Calendar lastModified;

        ResponseData(@NonNull String content, String etag, long lastModified, @Nullable String supportedApiVersions) {
        ResponseData(@NonNull String content, String etag, @NonNull Calendar lastModified, @Nullable String supportedApiVersions) {
            this.content = content;
            this.etag = etag;
            this.lastModified = lastModified;
@@ -128,7 +130,7 @@ public abstract class NotesClient {
            return etag;
        }

        public long getLastModified() {
        public Calendar getLastModified() {
            return lastModified;
        }

@@ -188,10 +190,11 @@ public abstract class NotesClient {
                etag = Objects.requireNonNull(eTagHeader.getValue()).replace("\"", "");
            }

            long lastModified = 0;
            final Calendar lastModified = Calendar.getInstance();
            lastModified.setTimeInMillis(0);
            final AidlNetworkRequest.PlainHeader lastModifiedHeader = response.getPlainHeader(HEADER_KEY_LAST_MODIFIED);
            if (lastModifiedHeader != null)
                lastModified = new Date(lastModifiedHeader.getValue()).getTime() / 1_000;
                lastModified.setTimeInMillis(Date.parse(lastModifiedHeader.getValue()));
            Log.d(TAG, "ETag: " + etag + "; Last-Modified: " + lastModified + " (" + lastModified + ")");

            String supportedApiVersions = null;
+4 −3
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ import com.nextcloud.android.sso.model.SingleSignOnAccount;

import org.json.JSONObject;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

@@ -25,9 +26,9 @@ public class NotesClientV02 extends NotesClient {
        super(appContext);
    }

    NotesResponse getNotes(SingleSignOnAccount ssoAccount, long lastModified, String lastETag) throws Exception {
        Map<String, String> parameter = new HashMap<>();
        parameter.put(GET_PARAM_KEY_PRUNE_BEFORE, Long.toString(lastModified));
    NotesResponse getNotes(SingleSignOnAccount ssoAccount, Calendar lastModified, String lastETag) throws Exception {
        final Map<String, String> parameter = new HashMap<>();
        parameter.put(GET_PARAM_KEY_PRUNE_BEFORE, Long.toString(lastModified == null ? 0 : lastModified.getTimeInMillis() / 1_000));
        return new NotesResponse(requestServer(ssoAccount, "notes", METHOD_GET, parameter, null, lastETag));
    }

+4 −3
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ import com.nextcloud.android.sso.model.SingleSignOnAccount;

import org.json.JSONObject;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

@@ -25,9 +26,9 @@ public class NotesClientV1 extends NotesClient {
        super(appContext);
    }

    NotesResponse getNotes(SingleSignOnAccount ssoAccount, long lastModified, String lastETag) throws Exception {
        Map<String, String> parameter = new HashMap<>();
        parameter.put(GET_PARAM_KEY_PRUNE_BEFORE, Long.toString(lastModified));
    NotesResponse getNotes(SingleSignOnAccount ssoAccount, Calendar lastModified, String lastETag) throws Exception {
        final Map<String, String> parameter = new HashMap<>();
        parameter.put(GET_PARAM_KEY_PRUNE_BEFORE, Long.toString(lastModified == null ? 0 : lastModified.getTimeInMillis() / 1_000));
        return new NotesResponse(requestServer(ssoAccount, "notes", METHOD_GET, parameter, null, lastETag));
    }

+4 −3
Original line number Diff line number Diff line
@@ -178,15 +178,16 @@ public abstract class NotesDatabase extends RoomDatabase {

    /**
     * Inserts a note directly into the Database.
     * Excerpt will be generated and {@link DBStatus#LOCAL_EDITED} will be applied.
     * No Synchronisation will be triggered! Use addNoteAndSync()!
     * Excerpt will be generated, {@link DBStatus#LOCAL_EDITED} will be applied in case the note has
     * already has a local ID, otherwise {@link DBStatus#VOID} will be applied.
     * No Synchronisation will be triggered! Use {@link #addNoteAndSync(Account, Note)}!
     *
     * @param note {@link Note} to be added.
     */
    @NonNull
    @WorkerThread
    public Note addNote(long accountId, @NonNull Note note) {
        note.setStatus(DBStatus.LOCAL_EDITED);
        note.setStatus(note.getId() > 0 ? DBStatus.LOCAL_EDITED : DBStatus.VOID);
        note.setAccountId(accountId);
        note.setExcerpt(generateNoteExcerpt(note.getContent(), note.getTitle()));
        return getNoteDao().getNoteById(getNoteDao().addNote(note));
+4 −9
Original line number Diff line number Diff line
@@ -9,7 +9,6 @@ import com.nextcloud.android.sso.exceptions.TokenMismatchException;
import com.nextcloud.android.sso.model.SingleSignOnAccount;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -164,12 +163,10 @@ abstract class NotesServerSyncTask extends Thread {
        Log.d(TAG, "pullRemoteChanges() for account " + localAccount.getAccountName());
        try {
            final Map<Long, Long> idMap = db.getIdMap(localAccount.getId());
            final Calendar modified = localAccount.getModified();
            final long modifiedForServer = modified == null ? 0 : modified.getTimeInMillis() / 1_000;
            // FIXME re-reading the localAccount is only a workaround for a not-up-to-date eTag in localAccount.
            final ServerResponse.NotesResponse response = notesClient.getNotes(ssoAccount, modifiedForServer, db.getAccountDao().getAccountById(localAccount.getId()).getETag());
            List<Note> remoteNotes = response.getNotes();
            Set<Long> remoteIDs = new HashSet<>();
            final ServerResponse.NotesResponse response = notesClient.getNotes(ssoAccount, localAccount.getModified(), db.getAccountDao().getAccountById(localAccount.getId()).getETag());
            final List<Note> remoteNotes = response.getNotes();
            final Set<Long> remoteIDs = new HashSet<>();
            // pull remote changes: update or create each remote note
            for (Note remoteNote : remoteNotes) {
                Log.v(TAG, "   Process Remote Note: " + remoteNote);
@@ -201,9 +198,7 @@ abstract class NotesServerSyncTask extends Thread {

            // update ETag and Last-Modified in order to reduce size of next response
            localAccount.setETag(response.getETag());
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(response.getLastModified());
            localAccount.setModified(calendar);
            localAccount.setModified(response.getLastModified());
            db.getAccountDao().updateETag(localAccount.getId(), localAccount.getETag());
            db.getAccountDao().updateModified(localAccount.getId(), localAccount.getModified().getTimeInMillis());
            try {
Loading