Private
Public Access
1
0

attachments: Add a generated attachment conversation

This commit is contained in:
2024-04-07 21:06:03 -07:00
parent fa76c7eac1
commit a1349eff1b
7 changed files with 69 additions and 13 deletions

View File

@@ -342,20 +342,34 @@ func GenerateRandomConversation() model.Conversation {
return conversation return conversation
} }
func GenerateRandomMessage(participants []string) model.Message { func randomParticipant(participants []string) *string {
var sender *string = nil
if len(participants) == 1 { if len(participants) == 1 {
if rand.Intn(2) == 0 { 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{ return model.Message{
Text: GenerateRandomMessageBody(), Text: GenerateRandomMessageBody(),
Guid: uuid.New().String(), Guid: uuid.NewString(),
Date: model.Date(time.Now().Add(-1 * time.Duration(rand.Intn(1000000)) * time.Second)), Date: model.Date(time.Now().Add(-1 * time.Duration(rand.Intn(1000000)) * time.Second)),
Sender: sender, 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},
}
}

View File

@@ -1,8 +1,9 @@
package model package model
import ( import (
"github.com/rs/zerolog"
"time" "time"
"github.com/rs/zerolog"
) )
type Message struct { type Message struct {
@@ -12,7 +13,8 @@ type Message struct {
Date Date `json:"date"` Date Date `json:"date"`
// Map of attachment GUID to attachment metadata // 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 { type AttributionInfo struct {

View File

@@ -20,8 +20,13 @@ type Prompt struct {
func (p *Prompt) conversationForGUID(guid string) (*model.Conversation, error) { func (p *Prompt) conversationForGUID(guid string) (*model.Conversation, error) {
if guid == "*" { if guid == "*" {
// This means any conversation: return the first one // This means any conversation: return the first one (that's not the special Attachments convo)
return &p.server.SortedConversations()[0], nil 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) return p.server.ConversationForGUID(guid)
@@ -196,9 +201,11 @@ func (p *Prompt) StartInteractive() error {
return nil return nil
default: default:
if len(line) > 0 {
fmt.Printf("Unknown command: %s\n", line) fmt.Printf("Unknown command: %s\n", line)
} }
} }
}
return nil return nil
} }

6
resources/resources.go Normal file
View File

@@ -0,0 +1,6 @@
package resources
import _ "embed"
//go:embed sedona.jpg
var TestAttachmentData []byte

BIN
resources/sedona.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 575 KiB

View File

@@ -1,17 +1,24 @@
package server package server
import ( import (
"bytes"
_ "embed"
"io" "io"
"os" "os"
"path" "path"
"sort" "sort"
"time"
"code.severnaya.net/kordophone-mock/v2/data" "code.severnaya.net/kordophone-mock/v2/data"
"code.severnaya.net/kordophone-mock/v2/model" "code.severnaya.net/kordophone-mock/v2/model"
"code.severnaya.net/kordophone-mock/v2/resources"
"github.com/google/uuid"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
const VERSION = "KordophoneMock-2.6" const VERSION string = "KordophoneMock-2.6"
var ATTACHMENT_CONVO_DISP_NAME string = "Attachments"
const ( const (
AUTH_USERNAME = "test" AUTH_USERNAME = "test"
@@ -129,6 +136,26 @@ func (s *Server) PopulateWithTestData() {
convo.LastMessagePreview = lastMessage.Text 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 s.conversations = cs
} }

View File

@@ -312,7 +312,7 @@ func (e MessageUpdateError) Error() string {
} }
func TestUpdatesWebsocket(t *testing.T) { func TestUpdatesWebsocket(t *testing.T) {
s := web.NewMockHTTPServer(web.MockHTTPServerConfiguration{AuthEnabled: true}) s := web.NewMockHTTPServer(web.MockHTTPServerConfiguration{AuthEnabled: false})
httpServer := httptest.NewServer(s) httpServer := httptest.NewServer(s)
// Mock conversation // Mock conversation