morpheusv2

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

room.go (3685B)


      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 rooms
     16 
     17 import (
     18 	"github.com/Nordgedanken/Morpheusv2/pkg/matrix"
     19 	"github.com/Nordgedanken/Morpheusv2/pkg/util"
     20 	"github.com/matrix-org/gomatrix"
     21 	"strings"
     22 )
     23 
     24 // Room holds the needed Room data and allows to work with that. It gets normally loaded from the cache
     25 type Room struct {
     26 	id       string
     27 	aliases  []string
     28 	name     string
     29 	avatar   []byte
     30 	topic    string
     31 	messages []matrix.Message
     32 }
     33 
     34 // SetRoomID adds the roomID to the current Room
     35 func (r *Room) SetRoomID(id string) {
     36 	r.id = id
     37 }
     38 
     39 // SetRoomAliases adds the aliases to the current Room
     40 func (r *Room) SetRoomAliases(aliases []string) {
     41 	r.aliases = aliases
     42 }
     43 
     44 // SetName adds the name to the current Room
     45 func (r *Room) SetName(name string) {
     46 	r.name = name
     47 }
     48 
     49 // SetAvatar adds the avatar to the current Room
     50 func (r *Room) SetAvatar(avatar []byte) {
     51 	r.avatar = avatar
     52 }
     53 
     54 // SetTopic adds the topic to the current Room
     55 func (r *Room) SetTopic(topic string) {
     56 	r.topic = topic
     57 }
     58 
     59 // SetMessages adds the messages to the current Room
     60 func (r *Room) SetMessages(messages []matrix.Message) {
     61 	r.messages = messages
     62 }
     63 
     64 // GetRoomID returns the room ID from the current Room
     65 func (r *Room) GetRoomID() string {
     66 	return r.id
     67 }
     68 
     69 // GetRoomAliases returns the room aliases from the current Room
     70 func (r *Room) GetRoomAliases() []string {
     71 	return r.aliases
     72 }
     73 
     74 // GetName returns the name from the current Room
     75 func (r *Room) GetName() (string, error) {
     76 	if r.name == "" {
     77 		type RespRoomName struct {
     78 			Name string `json:"name"`
     79 		}
     80 		resp := &RespRoomName{}
     81 		err := util.User.GetCli().StateEvent(r.id, "m.room.name", "", resp)
     82 		if err != nil {
     83 			return "", err
     84 		}
     85 		r.name = resp.Name
     86 	}
     87 	return r.name, nil
     88 }
     89 
     90 // GetAvatar returns the avatar from the current Room
     91 func (r *Room) GetAvatar() ([]byte, error) {
     92 	if r.avatar == nil {
     93 		resp := &gomatrix.Event{}
     94 		err := util.User.GetCli().StateEvent(r.id, "m.room.avatar", "", resp)
     95 		if err != nil {
     96 			return nil, err
     97 		}
     98 		var avatar []byte
     99 		value, exists := resp.Content["url"]
    100 		if !exists {
    101 			return nil, nil
    102 		}
    103 		url, ok := value.(string)
    104 		if !ok {
    105 			return nil, nil
    106 		}
    107 		split := strings.Split(url, "/")
    108 		servername := strings.TrimPrefix(split[0], "mxc://")
    109 		mediaID := split[1]
    110 		mediaURL := util.User.GetCli().BuildBaseURL("_matrix/media/r0/download", servername, mediaID)
    111 		avatar, err = util.User.GetCli().MakeRequest("GET", mediaURL, nil, nil)
    112 		if err != nil {
    113 			return nil, err
    114 		}
    115 		r.avatar = avatar
    116 	}
    117 	return r.avatar, nil
    118 }
    119 
    120 // GetTopic returns the topic from the current Room
    121 func (r *Room) GetTopic() (string, error) {
    122 	if r.topic == "" {
    123 		resp := &gomatrix.Event{}
    124 		err := util.User.GetCli().StateEvent(r.id, "m.room.topic", "", resp)
    125 		if err != nil {
    126 			return "", err
    127 		}
    128 		value, exists := resp.Content["topic"]
    129 		if !exists {
    130 			return "", nil
    131 		}
    132 		topic, ok := value.(string)
    133 		if !ok {
    134 			return "", nil
    135 		}
    136 		r.topic = topic
    137 	}
    138 	return r.topic, nil
    139 }
    140 
    141 // GetMessages returns the messages from the current Room
    142 func (r *Room) GetMessages() []matrix.Message {
    143 	// TODO Lazy load from DB
    144 	return r.messages
    145 }