attachments: Add a generated attachment conversation
This commit is contained in:
@@ -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},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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,9 +201,11 @@ func (p *Prompt) StartInteractive() error {
|
||||
return nil
|
||||
|
||||
default:
|
||||
if len(line) > 0 {
|
||||
fmt.Printf("Unknown command: %s\n", line)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
6
resources/resources.go
Normal file
6
resources/resources.go
Normal file
@@ -0,0 +1,6 @@
|
||||
package resources
|
||||
|
||||
import _ "embed"
|
||||
|
||||
//go:embed sedona.jpg
|
||||
var TestAttachmentData []byte
|
||||
BIN
resources/sedona.jpg
Normal file
BIN
resources/sedona.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 575 KiB |
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user