From 7d33a9cc8fc3774dcddbd196b5f7b519b265e459 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Wed, 5 Jun 2019 15:53:10 +1000 Subject: [PATCH] Always send a friend request if we're not friends. Pass down isFriendRequest to message sender. --- .../conversation/ConversationActivity.java | 5 ++++ .../securesms/database/SmsDatabase.java | 9 ++++-- .../database/model/SmsMessageRecord.java | 30 +++++++++++++++---- .../securesms/jobs/PushTextSendJob.java | 1 + .../securesms/sms/OutgoingTextMessage.java | 4 +++ 5 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java b/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java index bc8a4fc74f..cbbe4c3ba8 100644 --- a/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java +++ b/src/org/thoughtcrime/securesms/conversation/ConversationActivity.java @@ -146,6 +146,7 @@ import org.thoughtcrime.securesms.giph.ui.GiphyActivity; import org.thoughtcrime.securesms.linkpreview.LinkPreview; import org.thoughtcrime.securesms.linkpreview.LinkPreviewRepository; import org.thoughtcrime.securesms.linkpreview.LinkPreviewViewModel; +import org.thoughtcrime.securesms.loki.LokiFriendRequestMessage; import org.thoughtcrime.securesms.mediasend.MediaSendActivity; import org.thoughtcrime.securesms.mediasend.Media; import org.thoughtcrime.securesms.jobs.MultiDeviceBlockedUpdateJob; @@ -2147,6 +2148,10 @@ public class ConversationActivity extends PassphraseRequiredActionBarActivity ApplicationContext.getInstance(context).getTypingStatusSender().onTypingStopped(threadId); } else { message = new OutgoingTextMessage(recipient, messageBody, expiresIn, subscriptionId); + + // Loki - Always send friend requests if we're not friends with the user + int friendRequestStatus = DatabaseFactory.getThreadDatabase(context).getFriendRequestStatus(threadId); + message.setIsFriendRequest(friendRequestStatus != ThreadDatabase.LokiFriendRequestStatus.FRIENDS); } Permissions.with(this) diff --git a/src/org/thoughtcrime/securesms/database/SmsDatabase.java b/src/org/thoughtcrime/securesms/database/SmsDatabase.java index 9db2be22bc..7c54a04bf6 100644 --- a/src/org/thoughtcrime/securesms/database/SmsDatabase.java +++ b/src/org/thoughtcrime/securesms/database/SmsDatabase.java @@ -72,6 +72,9 @@ public class SmsDatabase extends MessagingDatabase { public static final String SUBJECT = "subject"; public static final String SERVICE_CENTER = "service_center"; + // Loki + public static final String IS_FRIEND_REQUEST = "is_friend_request"; + public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + ID + " integer PRIMARY KEY, " + THREAD_ID + " INTEGER, " + ADDRESS + " TEXT, " + ADDRESS_DEVICE_ID + " INTEGER DEFAULT 1, " + PERSON + " INTEGER, " + DATE_RECEIVED + " INTEGER, " + DATE_SENT + " INTEGER, " + PROTOCOL + " INTEGER, " + READ + " INTEGER DEFAULT 0, " + @@ -79,7 +82,7 @@ public class SmsDatabase extends MessagingDatabase { DELIVERY_RECEIPT_COUNT + " INTEGER DEFAULT 0," + SUBJECT + " TEXT, " + BODY + " TEXT, " + MISMATCHED_IDENTITIES + " TEXT DEFAULT NULL, " + SERVICE_CENTER + " TEXT, " + SUBSCRIPTION_ID + " INTEGER DEFAULT -1, " + EXPIRES_IN + " INTEGER DEFAULT 0, " + EXPIRE_STARTED + " INTEGER DEFAULT 0, " + NOTIFIED + " DEFAULT 0, " + - READ_RECEIPT_COUNT + " INTEGER DEFAULT 0, " + UNIDENTIFIED + " INTEGER DEFAULT 0);"; + READ_RECEIPT_COUNT + " INTEGER DEFAULT 0, " + UNIDENTIFIED + " INTEGER DEFAULT 0," + IS_FRIEND_REQUEST + "INTEGER DEFAULT 0);"; public static final String[] CREATE_INDEXS = { "CREATE INDEX IF NOT EXISTS sms_thread_id_index ON " + TABLE_NAME + " (" + THREAD_ID + ");", @@ -640,6 +643,7 @@ public class SmsDatabase extends MessagingDatabase { contentValues.put(EXPIRES_IN, message.getExpiresIn()); contentValues.put(DELIVERY_RECEIPT_COUNT, Stream.of(earlyDeliveryReceipts.values()).mapToLong(Long::longValue).sum()); contentValues.put(READ_RECEIPT_COUNT, Stream.of(earlyReadReceipts.values()).mapToLong(Long::longValue).sum()); + contentValues.put(IS_FRIEND_REQUEST, message.isFriendRequest() ? 1 : 0); SQLiteDatabase db = databaseHelper.getWritableDatabase(); long messageId = db.insert(TABLE_NAME, ADDRESS, contentValues); @@ -866,6 +870,7 @@ public class SmsDatabase extends MessagingDatabase { long expireStarted = cursor.getLong(cursor.getColumnIndexOrThrow(SmsDatabase.EXPIRE_STARTED)); String body = cursor.getString(cursor.getColumnIndexOrThrow(SmsDatabase.BODY)); boolean unidentified = cursor.getInt(cursor.getColumnIndexOrThrow(SmsDatabase.UNIDENTIFIED)) == 1; + boolean isFriendRequest = cursor.getInt(cursor.getColumnIndexOrThrow(SmsDatabase.IS_FRIEND_REQUEST)) == 1; if (!TextSecurePreferences.isReadReceiptsEnabled(context)) { readReceiptCount = 0; @@ -879,7 +884,7 @@ public class SmsDatabase extends MessagingDatabase { addressDeviceId, dateSent, dateReceived, deliveryReceiptCount, type, threadId, status, mismatches, subscriptionId, - expiresIn, expireStarted, readReceiptCount, unidentified); + expiresIn, expireStarted, readReceiptCount, unidentified, isFriendRequest); } private List getMismatches(String document) { diff --git a/src/org/thoughtcrime/securesms/database/model/SmsMessageRecord.java b/src/org/thoughtcrime/securesms/database/model/SmsMessageRecord.java index beede83f28..0b8cb39a11 100644 --- a/src/org/thoughtcrime/securesms/database/model/SmsMessageRecord.java +++ b/src/org/thoughtcrime/securesms/database/model/SmsMessageRecord.java @@ -36,9 +36,25 @@ import java.util.List; * @author Moxie Marlinspike * */ - public class SmsMessageRecord extends MessageRecord { + // Loki + private final boolean isFriendRequest; + + public SmsMessageRecord(long id, + String body, Recipient recipient, + Recipient individualRecipient, + int recipientDeviceId, + long dateSent, long dateReceived, + int deliveryReceiptCount, + long type, long threadId, + int status, List mismatches, + int subscriptionId, long expiresIn, long expireStarted, + int readReceiptCount, boolean unidentified) + { + this(id, body, recipient, individualRecipient, recipientDeviceId, dateSent, dateReceived, deliveryReceiptCount, type, threadId, status, mismatches, subscriptionId, expiresIn, expireStarted, readReceiptCount, unidentified, false); + } + public SmsMessageRecord(long id, String body, Recipient recipient, Recipient individualRecipient, @@ -48,18 +64,22 @@ public class SmsMessageRecord extends MessageRecord { long type, long threadId, int status, List mismatches, int subscriptionId, long expiresIn, long expireStarted, - int readReceiptCount, boolean unidentified) + int readReceiptCount, boolean unidentified, boolean isFriendRequest) { super(id, body, recipient, individualRecipient, recipientDeviceId, - dateSent, dateReceived, threadId, status, deliveryReceiptCount, type, - mismatches, new LinkedList<>(), subscriptionId, - expiresIn, expireStarted, readReceiptCount, unidentified); + dateSent, dateReceived, threadId, status, deliveryReceiptCount, type, + mismatches, new LinkedList<>(), subscriptionId, + expiresIn, expireStarted, readReceiptCount, unidentified); + this.isFriendRequest = isFriendRequest; } public long getType() { return type; } + // Loki + public boolean isFriendRequest() { return isFriendRequest; } + @Override public SpannableString getDisplayBody(@NonNull Context context) { if (SmsDatabase.Types.isFailedDecryptType(type)) { diff --git a/src/org/thoughtcrime/securesms/jobs/PushTextSendJob.java b/src/org/thoughtcrime/securesms/jobs/PushTextSendJob.java index 55532af70a..878ffc8447 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushTextSendJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushTextSendJob.java @@ -170,6 +170,7 @@ public class PushTextSendJob extends PushSendJob implements InjectableType { .withExpiration((int)(message.getExpiresIn() / 1000)) .withProfileKey(profileKey.orNull()) .asEndSessionMessage(message.isEndSession()) + .asFriendRequest(message.isFriendRequest()) .build(); if (address.getNumber().equals(TextSecurePreferences.getLocalNumber(context))) { diff --git a/src/org/thoughtcrime/securesms/sms/OutgoingTextMessage.java b/src/org/thoughtcrime/securesms/sms/OutgoingTextMessage.java index e74cb8ff99..541f05510e 100644 --- a/src/org/thoughtcrime/securesms/sms/OutgoingTextMessage.java +++ b/src/org/thoughtcrime/securesms/sms/OutgoingTextMessage.java @@ -9,6 +9,7 @@ public class OutgoingTextMessage { private final String message; private final int subscriptionId; private final long expiresIn; + private boolean isFriendRequest = false; public OutgoingTextMessage(Recipient recipient, String message, int subscriptionId) { this(recipient, message, 0, subscriptionId); @@ -68,6 +69,9 @@ public class OutgoingTextMessage { return false; } + public boolean isFriendRequest() { return isFriendRequest; } + public void setIsFriendRequest(boolean value) { this.isFriendRequest = value; } + public static OutgoingTextMessage from(SmsMessageRecord record) { if (record.isSecure()) { return new OutgoingEncryptedMessage(record.getRecipient(), record.getBody(), record.getExpiresIn());