mirror of
https://github.com/oxen-io/session-android.git
synced 2025-10-27 12:39:04 +00:00
Handle negative directory case and unlisted contacts.
This commit is contained in:
@@ -15,8 +15,8 @@ import org.whispersystems.textsecure.util.Util;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -53,20 +53,7 @@ public class Directory {
|
||||
this.databaseHelper = new DatabaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
|
||||
}
|
||||
|
||||
public boolean containsNumbers(List<String> e164numbers) {
|
||||
if (e164numbers == null || e164numbers.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (String e164number : e164numbers) {
|
||||
if (!containsNumber(e164number))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean containsNumber(String e164number) {
|
||||
public boolean isActiveNumber(String e164number) throws NotInDirectoryException {
|
||||
if (e164number == null || e164number.length() == 0) {
|
||||
return false;
|
||||
}
|
||||
@@ -82,29 +69,33 @@ public class Directory {
|
||||
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
return cursor.getInt(0) == 1;
|
||||
} else {
|
||||
throw new NotInDirectoryException();
|
||||
}
|
||||
|
||||
return false;
|
||||
} finally {
|
||||
if (cursor != null)
|
||||
cursor.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void setActiveTokens(List<String> tokens) {
|
||||
public void setToken(String token, boolean active) {
|
||||
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(TOKEN, token);
|
||||
values.put(REGISTERED, active ? 1 : 0);
|
||||
values.put(TIMESTAMP, System.currentTimeMillis());
|
||||
db.replace(TABLE_NAME, null, values);
|
||||
}
|
||||
|
||||
public void setTokens(List<String> activeTokens, Collection<String> inactiveTokens) {
|
||||
long timestamp = System.currentTimeMillis();
|
||||
SQLiteDatabase db = databaseHelper.getWritableDatabase();
|
||||
db.beginTransaction();
|
||||
|
||||
try {
|
||||
ContentValues clear = new ContentValues();
|
||||
clear.put(REGISTERED, 0);
|
||||
clear.put(TIMESTAMP, timestamp);
|
||||
db.update(TABLE_NAME, clear, null, null);
|
||||
|
||||
|
||||
for (String token : tokens) {
|
||||
Log.w("Directory", "Adding token: " + token);
|
||||
for (String token : activeTokens) {
|
||||
Log.w("Directory", "Adding active token: " + token);
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(TOKEN, token);
|
||||
values.put(REGISTERED, 1);
|
||||
@@ -112,6 +103,14 @@ public class Directory {
|
||||
db.replace(TABLE_NAME, null, values);
|
||||
}
|
||||
|
||||
for (String token : inactiveTokens) {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(TOKEN, token);
|
||||
values.put(REGISTERED, 0);
|
||||
values.put(TIMESTAMP, timestamp);
|
||||
db.replace(TABLE_NAME, null, values);
|
||||
}
|
||||
|
||||
db.setTransactionSuccessful();
|
||||
} finally {
|
||||
db.endTransaction();
|
||||
@@ -135,6 +134,15 @@ public class Directory {
|
||||
}
|
||||
}
|
||||
|
||||
cursor.close();
|
||||
|
||||
cursor = databaseHelper.getReadableDatabase().query(TABLE_NAME, new String[] {TOKEN},
|
||||
null, null, null, null, null);
|
||||
|
||||
while (cursor.moveToNext()) {
|
||||
results.add(cursor.getString(0));
|
||||
}
|
||||
|
||||
return results;
|
||||
} finally {
|
||||
if (cursor != null)
|
||||
@@ -142,7 +150,7 @@ public class Directory {
|
||||
}
|
||||
}
|
||||
|
||||
private String getToken(String e164number) {
|
||||
public String getToken(String e164number) {
|
||||
try {
|
||||
MessageDigest digest = MessageDigest.getInstance("SHA1");
|
||||
byte[] token = Util.trim(digest.digest(e164number.getBytes()), 10);
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
package org.whispersystems.textsecure.directory;
|
||||
|
||||
public class NotInDirectoryException extends Throwable {
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package org.whispersystems.textsecure.push;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class NotFoundException extends IOException {
|
||||
public NotFoundException(String s) {
|
||||
super(s);
|
||||
}
|
||||
}
|
||||
@@ -42,7 +42,8 @@ public class PushServiceSocket {
|
||||
private static final String REGISTER_GCM_PATH = "/v1/accounts/gcm/";
|
||||
private static final String PREKEY_PATH = "/v1/keys/%s";
|
||||
|
||||
private static final String DIRECTORY_PATH = "/v1/directory/";
|
||||
private static final String DIRECTORY_TOKENS_PATH = "/v1/directory/tokens";
|
||||
private static final String DIRECTORY_VERIFY_PATH = "/v1/directory/%s";
|
||||
private static final String MESSAGE_PATH = "/v1/messages/";
|
||||
private static final String ATTACHMENT_PATH = "/v1/attachments/%s";
|
||||
|
||||
@@ -172,7 +173,7 @@ public class PushServiceSocket {
|
||||
public List<String> retrieveDirectory(Set<String> contactTokens) {
|
||||
try {
|
||||
ContactTokenList contactTokenList = new ContactTokenList(new LinkedList(contactTokens));
|
||||
String response = makeRequest(DIRECTORY_PATH, "PUT", new Gson().toJson(contactTokenList));
|
||||
String response = makeRequest(DIRECTORY_TOKENS_PATH, "PUT", new Gson().toJson(contactTokenList));
|
||||
ContactTokenList activeTokens = new Gson().fromJson(response, ContactTokenList.class);
|
||||
|
||||
return activeTokens.getContacts();
|
||||
@@ -182,6 +183,15 @@ public class PushServiceSocket {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isRegisteredUser(String contactToken) throws IOException {
|
||||
try {
|
||||
makeRequest(String.format(DIRECTORY_VERIFY_PATH, contactToken), "GET", null);
|
||||
return true;
|
||||
} catch (NotFoundException nfe) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void downloadExternalFile(String url, File localDestination)
|
||||
throws IOException
|
||||
{
|
||||
@@ -284,6 +294,10 @@ public class PushServiceSocket {
|
||||
throw new AuthorizationFailedException("Authorization failed!");
|
||||
}
|
||||
|
||||
if (connection.getResponseCode() == 404) {
|
||||
throw new NotFoundException("Not found");
|
||||
}
|
||||
|
||||
if (connection.getResponseCode() != 200) {
|
||||
throw new IOException("Bad response: " + connection.getResponseCode() + " " + connection.getResponseMessage());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user