-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement codecs for attachment, reaction, and reply
- Loading branch information
1 parent
f51dc54
commit 7ac8862
Showing
11 changed files
with
402 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:xmtp_proto/xmtp_proto.dart' as xmtp; | ||
|
||
import 'codec.dart'; | ||
|
||
final contentTypeAttachment = xmtp.ContentTypeId( | ||
authorityId: "xmtp.org", | ||
typeId: "attachment", | ||
versionMajor: 1, | ||
versionMinor: 0, | ||
); | ||
|
||
/// This is a file attached as the message content. | ||
/// | ||
/// Note: this is limited to small files that can fit in the message payload. | ||
/// For larger files, use [RemoteAttachment]. | ||
class Attachment { | ||
final String filename; | ||
final String mimeType; | ||
final List<int> data; | ||
|
||
Attachment(this.filename, this.mimeType, this.data); | ||
} | ||
|
||
/// This is a [Codec] that encodes a file attached as the message content. | ||
class AttachmentCodec extends Codec<Attachment> { | ||
@override | ||
xmtp.ContentTypeId get contentType => contentTypeAttachment; | ||
|
||
@override | ||
Future<Attachment> decode(xmtp.EncodedContent encoded) async => | ||
Attachment( | ||
encoded.parameters["filename"] ?? "", | ||
encoded.parameters["mimeType"] ?? "", | ||
encoded.content, | ||
); | ||
|
||
@override | ||
Future<xmtp.EncodedContent> encode(Attachment decoded) async => xmtp.EncodedContent( | ||
type: contentTypeAttachment, | ||
parameters: { | ||
"filename": decoded.filename, | ||
"mimeType": decoded.mimeType, | ||
}, | ||
content: decoded.data, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import 'dart:convert'; | ||
|
||
import 'package:xmtp_proto/xmtp_proto.dart' as xmtp; | ||
|
||
import 'codec.dart'; | ||
|
||
final contentTypeReaction = xmtp.ContentTypeId( | ||
authorityId: "xmtp.org", | ||
typeId: "reaction", | ||
versionMajor: 1, | ||
versionMinor: 0, | ||
); | ||
|
||
enum ReactionAction { | ||
added, | ||
removed, | ||
} | ||
|
||
enum ReactionSchema { | ||
unicode, | ||
shortcode, | ||
custom, | ||
} | ||
|
||
/// This is a reaction to another [reference] message. | ||
class Reaction { | ||
final String reference; | ||
final ReactionAction action; | ||
final ReactionSchema schema; | ||
final String content; | ||
|
||
Reaction(this.reference, this.action, this.schema, this.content); | ||
|
||
String toJson() => json.encode({ | ||
"reference": reference, | ||
"action": action.toString().split(".").last, | ||
"schema": schema.toString().split(".").last, | ||
"content": content, | ||
}); | ||
|
||
static Reaction fromJson(String json) { | ||
var v = jsonDecode(json); | ||
return Reaction( | ||
v["reference"], | ||
ReactionAction.values | ||
.firstWhere((e) => e.toString().split(".").last == v["action"]), | ||
ReactionSchema.values | ||
.firstWhere((e) => e.toString().split(".").last == v["schema"]), | ||
v["content"], | ||
); | ||
} | ||
} | ||
|
||
/// This is a [Codec] that encodes a reaction to another message. | ||
class ReactionCodec extends Codec<Reaction> { | ||
@override | ||
xmtp.ContentTypeId get contentType => contentTypeReaction; | ||
|
||
@override | ||
Future<Reaction> decode(xmtp.EncodedContent encoded) async => | ||
Reaction.fromJson(utf8.decode(encoded.content)); | ||
|
||
@override | ||
Future<xmtp.EncodedContent> encode(Reaction decoded) async => | ||
xmtp.EncodedContent( | ||
type: contentTypeReaction, | ||
content: utf8.encode(decoded.toJson()), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import 'package:xmtp_proto/xmtp_proto.dart' as xmtp; | ||
|
||
import 'codec.dart'; | ||
import 'decoded.dart'; | ||
|
||
final contentTypeReply = xmtp.ContentTypeId( | ||
authorityId: "xmtp.org", | ||
typeId: "reply", | ||
versionMajor: 1, | ||
versionMinor: 0, | ||
); | ||
|
||
/// This is a reply to another [reference] message. | ||
class Reply { | ||
/// This is the message ID of the parent message. | ||
/// See [DecodedMessage.id] | ||
final String reference; | ||
final DecodedContent content; | ||
|
||
Reply(this.reference, this.content); | ||
} | ||
|
||
extension on xmtp.ContentTypeId { | ||
String toText() => "$authorityId/$typeId:$versionMajor.$versionMinor"; | ||
} | ||
|
||
/// This is a [Codec] that encodes a reply to another message. | ||
class ReplyCodec extends NestedContentCodec<Reply> { | ||
|
||
@override | ||
xmtp.ContentTypeId get contentType => contentTypeReply; | ||
|
||
@override | ||
Future<Reply> decode(xmtp.EncodedContent encoded) async => Reply( | ||
encoded.parameters["reference"] ?? "", | ||
await registry.decode(xmtp.EncodedContent.fromBuffer(encoded.content)), | ||
); | ||
|
||
@override | ||
Future<xmtp.EncodedContent> encode(Reply decoded) async => | ||
xmtp.EncodedContent( | ||
type: contentTypeReply, | ||
parameters: { | ||
"reference": decoded.reference, | ||
// TODO: cut when we know nothing looks here for the content type | ||
"contentType": decoded.content.contentType.toText(), | ||
}, | ||
content: (await registry.encode(decoded.content)).writeToBuffer(), | ||
); | ||
} |
Oops, something went wrong.