get rid of lastMessageTransmitted in favor of db query

This commit is contained in:
Daniel Gultsch 2015-12-11 13:52:04 +01:00
parent 61b0681109
commit 293e820a58
5 changed files with 29 additions and 55 deletions

View file

@ -46,7 +46,6 @@ public class Conversation extends AbstractEntity implements Blockable {
public static final String ATTRIBUTE_NEXT_ENCRYPTION = "next_encryption"; public static final String ATTRIBUTE_NEXT_ENCRYPTION = "next_encryption";
public static final String ATTRIBUTE_MUC_PASSWORD = "muc_password"; public static final String ATTRIBUTE_MUC_PASSWORD = "muc_password";
public static final String ATTRIBUTE_MUTED_TILL = "muted_till"; public static final String ATTRIBUTE_MUTED_TILL = "muted_till";
public static final String ATTRIBUTE_LAST_MESSAGE_TRANSMITTED = "last_message_transmitted";
private String name; private String name;
private String contactUuid; private String contactUuid;
@ -693,33 +692,16 @@ public class Conversation extends AbstractEntity implements Blockable {
} }
} }
public void resetLastMessageTransmitted() {
this.setAttribute(ATTRIBUTE_LAST_MESSAGE_TRANSMITTED,String.valueOf(-1));
}
public boolean setLastMessageTransmitted(long value) {
long before = getLastMessageTransmitted();
if (value - before > 1000) {
this.setAttribute(ATTRIBUTE_LAST_MESSAGE_TRANSMITTED, String.valueOf(value));
return true;
} else {
return false;
}
}
public long getLastMessageTransmitted() { public long getLastMessageTransmitted() {
long timestamp = getLongAttribute(ATTRIBUTE_LAST_MESSAGE_TRANSMITTED,0); synchronized (this.messages) {
if (timestamp == 0) { for(int i = this.messages.size() - 1; i >= 0; --i) {
synchronized (this.messages) { Message message = this.messages.get(i);
for(int i = this.messages.size() - 1; i >= 0; --i) { if (message.getStatus() == Message.STATUS_RECEIVED || message.isCarbon()) {
Message message = this.messages.get(i); return message.getTimeSent();
if (message.getStatus() == Message.STATUS_RECEIVED) {
return message.getTimeSent();
}
} }
} }
} }
return timestamp; return 0;
} }
public void setMutedTill(long value) { public void setMutedTill(long value) {

View file

@ -329,7 +329,7 @@ public class MessageParser extends AbstractParser implements
} }
if ((body != null || pgpEncrypted != null || axolotlEncrypted != null) && !isMucStatusMessage) { if ((body != null || pgpEncrypted != null || axolotlEncrypted != null) && !isMucStatusMessage) {
Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, counterpart.toBareJid(), isTypeGroupChat); Conversation conversation = mXmppConnectionService.findOrCreateConversation(account, counterpart.toBareJid(), isTypeGroupChat, query);
if (isTypeGroupChat) { if (isTypeGroupChat) {
if (counterpart.getResourcepart().equals(conversation.getMucOptions().getActualNick())) { if (counterpart.getResourcepart().equals(conversation.getMucOptions().getActualNick())) {
status = Message.STATUS_SEND_RECEIVED; status = Message.STATUS_SEND_RECEIVED;
@ -431,11 +431,6 @@ public class MessageParser extends AbstractParser implements
mXmppConnectionService.sendMessagePacket(account, receipt); mXmppConnectionService.sendMessagePacket(account, receipt);
} }
} }
if (account.isOnlineAndConnected() && query == null) {
if (conversation.setLastMessageTransmitted(System.currentTimeMillis())) {
mXmppConnectionService.updateConversation(conversation);
}
}
if (message.getStatus() == Message.STATUS_RECEIVED if (message.getStatus() == Message.STATUS_RECEIVED
&& conversation.getOtrSession() != null && conversation.getOtrSession() != null

View file

@ -9,6 +9,7 @@ import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
import android.util.Base64; import android.util.Base64;
import android.util.Log; import android.util.Log;
import android.util.Pair;
import org.whispersystems.libaxolotl.AxolotlAddress; import org.whispersystems.libaxolotl.AxolotlAddress;
import org.whispersystems.libaxolotl.IdentityKey; import org.whispersystems.libaxolotl.IdentityKey;
@ -591,6 +592,19 @@ public class DatabaseBackend extends SQLiteOpenHelper {
db.delete(Message.TABLENAME, Message.CONVERSATION + "=?", args); db.delete(Message.TABLENAME, Message.CONVERSATION + "=?", args);
} }
public Pair<Long,String> getLastMessageReceived(Account account) {
SQLiteDatabase db = this.getReadableDatabase();
String sql = "select messages.timeSent,messages.serverMsgId from accounts join conversations on accounts.uuid=conversations.accountUuid join messages on conversations.uuid=messages.conversationUuid where accounts.uuid=? and (messages.status=0 or messages.carbon=1 or messages.serverMsgId not null) order by messages.timesent desc limit 1";
String[] args = {account.getUuid()};
Cursor cursor = db.rawQuery(sql,args);
if (cursor.getCount() ==0) {
return null;
} else {
cursor.moveToFirst();
return new Pair<>(cursor.getLong(0),cursor.getString(1));
}
}
public Conversation findConversationByUuid(String conversationUuid) { public Conversation findConversationByUuid(String conversationUuid) {
SQLiteDatabase db = this.getReadableDatabase(); SQLiteDatabase db = this.getReadableDatabase();
String[] selectionArgs = {conversationUuid}; String[] selectionArgs = {conversationUuid};

View file

@ -1,6 +1,7 @@
package eu.siacs.conversations.services; package eu.siacs.conversations.services;
import android.util.Log; import android.util.Log;
import android.util.Pair;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.ArrayList; import java.util.ArrayList;
@ -75,16 +76,8 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
} }
private long getLastMessageTransmitted(final Account account) { private long getLastMessageTransmitted(final Account account) {
long timestamp = 0; Pair<Long,String> pair = mXmppConnectionService.databaseBackend.getLastMessageReceived(account);
for(final Conversation conversation : mXmppConnectionService.getConversations()) { return pair == null ? 0 : pair.first;
if (conversation.getAccount() == account) {
long tmp = conversation.getLastMessageTransmitted();
if (tmp > timestamp) {
timestamp = tmp;
}
}
}
return timestamp;
} }
public Query query(final Conversation conversation) { public Query query(final Conversation conversation) {
@ -166,25 +159,19 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
final Conversation conversation = query.getConversation(); final Conversation conversation = query.getConversation();
if (conversation != null) { if (conversation != null) {
conversation.sort(); conversation.sort();
if (conversation.setLastMessageTransmitted(query.getEnd())) {
this.mXmppConnectionService.databaseBackend.updateConversation(conversation);
}
conversation.setHasMessagesLeftOnServer(query.getMessageCount() > 0); conversation.setHasMessagesLeftOnServer(query.getMessageCount() > 0);
if (query.hasCallback()) {
query.callback();
} else {
this.mXmppConnectionService.updateConversationUi();
}
} else { } else {
for(Conversation tmp : this.mXmppConnectionService.getConversations()) { for(Conversation tmp : this.mXmppConnectionService.getConversations()) {
if (tmp.getAccount() == query.getAccount()) { if (tmp.getAccount() == query.getAccount()) {
tmp.sort(); tmp.sort();
if (tmp.setLastMessageTransmitted(query.getEnd())) {
this.mXmppConnectionService.databaseBackend.updateConversation(tmp);
}
} }
} }
} }
if (query.hasCallback()) {
query.callback();
} else {
this.mXmppConnectionService.updateConversationUi();
}
} }
public boolean queryInProgress(Conversation conversation, XmppConnectionService.OnMoreMessagesLoaded callback) { public boolean queryInProgress(Conversation conversation, XmppConnectionService.OnMoreMessagesLoaded callback) {

View file

@ -221,9 +221,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
Message message = conversation.findUnsentMessageWithUuid(uuid); Message message = conversation.findUnsentMessageWithUuid(uuid);
if (message != null) { if (message != null) {
markMessage(message, Message.STATUS_SEND); markMessage(message, Message.STATUS_SEND);
if (conversation.setLastMessageTransmitted(System.currentTimeMillis())) {
databaseBackend.updateConversation(conversation);
}
} }
} }
} }
@ -2883,7 +2880,6 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
public void clearConversationHistory(final Conversation conversation) { public void clearConversationHistory(final Conversation conversation) {
conversation.clearMessages(); conversation.clearMessages();
conversation.setHasMessagesLeftOnServer(false); //avoid messages getting loaded through mam conversation.setHasMessagesLeftOnServer(false); //avoid messages getting loaded through mam
conversation.resetLastMessageTransmitted();
Runnable runnable = new Runnable() { Runnable runnable = new Runnable() {
@Override @Override
public void run() { public void run() {