diff options
Diffstat (limited to 'vendor/gomod.garykim.dev/nc-talk/room/room.go')
-rw-r--r-- | vendor/gomod.garykim.dev/nc-talk/room/room.go | 53 |
1 files changed, 41 insertions, 12 deletions
diff --git a/vendor/gomod.garykim.dev/nc-talk/room/room.go b/vendor/gomod.garykim.dev/nc-talk/room/room.go index d8faf7c4..5452993d 100644 --- a/vendor/gomod.garykim.dev/nc-talk/room/room.go +++ b/vendor/gomod.garykim.dev/nc-talk/room/room.go @@ -16,7 +16,6 @@ package room import ( "context" - "encoding/json" "errors" "io/ioutil" "time" @@ -28,12 +27,38 @@ import ( "gomod.garykim.dev/nc-talk/user" ) +var ( + // ErrEmptyToken is returned when the room token is empty + ErrEmptyToken = errors.New("given an empty token") + // ErrRoomNotFound is returned when a room with the given token could not be found + ErrRoomNotFound = errors.New("room could not be found") + // ErrNotModeratorInLobby is returned when the room is in lobby mode but the user is not a moderator + ErrNotModeratorInLobby = errors.New("room is in lobby mode but user is not a moderator") + // ErrUnexpectedReturnCode is returned when the server did not respond with an expected return code + ErrUnexpectedReturnCode = errors.New("unexpected return code") +) + // TalkRoom represents a room in Nextcloud Talk type TalkRoom struct { User *user.TalkUser Token string } +// NewTalkRoom returns a new TalkRoom instance +// Token should be the Nextcloud Room Token (e.g. "d6zoa2zs" if the room URL is https://cloud.mydomain.me/call/d6zoa2zs) +func NewTalkRoom(tuser *user.TalkUser, token string) (*TalkRoom, error) { + if token == "" { + return nil, ErrEmptyToken + } + if tuser == nil { + return nil, user.ErrUserIsNil + } + return &TalkRoom{ + User: tuser, + Token: token, + }, nil +} + // SendMessage sends a message in the Talk room func (t *TalkRoom) SendMessage(msg string) (*ocs.TalkRoomMessageData, error) { url := t.User.NextcloudURL + constants.BaseEndpoint + "/chat/" + t.Token @@ -51,18 +76,22 @@ func (t *TalkRoom) SendMessage(msg string) (*ocs.TalkRoomMessageData, error) { return nil, err } if res.StatusCode() != 201 { - return nil, errors.New("unexpected return code") + return nil, ErrUnexpectedReturnCode } - var msgInfo struct { - OCS ocs.TalkRoomSentResponse `json:"ocs"` + msgInfo, err := ocs.TalkRoomSentResponseUnmarshal(&res.Data) + if err != nil { + return nil, err } - err = json.Unmarshal(res.Data, &msgInfo) return &msgInfo.OCS.TalkRoomMessage, err } // ReceiveMessages starts watching for new messages func (t *TalkRoom) ReceiveMessages(ctx context.Context) (chan ocs.TalkRoomMessageData, error) { c := make(chan ocs.TalkRoomMessageData) + err := t.TestConnection() + if err != nil { + return nil, err + } url := t.User.NextcloudURL + constants.BaseEndpoint + "/chat/" + t.Token requestParam := map[string]string{ "lookIntoFuture": "1", @@ -99,14 +128,11 @@ func (t *TalkRoom) ReceiveMessages(ctx context.Context) (chan ocs.TalkRoomMessag } if res.StatusCode == 200 { lastKnown = res.Header.Get("X-Chat-Last-Given") - var message struct { - OCS ocs.TalkRoomMessage `json:"ocs"` - } data, err := ioutil.ReadAll(res.Body) if err != nil { continue } - err = json.Unmarshal(data, &message) + message, err := ocs.TalkRoomMessageDataUnmarshal(&data) if err != nil { continue } @@ -121,6 +147,9 @@ func (t *TalkRoom) ReceiveMessages(ctx context.Context) (chan ocs.TalkRoomMessag // TestConnection tests the connection with the Nextcloud Talk instance and returns an error if it could not connect func (t *TalkRoom) TestConnection() error { + if t.Token == "" { + return ErrEmptyToken + } url := t.User.NextcloudURL + constants.BaseEndpoint + "/chat/" + t.Token requestParam := map[string]string{ "lookIntoFuture": "0", @@ -142,9 +171,9 @@ func (t *TalkRoom) TestConnection() error { case 304: return nil case 404: - return errors.New("room could not be found") + return ErrRoomNotFound case 412: - return errors.New("room is in lobby mode but user is not a moderator") + return ErrNotModeratorInLobby } - return errors.New("unknown return code") + return ErrUnexpectedReturnCode } |