Permalink
Browse files

Better handling for unregistered users on outgoing message.

Change-Id: I3d818135dbdcd27bfad9f3fd725a85adda24022a
  • Loading branch information...
moxie0 authored and ctso committed Dec 8, 2013
1 parent 0c6f66e commit 6726a35923460ab381b3141a36294dbcfb75380e
View
@@ -3,7 +3,7 @@ buildscript {
mavenCentral()
}
dependencies {
- classpath 'com.android.tools.build:gradle:0.6.1'
+ classpath 'com.android.tools.build:gradle:0.6.+'
}
}
@@ -116,12 +116,14 @@ public void sendMessage(List<PushDestination> recipients, List<PushBody> bodies)
sendMessage(new OutgoingPushMessageList(messages));
}
- private void sendMessage(OutgoingPushMessageList messages) throws IOException {
+ private void sendMessage(OutgoingPushMessageList messages)
+ throws IOException
+ {
String responseText = makeRequest(MESSAGE_PATH, "POST", new Gson().toJson(messages));
PushMessageResponse response = new Gson().fromJson(responseText, PushMessageResponse.class);
if (response.getFailure().size() != 0)
- throw new IOException("Got send failure: " + response.getFailure().get(0));
+ throw new UnregisteredUserException(response.getFailure());
}
public void registerPreKeys(IdentityKey identityKey,
@@ -0,0 +1,19 @@
+package org.whispersystems.textsecure.push;
+
+import java.io.IOException;
+import java.util.List;
+
+public class UnregisteredUserException extends IOException {
+
+ private final List<String> addresses;
+
+ public UnregisteredUserException(List<String> addresses) {
+ super();
+ this.addresses = addresses;
+ }
+
+ public List<String> getAddresses() {
+ return addresses;
+ }
+
+}
@@ -46,6 +46,7 @@
import org.whispersystems.textsecure.push.PushMessageProtos.PushMessageContent;
import org.whispersystems.textsecure.push.PushServiceSocket;
import org.whispersystems.textsecure.push.RateLimitException;
+import org.whispersystems.textsecure.push.UnregisteredUserException;
import org.whispersystems.textsecure.storage.SessionRecordV2;
import org.whispersystems.textsecure.util.InvalidNumberException;
@@ -83,6 +84,10 @@ public void deliver(SmsMessageRecord message) throws IOException {
socket.sendMessage(destination, pushBody);
context.sendBroadcast(constructSentIntent(context, message.getId(), message.getType(), true));
+ } catch (UnregisteredUserException e) {
+ Log.w("PushTransport", e);
+ destroySessions(e.getAddresses());
+ throw new IOException("Not push registered after all.");
} catch (RateLimitException e) {
Log.w("PushTransport", e);
throw new IOException("Rate limit exceeded.");
@@ -128,6 +133,10 @@ public void deliver(SendReq message, List<PushDestination> destinations, long th
socket.sendMessage(destinations, pushBodies);
+ } catch (UnregisteredUserException e) {
+ Log.w("PushTransport", e);
+ destroySessions(e.getAddresses());
+ throw new IOException("No push registered after all.");
} catch (RateLimitException e) {
Log.w("PushTransport", e);
throw new IOException("Rate limit exceeded.");
@@ -188,4 +197,16 @@ private PushBody getEncryptedMessage(PushServiceSocket socket, long threadId, Re
throw new AssertionError("Unknown ciphertext type: " + message.getType());
}
}
+
+ private void destroySessions(List<String> unregisteredUsers) {
+ for (String unregisteredUser : unregisteredUsers) {
+ Log.w("PushTransport", "Destroying session for: " + unregisteredUser);
+ try {
+ Recipients recipients = RecipientFactory.getRecipientsFromString(context, unregisteredUser, false);
+ SessionRecordV2.delete(context, recipients.getPrimaryRecipient());
+ } catch (RecipientFormattingException e) {
+ Log.w("PushTransport", e);
+ }
+ }
+ }
}

0 comments on commit 6726a35

Please sign in to comment.