summaryrefslogtreecommitdiffstats
path: root/vendor/gomod.garykim.dev/nc-talk/user/user.go
blob: 2f8a3c0a08499342ae861954aaa180329d63465b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// 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 user

import (
	"crypto/tls"
	"encoding/json"
	"errors"
	"strings"

	"github.com/monaco-io/request"

	"gomod.garykim.dev/nc-talk/constants"
	"gomod.garykim.dev/nc-talk/ocs"
)

const (
	ocsCapabilitiesEndpoint = "/ocs/v2.php/cloud/capabilities"
)

var (
	// ErrUserIsNil is returned when a funciton is called with an nil user.
	ErrUserIsNil = errors.New("user is nil")
)

// TalkUser represents a user of Nextcloud Talk
type TalkUser struct {
	User         string
	Pass         string
	NextcloudURL string
	Config       *TalkUserConfig
	capabilities *Capabilities
}

// TalkUserConfig is configuration options for TalkUsers
type TalkUserConfig struct {
	TLSConfig *tls.Config
}

// Capabilities describes the capabilities that the Nextcloud Talk instance is capable of. Visit https://nextcloud-talk.readthedocs.io/en/latest/capabilities/ for more info.
type Capabilities struct {
	AttachmentsFolder      string `ocscapability:"config => attachments => folder"`
	ChatMaxLength          int
	Audio                  bool `ocscapability:"audio"`
	Video                  bool `ocscapability:"video"`
	Chat                   bool `ocscapability:"chat"`
	GuestSignaling         bool `ocscapability:"guest-signaling"`
	EmptyGroupRoom         bool `ocscapability:"empty-group-room"`
	GuestDisplayNames      bool `ocscapability:"guest-display-names"`
	MultiRoomUsers         bool `ocscapability:"multi-room-users"`
	ChatV2                 bool `ocscapability:"chat-v2"`
	Favorites              bool `ocscapability:"favorites"`
	LastRoomActivity       bool `ocscapability:"last-room-activity"`
	NoPing                 bool `ocscapability:"no-ping"`
	SystemMessages         bool `ocscapability:"system-messages"`
	MentionFlag            bool `ocscapability:"mention-flag"`
	InCallFlags            bool `ocscapability:"in-call-flags"`
	InviteByMail           bool `ocscapability:"invite-by-mail"`
	NotificationLevels     bool `ocscapability:"notification-levels"`
	InviteGroupsAndMails   bool `ocscapability:"invite-groups-and-mails"`
	LockedOneToOneRooms    bool `ocscapability:"locked-one-to-one-rooms"`
	ReadOnlyRooms          bool `ocscapability:"read-only-rooms"`
	ChatReadMarker         bool `ocscapability:"chat-read-marker"`
	WebinaryLobby          bool `ocscapability:"webinary-lobby"`
	StartCallFlag          bool `ocscapability:"start-call-flag"`
	ChatReplies            bool `ocscapability:"chat-replies"`
	CirclesSupport         bool `ocscapability:"circles-support"`
	AttachmentsAllowed     bool `ocscapability:"config => attachments => allowed"`
	ConversationsCanCreate bool `ocscapability:"config => conversations => can-create"`
	ForceMute              bool `ocscapability:"force-mute"`
	ConversationV2         bool `ocscapability:"conversation-v2"`
	ChatReferenceID        bool `ocscapability:"chat-reference-id"`
}

// 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,
		User:         username,
		Pass:         password,
		Config:       config,
	}, nil
}

// RequestClient returns a monaco-io that is preconfigured to make OCS API calls
func (t *TalkUser) RequestClient(client request.Client) *request.Client {
	if client.Header == nil {
		client.Header = make(map[string]string)
	}
	if client.Header["OCS-APIRequest"] == "" {
		client.Header["OCS-APIRequest"] = "true"
	}
	if client.Header["Accept"] == "" {
		client.Header["Accept"] = "application/json"
	}
	client.BasicAuth = request.BasicAuth{
		Username: t.User,
		Password: t.Pass,
	}

	// Set Nextcloud URL if there is no host
	if !strings.HasPrefix(client.URL, t.NextcloudURL) {
		client.URL = t.NextcloudURL + "/" + client.URL
	}

	// Set TLS Config
	if t.Config != nil {
		client.TLSConfig = t.Config.TLSConfig
	}

	return &client
}

// Capabilities returns an instance of Capabilities that describes what the Nextcloud Talk instance supports
func (t *TalkUser) Capabilities() (*Capabilities, error) {
	if t.capabilities != nil {
		return t.capabilities, nil
	}

	client := t.RequestClient(request.Client{
		URL: ocsCapabilitiesEndpoint,
	})
	res, err := client.Do()
	if err != nil {
		return nil, err
	}

	capabilitiesRequest := &struct {
		Ocs ocs.Capabilities `json:"ocs"`
	}{}

	err = json.Unmarshal(res.Data, capabilitiesRequest)
	if err != nil {
		return nil, err
	}

	sc := capabilitiesRequest.Ocs.Data.Capabilities.SpreedCapabilities

	tr := &Capabilities{
		Audio:                  sliceContains(sc.Features, "audio"),
		Video:                  sliceContains(sc.Features, "video"),
		Chat:                   sliceContains(sc.Features, "chat"),
		GuestSignaling:         sliceContains(sc.Features, "guest-signaling"),
		EmptyGroupRoom:         sliceContains(sc.Features, "empty-group-room"),
		GuestDisplayNames:      sliceContains(sc.Features, "guest-display-names"),
		MultiRoomUsers:         sliceContains(sc.Features, "multi-room-users"),
		ChatV2:                 sliceContains(sc.Features, "chat-v2"),
		Favorites:              sliceContains(sc.Features, "favorites"),
		LastRoomActivity:       sliceContains(sc.Features, "last-room-activity"),
		NoPing:                 sliceContains(sc.Features, "no-ping"),
		SystemMessages:         sliceContains(sc.Features, "system-messages"),
		MentionFlag:            sliceContains(sc.Features, "mention-flag"),
		InCallFlags:            sliceContains(sc.Features, "in-call-flags"),
		InviteByMail:           sliceContains(sc.Features, "invite-by-mail"),
		NotificationLevels:     sliceContains(sc.Features, "notification-levels"),
		InviteGroupsAndMails:   sliceContains(sc.Features, "invite-groups-and-mails"),
		LockedOneToOneRooms:    sliceContains(sc.Features, "locked-one-to-one-rooms"),
		ReadOnlyRooms:          sliceContains(sc.Features, "read-only-rooms"),
		ChatReadMarker:         sliceContains(sc.Features, "chat-read-marker"),
		WebinaryLobby:          sliceContains(sc.Features, "webinary-lobby"),
		StartCallFlag:          sliceContains(sc.Features, "start-call-flag"),
		ChatReplies:            sliceContains(sc.Features, "chat-replies"),
		CirclesSupport:         sliceContains(sc.Features, "circles-support"),
		AttachmentsAllowed:     sc.Config.Attachments.Allowed,
		AttachmentsFolder:      sc.Config.Attachments.Folder,
		ConversationsCanCreate: sc.Config.Conversations.CanCreate,
		ForceMute:              sliceContains(sc.Features, "force-mute"),
		ConversationV2:         sliceContains(sc.Features, "conversation-v2"),
		ChatReferenceID:        sliceContains(sc.Features, "chat-reference-id"),
		ChatMaxLength:          sc.Config.Chat.MaxLength,
	}

	t.capabilities = tr
	return tr, nil
}

// sliceContains does the slice contain the string
func sliceContains(s []string, search string) bool {
	for _, n := range s {
		if n == search {
			return true
		}
	}
	return false
}

// DownloadFile downloads the file at the given path
//
// Meant to be used with rich object string's path.
func (t *TalkUser) DownloadFile(path string) (data *[]byte, err error) {
	url := t.NextcloudURL + constants.RemoteDavEndpoint(t.User, "files") + path
	c := t.RequestClient(request.Client{
		URL: url,
	})
	res, err := c.Do()
	if err != nil || res.StatusCode() != 200 {
		return
	}
	data = &res.Data
	return
}