From 18af7eae8133bb72f995f3ea90de204d460e1222 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 12 May 2024 22:00:22 +0800 Subject: [PATCH] :sparkles: Encrypted channels --- pkg/models/channels.go | 1 + pkg/models/messages.go | 21 +++++++++------------ pkg/server/channels_api.go | 8 +++++--- pkg/server/messages_api.go | 25 ++++++------------------- pkg/services/channels.go | 8 ++++++-- 5 files changed, 27 insertions(+), 36 deletions(-) diff --git a/pkg/models/channels.go b/pkg/models/channels.go index cda9f0d..ad14b2c 100644 --- a/pkg/models/channels.go +++ b/pkg/models/channels.go @@ -19,6 +19,7 @@ type Channel struct { Type ChannelType `json:"type"` Account Account `json:"account"` AccountID uint `json:"account_id"` + IsEncrypted bool `json:"is_encrypted"` Realm Realm `json:"realm"` RealmID *uint `json:"realm_id"` diff --git a/pkg/models/messages.go b/pkg/models/messages.go index 59b2502..11375e1 100644 --- a/pkg/models/messages.go +++ b/pkg/models/messages.go @@ -1,18 +1,15 @@ package models -import "gorm.io/datatypes" - type Message struct { BaseModel - Content []byte `json:"content"` - Metadata datatypes.JSONMap `json:"metadata"` - Type string `json:"type"` - Attachments []Attachment `json:"attachments"` - Channel Channel `json:"channel"` - Sender ChannelMember `json:"sender"` - ReplyID *uint `json:"reply_id"` - ReplyTo *Message `json:"reply_to" gorm:"foreignKey:ReplyID"` - ChannelID uint `json:"channel_id"` - SenderID uint `json:"sender_id"` + Content []byte `json:"content"` + Type string `json:"type"` + Attachments []Attachment `json:"attachments"` + Channel Channel `json:"channel"` + Sender ChannelMember `json:"sender"` + ReplyID *uint `json:"reply_id"` + ReplyTo *Message `json:"reply_to" gorm:"foreignKey:ReplyID"` + ChannelID uint `json:"channel_id"` + SenderID uint `json:"sender_id"` } diff --git a/pkg/server/channels_api.go b/pkg/server/channels_api.go index 3f8e437..68597ab 100644 --- a/pkg/server/channels_api.go +++ b/pkg/server/channels_api.go @@ -99,6 +99,7 @@ func createChannel(c *fiber.Ctx) error { Alias string `json:"alias" validate:"required,lowercase,min=4,max=32"` Name string `json:"name" validate:"required"` Description string `json:"description"` + IsEncrypted bool `json:"is_encrypted"` } if err := BindAndValidate(c, &data); err != nil { @@ -121,9 +122,9 @@ func createChannel(c *fiber.Ctx) error { var err error var channel models.Channel if realm != nil { - channel, err = services.NewChannel(user, data.Alias, data.Name, data.Description, realm.ID) + channel, err = services.NewChannel(user, data.Alias, data.Name, data.Description, data.IsEncrypted, realm.ID) } else { - channel, err = services.NewChannel(user, data.Alias, data.Name, data.Description) + channel, err = services.NewChannel(user, data.Alias, data.Name, data.Description, data.IsEncrypted) } if err != nil { @@ -141,6 +142,7 @@ func editChannel(c *fiber.Ctx) error { Alias string `json:"alias" validate:"required,min=4,max=32"` Name string `json:"name" validate:"required"` Description string `json:"description"` + IsEncrypted bool `json:"is_encrypted"` } if err := BindAndValidate(c, &data); err != nil { @@ -166,7 +168,7 @@ func editChannel(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusNotFound, err.Error()) } - channel, err := services.EditChannel(channel, data.Alias, data.Name, data.Description) + channel, err := services.EditChannel(channel, data.Alias, data.Name, data.Description, data.IsEncrypted) if err != nil { return fiber.NewError(fiber.StatusBadRequest, err.Error()) } diff --git a/pkg/server/messages_api.go b/pkg/server/messages_api.go index b2bcce7..597b7ca 100644 --- a/pkg/server/messages_api.go +++ b/pkg/server/messages_api.go @@ -1,7 +1,6 @@ package server import ( - "encoding/base64" "encoding/json" "fmt" "git.solsynth.dev/hydrogen/messaging/pkg/database" @@ -48,7 +47,6 @@ func newMessage(c *fiber.Ctx) error { var data struct { Type string `json:"type" validate:"required"` Content map[string]any `json:"content"` - Metadata map[string]any `json:"metadata"` Attachments []models.Attachment `json:"attachments"` ReplyTo *uint `json:"reply_to"` } @@ -74,22 +72,17 @@ func newMessage(c *fiber.Ctx) error { } } - var encodedContent []byte - if raw, err := json.Marshal(data.Content); err != nil { + rawContent, err := json.Marshal(data.Content) + if err != nil { return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("invalid message content, unable to encode: %v", err)) - } else { - encoder := base64.StdEncoding - encodedContent = make([]byte, encoder.EncodedLen(len(raw))) - encoder.Encode(encodedContent, raw) } message := models.Message{ - Content: encodedContent, + Content: rawContent, Sender: member, Channel: channel, ChannelID: channel.ID, SenderID: member.ID, - Metadata: data.Metadata, Attachments: data.Attachments, Type: data.Type, } @@ -119,7 +112,6 @@ func editMessage(c *fiber.Ctx) error { var data struct { Type string `json:"type" validate:"required"` Content map[string]any `json:"content"` - Metadata map[string]any `json:"metadata"` Attachments []models.Attachment `json:"attachments"` ReplyTo *uint `json:"reply_to"` } @@ -148,18 +140,13 @@ func editMessage(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusNotFound, err.Error()) } - var encodedContent []byte - if raw, err := json.Marshal(data.Content); err != nil { + rawContent, err := json.Marshal(data.Content) + if err != nil { return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("invalid message content, unable to encode: %v", err)) - } else { - encoder := base64.StdEncoding - encodedContent = make([]byte, encoder.EncodedLen(len(raw))) - encoder.Encode(encodedContent, raw) } message.Attachments = data.Attachments - message.Metadata = data.Metadata - message.Content = encodedContent + message.Content = rawContent message.Type = data.Type message, err = services.EditMessage(message) diff --git a/pkg/services/channels.go b/pkg/services/channels.go index a68c21a..a3c189e 100644 --- a/pkg/services/channels.go +++ b/pkg/services/channels.go @@ -134,11 +134,12 @@ func ListAvailableChannel(user models.Account, realmId ...uint) ([]models.Channe return channels, nil } -func NewChannel(user models.Account, alias, name, description string, realmId ...uint) (models.Channel, error) { +func NewChannel(user models.Account, alias, name, description string, isEncrypted bool, realmId ...uint) (models.Channel, error) { channel := models.Channel{ Alias: alias, Name: name, Description: description, + IsEncrypted: isEncrypted, AccountID: user.ID, Members: []models.ChannelMember{ {AccountID: user.ID}, @@ -153,10 +154,13 @@ func NewChannel(user models.Account, alias, name, description string, realmId .. return channel, err } -func EditChannel(channel models.Channel, alias, name, description string) (models.Channel, error) { +func EditChannel(channel models.Channel, alias, name, description string, isEncrypted bool) (models.Channel, error) { channel.Alias = alias channel.Name = name channel.Description = description + if !channel.IsEncrypted { + channel.IsEncrypted = isEncrypted + } err := database.C.Save(&channel).Error