Add support for downloading files (nctalk) (#1249)

Signed-off-by: Gary Kim <gary@garykim.dev>
This commit is contained in:
Gary Kim 2020-10-19 17:16:34 -04:00 committed by GitHub
parent 25c82ddf02
commit 950f2759bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 49 additions and 237 deletions

View File

@ -9,7 +9,6 @@ import (
"github.com/42wim/matterbridge/bridge"
"github.com/42wim/matterbridge/bridge/config"
talk "gomod.garykim.dev/nc-talk"
"gomod.garykim.dev/nc-talk/ocs"
"gomod.garykim.dev/nc-talk/room"
"gomod.garykim.dev/nc-talk/user"
@ -61,8 +60,12 @@ func (b *Btalk) Disconnect() error {
}
func (b *Btalk) JoinChannel(channel config.ChannelInfo) error {
tr, err := room.NewTalkRoom(b.user, channel.Name)
if err != nil {
return err
}
newRoom := Broom{
room: talk.NewRoom(b.user, channel.Name),
room: tr,
}
newRoom.ctx, newRoom.ctxCancel = context.WithCancel(context.Background())
c, err := newRoom.room.ReceiveMessages(newRoom.ctx)
@ -79,6 +82,7 @@ func (b *Btalk) JoinChannel(channel config.ChannelInfo) error {
go func() {
for msg := range c {
msg := msg
// ignore messages that are one of the following
// * not a message from a user
// * from ourselves
@ -97,6 +101,15 @@ func (b *Btalk) JoinChannel(channel config.ChannelInfo) error {
if msg.ID != 0 {
remoteMessage.ID = strconv.Itoa(msg.ID)
}
// Handle Files
err = b.handleFiles(&remoteMessage, &msg)
if err != nil {
b.Log.Errorf("Error handling file: %#v", msg)
continue
}
b.Log.Debugf("<= Message is %#v", remoteMessage)
b.Remote <- remoteMessage
}
@ -132,6 +145,31 @@ func (b *Btalk) getRoom(token string) *Broom {
return nil
}
func (b *Btalk) handleFiles(mmsg *config.Message, message *ocs.TalkRoomMessageData) error {
for _, parameter := range message.MessageParameters {
if parameter.Type == ocs.ROSTypeFile {
// Get the file
file, err := b.user.DownloadFile(parameter.Path)
if err != nil {
return err
}
if mmsg.Extra == nil {
mmsg.Extra = make(map[string][]interface{})
}
mmsg.Extra["file"] = append(mmsg.Extra["file"], config.FileInfo{
Name: parameter.Name,
Data: file,
Size: int64(len(*file)),
Avatar: false,
})
}
}
return nil
}
// Spec: https://github.com/nextcloud/server/issues/1706#issue-182308785
func formatRichObjectString(message string, parameters map[string]ocs.RichObjectString) string {
for id, parameter := range parameters {
@ -142,7 +180,7 @@ func formatRichObjectString(message string, parameters map[string]ocs.RichObject
text = "@" + text
case ocs.ROSTypeFile:
if parameter.Link != "" {
text = parameter.Link
text = parameter.Name
}
}

2
go.mod
View File

@ -50,7 +50,7 @@ require (
github.com/zfjagann/golang-ring v0.0.0-20190304061218-d34796e0a6c2
golang.org/x/image v0.0.0-20200801110659-972c09e46d76
golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43
gomod.garykim.dev/nc-talk v0.1.4
gomod.garykim.dev/nc-talk v0.1.5
gopkg.in/olahol/melody.v1 v1.0.0-20170518105555-d52139073376
layeh.com/gumble v0.0.0-20200818122324-146f9205029b
)

2
go.sum
View File

@ -993,6 +993,8 @@ golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1N
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gomod.garykim.dev/nc-talk v0.1.4 h1:U9viudEgq/biocorgWvZRVR+27IPEczYl/yszSvzN+8=
gomod.garykim.dev/nc-talk v0.1.4/go.mod h1:zKg8yxCk2KaTy6aPDEfRac0Jik72czX+nRsG8CZuhtc=
gomod.garykim.dev/nc-talk v0.1.5 h1:zZ/FviVpwJuhD/YrKiAvs6Z3Oew/DL/w6RKbKaanhFA=
gomod.garykim.dev/nc-talk v0.1.5/go.mod h1:zKg8yxCk2KaTy6aPDEfRac0Jik72czX+nRsG8CZuhtc=
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=

View File

@ -1,24 +0,0 @@
kind: pipeline
type: docker
name: test
steps:
- name: golangci-lint
image: golangci/golangci-lint:latest-alpine
commands:
- golangci-lint run
- name: test
image: golang:1.13
commands:
- go test ./...
- name: build-test
image: golang:1.13
commands:
- go build
trigger:
branch:
- master
event:
- pull_request
- push

View File

@ -1,12 +0,0 @@
user=gary-kim
project=go-nc-talk
add_sections={"dependencies": {"labels": ["dependencies"], "prefix": "### Dependencies"}, "Added": {"labels": ["feature"], "prefix": "### Added"}}
output=
header_label=# Go Library for Nextcloud Talk
enhancement_prefix=### Changed
deprecated_prefix=### Deprecated
removed_prefix=### Removed
security_prefix=### Security
bug_prefix=### Fixed
add_pr_wo_labels=false
issues=false

View File

@ -1,26 +0,0 @@
# golangci-lint configuration options
linters:
enable:
- deadcode
- errcheck
- goimports
- golint
- ineffassign
- structcheck
- varcheck
- govet
- unconvert
- prealloc
- maligned
disable-all: false
issues:
# Enable some lints excluded by default
exclude-use-default: false
# Maximum issues count per one linter. Set to 0 to disable. Default is 50.
max-per-linter: 0
# Maximum count of issues with the same text. Set to 0 to disable. Default is 3.
max-same-issues: 0

View File

@ -1,71 +0,0 @@
# Go Library for Nextcloud Talk
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [v0.1.4](https://github.com/gary-kim/go-nc-talk/tree/v0.1.4) - 2020-09-22
[Full Changelog](https://github.com/gary-kim/go-nc-talk/compare/v0.1.3...v0.1.4)
### Fixed
- Add ActorType for message data [\#18](https://github.com/gary-kim/go-nc-talk/pull/18) ([@gary-kim](https://github.com/gary-kim))
## [v0.1.3](https://github.com/gary-kim/go-nc-talk/tree/v0.1.3) - 2020-09-03
[Full Changelog](https://github.com/gary-kim/go-nc-talk/compare/v0.1.2...v0.1.3)
### Fixed
- Close response bodies [\#15](https://github.com/gary-kim/go-nc-talk/pull/15) ([@gary-kim](https://github.com/gary-kim))
## [v0.1.2](https://github.com/gary-kim/go-nc-talk/tree/v0.1.2) - 2020-08-28
[Full Changelog](https://github.com/gary-kim/go-nc-talk/compare/v0.1.1...v0.1.2)
### Fixed
- Use lastReadMessage for first lastKnownMessageId [\#14](https://github.com/gary-kim/go-nc-talk/pull/14) ([@tilosp](https://github.com/tilosp))
## [v0.1.1](https://github.com/gary-kim/go-nc-talk/tree/v0.1.1) - 2020-08-24
[Full Changelog](https://github.com/gary-kim/go-nc-talk/compare/v0.1.0...v0.1.1)
### Fixed
- ROS type should be of ROST type [\#12](https://github.com/gary-kim/go-nc-talk/pull/12) ([@gary-kim](https://github.com/gary-kim))
- Fix error when sending a message with no RCS data [\#10](https://github.com/gary-kim/go-nc-talk/pull/10) ([@gary-kim](https://github.com/gary-kim))
## [v0.1.0](https://github.com/gary-kim/go-nc-talk/tree/v0.1.0) - 2020-08-13
[Full Changelog](https://github.com/gary-kim/go-nc-talk/compare/v0.0.2...v0.1.0)
### Added
- Add TLSConfig [\#9](https://github.com/gary-kim/go-nc-talk/pull/9) ([@gary-kim](https://github.com/gary-kim))
- Add Software using this library in README [\#8](https://github.com/gary-kim/go-nc-talk/pull/8) ([@gary-kim](https://github.com/gary-kim))
- Add some basic tests [\#7](https://github.com/gary-kim/go-nc-talk/pull/7) ([@gary-kim](https://github.com/gary-kim))
- Add support for downloading files [\#1](https://github.com/gary-kim/go-nc-talk/pull/1) ([@gary-kim](https://github.com/gary-kim))
### Fixed
- Return error on blank token [\#6](https://github.com/gary-kim/go-nc-talk/pull/6) ([@gary-kim](https://github.com/gary-kim))
- Add v0.0.2 to changelog [\#4](https://github.com/gary-kim/go-nc-talk/pull/4) ([@gary-kim](https://github.com/gary-kim))
## [v0.0.2](https://github.com/gary-kim/go-nc-talk/tree/v0.0.2) - 2020-07-26
[Full Changelog](https://github.com/gary-kim/go-nc-talk/compare/v0.0.1...v0.0.2)
### Changed
- Add installation instructions to README.md [\#2](https://github.com/gary-kim/go-nc-talk/pull/2) ([@gary-kim](https://github.com/gary-kim))
### Fixed
- Fix Capabilities Request [\#3](https://github.com/gary-kim/go-nc-talk/pull/3) ([@gary-kim](https://github.com/gary-kim))
## [v0.0.1](https://github.com/gary-kim/riotchat/tree/v0.0.1) - 2020-07-10
* First release

View File

@ -1,27 +0,0 @@
# Go Library for Nextcloud Talk
[![Build Status](https://ghdrone.garykim.dev/api/badges/gary-kim/go-nc-talk/status.svg)](https://ghdrone.garykim.dev/gary-kim/go-nc-talk)
[![Godoc](https://img.shields.io/badge/godoc-gomod.garykim.dev%2Fnc--talk-informational)](https://pkg.go.dev/gomod.garykim.dev/nc-talk)
A Go library that can be used to communicate with [Nextcloud Talk](https://github.com/nextcloud/spreed) instances.
### Installing
You can use this library in your Go projects by installing it with go mod:
```bash
GO111MODULE=on go get gomod.garykim.dev/nc-talk
```
Check out the documentation for the package [here](https://pkg.go.dev/gomod.garykim.dev/nc-talk).
### Software using this library
* [Matterbridge](https://github.com/42wim/matterbridge)
### License
Copyright © 2020 Gary Kim &lt;<gary@garykim.dev>&gt;, All Rights Reserved
Licensed under [Apache-2.0](LICENSE)

View File

@ -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 + "/"
}

View File

@ -1,8 +0,0 @@
module gomod.garykim.dev/nc-talk
go 1.13
require (
github.com/monaco-io/request v1.0.4
github.com/stretchr/testify v1.6.1
)

View File

@ -1,15 +0,0 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/monaco-io/request v1.0.3 h1:FsiIwXCCbHEyWx9A7lgg6JBTMHhHlEEsADsgAOvZ9HA=
github.com/monaco-io/request v1.0.3/go.mod h1:EmggwHktBsbJmCgwZXqy7o0H1NNsAstQBWZrFVd3xtQ=
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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -1,44 +0,0 @@
// Copyright (c) 2020 Gary Kim <gary@garykim.dev>, All Rights Reserved
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package talk
import (
"gomod.garykim.dev/nc-talk/room"
"gomod.garykim.dev/nc-talk/user"
)
// NewUser returns a TalkUser instance
// The url should be the full URL of the Nextcloud instance (e.g. https://cloud.mydomain.me)
//
// Deprecated: Use user.NewUser instead for more options and error checks
func NewUser(url string, username string, password string) *user.TalkUser {
return &user.TalkUser{
NextcloudURL: url,
User: username,
Pass: password,
}
}
// NewRoom 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)
//
// Deprecated: Use room.NewRoom instead for extra error checks.
func NewRoom(tuser *user.TalkUser, token string) *room.TalkRoom {
tr := &room.TalkRoom{
User: tuser,
Token: token,
}
return tr
}

View File

@ -62,7 +62,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 +93,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",
@ -154,7 +154,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",

3
vendor/modules.txt vendored
View File

@ -293,8 +293,7 @@ golang.org/x/text/secure/bidirule
golang.org/x/text/transform
golang.org/x/text/unicode/bidi
golang.org/x/text/unicode/norm
# gomod.garykim.dev/nc-talk v0.1.4
gomod.garykim.dev/nc-talk
# gomod.garykim.dev/nc-talk v0.1.5
gomod.garykim.dev/nc-talk/constants
gomod.garykim.dev/nc-talk/ocs
gomod.garykim.dev/nc-talk/room