mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 20:59:54 +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 {
|
try {
|
||||||
//noinspection ConstantConditions init in loadGroupId()
|
//noinspection ConstantConditions init in loadGroupId()
|
||||||
storeMessage(privateMessageFactory.createPrivateMessage(
|
storeMessage(privateMessageFactory.createPrivateMessage(
|
||||||
messagingGroupId, timestamp, text), text);
|
messagingGroupId, timestamp, text, emptyList()), text);
|
||||||
} catch (FormatException e) {
|
} catch (FormatException e) {
|
||||||
throw new RuntimeException(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.bramble.api.sync.MessageId;
|
||||||
import org.briarproject.briar.api.conversation.ConversationManager.ConversationClient;
|
import org.briarproject.briar.api.conversation.ConversationManager.ConversationClient;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
public interface MessagingManager extends ConversationClient {
|
public interface MessagingManager extends ConversationClient {
|
||||||
|
|
||||||
@@ -31,6 +33,12 @@ public interface MessagingManager extends ConversationClient {
|
|||||||
*/
|
*/
|
||||||
void addLocalMessage(PrivateMessage m) throws DbException;
|
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.
|
* 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.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.sync.GroupId;
|
import org.briarproject.bramble.api.sync.GroupId;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@NotNullByDefault
|
@NotNullByDefault
|
||||||
public interface PrivateMessageFactory {
|
public interface PrivateMessageFactory {
|
||||||
|
|
||||||
PrivateMessage createPrivateMessage(GroupId groupId, long timestamp,
|
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.client.MessageTracker;
|
||||||
import org.briarproject.briar.api.conversation.ConversationMessageHeader;
|
import org.briarproject.briar.api.conversation.ConversationMessageHeader;
|
||||||
import org.briarproject.briar.api.messaging.Attachment;
|
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.MessagingManager;
|
||||||
import org.briarproject.briar.api.messaging.PrivateMessage;
|
import org.briarproject.briar.api.messaging.PrivateMessage;
|
||||||
import org.briarproject.briar.api.messaging.PrivateMessageHeader;
|
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)
|
private ContactId getContactId(Transaction txn, GroupId g)
|
||||||
throws DbException {
|
throws DbException {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -6,9 +6,12 @@ import org.briarproject.bramble.api.data.BdfList;
|
|||||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||||
import org.briarproject.bramble.api.sync.GroupId;
|
import org.briarproject.bramble.api.sync.GroupId;
|
||||||
import org.briarproject.bramble.api.sync.Message;
|
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.PrivateMessage;
|
||||||
import org.briarproject.briar.api.messaging.PrivateMessageFactory;
|
import org.briarproject.briar.api.messaging.PrivateMessageFactory;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import javax.annotation.concurrent.Immutable;
|
import javax.annotation.concurrent.Immutable;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
@@ -28,7 +31,8 @@ class PrivateMessageFactoryImpl implements PrivateMessageFactory {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PrivateMessage createPrivateMessage(GroupId groupId, long timestamp,
|
public PrivateMessage createPrivateMessage(GroupId groupId, long timestamp,
|
||||||
String text) throws FormatException {
|
String text, List<AttachmentHeader> attachments)
|
||||||
|
throws FormatException {
|
||||||
// Validate the arguments
|
// Validate the arguments
|
||||||
if (utf8IsTooLong(text, MAX_PRIVATE_MESSAGE_TEXT_LENGTH))
|
if (utf8IsTooLong(text, MAX_PRIVATE_MESSAGE_TEXT_LENGTH))
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
|
|||||||
@@ -53,6 +53,7 @@ import java.util.logging.Logger;
|
|||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import static java.util.Collections.emptyList;
|
||||||
import static java.util.logging.Level.INFO;
|
import static java.util.logging.Level.INFO;
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
import static org.briarproject.bramble.api.plugin.BluetoothConstants.UUID_BYTES;
|
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,
|
private void createPrivateMessage(GroupId groupId, String text,
|
||||||
long timestamp, boolean local) throws DbException, FormatException {
|
long timestamp, boolean local) throws DbException, FormatException {
|
||||||
PrivateMessage m = privateMessageFactory
|
PrivateMessage m = privateMessageFactory
|
||||||
.createPrivateMessage(groupId, timestamp, text);
|
.createPrivateMessage(groupId, timestamp, text, emptyList());
|
||||||
BdfDictionary meta = new BdfDictionary();
|
BdfDictionary meta = new BdfDictionary();
|
||||||
meta.put("timestamp", timestamp);
|
meta.put("timestamp", timestamp);
|
||||||
meta.put("local", local);
|
meta.put("local", local);
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import org.junit.Test;
|
|||||||
|
|
||||||
import javax.inject.Inject;
|
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_AUTHOR_NAME_LENGTH;
|
||||||
import static org.briarproject.bramble.api.identity.AuthorConstants.MAX_PUBLIC_KEY_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;
|
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;
|
long timestamp = Long.MAX_VALUE;
|
||||||
String text = getRandomString(MAX_PRIVATE_MESSAGE_TEXT_LENGTH);
|
String text = getRandomString(MAX_PRIVATE_MESSAGE_TEXT_LENGTH);
|
||||||
PrivateMessage message = privateMessageFactory.createPrivateMessage(
|
PrivateMessage message = privateMessageFactory.createPrivateMessage(
|
||||||
groupId, timestamp, text);
|
groupId, timestamp, text, emptyList());
|
||||||
// Check the size of the serialised message
|
// Check the size of the serialised message
|
||||||
int length = message.getMessage().getRawLength();
|
int length = message.getMessage().getRawLength();
|
||||||
assertTrue(length > UniqueId.LENGTH + 8
|
assertTrue(length > UniqueId.LENGTH + 8
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ import java.io.ByteArrayOutputStream;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.InputStream;
|
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.api.transport.TransportConstants.TAG_LENGTH;
|
||||||
import static org.briarproject.bramble.test.TestPluginConfigModule.MAX_LATENCY;
|
import static org.briarproject.bramble.test.TestPluginConfigModule.MAX_LATENCY;
|
||||||
import static org.briarproject.bramble.test.TestPluginConfigModule.TRANSPORT_ID;
|
import static org.briarproject.bramble.test.TestPluginConfigModule.TRANSPORT_ID;
|
||||||
@@ -121,7 +122,7 @@ public class SimplexMessagingIntegrationTest extends BriarTestCase {
|
|||||||
PrivateMessageFactory privateMessageFactory =
|
PrivateMessageFactory privateMessageFactory =
|
||||||
device.getPrivateMessageFactory();
|
device.getPrivateMessageFactory();
|
||||||
PrivateMessage message = privateMessageFactory.createPrivateMessage(
|
PrivateMessage message = privateMessageFactory.createPrivateMessage(
|
||||||
groupId, System.currentTimeMillis(), "Hi!");
|
groupId, System.currentTimeMillis(), "Hi!", emptyList());
|
||||||
messagingManager.addLocalMessage(message);
|
messagingManager.addLocalMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ constructor(
|
|||||||
|
|
||||||
val group = messagingManager.getContactGroup(contact)
|
val group = messagingManager.getContactGroup(contact)
|
||||||
val now = clock.currentTimeMillis()
|
val now = clock.currentTimeMillis()
|
||||||
val m = privateMessageFactory.createPrivateMessage(group.id, now, message)
|
val m = privateMessageFactory.createPrivateMessage(group.id, now, message, emptyList())
|
||||||
|
|
||||||
messagingManager.addLocalMessage(m)
|
messagingManager.addLocalMessage(m)
|
||||||
return ctx.json(m.output(contact.id, message))
|
return ctx.json(m.output(contact.id, message))
|
||||||
|
|||||||
@@ -110,7 +110,8 @@ internal class MessagingControllerImplTest : ControllerTest() {
|
|||||||
privateMessageFactory.createPrivateMessage(
|
privateMessageFactory.createPrivateMessage(
|
||||||
group.id,
|
group.id,
|
||||||
timestamp,
|
timestamp,
|
||||||
text
|
text,
|
||||||
|
emptyList()
|
||||||
)
|
)
|
||||||
} returns privateMessage
|
} returns privateMessage
|
||||||
every { messagingManager.addLocalMessage(privateMessage) } just runs
|
every { messagingManager.addLocalMessage(privateMessage) } just runs
|
||||||
|
|||||||
Reference in New Issue
Block a user