2024-04-06 09:08:01 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2024-04-26 16:04:01 +00:00
|
|
|
"context"
|
2024-04-06 09:08:01 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-07-20 08:02:16 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
|
2024-06-22 10:05:41 +00:00
|
|
|
"git.solsynth.dev/hydrogen/messaging/pkg/internal/database"
|
|
|
|
"git.solsynth.dev/hydrogen/messaging/pkg/internal/models"
|
2024-04-27 14:20:48 +00:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
2024-04-26 16:04:01 +00:00
|
|
|
"github.com/livekit/protocol/auth"
|
|
|
|
"github.com/livekit/protocol/livekit"
|
2024-04-06 09:08:01 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/samber/lo"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-17 03:03:35 +00:00
|
|
|
func GetCallParticipants(call models.Call) ([]*livekit.ParticipantInfo, error) {
|
|
|
|
res, err := Lk.ListParticipants(context.Background(), &livekit.ListParticipantsRequest{
|
|
|
|
Room: call.ExternalID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res.Participants, nil
|
|
|
|
}
|
|
|
|
|
2024-04-06 09:08:01 +00:00
|
|
|
func NewCall(channel models.Channel, founder models.ChannelMember) (models.Call, error) {
|
2024-08-02 09:25:19 +00:00
|
|
|
id := fmt.Sprintf("%s+%d", channel.Alias, channel.ID)
|
2024-04-06 09:08:01 +00:00
|
|
|
call := models.Call{
|
2024-08-02 09:25:19 +00:00
|
|
|
ExternalID: id,
|
2024-06-22 17:10:39 +00:00
|
|
|
FounderID: founder.AccountID,
|
2024-04-06 09:08:01 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2024-06-08 12:38:03 +00:00
|
|
|
_, err := Lk.CreateRoom(context.Background(), &livekit.CreateRoomRequest{
|
2024-08-02 09:25:19 +00:00
|
|
|
Name: id,
|
2024-04-26 16:04:01 +00:00
|
|
|
EmptyTimeout: viper.GetUint32("calling.empty_timeout_duration"),
|
|
|
|
MaxParticipants: viper.GetUint32("calling.max_participants"),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return call, fmt.Errorf("remote livekit error: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-04-06 09:08:01 +00:00
|
|
|
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)
|
2024-07-20 08:02:16 +00:00
|
|
|
var pendingUsers []models.Account
|
2024-04-06 09:08:01 +00:00
|
|
|
for _, member := range members {
|
|
|
|
if member.ID != call.Founder.ID {
|
2024-07-20 08:02:16 +00:00
|
|
|
pendingUsers = append(pendingUsers, member.Account)
|
2024-04-06 09:08:01 +00:00
|
|
|
}
|
|
|
|
PushCommand(member.AccountID, models.UnifiedCommand{
|
|
|
|
Action: "calls.new",
|
|
|
|
Payload: call,
|
|
|
|
})
|
|
|
|
}
|
2024-07-20 08:02:16 +00:00
|
|
|
|
2024-08-02 13:34:08 +00:00
|
|
|
channel, _ = GetChannel(channel.ID)
|
|
|
|
|
2024-07-20 08:02:16 +00:00
|
|
|
err = NotifyAccountMessagerBatch(
|
|
|
|
pendingUsers,
|
|
|
|
&proto.NotifyRequest{
|
|
|
|
Topic: "messaging.callStart",
|
2024-08-02 13:34:08 +00:00
|
|
|
Title: fmt.Sprintf("Call in (%s)", channel.DisplayText()),
|
2024-07-20 08:02:16 +00:00
|
|
|
Body: fmt.Sprintf("%s is calling", call.Founder.Account.Name),
|
|
|
|
Avatar: &call.Founder.Account.Avatar,
|
|
|
|
Metadata: EncodeJSONBody(map[string]any{
|
|
|
|
"user_id": call.Founder.Account.ExternalID,
|
|
|
|
"user_name": call.Founder.Account.Name,
|
|
|
|
"user_nick": call.Founder.Account.Nick,
|
|
|
|
"channel_id": call.ChannelID,
|
|
|
|
}),
|
|
|
|
IsRealtime: false,
|
|
|
|
IsForcePush: true,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn().Err(err).Msg("An error occurred when trying notify user.")
|
|
|
|
}
|
2024-04-06 09:08:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return call, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func EndCall(call models.Call) (models.Call, error) {
|
|
|
|
call.EndedAt = lo.ToPtr(time.Now())
|
|
|
|
|
2024-06-08 12:38:03 +00:00
|
|
|
if _, err := Lk.DeleteRoom(context.Background(), &livekit.DeleteRoomRequest{
|
2024-04-26 16:04:01 +00:00
|
|
|
Room: call.ExternalID,
|
|
|
|
}); err != nil {
|
|
|
|
log.Error().Err(err).Msg("Unable to delete room at livekit side")
|
|
|
|
}
|
|
|
|
|
2024-04-06 09:08:01 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-07-17 03:03:35 +00:00
|
|
|
func KickParticipantInCall(call models.Call, username string) error {
|
|
|
|
_, err := Lk.RemoveParticipant(context.Background(), &livekit.RoomParticipantIdentity{
|
|
|
|
Room: call.ExternalID,
|
|
|
|
Identity: username,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-26 16:04:01 +00:00
|
|
|
func EncodeCallToken(user models.Account, call models.Call) (string, error) {
|
2024-08-02 13:34:08 +00:00
|
|
|
isAdmin := user.ID == call.FounderID || user.ID == call.Channel.AccountID
|
2024-04-26 16:04:01 +00:00
|
|
|
|
|
|
|
grant := &auth.VideoGrant{
|
|
|
|
Room: call.ExternalID,
|
|
|
|
RoomJoin: true,
|
|
|
|
RoomAdmin: isAdmin,
|
|
|
|
}
|
|
|
|
|
2024-04-27 14:20:48 +00:00
|
|
|
metadata, _ := jsoniter.Marshal(user)
|
|
|
|
|
2024-04-26 16:04:01 +00:00
|
|
|
duration := time.Second * time.Duration(viper.GetInt("calling.token_duration"))
|
|
|
|
tk := auth.NewAccessToken(viper.GetString("calling.api_key"), viper.GetString("calling.api_secret"))
|
2024-04-27 14:20:48 +00:00
|
|
|
tk.AddGrant(grant).
|
|
|
|
SetIdentity(user.Name).
|
|
|
|
SetName(user.Nick).
|
|
|
|
SetMetadata(string(metadata)).
|
|
|
|
SetValidFor(duration)
|
2024-04-06 09:08:01 +00:00
|
|
|
|
2024-04-26 16:04:01 +00:00
|
|
|
return tk.ToJWT()
|
2024-04-06 09:08:01 +00:00
|
|
|
}
|