diff --git a/data/generators.go b/data/generators.go index 7cfe124..99e6eee 100644 --- a/data/generators.go +++ b/data/generators.go @@ -342,20 +342,34 @@ func GenerateRandomConversation() model.Conversation { return conversation } -func GenerateRandomMessage(participants []string) model.Message { - var sender *string = nil +func randomParticipant(participants []string) *string { if len(participants) == 1 { if rand.Intn(2) == 0 { - sender = &participants[0] + return &participants[0] } - } else { - sender = &participants[rand.Intn(len(participants))] } + return &participants[rand.Intn(len(participants))] +} + +func GenerateRandomMessage(participants []string) model.Message { + sender := randomParticipant(participants) + return model.Message{ Text: GenerateRandomMessageBody(), - Guid: uuid.New().String(), + Guid: uuid.NewString(), Date: model.Date(time.Now().Add(-1 * time.Duration(rand.Intn(1000000)) * time.Second)), Sender: sender, } } + +func GenerateAttachmentMessage(participants []string, attachmentGuid string) model.Message { + sender := randomParticipant(participants) + return model.Message{ + Text: "", // todo: try using attachment character here? + Guid: uuid.NewString(), + Date: model.Date(time.Now().Add(-1 * time.Duration(rand.Intn(1000000)) * time.Second)), + Sender: sender, + AttachmentGUIDs: []string{attachmentGuid}, + } +} diff --git a/model/message.go b/model/message.go index d88768a..6f4bb09 100644 --- a/model/message.go +++ b/model/message.go @@ -1,8 +1,9 @@ package model import ( - "github.com/rs/zerolog" "time" + + "github.com/rs/zerolog" ) type Message struct { @@ -12,7 +13,8 @@ type Message struct { Date Date `json:"date"` // Map of attachment GUID to attachment metadata - Attachments *map[string]AttributionInfo `json:"attachmentMetadata,omitempty"` // Optional + AttachmentGUIDs []string `json:"fileTransferGUIDs,omitempty"` + AttachmentMetadata *map[string]AttributionInfo `json:"attachmentMetadata,omitempty"` // Optional } type AttributionInfo struct { diff --git a/prompt/prompt.go b/prompt/prompt.go index 0e7959f..111c176 100644 --- a/prompt/prompt.go +++ b/prompt/prompt.go @@ -20,8 +20,13 @@ type Prompt struct { func (p *Prompt) conversationForGUID(guid string) (*model.Conversation, error) { if guid == "*" { - // This means any conversation: return the first one - return &p.server.SortedConversations()[0], nil + // This means any conversation: return the first one (that's not the special Attachments convo) + convo := p.server.SortedConversations()[0] + if *convo.DisplayName == server.ATTACHMENT_CONVO_DISP_NAME { + return &p.server.SortedConversations()[1], nil + } + + return &convo, nil } return p.server.ConversationForGUID(guid) @@ -196,7 +201,9 @@ func (p *Prompt) StartInteractive() error { return nil default: - fmt.Printf("Unknown command: %s\n", line) + if len(line) > 0 { + fmt.Printf("Unknown command: %s\n", line) + } } } diff --git a/resources/resources.go b/resources/resources.go new file mode 100644 index 0000000..b38ce15 --- /dev/null +++ b/resources/resources.go @@ -0,0 +1,6 @@ +package resources + +import _ "embed" + +//go:embed sedona.jpg +var TestAttachmentData []byte diff --git a/resources/sedona.jpg b/resources/sedona.jpg new file mode 100644 index 0000000..a57f9b0 Binary files /dev/null and b/resources/sedona.jpg differ diff --git a/server/server.go b/server/server.go index 5f4cfd1..0bd1a0b 100644 --- a/server/server.go +++ b/server/server.go @@ -1,17 +1,24 @@ package server import ( + "bytes" + _ "embed" "io" "os" "path" "sort" + "time" "code.severnaya.net/kordophone-mock/v2/data" "code.severnaya.net/kordophone-mock/v2/model" + "code.severnaya.net/kordophone-mock/v2/resources" + "github.com/google/uuid" "github.com/rs/zerolog/log" ) -const VERSION = "KordophoneMock-2.6" +const VERSION string = "KordophoneMock-2.6" + +var ATTACHMENT_CONVO_DISP_NAME string = "Attachments" const ( AUTH_USERNAME = "test" @@ -129,6 +136,26 @@ func (s *Server) PopulateWithTestData() { convo.LastMessagePreview = lastMessage.Text } + // Also add an "attachment" conversation + attachmentConversation := model.Conversation{ + Participants: []string{"Attachments"}, + DisplayName: &ATTACHMENT_CONVO_DISP_NAME, + UnreadCount: 0, + Guid: uuid.New().String(), + Date: model.Date(time.Now()), + } + + cs = append(cs, attachmentConversation) + + reader := bytes.NewReader(resources.TestAttachmentData) + attachmentGUID, err := s.attachmentStore.StoreAttachment("test.jpg", reader) + if err != nil { + log.Fatal().Msgf("Error storing test attachment: %s", err) + } else { + attachmentMessage := data.GenerateAttachmentMessage(attachmentConversation.Participants, *attachmentGUID) + s.AppendMessageToConversation(&attachmentConversation, attachmentMessage) + } + s.conversations = cs } diff --git a/web/server_test.go b/web/server_test.go index ca8d443..70c368b 100644 --- a/web/server_test.go +++ b/web/server_test.go @@ -312,7 +312,7 @@ func (e MessageUpdateError) Error() string { } func TestUpdatesWebsocket(t *testing.T) { - s := web.NewMockHTTPServer(web.MockHTTPServerConfiguration{AuthEnabled: true}) + s := web.NewMockHTTPServer(web.MockHTTPServerConfiguration{AuthEnabled: false}) httpServer := httptest.NewServer(s) // Mock conversation