✨ Encrypted channels
This commit is contained in:
parent
13fcc64f79
commit
18af7eae81
@ -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"`
|
||||
|
@ -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"`
|
||||
}
|
||||
|
@ -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())
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user