diff --git a/api_async.go b/api_async.go index a78adff..d3c0426 100644 --- a/api_async.go +++ b/api_async.go @@ -21,7 +21,11 @@ */ package signaling -import "time" +import ( + "encoding/json" + "fmt" + "time" +) type AsyncMessage struct { SendTime time.Time `json:"sendtime"` @@ -41,6 +45,14 @@ type AsyncMessage struct { Id string `json:"id"` } +func (m *AsyncMessage) String() string { + data, err := json.Marshal(m) + if err != nil { + return fmt.Sprintf("Could not serialize %#v: %s", m, err) + } + return string(data) +} + type AsyncRoomMessage struct { Type string `json:"type"` diff --git a/api_backend.go b/api_backend.go index 44e7672..e957a3c 100644 --- a/api_backend.go +++ b/api_backend.go @@ -33,6 +33,7 @@ import ( "net/url" "regexp" "strings" + "time" ) const ( @@ -107,6 +108,8 @@ type BackendServerRoomRequest struct { Dialout *BackendRoomDialoutRequest `json:"dialout,omitempty"` + Transient *BackendRoomTransientRequest `json:"transient,omitempty"` + // Internal properties ReceivedTime int64 `json:"received,omitempty"` } @@ -200,6 +203,20 @@ func (r *BackendRoomDialoutRequest) ValidateNumber() *Error { return nil } +type TransientAction string + +const ( + TransientActionSet TransientAction = "set" + TransientActionDelete TransientAction = "delete" +) + +type BackendRoomTransientRequest struct { + Action TransientAction `json:"action"` + Key string `json:"key"` + Value interface{} `json:"value,omitempty"` + TTL time.Duration `json:"ttl,omitempty"` +} + type BackendServerRoomResponse struct { Type string `json:"type"` diff --git a/hub.go b/hub.go index a2eb83f..bbcfc98 100644 --- a/hub.go +++ b/hub.go @@ -97,6 +97,9 @@ var ( // Delay after which a screen publisher should be cleaned up. cleanupScreenPublisherDelay = time.Second + + // Delay after which a "cleared" / "rejected" dialout status should be removed. + removeCallStatusTTL = 5 * time.Second ) const ( @@ -1942,14 +1945,34 @@ func (h *Hub) processInternalMsg(client *Client, message *ClientMessage) { case "dialout": roomId := msg.Dialout.RoomId msg.Dialout.RoomId = "" // Don't send room id to recipients. - if err := h.events.PublishRoomMessage(roomId, session.Backend(), &AsyncMessage{ - Type: "message", - Message: &ServerMessage{ - Type: "dialout", - Dialout: msg.Dialout, - }, - }); err != nil { - log.Printf("Error publishing dialout message %+v to room %s", msg.Dialout, roomId) + if msg.Dialout.Type == "status" { + asyncMessage := &AsyncMessage{ + Type: "room", + Room: &BackendServerRoomRequest{ + Type: "transient", + Transient: &BackendRoomTransientRequest{ + Action: TransientActionSet, + Key: "callstatus_" + msg.Dialout.Status.CallId, + Value: msg.Dialout.Status, + }, + }, + } + if msg.Dialout.Status.Status == DialoutStatusCleared || msg.Dialout.Status.Status == DialoutStatusRejected { + asyncMessage.Room.Transient.TTL = removeCallStatusTTL + } + if err := h.events.PublishBackendRoomMessage(roomId, session.Backend(), asyncMessage); err != nil { + log.Printf("Error publishing dialout message %+v to room %s", msg.Dialout, roomId) + } + } else { + if err := h.events.PublishRoomMessage(roomId, session.Backend(), &AsyncMessage{ + Type: "message", + Message: &ServerMessage{ + Type: "dialout", + Dialout: msg.Dialout, + }, + }); err != nil { + log.Printf("Error publishing dialout message %+v to room %s", msg.Dialout, roomId) + } } default: log.Printf("Ignore unsupported internal message %+v from %s", msg, session.PublicId()) diff --git a/room.go b/room.go index e0a533a..8d983ef 100644 --- a/room.go +++ b/room.go @@ -244,6 +244,15 @@ func (r *Room) processBackendRoomRequestRoom(message *BackendServerRoomRequest) r.publishRoomMessage(message.Message) case "switchto": r.publishSwitchTo(message.SwitchTo) + case "transient": + switch message.Transient.Action { + case TransientActionSet: + r.SetTransientDataTTL(message.Transient.Key, message.Transient.Value, message.Transient.TTL) + case TransientActionDelete: + r.RemoveTransientData(message.Transient.Key) + default: + log.Printf("Unsupported transient action in room %s: %+v", r.Id(), message.Transient) + } default: log.Printf("Unsupported backend room request with type %s in %s: %+v", message.Type, r.Id(), message) }