always insert autocrypt key before update operations

This commit is contained in:
Vincent Breitmoser 2018-07-02 16:58:59 +02:00
parent 905dd728cc
commit 60ec78b9ae
3 changed files with 18 additions and 20 deletions

View file

@ -94,37 +94,39 @@ public class AutocryptPeerDao extends AbstractDao {
return result;
}
private void ensureAutocryptPeerExists(String packageName, String autocryptId) {
InsertPeer insertStatement = new InsertPeer(getWritableDb());
insertStatement.bind(packageName, autocryptId);
insertStatement.executeInsert();
}
public void insertOrUpdateLastSeen(String packageName, String autocryptId, Date date) {
ensureAutocryptPeerExists(packageName, autocryptId);
UpdateLastSeen updateStatement = new UpdateLastSeen(getWritableDb(), AutocryptPeer.FACTORY);
updateStatement.bind(packageName, autocryptId, date);
int updated = updateStatement.executeUpdateDelete();
if (updated == 0) {
InsertPeer insertStatement = new InsertPeer(getWritableDb(), AutocryptPeer.FACTORY);
insertStatement.bind(packageName, autocryptId, date);
insertStatement.executeInsert();
}
updateStatement.executeUpdateDelete();
}
public void updateKey(String packageName, String autocryptId, Date effectiveDate, long masterKeyId,
boolean isMutual) {
ensureAutocryptPeerExists(packageName, autocryptId);
UpdateKey updateStatement = new UpdateKey(getWritableDb(), AutocryptPeer.FACTORY);
updateStatement.bind(packageName, autocryptId, effectiveDate, masterKeyId, isMutual);
int rowsUpdated = updateStatement.executeUpdateDelete();
if (rowsUpdated == 0) {
throw new IllegalStateException("No rows updated! Was this peer inserted before the update?");
}
updateStatement.executeUpdateDelete();
getDatabaseNotifyManager().notifyAutocryptUpdate(autocryptId, masterKeyId);
}
public void updateKeyGossip(String packageName, String autocryptId, Date effectiveDate, long masterKeyId,
GossipOrigin origin) {
ensureAutocryptPeerExists(packageName, autocryptId);
UpdateGossipKey updateStatement = new UpdateGossipKey(getWritableDb(), AutocryptPeer.FACTORY);
updateStatement.bind(packageName, autocryptId, effectiveDate, masterKeyId, origin);
int rowsUpdated = updateStatement.executeUpdateDelete();
if (rowsUpdated == 0) {
throw new IllegalStateException("No rows updated! Was this peer inserted before the update?");
}
updateStatement.executeUpdateDelete();
getDatabaseNotifyManager().notifyAutocryptUpdate(autocryptId, masterKeyId);
}

View file

@ -40,10 +40,6 @@ public abstract class AutocryptPeer implements AutocryptPeersModel {
return gossip_key_is_verified_int() != 0;
}
public boolean hasKey() {
return autocryptPeer().master_key_id() != null;
}
public boolean isKeyRevoked() {
Boolean revoked = key_is_revoked_int();
return revoked != null && revoked;

View file

@ -48,7 +48,7 @@ updateGossipKey:
UPDATE autocrypt_peers SET gossip_last_seen_key = ?3, gossip_master_key_id = ?4, gossip_origin = ?5 WHERE package_name = ?1 AND identifier = ?2;
insertPeer:
INSERT INTO autocrypt_peers (package_name, identifier, last_seen) VALUES (?, ?, ?);
INSERT OR IGNORE INTO autocrypt_peers (package_name, identifier) VALUES (?, ?);
selectAutocryptKeyStatus:
SELECT autocryptPeer.*,