Stricter message types with MessageType* constants
This commit is contained in:
@@ -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
|
||||
})
|
||||
|
||||
+49
-1
@@ -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
|
||||
}
|
||||
|
||||
@@ -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),
|
||||
|
||||
|
||||
Reference in New Issue
Block a user