Skip to content

Commit

Permalink
Handle nil refs in StageOriginate and Originate (#23)
Browse files Browse the repository at this point in the history
Fixes 22
  • Loading branch information
Ulexus authored Jan 26, 2019
1 parent ea70d92 commit 9ee6a2f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
13 changes: 6 additions & 7 deletions client/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ func (c *channel) List(filter *ari.Key) ([]*ari.Key, error) {
})
}

func (c *channel) Originate(key *ari.Key, o ari.OriginateRequest) (*ari.ChannelHandle, error) {
func (c *channel) Originate(referenceKey *ari.Key, o ari.OriginateRequest) (*ari.ChannelHandle, error) {
k, err := c.c.createRequest(&proxy.Request{
Kind: "ChannelOriginate",
Key: key,
Key: referenceKey,
ChannelOriginate: &proxy.ChannelOriginate{
OriginateRequest: o,
},
Expand All @@ -45,14 +45,15 @@ func (c *channel) Originate(key *ari.Key, o ari.OriginateRequest) (*ari.ChannelH
return ari.NewChannelHandle(k, c, nil), nil
}

func (c *channel) StageOriginate(key *ari.Key, o ari.OriginateRequest) (*ari.ChannelHandle, error) {
func (c *channel) StageOriginate(referenceKey *ari.Key, o ari.OriginateRequest) (*ari.ChannelHandle, error) {

if o.ChannelID == "" {
o.ChannelID = rid.New(rid.Channel)
}

k, err := c.c.createRequest(&proxy.Request{
Kind: "ChannelStageOriginate",
Key: key,
Key: referenceKey,
ChannelOriginate: &proxy.ChannelOriginate{
OriginateRequest: o,
},
Expand All @@ -61,9 +62,7 @@ func (c *channel) StageOriginate(key *ari.Key, o ari.OriginateRequest) (*ari.Cha
return nil, err
}
return ari.NewChannelHandle(k.New(ari.ChannelKey, o.ChannelID), c, func(h *ari.ChannelHandle) error {
// Call the subsequent Originate with the original key ID (the source
// channel), so that Originator mapping will work
_, err := c.Originate(k.New(ari.ChannelKey, key.ID), o)
_, err := c.Originate(referenceKey, o)
return err
}), nil
}
Expand Down
40 changes: 31 additions & 9 deletions server/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,17 @@ func (s *Server) channelOriginate(ctx context.Context, reply string, req *proxy.
orig.ChannelID = rid.New(rid.Channel)
}

if req.Key.Dialog != "" {
if req.Key != nil && req.Key.Dialog != "" {
s.Dialog.Bind(req.Key.Dialog, "channel", orig.ChannelID)
s.Dialog.Bind(req.Key.Dialog, "channel", orig.OtherChannelID)
s.Dialog.Bind(req.Key.Dialog, "channel", orig.Originator)
if orig.OtherChannelID != "" {
s.Dialog.Bind(req.Key.Dialog, "channel", orig.OtherChannelID)
}
if orig.Originator != "" {
s.Dialog.Bind(req.Key.Dialog, "channel", orig.Originator)
}
if req.Key.ID != orig.Originator {
s.Dialog.Bind(req.Key.Dialog, "channel", req.Key.ID)
}
}

h, err := s.ari.Channel().Originate(req.Key, orig)
Expand All @@ -143,18 +150,33 @@ func (s *Server) channelOriginate(ctx context.Context, reply string, req *proxy.
}

func (s *Server) channelStageOriginate(ctx context.Context, reply string, req *proxy.Request) {
h := s.ari.Channel().Get(req.Key)
orig := req.ChannelOriginate.OriginateRequest

if req.ChannelOriginate.OriginateRequest.ChannelID == "" {
req.ChannelOriginate.OriginateRequest.ChannelID = rid.New(rid.Channel)
if orig.ChannelID == "" {
orig.ChannelID = rid.New(rid.Channel)
}

if req.Key.Dialog != "" {
s.Dialog.Bind(req.Key.Dialog, "channel", req.Key.ID)
if req.Key != nil && req.Key.Dialog != "" {
s.Dialog.Bind(req.Key.Dialog, "channel", orig.ChannelID)
if orig.OtherChannelID != "" {
s.Dialog.Bind(req.Key.Dialog, "channel", orig.OtherChannelID)
}
if orig.Originator != "" {
s.Dialog.Bind(req.Key.Dialog, "channel", orig.Originator)
}
if req.Key.ID != orig.Originator {
s.Dialog.Bind(req.Key.Dialog, "channel", req.Key.ID)
}
}

h, err := s.ari.Channel().StageOriginate(req.Key, orig)
if err != nil {
s.sendError(reply, err)
return
}

s.publish(reply, &proxy.Response{
Key: h.Key().New(ari.ChannelKey, req.ChannelOriginate.OriginateRequest.ChannelID),
Key: h.Key(),
})
}

Expand Down

0 comments on commit 9ee6a2f

Please sign in to comment.