From 55aa5726f2dfdfa860f9c243a0c850d3df854b52 Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Tue, 11 Sep 2018 23:32:38 +0200 Subject: [PATCH] Stricter message types with MessageType* constants --- sam/service/attachment.go | 15 ++++++++---- sam/types/message.go | 50 ++++++++++++++++++++++++++++++++++++++- sam/websocket/payload.go | 2 +- 3 files changed, 61 insertions(+), 6 deletions(-) diff --git a/sam/service/attachment.go b/sam/service/attachment.go index fce84b52c..1d77db7b0 100644 --- a/sam/service/attachment.go +++ b/sam/service/attachment.go @@ -68,7 +68,7 @@ func (svc attachment) OpenPreview(att *types.Attachment) (io.ReadSeeker, error) func (svc attachment) LoadFromMessages(ctx context.Context, mm types.MessageSet) (err error) { var ids []uint64 mm.Walk(func(m *types.Message) error { - if m.Type == "attachment" { + if m.Type == types.MessageTypeAttachment || m.Type == types.MessageTypeInlineImage { ids = append(ids, m.ID) } return nil @@ -109,7 +109,9 @@ func (svc attachment) Create(ctx context.Context, channelId uint64, name string, // Extract extension but make sure path.Ext is not confused by any leading/trailing dots var ext = strings.Trim(path.Ext(strings.Trim(name, ".")), ".") - // @todo extract mimetype and update att.Mimetype + if err := svc.extractMeta(att, fh); err != nil { + // @todo logmeta extraction failure + } log.Printf("Processing uploaded file (name: %s, size: %d, mime: %s)", att.Name, att.Size, att.Mimetype) @@ -134,8 +136,13 @@ func (svc attachment) Create(ctx context.Context, channelId uint64, name string, msg := &types.Message{ Message: name, - Type: "attachment", + Type: types.MessageTypeAttachment, ChannelID: channelId, + UserID: currentUserID, + } + + if strings.HasPrefix(att.Mimetype, "image/") { + msg.Type = types.MessageTypeInlineImage } // Create the first message, doing this directly with repository to circumvent @@ -148,7 +155,7 @@ func (svc attachment) Create(ctx context.Context, channelId uint64, name string, return } - log.Printf("File %s (id: %s) attached to message (id: %d)", att.Name, att.ID, msg.ID) + log.Printf("File %s (id: %d) attached to message (id: %d)", att.Name, att.ID, msg.ID) return }) diff --git a/sam/types/message.go b/sam/types/message.go index 04bc90f0b..8a1dc1ee1 100644 --- a/sam/types/message.go +++ b/sam/types/message.go @@ -1,13 +1,14 @@ package types import ( + "database/sql/driver" "time" ) type ( Message struct { ID uint64 `json:"id" db:"id"` - Type string `json:"type" db:"type"` + Type MessageType `json:"type" db:"type"` Message string `json:"message" db:"message"` UserID uint64 `json:"userId" db:"rel_user"` ChannelID uint64 `json:"channelId" db:"rel_channel"` @@ -27,6 +28,15 @@ type ( UntilMessageID uint64 Limit uint } + + MessageType string +) + +const ( + MessageTypeSimpleMessage MessageType = "" + MessageTypeChannelEvent MessageType = "channelEvent" + MessageTypeInlineImage MessageType = "inlineImage" + MessageTypeAttachment MessageType = "attachment" ) func (mm MessageSet) Walk(w func(*Message) error) (err error) { @@ -48,3 +58,41 @@ func (mm MessageSet) FindById(ID uint64) *Message { return nil } + +func (mtype MessageType) String() string { + return string(mtype) +} + +func (mtype MessageType) IsValid() bool { + switch mtype { + case MessageTypeSimpleMessage, + MessageTypeChannelEvent, + MessageTypeInlineImage, + MessageTypeAttachment: + return true + } + + return false +} + +//func (mtype *MessageType) Scan(value interface{}) error { +// switch value.(type) { +// case nil: +// *mtype = MessageTypeSimpleMessage +// case []uint8: +// *mtype = MessageType(string(value.([]uint8))) +// if !mtype.IsValid() { +// return errors.Errorf("Can not scan %v into MessageType", value) +// } +// } +// +// return nil +//} + +func (mtype MessageType) Value() (driver.Value, error) { + if mtype == MessageTypeSimpleMessage { + return nil, nil + } + + return mtype.String(), nil +} diff --git a/sam/websocket/payload.go b/sam/websocket/payload.go index 6834f6364..dcb8e3b83 100644 --- a/sam/websocket/payload.go +++ b/sam/websocket/payload.go @@ -10,7 +10,7 @@ func payloadFromMessage(msg *types.Message) *outgoing.Message { Message: msg.Message, ID: uint64toa(msg.ID), ChannelID: uint64toa(msg.ChannelID), - Type: msg.Type, + Type: string(msg.Type), UserID: uint64toa(msg.UserID), ReplyTo: uint64toa(msg.ReplyTo),