save server id from mam messages. check for dups before adding mam

This commit is contained in:
iNPUTmice 2014-12-09 21:41:49 +01:00
parent 0ab530932a
commit ccdb0fd971
3 changed files with 40 additions and 5 deletions

View File

@ -45,6 +45,7 @@ public class Message extends AbstractEntity {
public static String STATUS = "status"; public static String STATUS = "status";
public static String TYPE = "type"; public static String TYPE = "type";
public static String REMOTE_MSG_ID = "remoteMsgId"; public static String REMOTE_MSG_ID = "remoteMsgId";
public static String SERVER_MSG_ID = "serverMsgId";
public static String RELATIVE_FILE_PATH = "relativeFilePath"; public static String RELATIVE_FILE_PATH = "relativeFilePath";
public boolean markable = false; public boolean markable = false;
protected String conversationUuid; protected String conversationUuid;
@ -59,6 +60,7 @@ public class Message extends AbstractEntity {
protected String relativeFilePath; protected String relativeFilePath;
protected boolean read = true; protected boolean read = true;
protected String remoteMsgId = null; protected String remoteMsgId = null;
protected String serverMsgId = null;
protected Conversation conversation = null; protected Conversation conversation = null;
protected Downloadable downloadable = null; protected Downloadable downloadable = null;
private Message mNextMessage = null; private Message mNextMessage = null;
@ -83,13 +85,15 @@ public class Message extends AbstractEntity {
status, status,
TYPE_TEXT, TYPE_TEXT,
null, null,
null,
null); null);
this.conversation = conversation; this.conversation = conversation;
} }
private Message(final String uuid, final String conversationUUid, final Jid counterpart, private Message(final String uuid, final String conversationUUid, final Jid counterpart,
final Jid trueCounterpart, final String body, final long timeSent, final Jid trueCounterpart, final String body, final long timeSent,
final int encryption, final int status, final int type, final String remoteMsgId, final String relativeFilePath) { final int encryption, final int status, final int type, final String remoteMsgId,
final String relativeFilePath, final String serverMsgId) {
this.uuid = uuid; this.uuid = uuid;
this.conversationUuid = conversationUUid; this.conversationUuid = conversationUUid;
this.counterpart = counterpart; this.counterpart = counterpart;
@ -101,6 +105,7 @@ public class Message extends AbstractEntity {
this.type = type; this.type = type;
this.remoteMsgId = remoteMsgId; this.remoteMsgId = remoteMsgId;
this.relativeFilePath = relativeFilePath; this.relativeFilePath = relativeFilePath;
this.serverMsgId = serverMsgId;
} }
public static Message fromCursor(Cursor cursor) { public static Message fromCursor(Cursor cursor) {
@ -136,7 +141,8 @@ public class Message extends AbstractEntity {
cursor.getInt(cursor.getColumnIndex(STATUS)), cursor.getInt(cursor.getColumnIndex(STATUS)),
cursor.getInt(cursor.getColumnIndex(TYPE)), cursor.getInt(cursor.getColumnIndex(TYPE)),
cursor.getString(cursor.getColumnIndex(REMOTE_MSG_ID)), cursor.getString(cursor.getColumnIndex(REMOTE_MSG_ID)),
cursor.getString(cursor.getColumnIndex(RELATIVE_FILE_PATH))); cursor.getString(cursor.getColumnIndex(RELATIVE_FILE_PATH)),
cursor.getString(cursor.getColumnIndex(SERVER_MSG_ID)));
} }
public static Message createStatusMessage(Conversation conversation) { public static Message createStatusMessage(Conversation conversation) {
@ -168,6 +174,7 @@ public class Message extends AbstractEntity {
values.put(TYPE, type); values.put(TYPE, type);
values.put(REMOTE_MSG_ID, remoteMsgId); values.put(REMOTE_MSG_ID, remoteMsgId);
values.put(RELATIVE_FILE_PATH, relativeFilePath); values.put(RELATIVE_FILE_PATH, relativeFilePath);
values.put(SERVER_MSG_ID,serverMsgId);
return values; return values;
} }
@ -248,6 +255,14 @@ public class Message extends AbstractEntity {
this.remoteMsgId = id; this.remoteMsgId = id;
} }
public String getServerMsgId() {
return this.serverMsgId;
}
public void setServerMsgId(String id) {
this.serverMsgId = id;
}
public boolean isRead() { public boolean isRead() {
return this.read; return this.read;
} }
@ -293,7 +308,15 @@ public class Message extends AbstractEntity {
} }
public boolean equals(Message message) { public boolean equals(Message message) {
return (this.remoteMsgId != null) && (this.body != null) && (this.counterpart != null) && this.remoteMsgId.equals(message.getRemoteMsgId()) && this.body.equals(message.getBody()) && this.counterpart.equals(message.getCounterpart()); if (this.serverMsgId != null && message.getServerMsgId() != null) {
return this.serverMsgId.equals(message.getServerMsgId());
} else {
return this.body != null
&& this.counterpart != null
&& ((this.remoteMsgId != null && this.remoteMsgId.equals(message.getRemoteMsgId()))
|| this.uuid.equals(message.getRemoteMsgId())) && this.body.equals(message.getBody())
&& this.counterpart.equals(message.getCounterpart());
}
} }
public Message next() { public Message next() {

View File

@ -320,7 +320,14 @@ public class MessageParser extends AbstractParser implements
Message finishedMessage = new Message(conversation,content,encryption,status); Message finishedMessage = new Message(conversation,content,encryption,status);
finishedMessage.setTime(timestamp); finishedMessage.setTime(timestamp);
finishedMessage.setCounterpart(counterpart); finishedMessage.setCounterpart(counterpart);
finishedMessage.setRemoteMsgId(message.getAttribute("id"));
finishedMessage.setServerMsgId(result.getAttribute("id"));
if (conversation.hasDuplicateMessage(finishedMessage)) {
Log.d(Config.LOGTAG, "received mam message " + content+ " (duplicate)");
return null;
} else {
Log.d(Config.LOGTAG, "received mam message " + content); Log.d(Config.LOGTAG, "received mam message " + content);
}
return finishedMessage; return finishedMessage;
} }

View File

@ -22,7 +22,7 @@ public class DatabaseBackend extends SQLiteOpenHelper {
private static DatabaseBackend instance = null; private static DatabaseBackend instance = null;
private static final String DATABASE_NAME = "history"; private static final String DATABASE_NAME = "history";
private static final int DATABASE_VERSION = 11; private static final int DATABASE_VERSION = 12;
private static String CREATE_CONTATCS_STATEMENT = "create table " private static String CREATE_CONTATCS_STATEMENT = "create table "
+ Contact.TABLENAME + "(" + Contact.ACCOUNT + " TEXT, " + Contact.TABLENAME + "(" + Contact.ACCOUNT + " TEXT, "
@ -65,6 +65,7 @@ public class DatabaseBackend extends SQLiteOpenHelper {
+ Message.BODY + " TEXT, " + Message.ENCRYPTION + " NUMBER, " + Message.BODY + " TEXT, " + Message.ENCRYPTION + " NUMBER, "
+ Message.STATUS + " NUMBER," + Message.TYPE + " NUMBER, " + Message.STATUS + " NUMBER," + Message.TYPE + " NUMBER, "
+ Message.RELATIVE_FILE_PATH + " TEXT, " + Message.RELATIVE_FILE_PATH + " TEXT, "
+ Message.SERVER_MSG_ID + " TEXT, "
+ Message.REMOTE_MSG_ID + " TEXT, FOREIGN KEY(" + Message.REMOTE_MSG_ID + " TEXT, FOREIGN KEY("
+ Message.CONVERSATION + ") REFERENCES " + Message.CONVERSATION + ") REFERENCES "
+ Conversation.TABLENAME + "(" + Conversation.UUID + Conversation.TABLENAME + "(" + Conversation.UUID
@ -121,6 +122,10 @@ public class DatabaseBackend extends SQLiteOpenHelper {
db.execSQL("delete from "+Contact.TABLENAME); db.execSQL("delete from "+Contact.TABLENAME);
db.execSQL("update "+Account.TABLENAME+" set "+Account.ROSTERVERSION+" = NULL"); db.execSQL("update "+Account.TABLENAME+" set "+Account.ROSTERVERSION+" = NULL");
} }
if (oldVersion < 12 && newVersion >= 12) {
db.execSQL("ALTER TABLE " + Message.TABLENAME + " ADD COLUMN "
+ Message.SERVER_MSG_ID + " TEXT");
}
} }
public static synchronized DatabaseBackend getInstance(Context context) { public static synchronized DatabaseBackend getInstance(Context context) {