properly restore LMC edits. switch to LMC v1.1
This commit is contained in:
parent
c1b0151474
commit
20b0c52e35
5 changed files with 115 additions and 91 deletions
|
@ -135,7 +135,7 @@ public final class Config {
|
|||
public static final boolean IGNORE_ID_REWRITE_IN_MUC = true;
|
||||
public static final boolean MUC_LEAVE_BEFORE_JOIN = true;
|
||||
|
||||
public static final boolean USE_LMC_VERSION_1_1 = false;
|
||||
public static final boolean USE_LMC_VERSION_1_1 = true;
|
||||
|
||||
public static final long MAM_MAX_CATCHUP = MILLISECONDS_IN_DAY * 5;
|
||||
public static final int MAM_MAX_MESSAGES = 750;
|
||||
|
|
97
src/main/java/de/pixart/messenger/entities/Edit.java
Normal file
97
src/main/java/de/pixart/messenger/entities/Edit.java
Normal file
|
@ -0,0 +1,97 @@
|
|||
package de.pixart.messenger.entities;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Edit {
|
||||
|
||||
private final String editedId;
|
||||
private final String serverMsgId;
|
||||
|
||||
Edit(String editedId, String serverMsgId) {
|
||||
this.editedId = editedId;
|
||||
this.serverMsgId = serverMsgId;
|
||||
}
|
||||
|
||||
static String toJson(List<Edit> edits) throws JSONException {
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
for (Edit edit : edits) {
|
||||
jsonArray.put(edit.toJson());
|
||||
}
|
||||
return jsonArray.toString();
|
||||
}
|
||||
|
||||
static boolean wasPreviouslyEditedRemoteMsgId(List<Edit> edits, String remoteMsgId) {
|
||||
for (Edit edit : edits) {
|
||||
if (edit.editedId != null && edit.editedId.equals(remoteMsgId)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static boolean wasPreviouslyEditedServerMsgId(List<Edit> edits, String serverMsgId) {
|
||||
for (Edit edit : edits) {
|
||||
if (edit.serverMsgId != null && edit.serverMsgId.equals(serverMsgId)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static Edit fromJson(JSONObject jsonObject) throws JSONException {
|
||||
String edited = jsonObject.has("edited_id") ? jsonObject.getString("edited_id") : null;
|
||||
String serverMsgId = jsonObject.has("server_msg_id") ? jsonObject.getString("server_msg_id") : null;
|
||||
return new Edit(edited, serverMsgId);
|
||||
}
|
||||
|
||||
static List<Edit> fromJson(String input) {
|
||||
final ArrayList<Edit> list = new ArrayList<>();
|
||||
if (input == null) {
|
||||
return list;
|
||||
}
|
||||
try {
|
||||
final JSONArray jsonArray = new JSONArray(input);
|
||||
for (int i = 0; i < jsonArray.length(); ++i) {
|
||||
list.add(fromJson(jsonArray.getJSONObject(i)));
|
||||
}
|
||||
return list;
|
||||
} catch (JSONException e) {
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
private JSONObject toJson() throws JSONException {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("edited_id", editedId);
|
||||
jsonObject.put("server_msg_id", serverMsgId);
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
String getEditedId() {
|
||||
return editedId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Edit edit = (Edit) o;
|
||||
|
||||
if (editedId != null ? !editedId.equals(edit.editedId) : edit.editedId != null)
|
||||
return false;
|
||||
return serverMsgId != null ? serverMsgId.equals(edit.serverMsgId) : edit.serverMsgId == null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = editedId != null ? editedId.hashCode() : 0;
|
||||
result = 31 * result + (serverMsgId != null ? serverMsgId.hashCode() : 0);
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -1,80 +0,0 @@
|
|||
package de.pixart.messenger.entities;
|
||||
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Edited {
|
||||
|
||||
private final String editedId;
|
||||
private final String serverMsgId;
|
||||
|
||||
public Edited(String editedId, String serverMsgId) {
|
||||
this.editedId = editedId;
|
||||
this.serverMsgId = serverMsgId;
|
||||
}
|
||||
|
||||
public static String toJson(List<Edited> edits) throws JSONException {
|
||||
JSONArray jsonArray = new JSONArray();
|
||||
for (Edited edited : edits) {
|
||||
jsonArray.put(edited.toJson());
|
||||
}
|
||||
return jsonArray.toString();
|
||||
}
|
||||
|
||||
public static boolean wasPreviouslyEditedRemoteMsgId(List<Edited> editeds, String remoteMsgId) {
|
||||
for (Edited edited : editeds) {
|
||||
if (edited.editedId != null && edited.editedId.equals(remoteMsgId)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static boolean wasPreviouslyEditedServerMsgId(List<Edited> editeds, String serverMsgId) {
|
||||
for (Edited edited : editeds) {
|
||||
if (edited.serverMsgId != null && edited.serverMsgId.equals(serverMsgId)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static Edited fromJson(JSONObject jsonObject) throws JSONException {
|
||||
String edited = jsonObject.getString("edited_id");
|
||||
String serverMsgId = jsonObject.getString("server_msg_id");
|
||||
return new Edited(edited, serverMsgId);
|
||||
}
|
||||
|
||||
public static List<Edited> fromJson(String input) {
|
||||
ArrayList<Edited> list = new ArrayList<>();
|
||||
if (input == null) {
|
||||
return list;
|
||||
}
|
||||
try {
|
||||
JSONArray jsonArray = new JSONArray(input);
|
||||
for (int i = 0; i < jsonArray.length(); ++i) {
|
||||
list.add(fromJson(jsonArray.getJSONObject(i)));
|
||||
}
|
||||
|
||||
} catch (JSONException e) {
|
||||
list = new ArrayList<>();
|
||||
list.add(new Edited(input, null));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public JSONObject toJson() throws JSONException {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("edited_id", editedId);
|
||||
jsonObject.put("server_msg_id", serverMsgId);
|
||||
return jsonObject;
|
||||
}
|
||||
|
||||
public String getEditedId() {
|
||||
return editedId;
|
||||
}
|
||||
}
|
|
@ -99,7 +99,7 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable
|
|||
protected boolean file_deleted = false;
|
||||
protected boolean carbon = false;
|
||||
protected boolean oob = false;
|
||||
protected List<Edited> edits = new ArrayList<>();
|
||||
protected List<Edit> edits = new ArrayList<>();
|
||||
protected String relativeFilePath;
|
||||
protected boolean read = true;
|
||||
protected boolean deleted = false;
|
||||
|
@ -181,7 +181,7 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable
|
|||
this.axolotlFingerprint = fingerprint;
|
||||
this.read = read;
|
||||
this.deleted = deleted;
|
||||
this.edits = Edited.fromJson(edited);
|
||||
this.edits = Edit.fromJson(edited);
|
||||
this.oob = oob;
|
||||
this.errorMessage = errorMessage;
|
||||
this.readByMarkers = readByMarkers == null ? new HashSet<>() : readByMarkers;
|
||||
|
@ -272,7 +272,7 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable
|
|||
values.put(READ, read ? 1 : 0);
|
||||
values.put(DELETED, deleted ? 1 : 0);
|
||||
try {
|
||||
values.put(EDITED, Edited.toJson(edits));
|
||||
values.put(EDITED, Edit.toJson(edits));
|
||||
} catch (JSONException e) {
|
||||
Log.e(Config.LOGTAG, "error persisting json for edits", e);
|
||||
}
|
||||
|
@ -453,11 +453,14 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable
|
|||
}
|
||||
|
||||
public void putEdited(String edited, String serverMsgId) {
|
||||
this.edits.add(new Edited(edited, serverMsgId));
|
||||
final Edit edit = new Edit(edited, serverMsgId);
|
||||
if (this.edits.size() < 128 && !this.edits.contains(edit)) {
|
||||
this.edits.add(edit);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean remoteMsgIdMatchInEdit(String id) {
|
||||
for (Edited edit : this.edits) {
|
||||
boolean remoteMsgIdMatchInEdit(String id) {
|
||||
for(Edit edit : this.edits) {
|
||||
if (id.equals(edit.getEditedId())) {
|
||||
return true;
|
||||
}
|
||||
|
@ -528,8 +531,8 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable
|
|||
|
||||
boolean similar(Message message) {
|
||||
if (!isPrivateMessage() && this.serverMsgId != null && message.getServerMsgId() != null) {
|
||||
return this.serverMsgId.equals(message.getServerMsgId()) || Edited.wasPreviouslyEditedServerMsgId(edits, message.getServerMsgId());
|
||||
} else if (Edited.wasPreviouslyEditedServerMsgId(edits, message.getServerMsgId())) {
|
||||
return this.serverMsgId.equals(message.getServerMsgId()) || Edit.wasPreviouslyEditedServerMsgId(edits, message.getServerMsgId());
|
||||
} else if (Edit.wasPreviouslyEditedServerMsgId(edits, message.getServerMsgId())) {
|
||||
return true;
|
||||
} else if (this.body == null || this.counterpart == null) {
|
||||
return false;
|
||||
|
@ -545,7 +548,7 @@ public class Message extends AbstractEntity implements AvatarService.Avatarable
|
|||
final boolean matchingCounterpart = this.counterpart.equals(message.getCounterpart());
|
||||
if (message.getRemoteMsgId() != null) {
|
||||
final boolean hasUuid = CryptoHelper.UUID_PATTERN.matcher(message.getRemoteMsgId()).matches();
|
||||
if (hasUuid && matchingCounterpart && Edited.wasPreviouslyEditedRemoteMsgId(edits, message.getRemoteMsgId())) {
|
||||
if (hasUuid && matchingCounterpart && Edit.wasPreviouslyEditedRemoteMsgId(edits, message.getRemoteMsgId())) {
|
||||
return true;
|
||||
}
|
||||
return (message.getRemoteMsgId().equals(this.remoteMsgId) || message.getRemoteMsgId().equals(this.uuid))
|
||||
|
|
|
@ -61,7 +61,7 @@ import rocks.xmpp.addr.Jid;
|
|||
public class DatabaseBackend extends SQLiteOpenHelper {
|
||||
|
||||
public static final String DATABASE_NAME = "history";
|
||||
public static final int DATABASE_VERSION = 49; // = Conversations DATABASE_VERSION + 4
|
||||
public static final int DATABASE_VERSION = 50; // = Conversations DATABASE_VERSION + 4
|
||||
private static DatabaseBackend instance = null;
|
||||
|
||||
private static String CREATE_CONTATCS_STATEMENT = "create table "
|
||||
|
@ -572,6 +572,10 @@ public class DatabaseBackend extends SQLiteOpenHelper {
|
|||
if (oldVersion < 49 && newVersion >= 49) {
|
||||
db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN " + Message.BODY_LANGUAGE);
|
||||
}
|
||||
|
||||
if (oldVersion < 50 && newVersion >= 50) {
|
||||
db.execSQL("update " + Message.TABLENAME + " set " + Message.EDITED + "=NULL");
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isColumnExisting(SQLiteDatabase db, String TableName, String ColumnName) {
|
||||
|
|
Reference in a new issue