3
0

add(messaging): message.Meta is typed struct now

This commit is contained in:
Tit Petric
2019-04-03 15:09:05 +02:00
parent b6b1d5211e
commit 106bbd19ae
+42 -11
View File
@@ -1,25 +1,32 @@
package types
import (
"database/sql/driver"
"time"
"encoding/json"
"database/sql/driver"
"github.com/pkg/errors"
"github.com/crusttech/crust/internal/rules"
systemTypes "github.com/crusttech/crust/system/types"
)
type (
Message struct {
ID uint64 `json:"id" db:"id"`
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"`
ReplyTo uint64 `json:"replyTo" db:"reply_to"`
Replies uint `json:"replies" db:"replies"`
CreatedAt time.Time `json:"createdAt,omitempty" db:"created_at"`
UpdatedAt *time.Time `json:"updatedAt,omitempty" db:"updated_at"`
DeletedAt *time.Time `json:"deletedAt,omitempty" db:"deleted_at"`
ID uint64 `json:"id" db:"id"`
Type MessageType `json:"type" db:"type"`
Message string `json:"message" db:"message"`
Meta *MessageMeta `json:"meta" db:"meta"`
UserID uint64 `json:"userId" db:"rel_user"`
ChannelID uint64 `json:"channelId" db:"rel_channel"`
ReplyTo uint64 `json:"replyTo" db:"reply_to"`
Replies uint `json:"replies" db:"replies"`
CreatedAt time.Time `json:"createdAt,omitempty" db:"created_at"`
UpdatedAt *time.Time `json:"updatedAt,omitempty" db:"updated_at"`
DeletedAt *time.Time `json:"deletedAt,omitempty" db:"deleted_at"`
Attachment *Attachment `json:"attachment,omitempty"`
User *systemTypes.User `json:"user,omitempty"`
Flags MessageFlagSet `json:"flags,omitempty"`
@@ -28,6 +35,12 @@ type (
RepliesFrom []uint64
}
MessageMeta struct {
// Bot users can override username/avatar
Username string `json:"username"`
Avatar string `json:"avatar"`
}
MessageFilter struct {
Query string
@@ -131,3 +144,21 @@ func (m *Message) IsValid() bool {
func (m Message) PermissionResource() rules.Resource {
return ChannelPermissionResource.AppendID(m.ChannelID)
}
func (meta *MessageMeta) Scan(value interface{}) error {
switch value.(type) {
case nil:
*meta = MessageMeta{}
return nil
case []uint8:
if err := json.Unmarshal(value.([]byte), meta); err != nil {
return errors.Wrapf(err, "Can not scan '%v' into Message.Meta", value)
}
return nil
}
return errors.Errorf("Message.Meta: unknown type %T, expected []uint8", value)
}
func (meta *MessageMeta) Value() (driver.Value, error) {
return json.Marshal(meta)
}