aboutsummaryrefslogtreecommitdiff
path: root/matrix
diff options
context:
space:
mode:
authorTulir Asokan <tulir@maunium.net>2020-02-22 01:17:52 +0200
committerTulir Asokan <tulir@maunium.net>2020-02-22 01:17:52 +0200
commit455d9fc4c5726e4af9c40b36ee4f4ea18f65a8e9 (patch)
treeed61048b008b627371943370a16244c10e33602b /matrix
parentecdb1166e1cead31518c058a0e7fd749e61b193e (diff)
Improve tags and add initial invite handling
Diffstat (limited to 'matrix')
-rw-r--r--matrix/matrix.go19
-rw-r--r--matrix/pushrules/condition.go5
-rw-r--r--matrix/rooms/room.go53
3 files changed, 48 insertions, 29 deletions
diff --git a/matrix/matrix.go b/matrix/matrix.go
index d3edde5..8819a5b 100644
--- a/matrix/matrix.go
+++ b/matrix/matrix.go
@@ -663,7 +663,7 @@ func (c *Container) parseDirectChatInfo(evt *mautrix.Event) map[*rooms.Room]bool
return directChats
}
-func (c *Container) HandleDirectChatInfo(source EventSource, evt *mautrix.Event) {
+func (c *Container) HandleDirectChatInfo(_ EventSource, evt *mautrix.Event) {
directChats := c.parseDirectChatInfo(evt)
for _, room := range c.config.Rooms.Map {
shouldBeDirect := directChats[room]
@@ -677,7 +677,7 @@ func (c *Container) HandleDirectChatInfo(source EventSource, evt *mautrix.Event)
}
// HandlePushRules is the event handler for the m.push_rules account data event.
-func (c *Container) HandlePushRules(source EventSource, evt *mautrix.Event) {
+func (c *Container) HandlePushRules(_ EventSource, evt *mautrix.Event) {
debug.Print("Received updated push rules")
var err error
c.config.PushRules, err = pushrules.EventToPushRules(evt)
@@ -689,15 +689,16 @@ func (c *Container) HandlePushRules(source EventSource, evt *mautrix.Event) {
}
// HandleTag is the event handler for the m.tag account data event.
-func (c *Container) HandleTag(source EventSource, evt *mautrix.Event) {
+func (c *Container) HandleTag(_ EventSource, evt *mautrix.Event) {
+ debug.Printf("Received tags for %s: %s -- %s", evt.RoomID, evt.Content.RoomTags, string(evt.Content.VeryRaw))
room := c.GetOrCreateRoom(evt.RoomID)
newTags := make([]rooms.RoomTag, len(evt.Content.RoomTags))
index := 0
for tag, info := range evt.Content.RoomTags {
- order := "0.5"
+ order := json.Number("0.5")
if len(info.Order) > 0 {
- order = info.Order.String()
+ order = info.Order
}
newTags[index] = rooms.RoomTag{
Tag: tag,
@@ -714,7 +715,7 @@ func (c *Container) HandleTag(source EventSource, evt *mautrix.Event) {
}
// HandleTyping is the event handler for the m.typing event.
-func (c *Container) HandleTyping(source EventSource, evt *mautrix.Event) {
+func (c *Container) HandleTyping(_ EventSource, evt *mautrix.Event) {
if !c.config.AuthCache.InitialSyncDone {
return
}
@@ -723,7 +724,7 @@ func (c *Container) HandleTyping(source EventSource, evt *mautrix.Event) {
func (c *Container) MarkRead(roomID, eventID string) {
urlPath := c.client.BuildURL("rooms", roomID, "receipt", "m.read", eventID)
- c.client.MakeRequest("POST", urlPath, struct{}{}, nil)
+ _, _ = c.client.MakeRequest("POST", urlPath, struct{}{}, nil)
}
var mentionRegex = regexp.MustCompile("\\[(.+?)]\\(https://matrix.to/#/@.+?:.+?\\)")
@@ -791,10 +792,10 @@ func (c *Container) SendTyping(roomID string, typing bool) {
}
if typing {
- c.client.UserTyping(roomID, true, 20000)
+ _, _ = c.client.UserTyping(roomID, true, 20000)
c.typing = ts + 15
} else {
- c.client.UserTyping(roomID, false, 0)
+ _, _ = c.client.UserTyping(roomID, false, 0)
c.typing = 0
}
}
diff --git a/matrix/pushrules/condition.go b/matrix/pushrules/condition.go
index 0b7776a..cc62da1 100644
--- a/matrix/pushrules/condition.go
+++ b/matrix/pushrules/condition.go
@@ -22,6 +22,7 @@ import (
"strings"
"unicode"
+ "maunium.net/go/gomuks/matrix/rooms"
"maunium.net/go/mautrix"
"maunium.net/go/gomuks/lib/glob"
@@ -29,8 +30,8 @@ import (
// Room is an interface with the functions that are needed for processing room-specific push conditions
type Room interface {
- GetMember(mxid string) *mautrix.Member
- GetMembers() map[string]*mautrix.Member
+ GetMember(mxid string) *rooms.Member
+ GetMembers() map[string]*rooms.Member
GetSessionOwner() string
}
diff --git a/matrix/rooms/room.go b/matrix/rooms/room.go
index 9081ba5..7df29ad 100644
--- a/matrix/rooms/room.go
+++ b/matrix/rooms/room.go
@@ -19,6 +19,7 @@ package rooms
import (
"compress/gzip"
"encoding/gob"
+ "encoding/json"
"fmt"
"os"
"time"
@@ -49,7 +50,7 @@ type RoomTag struct {
// The name of the tag.
Tag string
// The order of the tag.
- Order string
+ Order json.Number
}
type UnreadMessage struct {
@@ -58,6 +59,13 @@ type UnreadMessage struct {
Highlight bool
}
+type Member struct {
+ mautrix.Member
+
+ // The user who sent the membership event
+ Sender string `json:"-"`
+}
+
// Room represents a single Matrix room.
type Room struct {
// The room ID.
@@ -73,7 +81,7 @@ type Room struct {
LastPrevBatch string
// The MXID of the user whose session this room was created for.
SessionUserID string
- SessionMember *mautrix.Member
+ SessionMember *Member
// The number of unread messages that were notified about.
UnreadMessages []UnreadMessage
@@ -95,12 +103,12 @@ type Room struct {
// Room state cache.
state map[mautrix.EventType]map[string]*mautrix.Event
// MXID -> Member cache calculated from membership events.
- memberCache map[string]*mautrix.Member
- exMemberCache map[string]*mautrix.Member
+ memberCache map[string]*Member
+ exMemberCache map[string]*Member
// The first two non-SessionUserID members in the room. Calculated at
// the same time as memberCache.
- firstMemberCache *mautrix.Member
- secondMemberCache *mautrix.Member
+ firstMemberCache *Member
+ secondMemberCache *Member
// The name of the room. Calculated from the state event name,
// canonical_alias or alias or the member cache.
NameCache string
@@ -337,8 +345,13 @@ func (room *Room) Tags() []RoomTag {
room.lock.RLock()
defer room.lock.RUnlock()
if len(room.RawTags) == 0 {
+ sessionMember := room.GetMember(room.SessionUserID)
if room.IsDirect {
return []RoomTag{{"net.maunium.gomuks.fake.direct", "0.5"}}
+ } else if sessionMember != nil && sessionMember.Membership == mautrix.MembershipInvite {
+ return []RoomTag{{"net.maunium.gomuks.fake.invite", "0.5"}}
+ } else if sessionMember != nil && sessionMember.Membership != mautrix.MembershipJoin {
+ return []RoomTag{{"net.maunium.gomuks.fake.leave", "0.5"}}
}
return []RoomTag{{"", "0.5"}}
}
@@ -403,10 +416,11 @@ func (room *Room) UpdateState(event *mautrix.Event) {
func (room *Room) updateMemberState(event *mautrix.Event) {
userID := event.GetStateKey()
if userID == room.SessionUserID {
- room.SessionMember = room.eventToMember(userID, &event.Content)
+ debug.Print("Updating session user state:", string(event.Content.VeryRaw))
+ room.SessionMember = room.eventToMember(userID, event.Sender, &event.Content)
}
if room.memberCache != nil {
- member := room.eventToMember(userID, &event.Content)
+ member := room.eventToMember(userID, event.Sender, &event.Content)
if member.Membership.IsInviteOrJoin() {
existingMember, ok := room.memberCache[userID]
if ok {
@@ -553,16 +567,19 @@ func (room *Room) ReplacedBy() string {
return *room.replacedByCache
}
-func (room *Room) eventToMember(userID string, content *mautrix.Content) *mautrix.Member {
- member := &content.Member
+func (room *Room) eventToMember(userID string, sender string, content *mautrix.Content) *Member {
+ member := content.Member
member.Membership = content.Membership
if len(member.Displayname) == 0 {
member.Displayname = userID
}
- return member
+ return &Member{
+ Member: member,
+ Sender: sender,
+ }
}
-func (room *Room) updateNthMemberCache(userID string, member *mautrix.Member) {
+func (room *Room) updateNthMemberCache(userID string, member *Member) {
if userID != room.SessionUserID {
if room.firstMemberCache == nil {
room.firstMemberCache = member
@@ -573,19 +590,19 @@ func (room *Room) updateNthMemberCache(userID string, member *mautrix.Member) {
}
// createMemberCache caches all member events into a easily processable MXID -> *Member map.
-func (room *Room) createMemberCache() map[string]*mautrix.Member {
+func (room *Room) createMemberCache() map[string]*Member {
if len(room.memberCache) > 0 {
return room.memberCache
}
- cache := make(map[string]*mautrix.Member)
- exCache := make(map[string]*mautrix.Member)
+ cache := make(map[string]*Member)
+ exCache := make(map[string]*Member)
room.lock.RLock()
events := room.getStateEvents(mautrix.StateMember)
room.firstMemberCache = nil
room.secondMemberCache = nil
if events != nil {
for userID, event := range events {
- member := room.eventToMember(userID, &event.Content)
+ member := room.eventToMember(userID, event.Sender, &event.Content)
if member.Membership.IsInviteOrJoin() {
cache[userID] = member
room.updateNthMemberCache(userID, member)
@@ -615,7 +632,7 @@ func (room *Room) createMemberCache() map[string]*mautrix.Member {
//
// The members are returned from the cache.
// If the cache is empty, it is updated first.
-func (room *Room) GetMembers() map[string]*mautrix.Member {
+func (room *Room) GetMembers() map[string]*Member {
room.Load()
room.createMemberCache()
return room.memberCache
@@ -623,7 +640,7 @@ func (room *Room) GetMembers() map[string]*mautrix.Member {
// GetMember returns the member with the given MXID.
// If the member doesn't exist, nil is returned.
-func (room *Room) GetMember(userID string) *mautrix.Member {
+func (room *Room) GetMember(userID string) *Member {
if userID == room.SessionUserID && room.SessionMember != nil {
return room.SessionMember
}