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-15 18:20:32 +0100
committerGitHub <noreply@github.com>2019-02-15 18:20:32 +0100
commit716751cf7685471bbc969e19fe26f23e66f3c0b4 (patch)
tree6db2350f28a76ae6e1cf7c3cea7263503057d82f /vendor/github.com/matterbridge/Rocket.Chat.Go.SDK/realtime/users.go
parent077b818d82a98855d5577f4f9de5d2b2d2cc6a50 (diff)
parent6ebd5cbbd8a941e0bc5f99f0d8e99cfd1d8ac0d7 (diff)
downloadmatterbridge-msglm-716751cf7685471bbc969e19fe26f23e66f3c0b4.tar.gz
matterbridge-msglm-716751cf7685471bbc969e19fe26f23e66f3c0b4.tar.bz2
matterbridge-msglm-716751cf7685471bbc969e19fe26f23e66f3c0b4.zip
Refactor and update RocketChat bridge (#707)
* 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
+}