Update go-nc-talk (nctalk) (#1333)

Signed-off-by: Gary Kim <gary@garykim.dev>
pull/1323/head
Gary Kim 4 years ago committed by GitHub
parent 0d7315249d
commit 41b4e64be9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -83,6 +83,13 @@ func (b *Btalk) JoinChannel(channel config.ChannelInfo) error {
go func() {
for msg := range c {
msg := msg
if msg.Error != nil {
b.Log.Errorf("Fatal message poll error: %s\n", msg.Error)
return
}
// ignore messages that are one of the following
// * not a message from a user
// * from ourselves

@ -49,7 +49,7 @@ require (
github.com/zfjagann/golang-ring v0.0.0-20190304061218-d34796e0a6c2
golang.org/x/image v0.0.0-20200927104501-e162460cd6b5
golang.org/x/oauth2 v0.0.0-20201203001011-0b49973bad19
gomod.garykim.dev/nc-talk v0.1.6
gomod.garykim.dev/nc-talk v0.1.7
gopkg.in/olahol/melody.v1 v1.0.0-20170518105555-d52139073376
layeh.com/gumble v0.0.0-20200818122324-146f9205029b
)

@ -581,8 +581,8 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJ
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/monaco-io/request v1.0.4 h1:AbogA+IvPOWqyGZIFU7kSb8YS2Jv5Dnl5ncMj8cQV+o=
github.com/monaco-io/request v1.0.4/go.mod h1:EmggwHktBsbJmCgwZXqy7o0H1NNsAstQBWZrFVd3xtQ=
github.com/monaco-io/request v1.0.5 h1:QAJb5m1pCPZUGv3zzTZn7GlQI3q+uJWi7fH9QxDGbm4=
github.com/monaco-io/request v1.0.5/go.mod h1:EmggwHktBsbJmCgwZXqy7o0H1NNsAstQBWZrFVd3xtQ=
github.com/moul/http2curl v1.0.0/go.mod h1:8UbvGypXm98wA/IqH45anm5Y2Z6ep6O31QGOAZ3H0fQ=
github.com/mreiferson/go-httpclient v0.0.0-20160630210159-31f0106b4474 h1:oKIteTqeSpenyTrOVj5zkiyCaflLa8B+CD0324otT+o=
github.com/mreiferson/go-httpclient v0.0.0-20160630210159-31f0106b4474/go.mod h1:OQA4XLvDbMgS8P0CevmM4m9Q3Jq4phKUzcocxuGJ5m8=
@ -1208,8 +1208,8 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IV
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gomod.garykim.dev/nc-talk v0.1.6 h1:ZanTsFiAYKwuEJkV5GjMHop5dEGuCNzEDi4kWh/aqYg=
gomod.garykim.dev/nc-talk v0.1.6/go.mod h1:zKg8yxCk2KaTy6aPDEfRac0Jik72czX+nRsG8CZuhtc=
gomod.garykim.dev/nc-talk v0.1.7 h1:G2qsiRcyaj5FEADQlulsBAFJHs27tPmH9VtKK+at9SM=
gomod.garykim.dev/nc-talk v0.1.7/go.mod h1:DNucAJ6zeaumBEwV5NiYk+Eea8Ca+Q5f+plhz9F7d58=
google.golang.org/api v0.0.0-20180910000450-7ca32eb868bf/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0=
google.golang.org/api v0.0.0-20181030000543-1d582fd0359e/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0=
google.golang.org/api v0.0.0-20181220000619-583d854617af/go.mod h1:4mhQ8q/RsB7i+udVvVy5NUi08OU8ZlA0gRVgrF7VFY0=

@ -14,17 +14,18 @@ func (c *Client) buildRequest() (err error) {
return
}
c.transport = &http.Transport{}
c.applyHTTPHeader()
c.applyBasicAuth()
c.applyClient()
c.applyTimeout()
c.applyCookies()
// Apply transport needs to be called before TLSConfig as TLSConfig modifies
// the http transport
c.applyTransport()
c.applyTLSConfig()
err = c.applyProxy()
c.client.Transport = c.transport
c.client.Transport = c.Transport
return
}
@ -84,15 +85,20 @@ func (c *Client) applyProxy() (err error) {
if proxy, err = url.Parse(c.ProxyURL); err != nil {
return
} else if proxy != nil {
c.transport.Proxy = http.ProxyURL(proxy)
c.Transport.Proxy = http.ProxyURL(proxy)
}
}
return
}
func (c *Client) applyTLSConfig() {
// &tls.Config{InsecureSkipVerify: true}
if c.TLSConfig != nil {
c.transport.TLSClientConfig = c.TLSConfig
c.Transport.TLSClientConfig = c.TLSConfig
}
}
func (c *Client) applyTransport() {
if c.Transport == nil {
c.Transport = &http.Transport{}
}
}

@ -39,12 +39,12 @@ type Client struct {
ContentType ContentType
Cookies []*http.Cookie
TLSConfig *tls.Config
Transport *http.Transport
// private
client *http.Client
requestURL requestURL
req *http.Request
transport *http.Transport
}
// BasicAuth Add Username:Password as Basic Auth

@ -21,5 +21,5 @@ const (
// RemoteDavEndpoint returns the endpoint for the Dav API for Nextcloud
func RemoteDavEndpoint(username string, davType string) string {
return "/remote.php/dav/" + username + "/" + davType + "/"
return "/remote.php/dav/" + davType + "/" + username + "/"
}

@ -43,7 +43,10 @@ const (
)
// TalkRoomMessageData describes the data part of a ocs response for a Talk room message
//
// Error will be set if a message request ran into an error.
type TalkRoomMessageData struct {
Error error `json:"-"`
Message string `json:"message"`
ID int `json:"id"`
ActorType ActorType `json:"actorType"`

@ -33,10 +33,14 @@ var (
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")
// ErrUnauthorized is returned when the room could not be accessed due to being unauthorized
ErrUnauthorized = errors.New("unauthorized error when accessing room")
// 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")
// ErrTooManyRequests is returned if the server returns a 429
ErrTooManyRequests = errors.New("too many requests")
)
// TalkRoom represents a room in Nextcloud Talk
@ -62,7 +66,7 @@ func NewTalkRoom(tuser *user.TalkUser, token string) (*TalkRoom, error) {
// 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
url := t.User.NextcloudURL + constants.BaseEndpoint + "chat/" + t.Token
requestParams := map[string]string{
"message": msg,
}
@ -93,7 +97,7 @@ func (t *TalkRoom) ReceiveMessages(ctx context.Context) (chan ocs.TalkRoomMessag
if err != nil {
return nil, err
}
url := t.User.NextcloudURL + constants.BaseEndpoint + "/chat/" + t.Token
url := t.User.NextcloudURL + constants.BaseEndpoint + "chat/" + t.Token
requestParam := map[string]string{
"lookIntoFuture": "1",
"includeLastKnown": "0",
@ -127,6 +131,24 @@ func (t *TalkRoom) ReceiveMessages(ctx context.Context) (chan ocs.TalkRoomMessag
if err != nil {
continue
}
// If it seems that we no longer have access to the chat for one reason or another, stop the goroutine and set error in the next return.
if res.StatusCode == 404 {
_ = res.Body.Close()
c <- ocs.TalkRoomMessageData{Error: ErrRoomNotFound}
return
}
if res.StatusCode == 401 {
_ = res.Body.Close()
c <- ocs.TalkRoomMessageData{Error: ErrUnauthorized}
return
}
if res.StatusCode == 429 {
_ = res.Body.Close()
c <- ocs.TalkRoomMessageData{Error: ErrTooManyRequests}
return
}
if res.StatusCode == 200 {
lastKnown = res.Header.Get("X-Chat-Last-Given")
data, err := ioutil.ReadAll(res.Body)
@ -154,7 +176,7 @@ func (t *TalkRoom) TestConnection() error {
if t.Token == "" {
return ErrEmptyToken
}
url := t.User.NextcloudURL + constants.BaseEndpoint + "/chat/" + t.Token
url := t.User.NextcloudURL + constants.BaseEndpoint + "chat/" + t.Token
requestParam := map[string]string{
"lookIntoFuture": "0",
"includeLastKnown": "0",

@ -87,38 +87,38 @@ type Capabilities struct {
// RoomInfo contains information about a room
type RoomInfo struct {
Token string `json:"token"`
Name string `json:"name"`
DisplayName string `json:"displayName"`
SessionID string `json:"sessionId"`
ObjectType string `json:"objectType"`
ObjectID string `json:"objectId"`
Type int `json:"type"`
ParticipantType int `json:"participantType"`
ParticipantFlags int `json:"participantFlags"`
ReadOnly int `json:"readOnly"`
LastPing int `json:"lastPing"`
LastActivity int `json:"lastActivity"`
NotificationLevel int `json:"notificationLevel"`
LobbyState int `json:"lobbyState"`
LobbyTimer int `json:"lobbyTimer"`
UnreadMessages int `json:"unreadMessages"`
LastReadMessage int `json:"lastReadMessage"`
HasPassword bool `json:"hasPassword"`
HasCall bool `json:"hasCall"`
CanStartCall bool `json:"canStartCall"`
CanDeleteConversation bool `json:"canDeleteConversation"`
CanLeaveConversation bool `json:"canLeaveConversation"`
IsFavorite bool `json:"isFavorite"`
UnreadMention bool `json:"unreadMention"`
LastMessage ocs.TalkRoomMessageData `json:"lastMessage"`
Token string `json:"token"`
Name string `json:"name"`
DisplayName string `json:"displayName"`
SessionID string `json:"sessionId"`
ObjectType string `json:"objectType"`
ObjectID string `json:"objectId"`
Type int `json:"type"`
ParticipantType int `json:"participantType"`
ParticipantFlags int `json:"participantFlags"`
ReadOnly int `json:"readOnly"`
LastPing int `json:"lastPing"`
LastActivity int `json:"lastActivity"`
NotificationLevel int `json:"notificationLevel"`
LobbyState int `json:"lobbyState"`
LobbyTimer int `json:"lobbyTimer"`
UnreadMessages int `json:"unreadMessages"`
LastReadMessage int `json:"lastReadMessage"`
HasPassword bool `json:"hasPassword"`
HasCall bool `json:"hasCall"`
CanStartCall bool `json:"canStartCall"`
CanDeleteConversation bool `json:"canDeleteConversation"`
CanLeaveConversation bool `json:"canLeaveConversation"`
IsFavorite bool `json:"isFavorite"`
UnreadMention bool `json:"unreadMention"`
LastMessage *ocs.TalkRoomMessageData `json:"lastMessage"`
}
// NewUser returns a TalkUser instance
// The url should be the full URL of the Nextcloud instance (e.g. https://cloud.mydomain.me)
func NewUser(url string, username string, password string, config *TalkUserConfig) (*TalkUser, error) {
return &TalkUser{
NextcloudURL: url,
NextcloudURL: strings.TrimSuffix(url, "/"),
User: username,
Pass: password,
Config: config,
@ -143,7 +143,11 @@ func (t *TalkUser) RequestClient(client request.Client) *request.Client {
// Set Nextcloud URL if there is no host
if !strings.HasPrefix(client.URL, t.NextcloudURL) {
client.URL = t.NextcloudURL + "/" + client.URL
if strings.HasPrefix(client.URL, "/") {
client.URL = t.NextcloudURL + client.URL
} else {
client.URL = t.NextcloudURL + "/" + client.URL
}
}
// Set TLS Config

@ -180,7 +180,7 @@ github.com/mgutz/ansi
github.com/missdeer/golib/ic
# github.com/mitchellh/mapstructure v1.3.3
github.com/mitchellh/mapstructure
# github.com/monaco-io/request v1.0.4
# github.com/monaco-io/request v1.0.5
github.com/monaco-io/request
# github.com/mreiferson/go-httpclient v0.0.0-20160630210159-31f0106b4474
## explicit
@ -362,7 +362,7 @@ golang.org/x/text/transform
golang.org/x/text/unicode/bidi
golang.org/x/text/unicode/norm
golang.org/x/text/width
# gomod.garykim.dev/nc-talk v0.1.6
# gomod.garykim.dev/nc-talk v0.1.7
## explicit
gomod.garykim.dev/nc-talk/constants
gomod.garykim.dev/nc-talk/ocs

Loading…
Cancel
Save