2024-06-22 10:05:41 +00:00
|
|
|
package api
|
2024-04-06 09:08:01 +00:00
|
|
|
|
|
|
|
import (
|
2024-06-22 10:05:41 +00:00
|
|
|
"git.solsynth.dev/hydrogen/messaging/pkg/internal/database"
|
2024-06-22 10:29:41 +00:00
|
|
|
"git.solsynth.dev/hydrogen/messaging/pkg/internal/gap"
|
2024-06-22 10:05:41 +00:00
|
|
|
"git.solsynth.dev/hydrogen/messaging/pkg/internal/models"
|
2024-07-17 03:03:35 +00:00
|
|
|
"git.solsynth.dev/hydrogen/messaging/pkg/internal/server/exts"
|
2024-06-22 10:05:41 +00:00
|
|
|
"git.solsynth.dev/hydrogen/messaging/pkg/internal/services"
|
2024-04-06 09:08:01 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2024-06-27 20:34:49 +00:00
|
|
|
"github.com/google/uuid"
|
2024-04-06 09:08:01 +00:00
|
|
|
"github.com/spf13/viper"
|
2024-07-17 03:03:35 +00:00
|
|
|
"sync"
|
2024-04-06 09:08:01 +00:00
|
|
|
)
|
|
|
|
|
2024-07-17 03:03:35 +00:00
|
|
|
var callLocks sync.Map
|
|
|
|
|
2024-04-06 09:08:01 +00:00
|
|
|
func listCall(c *fiber.Ctx) error {
|
|
|
|
take := c.QueryInt("take", 0)
|
|
|
|
offset := c.QueryInt("offset", 0)
|
|
|
|
alias := c.Params("channel")
|
|
|
|
|
|
|
|
var channel models.Channel
|
|
|
|
if err := database.C.Where(&models.Channel{
|
|
|
|
Alias: alias,
|
|
|
|
}).First(&channel).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if calls, err := services.ListCall(channel, take, offset); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
} else {
|
|
|
|
return c.JSON(calls)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getOngoingCall(c *fiber.Ctx) error {
|
|
|
|
alias := c.Params("channel")
|
|
|
|
|
|
|
|
var channel models.Channel
|
|
|
|
if err := database.C.Where(&models.Channel{
|
|
|
|
Alias: alias,
|
|
|
|
}).First(&channel).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if call, err := services.GetOngoingCall(channel); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
2024-07-17 03:03:35 +00:00
|
|
|
} else if res, err := services.GetCallParticipants(call); err != nil {
|
|
|
|
return c.JSON(call)
|
2024-04-06 09:08:01 +00:00
|
|
|
} else {
|
2024-07-17 03:03:35 +00:00
|
|
|
call.Participants = res
|
2024-04-06 09:08:01 +00:00
|
|
|
return c.JSON(call)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func startCall(c *fiber.Ctx) error {
|
2024-07-17 03:03:35 +00:00
|
|
|
if err := gap.H.EnsureGrantedPerm(c, "CreateCalls", true); err != nil {
|
2024-06-22 10:29:41 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-06-22 12:15:01 +00:00
|
|
|
user := c.Locals("user").(models.Account)
|
2024-04-06 09:08:01 +00:00
|
|
|
alias := c.Params("channel")
|
|
|
|
|
|
|
|
var channel models.Channel
|
|
|
|
if err := database.C.Where(&models.Channel{
|
|
|
|
Alias: alias,
|
|
|
|
}).First(&channel).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
var membership models.ChannelMember
|
|
|
|
if err := database.C.Where(&models.ChannelMember{
|
|
|
|
ChannelID: channel.ID,
|
|
|
|
AccountID: user.ID,
|
|
|
|
}).Find(&membership).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
2024-07-17 03:03:35 +00:00
|
|
|
} else if membership.PowerLevel < 0 {
|
|
|
|
return fiber.NewError(fiber.StatusForbidden, "you have not enough permission to create a call")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := callLocks.Load(channel.ID); ok {
|
|
|
|
return fiber.NewError(fiber.StatusLocked, "there is already a call in creation progress for this channel")
|
|
|
|
} else {
|
|
|
|
callLocks.Store(channel.ID, true)
|
2024-04-06 09:08:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
call, err := services.NewCall(channel, membership)
|
|
|
|
if err != nil {
|
2024-07-17 03:03:35 +00:00
|
|
|
callLocks.Delete(channel.ID)
|
2024-04-06 09:08:01 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
} else {
|
2024-06-27 20:34:49 +00:00
|
|
|
_, _ = services.NewEvent(models.Event{
|
|
|
|
Uuid: uuid.NewString(),
|
|
|
|
Body: map[string]any{},
|
|
|
|
Type: "calls.start",
|
|
|
|
Channel: channel,
|
|
|
|
Sender: membership,
|
|
|
|
ChannelID: channel.ID,
|
|
|
|
SenderID: membership.ID,
|
|
|
|
})
|
|
|
|
|
2024-07-17 03:03:35 +00:00
|
|
|
callLocks.Delete(channel.ID)
|
2024-04-06 09:08:01 +00:00
|
|
|
return c.JSON(call)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func endCall(c *fiber.Ctx) error {
|
2024-06-22 10:29:41 +00:00
|
|
|
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-22 12:15:01 +00:00
|
|
|
user := c.Locals("user").(models.Account)
|
2024-04-06 09:08:01 +00:00
|
|
|
alias := c.Params("channel")
|
|
|
|
|
|
|
|
var channel models.Channel
|
|
|
|
if err := database.C.Where(&models.Channel{
|
|
|
|
Alias: alias,
|
|
|
|
}).First(&channel).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
var membership models.ChannelMember
|
|
|
|
if err := database.C.Where(&models.ChannelMember{
|
|
|
|
ChannelID: channel.ID,
|
|
|
|
AccountID: user.ID,
|
|
|
|
}).Find(&membership).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
call, err := services.GetOngoingCall(channel)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
2024-07-17 03:03:35 +00:00
|
|
|
} else if call.FounderID != user.ID && membership.PowerLevel < 50 {
|
2024-07-13 03:13:01 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "only call founder or channel admin can end this call")
|
2024-04-06 09:08:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if call, err := services.EndCall(call); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
} else {
|
2024-06-27 20:34:49 +00:00
|
|
|
_, _ = services.NewEvent(models.Event{
|
|
|
|
Uuid: uuid.NewString(),
|
|
|
|
Body: map[string]any{"last": call.EndedAt.Unix() - call.CreatedAt.Unix()},
|
|
|
|
Type: "calls.end",
|
|
|
|
Channel: channel,
|
|
|
|
Sender: membership,
|
|
|
|
ChannelID: channel.ID,
|
|
|
|
SenderID: membership.ID,
|
|
|
|
})
|
|
|
|
|
2024-04-06 09:08:01 +00:00
|
|
|
return c.JSON(call)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-17 03:03:35 +00:00
|
|
|
func kickParticipantInCall(c *fiber.Ctx) error {
|
|
|
|
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
|
|
|
alias := c.Params("channel")
|
|
|
|
|
|
|
|
var data struct {
|
|
|
|
Username string `json:"username" validate:"required"`
|
|
|
|
}
|
|
|
|
if err := exts.BindAndValidate(c, &data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var channel models.Channel
|
|
|
|
if err := database.C.Where(&models.Channel{
|
|
|
|
Alias: alias,
|
|
|
|
}).First(&channel).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
var membership models.ChannelMember
|
|
|
|
if err := database.C.Where(&models.ChannelMember{
|
|
|
|
ChannelID: channel.ID,
|
|
|
|
AccountID: user.ID,
|
|
|
|
}).Find(&membership).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
call, err := services.GetOngoingCall(channel)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
} else if call.FounderID != user.ID && membership.PowerLevel < 50 {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, "only call founder or channel admin can kick participant in this call")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = services.KickParticipantInCall(call, data.Username); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|
|
|
|
|
2024-04-06 09:08:01 +00:00
|
|
|
func exchangeCallToken(c *fiber.Ctx) error {
|
2024-06-22 10:29:41 +00:00
|
|
|
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-22 12:15:01 +00:00
|
|
|
user := c.Locals("user").(models.Account)
|
2024-04-06 09:08:01 +00:00
|
|
|
alias := c.Params("channel")
|
|
|
|
|
|
|
|
var channel models.Channel
|
|
|
|
if err := database.C.Where(&models.Channel{
|
|
|
|
Alias: alias,
|
|
|
|
}).First(&channel).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
var membership models.ChannelMember
|
|
|
|
if err := database.C.Where(&models.ChannelMember{
|
|
|
|
ChannelID: channel.ID,
|
|
|
|
AccountID: user.ID,
|
|
|
|
}).Find(&membership).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
call, err := services.GetOngoingCall(channel)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-04-26 16:07:44 +00:00
|
|
|
tk, err := services.EncodeCallToken(user, call)
|
2024-04-06 09:08:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
} else {
|
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"token": tk,
|
2024-04-26 16:04:01 +00:00
|
|
|
"endpoint": viper.GetString("calling.endpoint"),
|
2024-04-06 09:08:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|