Skip to content

Commit

Permalink
Merge branch 'release/0.7.12'
Browse files Browse the repository at this point in the history
  • Loading branch information
gildas committed Apr 28, 2022
2 parents 5e5a0ad + a247a38 commit acb5de9
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 72 deletions.
23 changes: 23 additions & 0 deletions conversation.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type Conversation struct {
ID uuid.UUID `json:"id"`
SelfURI URI `json:"selfUri,omitempty"`
Name string `json:"name"`
ExternalTag string `json:"externalTag,omitempty"`
StartTime time.Time `json:"startTime"`
EndTime time.Time `json:"endTime"`
Address string `json:"address"`
Expand Down Expand Up @@ -118,6 +119,28 @@ func (conversation Conversation) String() string {
return conversation.ID.String()
}

// Disconnect disconnect an Identifiable from this
// implements Disconnecter
func (conversation Conversation) Disconnect(context context.Context, identifiable Identifiable) error {
return conversation.client.Patch(
conversation.logger.ToContext(context),
NewURI("/conversations/%s/participants/%s", conversation.ID, identifiable.GetID()),
MediaParticipantRequest{State: "disconnected"},
nil,
)
}

// UpdateState update the state of an identifiable in this
// implements StateUpdater
func (conversation Conversation) UpdateState(context context.Context, identifiable Identifiable, state string) error {
return conversation.client.Patch(
conversation.logger.ToContext(context),
NewURI("/conversations/%s/participants/%s", conversation.ID, identifiable.GetID()),
MediaParticipantRequest{State: state},
nil,
)
}

/*
func (conversation *Conversation) GetParticipants(context context.Context) []*Participant {
log := conversation.logger.Child(nil, "participants")
Expand Down
42 changes: 9 additions & 33 deletions conversation_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,18 @@ import (
// ConversationMessage describes a Message (like belonging to Participant)
type ConversationMessage struct {
ID uuid.UUID `json:"id"`
Type string `json:"type"`
Direction string `json:"direction"` // inbound,outbound
State string `json:"state"` // alerting,dialing,contacting,offering,connected,disconnected,terminated,converting,uploading,transmitting,scheduled,none
Held bool `json:"held"`

RecordingID string `json:"recordingId"`
RecordingID string `json:"recordingId,omitempty"`

Segments []Segment `json:"segments"`
Provider string `json:"provider"`
ScriptID string `json:"scriptId"`
PeerID string `json:"peerId"`
Type string `json:"type"`
RecipientCountry string `json:"recipientCountry"`
RecipientType string `json:"recipientType"`
ScriptID string `json:"scriptId,omitempty"`
PeerID uuid.UUID `json:"peerId"`
RecipientCountry string `json:"recipientCountry,omitempty"`
ToAddress Address `json:"toAddress"`
FromAddress Address `json:"fromAddress"`

Expand All @@ -30,34 +29,11 @@ type ConversationMessage struct {
StartAlertingTime time.Time `json:"startAlertingTime"`
StartHoldTime time.Time `json:"startHoldTime"`

Messages []MessageDetail `json:"messages"`
Messages []MessageDetails `json:"messages"`

DisconnectType string `json:"disconnectType"` // endpoint,client,system,transfer,timeout,transfer.conference,transfer.consult,transfer.forward,transfer.noanswer,transfer.notavailable,transport.failure,error,peer,other,spam,uncallable
ErrorInfo ErrorBody `json:"errorInfo"`
}

// MessageDetail describes details about a Message
type MessageDetail struct {
ID string `json:"messageId"`
MessageURI string `json:"messageURI"`
Status string `json:"messageStatus"`
SegmentCount string `json:"messageSegmentCount"`
Time time.Time `json:"messageTime"`
Media MessageMedia `json:"media"`
Stickers []MessageSticker `json:"stickers"`
}

// MessageMedia describes the Media of a Message
type MessageMedia struct {
ID string `json:"id"`
Name string `json:"name"`
URL string `json:"url"`
MediaType string `json:"mediaType"`
ContentLength string `json:"contentLengthBytes"`
}

// MessageSticker describes a Message Sticker
type MessageSticker struct {
ID string `json:"id"`
URL string `json:"url"`
// Screenshares []ScreenShare `json:"screenshares"`
// SocialExpressions []SocialExpression `json:"socialExpressions"`
// Videos []Video `json:"videos"`
}
28 changes: 28 additions & 0 deletions message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package gcloudcx

import "time"

// MessageDetails describes details of a Message in a Message Conversation
type MessageDetails struct {
ID string `json:"messageId"`
Status string `json:"messageStatus"`
SegmentCount int `json:"messageSegmentCount"`
Time time.Time `json:"messageTime"`
Media []MessageMedia `json:"media"`
Stickers []MessageSticker `json:"stickers"`
}

// MessageMedia describes the Media of a Message
type MessageMedia struct {
ID string `json:"id"`
Name string `json:"name"`
URL string `json:"url"`
MediaType string `json:"mediaType"`
ContentLength int64 `json:"contentLengthBytes"`
}

// MessageSticker describes a Message Sticker
type MessageSticker struct {
ID string `json:"id"`
URL string `json:"url"`
}
41 changes: 40 additions & 1 deletion openmessaging_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ package gcloudcx

import (
"encoding/json"
"mime"
"net/url"
"path"
"strings"

"github.com/gildas/go-core"
"github.com/gildas/go-errors"
"github.com/gildas/go-request"
nanoid "github.com/matoous/go-nanoid/v2"
)

type OpenMessageAttachment struct {
ID string `json:"id"`
ID string `json:"id,omitempty"`
Type string `json:"mediaType"`
URL *url.URL `json:"-"`
Mime string `json:"mime,omitempty"`
Expand All @@ -18,6 +23,40 @@ type OpenMessageAttachment struct {
Hash string `json:"sha256,omitempty"`
}

func (attachment OpenMessageAttachment) WithContent(content *request.Content) *OpenMessageAttachment {
var attachmentType string
switch {
case len(content.Type) == 0:
attachmentType = "Link"
case strings.HasPrefix(content.Type, "audio"):
attachmentType = "Audio"
case strings.HasPrefix(content.Type, "image"):
attachmentType = "Image"
case strings.HasPrefix(content.Type, "video"):
attachmentType = "Video"
default:
attachmentType = "File"
}

attachment.Type = attachmentType
attachment.Mime = content.Type
attachment.Filename = content.Name
attachment.URL = content.URL

if attachmentType != "Link" && len(content.Name) == 0 {
fileExtension := path.Ext(content.URL.Path)
if content.Type == "audio/mpeg" {
fileExtension = ".mp3"
} else if fileExtensions, err := mime.ExtensionsByType(content.Type); err == nil && len(fileExtensions) > 0 {
fileExtension = fileExtensions[0]
}
fileID, _ := nanoid.New()
attachment.Filename = strings.ToLower(attachmentType) + "-" + fileID + fileExtension
}

return &attachment
}

// MarshalJSON marshals this into JSON
func (attachment OpenMessageAttachment) MarshalJSON() ([]byte, error) {
type surrogate OpenMessageAttachment
Expand Down
41 changes: 4 additions & 37 deletions openmessaging_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ package gcloudcx
import (
"context"
"encoding/json"
"mime"
"net/url"
"path"
"strings"
"time"

"github.com/gildas/go-core"
"github.com/gildas/go-errors"
"github.com/gildas/go-logger"
"github.com/google/uuid"
nanoid "github.com/matoous/go-nanoid/v2"
)

// OpenMessagingIntegration describes an GCloud OpenMessaging Integration
Expand Down Expand Up @@ -255,41 +252,17 @@ func (integration *OpenMessagingIntegration) SendInboundMessage(context context.
//
// See https://developer.genesys.cloud/api/digital/openmessaging/inboundMessages#inbound-message-with-attached-photo
// See https://developer.genesys.cloud/api/rest/v2/conversations/#post-api-v2-conversations-messages-inbound-open
func (integration *OpenMessagingIntegration) SendInboundMessageWithAttachment(context context.Context, from *OpenMessageFrom, messageID, text string, attachmentURL *url.URL, attachmentMimeType, attachmentID string, attributes map[string]string, metadata map[string]string) (id string, err error) {
func (integration *OpenMessagingIntegration) SendInboundMessageWithAttachment(context context.Context, from *OpenMessageFrom, messageID, text string, attachment *OpenMessageAttachment, attributes map[string]string, metadata map[string]string) (id string, err error) {
if integration.ID == uuid.Nil {
return "", errors.ArgumentMissing.With("ID")
}
if len(messageID) == 0 {
return "", errors.ArgumentMissing.With("messageID")
}
if attachmentURL == nil {
if attachment.URL == nil {
return "", errors.ArgumentMissing.With("url")
}

var attachmentType string
switch {
case len(attachmentMimeType) == 0:
attachmentType = "Link"
case strings.HasPrefix(attachmentMimeType, "audio"):
attachmentType = "Audio"
case strings.HasPrefix(attachmentMimeType, "image"):
attachmentType = "Image"
case strings.HasPrefix(attachmentMimeType, "video"):
attachmentType = "Video"
default:
attachmentType = "File"
}

var attachmentFilename string
if attachmentType != "Link" {
fileExtension := path.Ext(attachmentURL.Path)
if fileExtensions, err := mime.ExtensionsByType(attachmentMimeType); err == nil && len(fileExtensions) > 0 {
fileExtension = fileExtensions[0]
}
fileID, _ := nanoid.New()
attachmentFilename = strings.ToLower(attachmentType) + "-" + fileID + fileExtension
}

result := OpenMessageText{}
err = integration.client.Post(
integration.logger.ToContext(context),
Expand All @@ -304,14 +277,8 @@ func (integration *OpenMessagingIntegration) SendInboundMessageWithAttachment(co
Text: text,
Content: []*OpenMessageContent{
{
Type: "Attachment",
Attachment: &OpenMessageAttachment{
Type: attachmentType,
ID: attachmentID,
Mime: attachmentMimeType,
URL: attachmentURL,
Filename: attachmentFilename,
},
Type: "Attachment",
Attachment: attachment,
},
},
Metadata: metadata,
Expand Down
8 changes: 8 additions & 0 deletions participant.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Participant struct {
Queue *Queue `json:"queue,omitempty"`
QueueID string `json:"queueId,omitempty"`
GroupID string `json:"groupId,omitempty"`
TeamID string `json:"teamId,omitempty"`
QueueName string `json:"queueName,omitempty"`
ConsultParticipantID string `json:"consultParticipantId,omitempty"`
MonitoredParticipantID string `json:"monitoredParticipantId,omitempty"`
Expand Down Expand Up @@ -167,6 +168,11 @@ func (participant Participant) String() string {
return participant.ID.String()
}

// Disconnect disconnects the Participant from the target
func (participant *Participant) Disconnect(context context.Context, target Disconnecter) error {
return target.Disconnect(context, participant)
}

// UpdateState updates the state of the Participant in target
func (participant *Participant) UpdateState(context context.Context, target StateUpdater, state string) error {
return target.UpdateState(context, participant, state)
Expand Down Expand Up @@ -202,6 +208,8 @@ func (participant *Participant) UnmarshalJSON(payload []byte) (err error) {
type surrogate Participant
var inner struct {
surrogate
QueueID uuid.UUID `json:"queueId"`
QueueName string `json:"queueName"`
UserID uuid.UUID `json:"userId"`
UserURI URI `json:"userUri"`
AlertingTimeoutMs int64 `json:"alertingTimeoutMs"`
Expand Down
2 changes: 1 addition & 1 deletion version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package gcloudcx
var commit string

// VERSION is the version of this application
var VERSION = "0.7.11" + commit
var VERSION = "0.7.12" + commit

// APP is the name of the application
const APP string = "GCloudCX Client"

0 comments on commit acb5de9

Please sign in to comment.