From f20681adab2b3e51367e41fb82e5fb99430ff11c Mon Sep 17 00:00:00 2001 From: ycc Date: Thu, 29 Feb 2024 21:03:15 +0100 Subject: [PATCH] Store messages with DbMessage Type --- client/dbmessage.go | 34 +++++++ client/message.go | 10 +- client/messagestorage.go | 22 ++-- client/messagestorage_test.go | 6 +- client/peer.go | 4 +- messages.pb.go | 183 ++++++++++++++++++++++++++++++++-- pb/messages.proto | 23 +++-- 7 files changed, 244 insertions(+), 38 deletions(-) create mode 100644 client/dbmessage.go diff --git a/client/dbmessage.go b/client/dbmessage.go new file mode 100644 index 0000000..7380090 --- /dev/null +++ b/client/dbmessage.go @@ -0,0 +1,34 @@ +package client + +import ( + "forge.redroom.link/yves/meowlib" +) + +func DbMessageToInternalUserMessage(id int64, dbFile string, dbm *meowlib.DbMessage) *InternalUserMessage { + var ium InternalUserMessage + ium.Dbid = id + ium.Dbfile = dbFile + ium.Outbound = dbm.Outbound + ium.Message = string(dbm.Data) + ium.ConversationStatus = dbm.Status + ium.Contact = dbm.Contact + ium.CurrentLocation = dbm.CurrentLocation + ium.Messagetype = dbm.Type + ium.Appdata = dbm.Appdata + ium.FilePaths = dbm.FilePaths + return &ium +} + +func UserMessageToDbMessage(outbound bool, um *meowlib.UserMessage, filepaths []string) *meowlib.DbMessage { + var dbm meowlib.DbMessage + dbm.Outbound = outbound + dbm.Type = um.Type + dbm.Data = um.Data + dbm.Appdata = um.Appdata + dbm.Contact = um.Contact + dbm.CurrentLocation = um.CurrentLocation + dbm.Status = um.Status + dbm.FilePaths = filepaths + return &dbm + +} diff --git a/client/message.go b/client/message.go index d4762fc..1e563dc 100644 --- a/client/message.go +++ b/client/message.go @@ -8,15 +8,15 @@ const ( ) type InternalUserMessage struct { - Direction int // 0 = inbound, 1 = outbound + Outbound bool // 0 = inbound, 1 = outbound Messagetype string Message string ConversationStatus *meowlib.ConversationStatus Contact *meowlib.ContactCard //Group group FilePaths []string - CurrentLocation meowlib.Location - appdata []byte + CurrentLocation *meowlib.Location + Appdata []byte Dbfile string Dbid int64 } @@ -25,9 +25,9 @@ type InternalUserMessage struct { func InternalUserMessageFromUserMessage(peer *Peer, msg *meowlib.UserMessage) *InternalUserMessage { iu := new(InternalUserMessage) if peer.ContactPublicKey == msg.From { - iu.Direction = Inbound + iu.Outbound = false } else { - iu.Direction = Outbound + iu.Outbound = true } iu.Messagetype = msg.Type iu.Message = string(msg.Data) diff --git a/client/messagestorage.go b/client/messagestorage.go index 1246c9a..87c5e35 100644 --- a/client/messagestorage.go +++ b/client/messagestorage.go @@ -12,7 +12,7 @@ import ( "google.golang.org/protobuf/proto" ) -func StoreMessage(peer *Peer, usermessage *meowlib.UserMessage, password string) error { +func StoreMessage(peer *Peer, usermessage *meowlib.UserMessage, filenames []string, password string) error { var dbid string // If no db/no ID create DB + Tablz // TODO : if file size > X new db @@ -52,8 +52,14 @@ func StoreMessage(peer *Peer, usermessage *meowlib.UserMessage, password string) f.Data = []byte(hiddenFilename) } } + outbound := true + if usermessage.From == peer.ContactPublicKey { + outbound = false + } + // Convert UserMessage to DbMessage + dbm := UserMessageToDbMessage(outbound, usermessage, filenames) // Encrypt message - out, err := proto.Marshal(usermessage) + out, err := proto.Marshal(dbm) if err != nil { return err } @@ -98,7 +104,7 @@ func GetNewMessages(peer *Peer, lastDbId int, password string) ([]*InternalUserM for rows.Next() { var ium *InternalUserMessage - var um meowlib.UserMessage + var dbm meowlib.DbMessage var id int64 var m []byte err = rows.Scan(&id, &m) @@ -109,12 +115,12 @@ func GetNewMessages(peer *Peer, lastDbId int, password string) ([]*InternalUserM if err != nil { return nil, err } - err = proto.Unmarshal(decdata, &um) + err = proto.Unmarshal(decdata, &dbm) if err != nil { return nil, err } - ium = InternalUserMessageFromUserMessage(peer, &um) + ium = DbMessageToInternalUserMessage(id, peer.DbIds[fileidx], &dbm) ium.Dbid = id ium.Dbfile = peer.DbIds[fileidx] messages = append(messages, ium) @@ -164,7 +170,7 @@ func GetMessagesHistory(peer *Peer, inAppMsgCount int, lastDbId int, wantMore in for rows.Next() { var ium *InternalUserMessage - var um meowlib.UserMessage + var dbm meowlib.DbMessage var id int64 var m []byte err = rows.Scan(&id, &m) @@ -175,12 +181,12 @@ func GetMessagesHistory(peer *Peer, inAppMsgCount int, lastDbId int, wantMore in if err != nil { return nil, err } - err = proto.Unmarshal(decdata, &um) + err = proto.Unmarshal(decdata, &dbm) if err != nil { return nil, err } - ium = InternalUserMessageFromUserMessage(peer, &um) + ium = DbMessageToInternalUserMessage(id, peer.DbIds[fileidx], &dbm) ium.Dbid = id ium.Dbfile = peer.DbIds[fileidx] messages = append(messages, *ium) diff --git a/client/messagestorage_test.go b/client/messagestorage_test.go index 5f45492..439de42 100644 --- a/client/messagestorage_test.go +++ b/client/messagestorage_test.go @@ -14,7 +14,7 @@ func TestStoreMessage(t *testing.T) { id := createId() var um meowlib.UserMessage um.Data = []byte("blabla") - err := StoreMessage(&id.Peers[0], &um, GetConfig().memoryPassword) + err := StoreMessage(&id.Peers[0], &um, []string{}, GetConfig().memoryPassword) if err != nil { log.Fatal(err) } @@ -24,7 +24,7 @@ func TestStoreMessage(t *testing.T) { } // Checks assert.Equal(t, len(messages), 1, "not 1 message") - assert.Equal(t, messages[0].Message, um.Data, "not 1 message") + assert.Equal(t, messages[0].Message, string(um.Data), "not 1 message") // Cleanup if exists("test.id") { os.Remove("test.id") @@ -45,7 +45,7 @@ func TestManyStoreMessage(t *testing.T) { for i := 1; i < 100; i++ { var um meowlib.UserMessage um.Data = []byte(randomLenString(20, 200)) - err := StoreMessage(&id.Peers[0], &um, GetConfig().memoryPassword) + err := StoreMessage(&id.Peers[0], &um, []string{}, GetConfig().memoryPassword) if err != nil { log.Fatal(err) } diff --git a/client/peer.go b/client/peer.go index c039467..d9e951b 100644 --- a/client/peer.go +++ b/client/peer.go @@ -287,8 +287,8 @@ func (p *Peer) GetDbPassword() string { return p.dbPassword } -func (p *Peer) StoreMessage(msg *meowlib.UserMessage) error { - return StoreMessage(p, msg, p.GetDbPassword()) +func (p *Peer) StoreMessage(msg *meowlib.UserMessage, filenames []string) error { + return StoreMessage(p, msg, filenames, p.GetDbPassword()) } func (p *Peer) UpdateMessage(msg InternalUserMessage) error { diff --git a/messages.pb.go b/messages.pb.go index 07d8397..df6c772 100644 --- a/messages.pb.go +++ b/messages.pb.go @@ -1457,6 +1457,125 @@ func (x *Location) GetAltitude() int32 { return 0 } +type DbMessage struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Outbound bool `protobuf:"varint,1,opt,name=outbound,proto3" json:"outbound,omitempty"` // direction of the message + Type string `protobuf:"bytes,2,opt,name=type,proto3" json:"type,omitempty"` + Data []byte `protobuf:"bytes,3,opt,name=data,proto3" json:"data,omitempty"` // text data + Status *ConversationStatus `protobuf:"bytes,4,opt,name=status,proto3" json:"status,omitempty"` + Contact *ContactCard `protobuf:"bytes,5,opt,name=contact,proto3" json:"contact,omitempty"` + Group *Group `protobuf:"bytes,6,opt,name=group,proto3" json:"group,omitempty"` + FilePaths []string `protobuf:"bytes,7,rep,name=file_paths,json=filePaths,proto3" json:"file_paths,omitempty"` + CurrentLocation *Location `protobuf:"bytes,8,opt,name=current_location,json=currentLocation,proto3" json:"current_location,omitempty"` + Appdata []byte `protobuf:"bytes,9,opt,name=appdata,proto3" json:"appdata,omitempty"` + Invitation *Invitation `protobuf:"bytes,10,opt,name=invitation,proto3" json:"invitation,omitempty"` +} + +func (x *DbMessage) Reset() { + *x = DbMessage{} + if protoimpl.UnsafeEnabled { + mi := &file_messages_proto_msgTypes[17] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *DbMessage) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*DbMessage) ProtoMessage() {} + +func (x *DbMessage) ProtoReflect() protoreflect.Message { + mi := &file_messages_proto_msgTypes[17] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use DbMessage.ProtoReflect.Descriptor instead. +func (*DbMessage) Descriptor() ([]byte, []int) { + return file_messages_proto_rawDescGZIP(), []int{17} +} + +func (x *DbMessage) GetOutbound() bool { + if x != nil { + return x.Outbound + } + return false +} + +func (x *DbMessage) GetType() string { + if x != nil { + return x.Type + } + return "" +} + +func (x *DbMessage) GetData() []byte { + if x != nil { + return x.Data + } + return nil +} + +func (x *DbMessage) GetStatus() *ConversationStatus { + if x != nil { + return x.Status + } + return nil +} + +func (x *DbMessage) GetContact() *ContactCard { + if x != nil { + return x.Contact + } + return nil +} + +func (x *DbMessage) GetGroup() *Group { + if x != nil { + return x.Group + } + return nil +} + +func (x *DbMessage) GetFilePaths() []string { + if x != nil { + return x.FilePaths + } + return nil +} + +func (x *DbMessage) GetCurrentLocation() *Location { + if x != nil { + return x.CurrentLocation + } + return nil +} + +func (x *DbMessage) GetAppdata() []byte { + if x != nil { + return x.Appdata + } + return nil +} + +func (x *DbMessage) GetInvitation() *Invitation { + if x != nil { + return x.Invitation + } + return nil +} + var File_messages_proto protoreflect.FileDescriptor var file_messages_proto_rawDesc = []byte{ @@ -1684,10 +1803,34 @@ var file_messages_proto_rawDesc = []byte{ 0x09, 0x6c, 0x6f, 0x6e, 0x67, 0x69, 0x74, 0x75, 0x64, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x02, 0x52, 0x09, 0x6c, 0x6f, 0x6e, 0x67, 0x69, 0x74, 0x75, 0x64, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x61, 0x6c, 0x74, 0x69, 0x74, 0x75, 0x64, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x08, 0x61, - 0x6c, 0x74, 0x69, 0x74, 0x75, 0x64, 0x65, 0x42, 0x21, 0x5a, 0x1f, 0x66, 0x6f, 0x72, 0x67, 0x65, - 0x2e, 0x72, 0x65, 0x64, 0x72, 0x6f, 0x6f, 0x6d, 0x2e, 0x6c, 0x69, 0x6e, 0x6b, 0x2f, 0x79, 0x76, - 0x65, 0x73, 0x2f, 0x6d, 0x65, 0x6f, 0x77, 0x6c, 0x69, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x33, + 0x6c, 0x74, 0x69, 0x74, 0x75, 0x64, 0x65, 0x22, 0x86, 0x03, 0x0a, 0x09, 0x44, 0x62, 0x4d, 0x65, + 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x6f, 0x75, 0x74, 0x62, 0x6f, 0x75, 0x6e, + 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x08, 0x6f, 0x75, 0x74, 0x62, 0x6f, 0x75, 0x6e, + 0x64, 0x12, 0x12, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x03, 0x20, + 0x01, 0x28, 0x0c, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x12, 0x33, 0x0a, 0x06, 0x73, 0x74, 0x61, + 0x74, 0x75, 0x73, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x1b, 0x2e, 0x6d, 0x65, 0x6f, 0x77, + 0x6c, 0x69, 0x62, 0x2e, 0x43, 0x6f, 0x6e, 0x76, 0x65, 0x72, 0x73, 0x61, 0x74, 0x69, 0x6f, 0x6e, + 0x53, 0x74, 0x61, 0x74, 0x75, 0x73, 0x52, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75, 0x73, 0x12, 0x2e, + 0x0a, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x61, 0x63, 0x74, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0b, 0x32, + 0x14, 0x2e, 0x6d, 0x65, 0x6f, 0x77, 0x6c, 0x69, 0x62, 0x2e, 0x43, 0x6f, 0x6e, 0x74, 0x61, 0x63, + 0x74, 0x43, 0x61, 0x72, 0x64, 0x52, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x61, 0x63, 0x74, 0x12, 0x24, + 0x0a, 0x05, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, + 0x6d, 0x65, 0x6f, 0x77, 0x6c, 0x69, 0x62, 0x2e, 0x47, 0x72, 0x6f, 0x75, 0x70, 0x52, 0x05, 0x67, + 0x72, 0x6f, 0x75, 0x70, 0x12, 0x1d, 0x0a, 0x0a, 0x66, 0x69, 0x6c, 0x65, 0x5f, 0x70, 0x61, 0x74, + 0x68, 0x73, 0x18, 0x07, 0x20, 0x03, 0x28, 0x09, 0x52, 0x09, 0x66, 0x69, 0x6c, 0x65, 0x50, 0x61, + 0x74, 0x68, 0x73, 0x12, 0x3c, 0x0a, 0x10, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x74, 0x5f, 0x6c, + 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x08, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x11, 0x2e, + 0x6d, 0x65, 0x6f, 0x77, 0x6c, 0x69, 0x62, 0x2e, 0x4c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, + 0x52, 0x0f, 0x63, 0x75, 0x72, 0x72, 0x65, 0x6e, 0x74, 0x4c, 0x6f, 0x63, 0x61, 0x74, 0x69, 0x6f, + 0x6e, 0x12, 0x18, 0x0a, 0x07, 0x61, 0x70, 0x70, 0x64, 0x61, 0x74, 0x61, 0x18, 0x09, 0x20, 0x01, + 0x28, 0x0c, 0x52, 0x07, 0x61, 0x70, 0x70, 0x64, 0x61, 0x74, 0x61, 0x12, 0x33, 0x0a, 0x0a, 0x69, + 0x6e, 0x76, 0x69, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x18, 0x0a, 0x20, 0x01, 0x28, 0x0b, 0x32, + 0x13, 0x2e, 0x6d, 0x65, 0x6f, 0x77, 0x6c, 0x69, 0x62, 0x2e, 0x49, 0x6e, 0x76, 0x69, 0x74, 0x61, + 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x0a, 0x69, 0x6e, 0x76, 0x69, 0x74, 0x61, 0x74, 0x69, 0x6f, 0x6e, + 0x42, 0x21, 0x5a, 0x1f, 0x66, 0x6f, 0x72, 0x67, 0x65, 0x2e, 0x72, 0x65, 0x64, 0x72, 0x6f, 0x6f, + 0x6d, 0x2e, 0x6c, 0x69, 0x6e, 0x6b, 0x2f, 0x79, 0x76, 0x65, 0x73, 0x2f, 0x6d, 0x65, 0x6f, 0x77, + 0x6c, 0x69, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -1702,7 +1845,7 @@ func file_messages_proto_rawDescGZIP() []byte { return file_messages_proto_rawDescData } -var file_messages_proto_msgTypes = make([]protoimpl.MessageInfo, 17) +var file_messages_proto_msgTypes = make([]protoimpl.MessageInfo, 18) var file_messages_proto_goTypes = []interface{}{ (*PackedServerMessage)(nil), // 0: meowlib.PackedServerMessage (*Invitation)(nil), // 1: meowlib.Invitation @@ -1721,6 +1864,7 @@ var file_messages_proto_goTypes = []interface{}{ (*UserMessage)(nil), // 14: meowlib.UserMessage (*File)(nil), // 15: meowlib.File (*Location)(nil), // 16: meowlib.Location + (*DbMessage)(nil), // 17: meowlib.DbMessage } var file_messages_proto_depIdxs = []int32{ 10, // 0: meowlib.Meet.contact_card:type_name -> meowlib.ContactCard @@ -1746,11 +1890,16 @@ var file_messages_proto_depIdxs = []int32{ 15, // 20: meowlib.UserMessage.files:type_name -> meowlib.File 16, // 21: meowlib.UserMessage.currentLocation:type_name -> meowlib.Location 1, // 22: meowlib.UserMessage.invitation:type_name -> meowlib.Invitation - 23, // [23:23] is the sub-list for method output_type - 23, // [23:23] is the sub-list for method input_type - 23, // [23:23] is the sub-list for extension type_name - 23, // [23:23] is the sub-list for extension extendee - 0, // [0:23] is the sub-list for field type_name + 12, // 23: meowlib.DbMessage.status:type_name -> meowlib.ConversationStatus + 10, // 24: meowlib.DbMessage.contact:type_name -> meowlib.ContactCard + 13, // 25: meowlib.DbMessage.group:type_name -> meowlib.Group + 16, // 26: meowlib.DbMessage.current_location:type_name -> meowlib.Location + 1, // 27: meowlib.DbMessage.invitation:type_name -> meowlib.Invitation + 28, // [28:28] is the sub-list for method output_type + 28, // [28:28] is the sub-list for method input_type + 28, // [28:28] is the sub-list for extension type_name + 28, // [28:28] is the sub-list for extension extendee + 0, // [0:28] is the sub-list for field type_name } func init() { file_messages_proto_init() } @@ -1963,6 +2112,18 @@ func file_messages_proto_init() { return nil } } + file_messages_proto_msgTypes[17].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*DbMessage); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } } type x struct{} out := protoimpl.TypeBuilder{ @@ -1970,7 +2131,7 @@ func file_messages_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_messages_proto_rawDesc, NumEnums: 0, - NumMessages: 17, + NumMessages: 18, NumExtensions: 0, NumServices: 0, }, diff --git a/pb/messages.proto b/pb/messages.proto index 781b31b..0d9b7ff 100644 --- a/pb/messages.proto +++ b/pb/messages.proto @@ -158,21 +158,13 @@ message UserMessage { string from = 2; // My public key for that contact string type = 3; bytes data = 4; - ConversationStatus Status = 5; - ContactCard contact = 6; - ServerCard knownServers = 7; - Group group = 8; - repeated File files = 9; - Location currentLocation = 10; - bytes appdata = 11; - Invitation invitation = 12; } @@ -188,4 +180,17 @@ message Location { float latitude=2; float longitude=3; int32 altitude=4; -} \ No newline at end of file +} + +message DbMessage { + bool outbound = 1; // direction of the message + string type = 2; + bytes data = 3; // text data + ConversationStatus status = 4; + ContactCard contact = 5; + Group group = 6; + repeated string file_paths = 7; + Location current_location = 8; + bytes appdata = 9; + Invitation invitation = 10; +}