fix im p0 backend issues

This commit is contained in:
richjohnrambo
2026-09-07 15:12:14 +08:00
parent b92154bece
commit 7e8fc8237b
7 changed files with 371 additions and 13 deletions
+137 -4
View File
@@ -109,10 +109,18 @@ func normalizeCallInvite(head map[string]any, content any) (map[string]any, any,
if state != "started" {
return nil, nil, types.ErrMalformed
}
call, ok := content.(map[string]any)
if !ok {
if typed, typedOK := content.(types.KVMap); typedOK {
call = map[string]any(typed)
call := callInvitePayloadMap(content)
if len(call) == 0 || !hasCallIdentity(call) {
headCall := callInvitePayloadMap(head)
if len(call) == 0 {
call = headCall
} else {
for key, value := range headCall {
if _, exists := call[key]; !exists {
call[key] = value
}
}
normalizeCallPayloadAliases(call)
}
}
if len(call) == 0 {
@@ -123,6 +131,16 @@ func normalizeCallInvite(head map[string]any, content any) (map[string]any, any,
if callType == "" {
callType, _ = call["media_type"].(string)
}
if callType == "" {
callType, _ = call["type"].(string)
}
if callType == "" {
if aonly, _ := call["aonly"].(bool); aonly {
callType = constCallTypeAudio
} else {
callType = constCallTypeVideo
}
}
callType = strings.ToLower(strings.TrimSpace(callType))
if strings.TrimSpace(callID) == "" || (callType != constCallTypeAudio && callType != constCallTypeVideo) {
return nil, nil, types.ErrMalformed
@@ -147,6 +165,121 @@ func normalizeCallInvite(head map[string]any, content any) (map[string]any, any,
return normalizedHead, normalizedContent, nil
}
func callInvitePayloadMap(content any) map[string]any {
call := anyMap(content)
if len(call) == 0 {
return nil
}
normalizeCallPayloadAliases(call)
if hasCallIdentity(call) {
return call
}
if draftyCall := draftyVideoCallPayload(call); len(draftyCall) > 0 {
for key, value := range call {
if _, exists := draftyCall[key]; !exists && key != "fmt" && key != "ent" && key != "txt" {
draftyCall[key] = value
}
}
normalizeCallPayloadAliases(draftyCall)
return draftyCall
}
return call
}
func anyMap(value any) map[string]any {
if typed, ok := value.(map[string]any); ok {
return typed
}
if typed, ok := value.(types.KVMap); ok {
return map[string]any(typed)
}
return nil
}
func normalizeCallPayloadAliases(call map[string]any) {
if call == nil {
return
}
if _, ok := call["callId"]; !ok {
for _, key := range []string{"call_id", "id"} {
if value, exists := call[key]; exists {
call["callId"] = value
break
}
}
}
if _, ok := call["callType"]; !ok {
for _, key := range []string{"call_type", "mediaType", "media_type"} {
if value, exists := call[key]; exists {
call["callType"] = value
break
}
}
}
}
func hasCallIdentity(call map[string]any) bool {
if call == nil {
return false
}
callID, _ := call["callId"].(string)
if strings.TrimSpace(callID) == "" {
return false
}
callType, _ := call["callType"].(string)
if strings.TrimSpace(callType) != "" {
return true
}
if mediaType, _ := call["media_type"].(string); strings.TrimSpace(mediaType) != "" {
return true
}
if _, ok := call["aonly"].(bool); ok {
return true
}
return false
}
func draftyVideoCallPayload(doc map[string]any) map[string]any {
for _, raw := range anySlice(doc["ent"]) {
entity := anyMap(raw)
if entityType, _ := entity["tp"].(string); entityType != "VC" {
continue
}
if data := anyMap(entity["data"]); len(data) > 0 {
return data
}
return map[string]any{}
}
for _, raw := range anySlice(doc["fmt"]) {
format := anyMap(raw)
if formatType, _ := format["tp"].(string); formatType == "VC" {
return map[string]any{}
}
}
return nil
}
func anySlice(value any) []any {
switch typed := value.(type) {
case []any:
return typed
case []map[string]any:
items := make([]any, 0, len(typed))
for _, item := range typed {
items = append(items, item)
}
return items
case []types.KVMap:
items := make([]any, 0, len(typed))
for _, item := range typed {
items = append(items, item)
}
return items
default:
return nil
}
}
func callPayloadString(payload any, key string) string {
data, ok := payload.(map[string]any)
if !ok {