Call APIs

This commit is contained in:
LittleSheep 2024-04-06 17:08:01 +08:00
parent 35b28d1003
commit eaf074609e
7 changed files with 325 additions and 0 deletions

View File

@ -9,6 +9,7 @@ var DatabaseAutoActionRange = []any{
&models.Account{}, &models.Account{},
&models.Channel{}, &models.Channel{},
&models.ChannelMember{}, &models.ChannelMember{},
&models.Call{},
&models.Message{}, &models.Message{},
&models.Attachment{}, &models.Attachment{},
} }

22
pkg/models/calls.go Normal file
View File

@ -0,0 +1,22 @@
package models
import "time"
type CallProvider = string
const (
CallProviderJitsi = "jitsi"
)
type Call struct {
BaseModel
Provider string `json:"provider"`
EndedAt *time.Time `json:"ended_at"`
ExternalID string `json:"external_id"`
FounderID uint `json:"founder_id"`
ChannelID uint `json:"channel_id"`
Founder ChannelMember `json:"founder"`
Channel Channel `json:"channel"`
}

View File

@ -15,6 +15,7 @@ type Channel struct {
Description string `json:"description"` Description string `json:"description"`
Members []ChannelMember `json:"members"` Members []ChannelMember `json:"members"`
Messages []Message `json:"messages"` Messages []Message `json:"messages"`
Calls []Call `json:"calls"`
Type ChannelType `json:"type"` Type ChannelType `json:"type"`
Account Account `json:"account"` Account Account `json:"account"`
AccountID uint `json:"account_id"` AccountID uint `json:"account_id"`
@ -38,5 +39,6 @@ type ChannelMember struct {
Account Account `json:"account"` Account Account `json:"account"`
Notify NotifyLevel `json:"notify"` Notify NotifyLevel `json:"notify"`
Calls []Call `json:"calls" gorm:"foreignKey:FounderID"`
Messages []Message `json:"messages" gorm:"foreignKey:SenderID"` Messages []Message `json:"messages" gorm:"foreignKey:SenderID"`
} }

143
pkg/server/calls_api.go Normal file
View File

@ -0,0 +1,143 @@
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"
"github.com/gofiber/fiber/v2"
"github.com/spf13/viper"
"net/url"
)
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())
} else {
return c.JSON(call)
}
}
func startCall(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account)
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.NewCall(channel, membership)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
} else {
return c.JSON(call)
}
}
func endCall(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account)
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())
} else if call.FounderID != user.ID && channel.AccountID != user.ID {
return fiber.NewError(fiber.StatusBadRequest, "only call founder or channel owner can end this call")
}
if call, err := services.EndCall(call); err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else {
return c.JSON(call)
}
}
func exchangeCallToken(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account)
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())
}
tk, err := services.EncodeCallToken(call, user)
if err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else {
return c.JSON(fiber.Map{
"token": tk,
"endpoint": viper.GetString("meeting.endpoint"),
"full_url": fmt.Sprintf("%s?jwt=%s", viper.GetString("meeting.endpoint"), url.QueryEscape(tk)),
})
}
}

View File

@ -95,6 +95,12 @@ func NewServer() {
channels.Post("/:channel/messages", authMiddleware, newTextMessage) channels.Post("/:channel/messages", authMiddleware, newTextMessage)
channels.Put("/:channel/messages/:messageId", authMiddleware, editMessage) channels.Put("/:channel/messages/:messageId", authMiddleware, editMessage)
channels.Delete("/:channel/messages/:messageId", authMiddleware, deleteMessage) channels.Delete("/:channel/messages/:messageId", authMiddleware, deleteMessage)
channels.Get("/:channel/calls", listCall)
channels.Get("/:channel/calls/ongoing", getOngoingCall)
channels.Post("/:channel/calls", authMiddleware, startCall)
channels.Delete("/:channel/calls", authMiddleware, endCall)
channels.Post("/:channel/calls/ongoing/token", authMiddleware, exchangeCallToken)
} }
api.Get("/unified", authMiddleware, websocket.New(unifiedGateway)) api.Get("/unified", authMiddleware, websocket.New(unifiedGateway))

146
pkg/services/calls.go Normal file
View File

@ -0,0 +1,146 @@
package services
import (
"errors"
"fmt"
"git.solsynth.dev/hydrogen/messaging/pkg/database"
"git.solsynth.dev/hydrogen/messaging/pkg/models"
"github.com/golang-jwt/jwt/v5"
"github.com/rs/zerolog/log"
"github.com/samber/lo"
"github.com/spf13/viper"
"gorm.io/gorm"
"time"
)
func ListCall(channel models.Channel, take, offset int) ([]models.Call, error) {
var calls []models.Call
if err := database.C.
Where(models.Call{ChannelID: channel.ID}).
Limit(take).
Offset(offset).
Preload("Founder").
Preload("Founder.Account").
Preload("Channel").
Order("created_at DESC").
Find(&calls).Error; err != nil {
return calls, err
} else {
return calls, nil
}
}
func GetCall(channel models.Channel, id uint) (models.Call, error) {
var call models.Call
if err := database.C.
Where(models.Call{
BaseModel: models.BaseModel{ID: id},
ChannelID: channel.ID,
}).
Preload("Founder").
Preload("Founder.Account").
Preload("Channel").
Order("created_at DESC").
First(&call).Error; err != nil {
return call, err
} else {
return call, nil
}
}
func GetOngoingCall(channel models.Channel) (models.Call, error) {
var call models.Call
if err := database.C.
Where(models.Call{ChannelID: channel.ID}).
Where("ended_at IS NULL").
Preload("Founder").
Preload("Channel").
Order("created_at DESC").
First(&call).Error; err != nil {
return call, err
} else {
return call, nil
}
}
func NewCall(channel models.Channel, founder models.ChannelMember) (models.Call, error) {
call := models.Call{
Provider: models.CallProviderJitsi,
ExternalID: channel.Name,
FounderID: founder.ID,
ChannelID: channel.ID,
Founder: founder,
Channel: channel,
}
if _, err := GetOngoingCall(channel); err == nil || !errors.Is(err, gorm.ErrRecordNotFound) {
return call, fmt.Errorf("this channel already has an ongoing call")
}
var members []models.ChannelMember
if err := database.C.Save(&call).Error; err != nil {
return call, err
} else if err = database.C.Where(models.ChannelMember{
ChannelID: call.ChannelID,
}).Preload("Account").Find(&members).Error; err == nil {
call, _ = GetCall(call.Channel, call.ID)
for _, member := range members {
if member.ID != call.Founder.ID {
if member.Notify == models.NotifyLevelAll {
err = NotifyAccount(member.Account,
fmt.Sprintf("New Call #%s", call.Channel.Alias),
fmt.Sprintf("%s starts a new call", call.Founder.Account.Name),
false,
)
if err != nil {
log.Warn().Err(err).Msg("An error occurred when trying notify user.")
}
}
}
PushCommand(member.AccountID, models.UnifiedCommand{
Action: "calls.new",
Payload: call,
})
}
}
return call, nil
}
func EndCall(call models.Call) (models.Call, error) {
call.EndedAt = lo.ToPtr(time.Now())
var members []models.ChannelMember
if err := database.C.Save(&call).Error; err != nil {
return call, err
} else if err = database.C.Where(models.ChannelMember{
ChannelID: call.ChannelID,
}).Preload("Account").Find(&members).Error; err == nil {
call, _ = GetCall(call.Channel, call.ID)
for _, member := range members {
PushCommand(member.AccountID, models.UnifiedCommand{
Action: "calls.end",
Payload: call,
})
}
}
return call, nil
}
func EncodeCallToken(call models.Call, user models.Account) (string, error) {
tk := jwt.NewWithClaims(jwt.SigningMethodHS512, jwt.MapClaims{
"context": jwt.MapClaims{
"user": jwt.MapClaims{
"avatar": user.Avatar,
"name": user.Name,
},
},
"aud": viper.GetString("meeting.client_id"),
"iss": viper.GetString("domain"),
"sub": "meet.jitsi",
"room": call.ExternalID,
})
return tk.SignedString([]byte(viper.GetString("meeting.client_secret")))
}

View File

@ -19,6 +19,11 @@ client_secret = "Z9k9AFTj^p"
endpoint = "https://id.solsynth.dev" endpoint = "https://id.solsynth.dev"
grpc_endpoint = "id.solsynth.dev:7444" grpc_endpoint = "id.solsynth.dev:7444"
[meeting]
client_id = "solarmeet"
client_secret = "1234567890"
endpoint = "https://meet.solsynth.dev"
[mailer] [mailer]
name = "Alphabot <alphabot@smartsheep.studio>" name = "Alphabot <alphabot@smartsheep.studio>"
smtp_host = "smtp.exmail.qq.com" smtp_host = "smtp.exmail.qq.com"