Skip to content

Commit

Permalink
Refactor group database model and flow.
Browse files Browse the repository at this point in the history
1) Use existing DB types instead of adding new columns.

2) Store group attributes in message body, like everything else.
  • Loading branch information
moxie0 committed Feb 20, 2014
1 parent 0cdc6fd commit 9614dc9
Show file tree
Hide file tree
Showing 28 changed files with 432 additions and 381 deletions.
27 changes: 18 additions & 9 deletions src/org/thoughtcrime/securesms/ConversationActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@
import org.thoughtcrime.securesms.sms.MessageSender;
import org.thoughtcrime.securesms.sms.OutgoingEncryptedMessage;
import org.thoughtcrime.securesms.sms.OutgoingEndSessionMessage;
import org.thoughtcrime.securesms.mms.OutgoingMediaMessage;
import org.thoughtcrime.securesms.mms.OutgoingSecureMediaMessage;
import org.thoughtcrime.securesms.sms.OutgoingTextMessage;
import org.thoughtcrime.securesms.util.ActionBarUtil;
import org.thoughtcrime.securesms.util.BitmapDecodingException;
Expand Down Expand Up @@ -922,20 +924,26 @@ private void sendMessage(boolean forcePlaintext) {
if (recipients == null)
throw new RecipientFormattingException("Badly formatted");

String body = getMessage();
String body = getMessage();
long allocatedThreadId;

if ((!recipients.isSingleRecipient() || recipients.isEmailRecipient()) && !isMmsEnabled) {
handleManualMmsRequired();
return;
} else if (attachmentManager.isAttachmentPresent()) {
allocatedThreadId = MessageSender.sendMms(ConversationActivity.this, masterSecret, recipients,
threadId, attachmentManager.getSlideDeck(), body,
distributionType, isEncryptedConversation && !forcePlaintext);
} else if (recipients.isEmailRecipient() || !recipients.isSingleRecipient() || recipients.isGroupRecipient()) {
allocatedThreadId = MessageSender.sendMms(ConversationActivity.this, masterSecret, recipients,
threadId, new SlideDeck(), body, distributionType,
isEncryptedConversation && !forcePlaintext);
} else if (attachmentManager.isAttachmentPresent() || !recipients.isSingleRecipient() || recipients.isGroupRecipient()) {
SlideDeck slideDeck;

if (attachmentManager.isAttachmentPresent()) slideDeck = attachmentManager.getSlideDeck();
else slideDeck = new SlideDeck();

OutgoingMediaMessage outgoingMessage = new OutgoingMediaMessage(this, recipients, slideDeck,
body, distributionType);

if (isEncryptedConversation && !forcePlaintext) {
outgoingMessage = new OutgoingSecureMediaMessage(outgoingMessage);
}

allocatedThreadId = MessageSender.send(this, masterSecret, outgoingMessage, threadId);
} else {
OutgoingTextMessage message;

Expand All @@ -949,6 +957,7 @@ threadId, new SlideDeck(), body, distributionType,
allocatedThreadId = MessageSender.send(ConversationActivity.this, masterSecret,
message, threadId);
}

sendComplete(recipients, allocatedThreadId, allocatedThreadId != this.threadId);
} catch (RecipientFormattingException ex) {
Toast.makeText(ConversationActivity.this,
Expand Down
5 changes: 3 additions & 2 deletions src/org/thoughtcrime/securesms/ConversationAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,9 @@ private int getItemViewType(Cursor cursor) {
long id = cursor.getLong(cursor.getColumnIndexOrThrow(MmsSmsColumns.ID));
String type = cursor.getString(cursor.getColumnIndexOrThrow(MmsSmsDatabase.TRANSPORT));
MessageRecord messageRecord = getMessageRecord(id, cursor, type);
if (GroupUtil.isMetaGroupAction(messageRecord.getGroupAction())) return MESSAGE_TYPE_GROUP_ACTION;
if (messageRecord.isOutgoing()) return MESSAGE_TYPE_OUTGOING;

if (messageRecord.isGroupAction()) return MESSAGE_TYPE_GROUP_ACTION;
else if (messageRecord.isOutgoing()) return MESSAGE_TYPE_OUTGOING;
else return MESSAGE_TYPE_INCOMING;
}

Expand Down
28 changes: 4 additions & 24 deletions src/org/thoughtcrime/securesms/ConversationItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,16 @@
import android.os.Message;
import android.provider.Contacts.Intents;
import android.provider.ContactsContract.QuickContact;
import org.thoughtcrime.securesms.util.DateUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.webkit.MimeTypeMap;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.webkit.MimeTypeMap;

import org.thoughtcrime.securesms.util.GroupUtil;
import org.whispersystems.textsecure.crypto.MasterSecret;
import org.thoughtcrime.securesms.database.MmsDatabase;
import org.thoughtcrime.securesms.database.model.MediaMmsMessageRecord;
import org.thoughtcrime.securesms.database.model.MessageRecord;
Expand All @@ -53,19 +50,18 @@
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.service.SendReceiveService;
import org.thoughtcrime.securesms.util.BitmapUtil;
import org.thoughtcrime.securesms.util.DateUtils;
import org.thoughtcrime.securesms.util.Emoji;
import org.whispersystems.textsecure.crypto.MasterSecret;
import org.whispersystems.textsecure.util.FutureTaskListener;
import org.whispersystems.textsecure.util.ListenableFutureTask;
import org.whispersystems.textsecure.util.Util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import static org.whispersystems.textsecure.push.PushMessageProtos.PushMessageContent.GroupContext;

/**
* A view that displays an individual conversation item within a conversation
* thread. Used by ComposeMessageActivity's ListActivity via a ConversationAdapter.
Expand Down Expand Up @@ -145,7 +141,7 @@ public void set(MasterSecret masterSecret, MessageRecord messageRecord,

setBodyText(messageRecord);

if (!GroupUtil.isMetaGroupAction(messageRecord.getGroupAction())) {
if (!messageRecord.isGroupAction()) {
setStatusIcons(messageRecord);
setContactPhoto(messageRecord);
setGroupMessageStatus(messageRecord);
Expand Down Expand Up @@ -175,22 +171,6 @@ public void setHandler(Handler failedIconHandler) {
/// MessageRecord Attribute Parsers

private void setBodyText(MessageRecord messageRecord) {
switch (messageRecord.getGroupAction()) {
case GroupContext.Type.QUIT_VALUE:
bodyText.setText(context.getString(R.string.ConversationItem_group_action_left,
messageRecord.getIndividualRecipient().toShortString()));
return;
case GroupContext.Type.ADD_VALUE:
case GroupContext.Type.CREATE_VALUE:
bodyText.setText(context.getString(R.string.ConversationItem_group_action_joined,
Util.join(GroupUtil.getSerializedArgumentMembers(messageRecord.getGroupActionArguments()), ", ")));
return;
case GroupContext.Type.MODIFY_VALUE:
bodyText.setText(context.getString(R.string.ConversationItem_group_action_modify,
messageRecord.getIndividualRecipient()));
return;
}

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
bodyText.setText(Emoji.getInstance(context).emojify(messageRecord.getDisplayBody(), Emoji.EMOJI_LARGE),
TextView.BufferType.SPANNABLE);
Expand Down
24 changes: 15 additions & 9 deletions src/org/thoughtcrime/securesms/GroupCreateActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.google.protobuf.ByteString;

import org.thoughtcrime.securesms.components.PushRecipientsPanel;
import org.thoughtcrime.securesms.contacts.ContactAccessor;
Expand All @@ -37,6 +36,7 @@
import org.thoughtcrime.securesms.recipients.RecipientFormattingException;
import org.thoughtcrime.securesms.recipients.Recipients;
import org.thoughtcrime.securesms.sms.MessageSender;
import org.thoughtcrime.securesms.mms.OutgoingGroupMediaMessage;
import org.thoughtcrime.securesms.util.ActionBarUtil;
import org.thoughtcrime.securesms.util.BitmapUtil;
import org.thoughtcrime.securesms.util.DynamicLanguage;
Expand Down Expand Up @@ -367,8 +367,8 @@ public void onClick(View v) {
}

private Pair<Long, Recipients> handleCreatePushGroup(String groupName,
byte[] avatar,
Set<Recipient> members)
byte[] avatar,
Set<Recipient> members)
throws InvalidNumberException, MmsException
{
GroupDatabase groupDatabase = DatabaseFactory.getGroupDatabase(this);
Expand All @@ -378,17 +378,23 @@ private Pair<Long, Recipients> handleCreatePushGroup(String groupName,
List<String> memberE164Numbers = getE164Numbers(members);
String groupRecipientId = GroupUtil.getEncodedId(groupId);

String groupActionArguments = GroupUtil.serializeArguments(groupId, groupName, memberE164Numbers);

groupDatabase.create(groupId, TextSecurePreferences.getLocalNumber(this), groupName,
memberE164Numbers, null, null);
groupDatabase.updateAvatar(groupId, avatar);

Recipients groupRecipient = RecipientFactory.getRecipientsFromString(this, groupRecipientId, false);

return new Pair<Long, Recipients>(MessageSender.sendGroupAction(this, masterSecret, groupRecipient, -1,
GroupContext.Type.CREATE_VALUE,
groupActionArguments, avatar), groupRecipient);
GroupContext context = GroupContext.newBuilder()
.setId(ByteString.copyFrom(groupId))
.setType(GroupContext.Type.CREATE)
.setName(groupName)
.addAllMembers(memberE164Numbers)
.build();

OutgoingGroupMediaMessage outgoingMessage = new OutgoingGroupMediaMessage(this, groupRecipient, context, avatar);
long threadId = MessageSender.send(this, masterSecret, outgoingMessage, -1);

return new Pair<Long, Recipients>(threadId, groupRecipient);
} catch (RecipientFormattingException e) {
throw new AssertionError(e);
} catch (MmsException e) {
Expand Down
6 changes: 0 additions & 6 deletions src/org/thoughtcrime/securesms/database/DatabaseFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -647,12 +647,6 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("ALTER TABLE push ADD COLUMN device_id INTEGER DEFAULT 1;");
db.execSQL("ALTER TABLE sms ADD COLUMN address_device_id INTEGER DEFAULT 1;");
db.execSQL("ALTER TABLE mms ADD COLUMN address_device_id INTEGER DEFAULT 1;");
db.execSQL("ALTER TABLE sms ADD COLUMN group_action INTEGER DEFAULT -1;");
db.execSQL("ALTER TABLE sms ADD COLUMN group_action_arguments TEXT;");
db.execSQL("ALTER TABLE mms ADD COLuMN group_action INTEGER DEFAULT -1;");
db.execSQL("ALTER TABLE mms ADD COLUMN group_action_arguments TEXT;");
db.execSQL("ALTER TABLE thread ADD COLUMN group_action INTEGER DEFAULT -1;");
db.execSQL("ALTER TABLE thread ADD COLUMN group_action_arguments TEXT;");
}

db.setTransactionSuccessful();
Expand Down
7 changes: 6 additions & 1 deletion src/org/thoughtcrime/securesms/database/GroupDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public Reader getGroup(byte[] groupId) {
}

public Recipients getGroupMembers(byte[] groupId) {
List<String> members = getCurrentMembers(groupId);
List<String> members = getCurrentMembers(groupId);
List<Recipient> recipients = new LinkedList<Recipient>();

for (String member : members) {
Expand All @@ -94,12 +94,17 @@ public void create(byte[] groupId, String owner, String title,
List<String> filteredMembers = new LinkedList<String>();
String localNumber = TextSecurePreferences.getLocalNumber(context);

if (!localNumber.equals(owner)) {
filteredMembers.add(owner);
}

for (String member : members) {
if (!member.equals(localNumber)) {
filteredMembers.add(member);
}
}


ContentValues contentValues = new ContentValues();
contentValues.put(GROUP_ID, GroupUtil.getEncodedId(groupId));
contentValues.put(OWNER, owner);
Expand Down

1 comment on commit 9614dc9

@WhisperBTC
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! BitHub has sent payment of $14.56USD for this commit.

Please sign in to comment.