⬆️ Switch to Paperclip
This commit is contained in:
@ -1,61 +0,0 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
|
||||
"git.solsynth.dev/hydrogen/messaging/pkg/models"
|
||||
"git.solsynth.dev/hydrogen/messaging/pkg/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func readAttachment(c *fiber.Ctx) error {
|
||||
id := c.Params("fileId")
|
||||
basepath := viper.GetString("content")
|
||||
|
||||
return c.SendFile(filepath.Join(basepath, id))
|
||||
}
|
||||
|
||||
func uploadAttachment(c *fiber.Ctx) error {
|
||||
user := c.Locals("principal").(models.Account)
|
||||
hashcode := c.FormValue("hashcode")
|
||||
if len(hashcode) != 64 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "please provide a SHA256 hashcode, length should be 64 characters")
|
||||
}
|
||||
file, err := c.FormFile("attachment")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
attachment, err := services.NewAttachment(user, file, hashcode)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
if err := c.SaveFile(file, attachment.GetStoragePath()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(fiber.Map{
|
||||
"info": attachment,
|
||||
"url": attachment.GetAccessPath(),
|
||||
})
|
||||
}
|
||||
|
||||
func deleteAttachment(c *fiber.Ctx) error {
|
||||
id, _ := c.ParamsInt("id", 0)
|
||||
user := c.Locals("principal").(models.Account)
|
||||
|
||||
attachment, err := services.GetAttachmentByID(uint(id))
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
} else if attachment.AuthorID != user.ID {
|
||||
return fiber.NewError(fiber.StatusNotFound, "record not created by you")
|
||||
}
|
||||
|
||||
if err := services.DeleteAttachment(attachment); err != nil {
|
||||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||||
} else {
|
||||
return c.SendStatus(fiber.StatusOK)
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@ package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.solsynth.dev/hydrogen/messaging/pkg/database"
|
||||
"git.solsynth.dev/hydrogen/messaging/pkg/models"
|
||||
"git.solsynth.dev/hydrogen/messaging/pkg/services"
|
||||
@ -43,10 +44,13 @@ func addChannelMember(c *fiber.Ctx) error {
|
||||
|
||||
var channel models.Channel
|
||||
if err := database.C.Where(&models.Channel{
|
||||
Alias: alias,
|
||||
AccountID: user.ID,
|
||||
Alias: alias,
|
||||
}).First(&channel).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
} else if member, err := services.GetChannelMember(user, channel.ID); err != nil {
|
||||
return fiber.NewError(fiber.StatusForbidden, err.Error())
|
||||
} else if member.PowerLevel < 50 {
|
||||
return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of a channel to add member into it")
|
||||
}
|
||||
|
||||
var account models.Account
|
||||
@ -81,6 +85,10 @@ func removeChannelMember(c *fiber.Ctx) error {
|
||||
AccountID: user.ID,
|
||||
}).First(&channel).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
} else if member, err := services.GetChannelMember(user, channel.ID); err != nil {
|
||||
return fiber.NewError(fiber.StatusForbidden, err.Error())
|
||||
} else if member.PowerLevel < 50 {
|
||||
return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of a channel to remove member into it")
|
||||
}
|
||||
|
||||
var account models.Account
|
||||
|
@ -2,6 +2,7 @@ package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.solsynth.dev/hydrogen/messaging/pkg/database"
|
||||
"git.solsynth.dev/hydrogen/messaging/pkg/models"
|
||||
"git.solsynth.dev/hydrogen/messaging/pkg/services"
|
||||
@ -111,22 +112,31 @@ func createChannel(c *fiber.Ctx) error {
|
||||
var realm *models.Realm
|
||||
if val, ok := c.Locals("realm").(models.Realm); ok {
|
||||
if info, err := services.GetRealmMember(val.ExternalID, user.ExternalID); err != nil {
|
||||
return fmt.Errorf("you must be a part of that realm then can create channel related to it")
|
||||
return fiber.NewError(fiber.StatusForbidden, "you must be a part of that realm then can create channel related to it")
|
||||
} else if info.GetPowerLevel() < 50 {
|
||||
return fmt.Errorf("you must be a moderator of that realm then can create channel related to it")
|
||||
return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of that realm then can create channel related to it")
|
||||
} else {
|
||||
realm = &val
|
||||
}
|
||||
}
|
||||
|
||||
var err error
|
||||
var channel models.Channel
|
||||
if realm != nil {
|
||||
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, data.IsEncrypted)
|
||||
channel := models.Channel{
|
||||
Alias: data.Alias,
|
||||
Name: data.Name,
|
||||
Description: data.Description,
|
||||
IsEncrypted: data.IsEncrypted,
|
||||
AccountID: user.ID,
|
||||
Type: models.ChannelTypeCommon,
|
||||
Members: []models.ChannelMember{
|
||||
{AccountID: user.ID, PowerLevel: 100},
|
||||
},
|
||||
}
|
||||
|
||||
if realm != nil {
|
||||
channel.RealmID = &realm.ID
|
||||
}
|
||||
|
||||
channel, err := services.NewChannel(channel)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
@ -153,14 +163,14 @@ func editChannel(c *fiber.Ctx) error {
|
||||
|
||||
if val, ok := c.Locals("realm").(models.Realm); ok {
|
||||
if info, err := services.GetRealmMember(val.ExternalID, user.ExternalID); err != nil {
|
||||
return fmt.Errorf("you must be a part of that realm then can edit channel related to it")
|
||||
return fiber.NewError(fiber.StatusForbidden, "you must be a part of that realm then can edit channel related to it")
|
||||
} else if info.GetPowerLevel() < 50 {
|
||||
return fmt.Errorf("you must be a moderator of that realm then can edit channel related to it")
|
||||
return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of that realm then can edit channel related to it")
|
||||
} else {
|
||||
tx = tx.Where("realm_id = ?", val.ID)
|
||||
}
|
||||
} else {
|
||||
tx = tx.Where("account_id = ? AND realm_id IS NULL", user.ID)
|
||||
tx = tx.Where("realm_id IS NULL")
|
||||
}
|
||||
|
||||
var channel models.Channel
|
||||
@ -168,6 +178,14 @@ func editChannel(c *fiber.Ctx) error {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
||||
if channel.RealmID != nil {
|
||||
if member, err := services.GetChannelMember(user, channel.ID); err != nil {
|
||||
return fiber.NewError(fiber.StatusForbidden, "you must be a part of this channel to edit it")
|
||||
} else if member.PowerLevel < 100 {
|
||||
return fiber.NewError(fiber.StatusForbidden, "you must be channel admin to edit it")
|
||||
}
|
||||
}
|
||||
|
||||
channel, err := services.EditChannel(channel, data.Alias, data.Name, data.Description, data.IsEncrypted)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
|
68
pkg/server/direct_channels_api.go
Normal file
68
pkg/server/direct_channels_api.go
Normal file
@ -0,0 +1,68 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hydrogen/messaging/pkg/database"
|
||||
"git.solsynth.dev/hydrogen/messaging/pkg/models"
|
||||
"git.solsynth.dev/hydrogen/messaging/pkg/services"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
func createDirectChannel(c *fiber.Ctx) error {
|
||||
user := c.Locals("principal").(models.Account)
|
||||
|
||||
var data struct {
|
||||
Alias string `json:"alias" validate:"required,lowercase,min=4,max=32"`
|
||||
Name string `json:"name" validate:"required"`
|
||||
Description string `json:"description"`
|
||||
Members []uint `json:"members"`
|
||||
IsEncrypted bool `json:"is_encrypted"`
|
||||
}
|
||||
|
||||
if err := BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
} else if err = services.GetChannelAliasAvailability(data.Alias); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
var realm *models.Realm
|
||||
if val, ok := c.Locals("realm").(models.Realm); ok {
|
||||
if info, err := services.GetRealmMember(val.ExternalID, user.ExternalID); err != nil {
|
||||
return fiber.NewError(fiber.StatusForbidden, "you must be a part of that realm then can create channel related to it")
|
||||
} else if info.GetPowerLevel() < 50 {
|
||||
return fiber.NewError(fiber.StatusForbidden, "you must be a moderator of that realm then can create channel related to it")
|
||||
} else {
|
||||
realm = &val
|
||||
}
|
||||
}
|
||||
|
||||
var members []models.Account
|
||||
if err := database.C.Where("id IN ?", data.Members).Find(&members).Error; err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
channel := models.Channel{
|
||||
Alias: data.Alias,
|
||||
Name: data.Name,
|
||||
Description: data.Description,
|
||||
IsEncrypted: data.IsEncrypted,
|
||||
AccountID: user.ID,
|
||||
Type: models.ChannelTypeDirect,
|
||||
Members: append([]models.ChannelMember{
|
||||
{AccountID: user.ID, PowerLevel: 100},
|
||||
}, lo.Map(members, func(item models.Account, idx int) models.ChannelMember {
|
||||
return models.ChannelMember{AccountID: item.ID, PowerLevel: 100}
|
||||
})...),
|
||||
}
|
||||
|
||||
if realm != nil {
|
||||
channel.RealmID = &realm.ID
|
||||
}
|
||||
|
||||
channel, err := services.NewChannel(channel)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
}
|
||||
|
||||
return c.JSON(channel)
|
||||
}
|
@ -1,8 +1,8 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"git.solsynth.dev/hydrogen/messaging/pkg/database"
|
||||
"git.solsynth.dev/hydrogen/messaging/pkg/models"
|
||||
"git.solsynth.dev/hydrogen/messaging/pkg/services"
|
||||
@ -45,16 +45,25 @@ func newMessage(c *fiber.Ctx) error {
|
||||
alias := c.Params("channel")
|
||||
|
||||
var data struct {
|
||||
Type string `json:"type" validate:"required"`
|
||||
Content map[string]any `json:"content"`
|
||||
Attachments []models.Attachment `json:"attachments"`
|
||||
ReplyTo *uint `json:"reply_to"`
|
||||
Uuid string `json:"uuid" validate:"required"`
|
||||
Type string `json:"type" validate:"required"`
|
||||
Content map[string]any `json:"content"`
|
||||
Attachments []uint `json:"attachments"`
|
||||
ReplyTo *uint `json:"reply_to"`
|
||||
}
|
||||
|
||||
if err := BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
} else if len(data.Attachments) == 0 && len(data.Content) == 0 {
|
||||
return fmt.Errorf("you must write or upload some content in a single message")
|
||||
return fiber.NewError(fiber.StatusBadRequest, "you must write or upload some content in a single message")
|
||||
} else if len(data.Uuid) < 36 {
|
||||
return fiber.NewError(fiber.StatusBadRequest, "message uuid was not valid")
|
||||
}
|
||||
|
||||
for _, attachment := range data.Attachments {
|
||||
if !services.CheckAttachmentByIDExists(attachment, "m.attachment") {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("attachment %d not found", attachment))
|
||||
}
|
||||
}
|
||||
|
||||
var err error
|
||||
@ -72,13 +81,9 @@ func newMessage(c *fiber.Ctx) error {
|
||||
}
|
||||
}
|
||||
|
||||
rawContent, err := json.Marshal(data.Content)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("invalid message content, unable to encode: %v", err))
|
||||
}
|
||||
|
||||
message := models.Message{
|
||||
Content: rawContent,
|
||||
Uuid: data.Uuid,
|
||||
Content: data.Content,
|
||||
Sender: member,
|
||||
Channel: channel,
|
||||
ChannelID: channel.ID,
|
||||
@ -110,16 +115,22 @@ func editMessage(c *fiber.Ctx) error {
|
||||
messageId, _ := c.ParamsInt("messageId", 0)
|
||||
|
||||
var data struct {
|
||||
Type string `json:"type" validate:"required"`
|
||||
Content map[string]any `json:"content"`
|
||||
Attachments []models.Attachment `json:"attachments"`
|
||||
ReplyTo *uint `json:"reply_to"`
|
||||
Type string `json:"type" validate:"required"`
|
||||
Content map[string]any `json:"content"`
|
||||
Attachments []uint `json:"attachments"`
|
||||
ReplyTo *uint `json:"reply_to"`
|
||||
}
|
||||
|
||||
if err := BindAndValidate(c, &data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, attachment := range data.Attachments {
|
||||
if !services.CheckAttachmentByIDExists(attachment, "m.attachment") {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("attachment %d not found", attachment))
|
||||
}
|
||||
}
|
||||
|
||||
var err error
|
||||
var channel models.Channel
|
||||
var member models.ChannelMember
|
||||
@ -140,13 +151,8 @@ func editMessage(c *fiber.Ctx) error {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
}
|
||||
|
||||
rawContent, err := json.Marshal(data.Content)
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("invalid message content, unable to encode: %v", err))
|
||||
}
|
||||
|
||||
message.Attachments = data.Attachments
|
||||
message.Content = rawContent
|
||||
message.Content = data.Content
|
||||
message.Type = data.Type
|
||||
|
||||
message, err = services.EditMessage(message)
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
"git.solsynth.dev/hydrogen/messaging/pkg/services"
|
||||
"github.com/gofiber/contrib/websocket"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func messageGateway(c *websocket.Conn) {
|
||||
@ -12,6 +13,7 @@ func messageGateway(c *websocket.Conn) {
|
||||
|
||||
// Push connection
|
||||
services.ClientRegister(user, c)
|
||||
log.Debug().Uint("user", user.ID).Msg("New websocket connection established...")
|
||||
|
||||
// Event loop
|
||||
var task models.UnifiedCommand
|
||||
@ -42,4 +44,5 @@ func messageGateway(c *websocket.Conn) {
|
||||
|
||||
// Pop connection
|
||||
services.ClientUnregister(user, c)
|
||||
log.Debug().Uint("user", user.ID).Msg("A websocket connection disconnected...")
|
||||
}
|
||||
|
@ -3,14 +3,12 @@ package server
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.solsynth.dev/hydrogen/messaging/pkg"
|
||||
"github.com/gofiber/contrib/websocket"
|
||||
"github.com/gofiber/fiber/v2/middleware/favicon"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/middleware/cache"
|
||||
"github.com/gofiber/fiber/v2/middleware/cors"
|
||||
"github.com/gofiber/fiber/v2/middleware/idempotency"
|
||||
"github.com/gofiber/fiber/v2/middleware/logger"
|
||||
@ -68,22 +66,16 @@ func NewServer() {
|
||||
api.Get("/users/me", authMiddleware, getUserinfo)
|
||||
api.Get("/users/:accountId", getOthersInfo)
|
||||
|
||||
api.Get("/attachments/o/:fileId", cache.New(cache.Config{
|
||||
Expiration: 365 * 24 * time.Hour,
|
||||
CacheControl: true,
|
||||
}), readAttachment)
|
||||
api.Post("/attachments", authMiddleware, uploadAttachment)
|
||||
api.Delete("/attachments/:id", authMiddleware, deleteAttachment)
|
||||
|
||||
channels := api.Group("/channels/:realm").Use(realmMiddleware).Name("Channels API")
|
||||
{
|
||||
channels.Get("/", listChannel)
|
||||
channels.Get("/:channel", getChannel)
|
||||
channels.Get("/:channel/availability", authMiddleware, getChannelAvailability)
|
||||
channels.Get("/me", authMiddleware, listOwnedChannel)
|
||||
channels.Get("/me/available", authMiddleware, listAvailableChannel)
|
||||
channels.Get("/:channel", getChannel)
|
||||
channels.Get("/:channel/availability", authMiddleware, getChannelAvailability)
|
||||
|
||||
channels.Post("/", authMiddleware, createChannel)
|
||||
channels.Post("/dm", authMiddleware, createDirectChannel)
|
||||
channels.Put("/:channelId", authMiddleware, editChannel)
|
||||
channels.Delete("/:channelId", authMiddleware, deleteChannel)
|
||||
|
||||
|
Reference in New Issue
Block a user