morpheusv2

A Matrix client written in Go-QT
git clone git://archive.git.mtrnord.blog/Nordgedanken/morpheusv2.git
Log | Files | Refs | README | LICENSE

user.go (3671B)


      1 // Copyright © 2018 MTRNord <info@nordgedanken.de>
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 package users
     16 
     17 import (
     18 	"github.com/Nordgedanken/Morpheusv2/pkg/util"
     19 	"github.com/matrix-org/gomatrix"
     20 	"strings"
     21 )
     22 
     23 // User holds the needed User data and allows to work with that. It gets normally loaded from the cache
     24 type User struct {
     25 	cli                *gomatrix.Client
     26 	mxid               string
     27 	defaultDisplayName string
     28 	displayName        map[string]string
     29 	defaultAvatar      []byte
     30 	avatar             map[string][]byte
     31 }
     32 
     33 // SetCli adds the gomatrix.Client to the current User. This only happens if the User is the current User aka the one that logged into the client.
     34 func (u *User) SetCli(cli *gomatrix.Client) {
     35 	u.cli = cli
     36 }
     37 
     38 // SetMXID adds the mxid to the current User
     39 func (u *User) SetMXID(id string) {
     40 	u.mxid = id
     41 }
     42 
     43 // SetDisplayName adds the displayName to the current User
     44 func (u *User) SetDisplayName(roomID string, name string) {
     45 	if roomID == "" {
     46 		u.defaultDisplayName = name
     47 	} else {
     48 		if u.displayName == nil {
     49 			u.displayName = make(map[string]string)
     50 		}
     51 		u.displayName[roomID] = name
     52 	}
     53 }
     54 
     55 // SetAvatar adds the avatar to the current User
     56 func (u *User) SetAvatar(roomID string, avatar []byte) {
     57 	if roomID == "" {
     58 		u.defaultAvatar = avatar
     59 	} else {
     60 		if u.avatar == nil {
     61 			u.avatar = make(map[string][]byte)
     62 		}
     63 		u.avatar[roomID] = avatar
     64 	}
     65 }
     66 
     67 // GetMXID returns the mxid from the current User
     68 func (u *User) GetMXID() string {
     69 	return u.mxid
     70 }
     71 
     72 // GetDisplayName returns the displayName from the current User
     73 func (u *User) GetDisplayName(roomID string) (string, error) {
     74 	if roomID == "" {
     75 		if u.defaultDisplayName == "" {
     76 			resp, err := util.User.GetCli().GetDisplayName(u.mxid)
     77 			if err != nil {
     78 				return "", err
     79 			}
     80 			u.defaultDisplayName = resp.DisplayName
     81 		}
     82 		return u.defaultDisplayName, nil
     83 	}
     84 	// TODO get Membership Event from Room instead returning default directly
     85 	if u.displayName[roomID] == "" {
     86 		return u.defaultDisplayName, nil
     87 	}
     88 	return u.displayName[roomID], nil
     89 }
     90 
     91 // GetAvatar returns the avatar from the current User
     92 func (u *User) GetAvatar(roomID string) ([]byte, error) {
     93 	if roomID == "" {
     94 		if u.defaultAvatar == nil {
     95 			urlPath := util.User.GetCli().BuildURL("profile", u.mxid, "avatar_url")
     96 			s := struct {
     97 				AvatarURL string `json:"avatar_url"`
     98 			}{}
     99 
    100 			_, err := util.User.GetCli().MakeRequest("GET", urlPath, nil, &s)
    101 			if err != nil {
    102 				return nil, err
    103 			}
    104 
    105 			split := strings.Split(s.AvatarURL, "/")
    106 			servername := strings.TrimPrefix(split[0], "mxc://")
    107 			mediaID := split[1]
    108 			mediaURL := util.User.GetCli().BuildBaseURL("_matrix/media/r0/download", servername, mediaID)
    109 			avatar, err := util.User.GetCli().MakeRequest("GET", mediaURL, nil, nil)
    110 			if err != nil {
    111 				return nil, err
    112 			}
    113 			u.defaultAvatar = avatar
    114 		}
    115 		return u.defaultAvatar, nil
    116 	}
    117 	// TODO get Membership Event from Room instead returning default directly
    118 	if u.avatar[roomID] == nil {
    119 		return u.defaultAvatar, nil
    120 	}
    121 	return u.avatar[roomID], nil
    122 }
    123 
    124 // GetCli returns the gomatrix.Client from the current User
    125 func (u *User) GetCli() *gomatrix.Client {
    126 	return u.cli
    127 }