mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 02:39:05 +01:00
[core] Add API to add messages with attachments
This commit is contained in:
@@ -519,7 +519,7 @@ public class ConversationActivity extends BriarActivity
|
||||
try {
|
||||
//noinspection ConstantConditions init in loadGroupId()
|
||||
storeMessage(privateMessageFactory.createPrivateMessage(
|
||||
messagingGroupId, timestamp, text), text);
|
||||
messagingGroupId, timestamp, text, emptyList()), text);
|
||||
} catch (FormatException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@ import org.briarproject.bramble.api.sync.GroupId;
|
||||
import org.briarproject.bramble.api.sync.MessageId;
|
||||
import org.briarproject.briar.api.conversation.ConversationManager.ConversationClient;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface MessagingManager extends ConversationClient {
|
||||
|
||||
@@ -31,6 +33,12 @@ public interface MessagingManager extends ConversationClient {
|
||||
*/
|
||||
void addLocalMessage(PrivateMessage m) throws DbException;
|
||||
|
||||
/**
|
||||
* Stores a local attachment message.
|
||||
*/
|
||||
AttachmentHeader addLocalAttachment(GroupId groupId, long timestamp,
|
||||
String contentType, ByteBuffer data) throws DbException;
|
||||
|
||||
/**
|
||||
* Returns the ID of the contact with the given private conversation.
|
||||
*/
|
||||
|
||||
@@ -4,10 +4,13 @@ import org.briarproject.bramble.api.FormatException;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.GroupId;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@NotNullByDefault
|
||||
public interface PrivateMessageFactory {
|
||||
|
||||
PrivateMessage createPrivateMessage(GroupId groupId, long timestamp,
|
||||
String text) throws FormatException;
|
||||
String text, List<AttachmentHeader> attachments)
|
||||
throws FormatException;
|
||||
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import org.briarproject.bramble.api.versioning.ClientVersioningManager.ClientVer
|
||||
import org.briarproject.briar.api.client.MessageTracker;
|
||||
import org.briarproject.briar.api.conversation.ConversationMessageHeader;
|
||||
import org.briarproject.briar.api.messaging.Attachment;
|
||||
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
||||
import org.briarproject.briar.api.messaging.MessagingManager;
|
||||
import org.briarproject.briar.api.messaging.PrivateMessage;
|
||||
import org.briarproject.briar.api.messaging.PrivateMessageHeader;
|
||||
@@ -150,6 +151,15 @@ class MessagingManagerImpl extends ConversationClientImpl
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public AttachmentHeader addLocalAttachment(GroupId groupId, long timestamp,
|
||||
String contentType, ByteBuffer data) {
|
||||
// TODO add real implementation
|
||||
byte[] b = new byte[MessageId.LENGTH];
|
||||
new Random().nextBytes(b);
|
||||
return new AttachmentHeader(new MessageId(b), "image/png");
|
||||
}
|
||||
|
||||
private ContactId getContactId(Transaction txn, GroupId g)
|
||||
throws DbException {
|
||||
try {
|
||||
|
||||
@@ -6,9 +6,12 @@ import org.briarproject.bramble.api.data.BdfList;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.GroupId;
|
||||
import org.briarproject.bramble.api.sync.Message;
|
||||
import org.briarproject.briar.api.messaging.AttachmentHeader;
|
||||
import org.briarproject.briar.api.messaging.PrivateMessage;
|
||||
import org.briarproject.briar.api.messaging.PrivateMessageFactory;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
import javax.inject.Inject;
|
||||
|
||||
@@ -28,7 +31,8 @@ class PrivateMessageFactoryImpl implements PrivateMessageFactory {
|
||||
|
||||
@Override
|
||||
public PrivateMessage createPrivateMessage(GroupId groupId, long timestamp,
|
||||
String text) throws FormatException {
|
||||
String text, List<AttachmentHeader> attachments)
|
||||
throws FormatException {
|
||||
// Validate the arguments
|
||||
if (utf8IsTooLong(text, MAX_PRIVATE_MESSAGE_TEXT_LENGTH))
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
@@ -53,6 +53,7 @@ import java.util.logging.Logger;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.logging.Level.INFO;
|
||||
import static java.util.logging.Level.WARNING;
|
||||
import static org.briarproject.bramble.api.plugin.BluetoothConstants.UUID_BYTES;
|
||||
@@ -327,7 +328,7 @@ public class TestDataCreatorImpl implements TestDataCreator {
|
||||
private void createPrivateMessage(GroupId groupId, String text,
|
||||
long timestamp, boolean local) throws DbException, FormatException {
|
||||
PrivateMessage m = privateMessageFactory
|
||||
.createPrivateMessage(groupId, timestamp, text);
|
||||
.createPrivateMessage(groupId, timestamp, text, emptyList());
|
||||
BdfDictionary meta = new BdfDictionary();
|
||||
meta.put("timestamp", timestamp);
|
||||
meta.put("local", local);
|
||||
|
||||
@@ -24,6 +24,7 @@ import org.junit.Test;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_AUTHOR_NAME_LENGTH;
|
||||
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_KEY_LENGTH;
|
||||
import static org.briarproject.bramble.api.record.Record.MAX_RECORD_PAYLOAD_BYTES;
|
||||
@@ -58,7 +59,7 @@ public class MessageSizeIntegrationTest extends BriarTestCase {
|
||||
long timestamp = Long.MAX_VALUE;
|
||||
String text = getRandomString(MAX_PRIVATE_MESSAGE_TEXT_LENGTH);
|
||||
PrivateMessage message = privateMessageFactory.createPrivateMessage(
|
||||
groupId, timestamp, text);
|
||||
groupId, timestamp, text, emptyList());
|
||||
// Check the size of the serialised message
|
||||
int length = message.getMessage().getRawLength();
|
||||
assertTrue(length > UniqueId.LENGTH + 8
|
||||
|
||||
@@ -41,6 +41,7 @@ import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
|
||||
import static java.util.Collections.emptyList;
|
||||
import static org.briarproject.bramble.api.transport.TransportConstants.TAG_LENGTH;
|
||||
import static org.briarproject.bramble.test.TestPluginConfigModule.MAX_LATENCY;
|
||||
import static org.briarproject.bramble.test.TestPluginConfigModule.TRANSPORT_ID;
|
||||
@@ -121,7 +122,7 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
|
||||
PrivateMessageFactory privateMessageFactory =
|
||||
device.getPrivateMessageFactory();
|
||||
PrivateMessage message = privateMessageFactory.createPrivateMessage(
|
||||
groupId, System.currentTimeMillis(), "Hi!");
|
||||
groupId, System.currentTimeMillis(), "Hi!", emptyList());
|
||||
messagingManager.addLocalMessage(message);
|
||||
}
|
||||
|
||||
|
||||
@@ -73,7 +73,7 @@ constructor(
|
||||
|
||||
val group = messagingManager.getContactGroup(contact)
|
||||
val now = clock.currentTimeMillis()
|
||||
val m = privateMessageFactory.createPrivateMessage(group.id, now, message)
|
||||
val m = privateMessageFactory.createPrivateMessage(group.id, now, message, emptyList())
|
||||
|
||||
messagingManager.addLocalMessage(m)
|
||||
return ctx.json(m.output(contact.id, message))
|
||||
|
||||
@@ -110,7 +110,8 @@ internal class MessagingControllerImplTest : ControllerTest() {
|
||||
privateMessageFactory.createPrivateMessage(
|
||||
group.id,
|
||||
timestamp,
|
||||
text
|
||||
text,
|
||||
emptyList()
|
||||
)
|
||||
} returns privateMessage
|
||||
every { messagingManager.addLocalMessage(privateMessage) } just runs
|
||||
|
||||
Reference in New Issue
Block a user