summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/matterbridge/Rocket.Chat.Go.SDK/realtime/users.go
diff options
context:
space:
mode:
authorWim <wim@42.be>2019-02-10 17:00:11 +0100
committerWim <wim@42.be>2019-02-15 18:19:34 +0100
commit6ebd5cbbd8a941e0bc5f99f0d8e99cfd1d8ac0d7 (patch)
tree47070e58e7802afd80fce53f0048a87a014a7c0d /vendor/github.com/matterbridge/Rocket.Chat.Go.SDK/realtime/users.go
parent2cfd880cdb0df29771bf8f31df8d990ab897889d (diff)
downloadmatterbridge-msglm-6ebd5cbbd8a941e0bc5f99f0d8e99cfd1d8ac0d7.tar.gz
matterbridge-msglm-6ebd5cbbd8a941e0bc5f99f0d8e99cfd1d8ac0d7.tar.bz2
matterbridge-msglm-6ebd5cbbd8a941e0bc5f99f0d8e99cfd1d8ac0d7.zip
Refactor and update RocketChat bridge
* Add support for editing/deleting messages * Add support for uploading files * Add support for avatars * Use the Rocket.Chat.Go.SDK * Use the rest and streaming api
Diffstat (limited to 'vendor/github.com/matterbridge/Rocket.Chat.Go.SDK/realtime/users.go')
-rw-r--r--vendor/github.com/matterbridge/Rocket.Chat.Go.SDK/realtime/users.go103
1 files changed, 103 insertions, 0 deletions
diff --git a/vendor/github.com/matterbridge/Rocket.Chat.Go.SDK/realtime/users.go b/vendor/github.com/matterbridge/Rocket.Chat.Go.SDK/realtime/users.go
new file mode 100644
index 00000000..09a4f1f4
--- /dev/null
+++ b/vendor/github.com/matterbridge/Rocket.Chat.Go.SDK/realtime/users.go
@@ -0,0 +1,103 @@
+package realtime
+
+import (
+ "crypto/sha256"
+ "encoding/hex"
+ "strconv"
+
+ "github.com/Jeffail/gabs"
+ "github.com/matterbridge/Rocket.Chat.Go.SDK/models"
+)
+
+type ddpLoginRequest struct {
+ User ddpUser `json:"user"`
+ Password ddpPassword `json:"password"`
+}
+
+type ddpTokenLoginRequest struct {
+ Token string `json:"resume"`
+}
+
+type ddpUser struct {
+ Email string `json:"email"`
+}
+
+type ddpPassword struct {
+ Digest string `json:"digest"`
+ Algorithm string `json:"algorithm"`
+}
+
+// RegisterUser a new user on the server. This function does not need a logged in user. The registered user gets logged in
+// to set its username.
+func (c *Client) RegisterUser(credentials *models.UserCredentials) (*models.User, error) {
+
+ if _, err := c.ddp.Call("registerUser", credentials); err != nil {
+ return nil, err
+ }
+
+ user, err := c.Login(credentials)
+ if err != nil {
+ return nil, err
+ }
+
+ if _, err := c.ddp.Call("setUsername", credentials.Name); err != nil {
+ return nil, err
+ }
+
+ return user, nil
+}
+
+// Login a user.
+// token shouldn't be nil, otherwise the password and the email are not allowed to be nil.
+//
+// https://rocket.chat/docs/developer-guides/realtime-api/method-calls/login/
+func (c *Client) Login(credentials *models.UserCredentials) (*models.User, error) {
+ var request interface{}
+ if credentials.Token != "" {
+ request = ddpTokenLoginRequest{
+ Token: credentials.Token,
+ }
+ } else {
+ digest := sha256.Sum256([]byte(credentials.Password))
+ request = ddpLoginRequest{
+ User: ddpUser{Email: credentials.Email},
+ Password: ddpPassword{
+ Digest: hex.EncodeToString(digest[:]),
+ Algorithm: "sha-256",
+ },
+ }
+ }
+
+ rawResponse, err := c.ddp.Call("login", request)
+ if err != nil {
+ return nil, err
+ }
+
+ user := getUserFromData(rawResponse.(map[string]interface{}))
+ if credentials.Token == "" {
+ credentials.ID, credentials.Token = user.ID, user.Token
+ }
+
+ return user, nil
+}
+
+func getUserFromData(data interface{}) *models.User {
+ document, _ := gabs.Consume(data)
+
+ expires, _ := strconv.ParseFloat(stringOrZero(document.Path("tokenExpires.$date").Data()), 64)
+ return &models.User{
+ ID: stringOrZero(document.Path("id").Data()),
+ Token: stringOrZero(document.Path("token").Data()),
+ TokenExpires: int64(expires),
+ }
+}
+
+// SetPresence set user presence
+func (c *Client) SetPresence(status string) error {
+ _, err := c.ddp.Call("UserPresence:setDefaultStatus", status)
+ if err != nil {
+ return err
+ }
+
+ return nil
+}