aboutsummaryrefslogtreecommitdiff
path: root/matrix
diff options
context:
space:
mode:
authorTulir Asokan <tulir@maunium.net>2018-05-17 16:29:15 +0300
committerTulir Asokan <tulir@maunium.net>2018-05-17 16:29:18 +0300
commit76cff9554001ca3727e2ba11b790e9bba27d6b77 (patch)
tree325015fa4b331aeb4487d2ed0aaf94f2f3d40fe4 /matrix
parenta1f9ee23fa07bdeb548953cdadb7e2cfb0fa05de (diff)
Move all cache to ~/.cache/gomuks
Now `rm -rf ~/.cache/gomuks` has the same effect as `/clearcache`
Diffstat (limited to 'matrix')
-rw-r--r--matrix/matrix.go45
-rw-r--r--matrix/pushrules/rule.go6
-rw-r--r--matrix/rooms/room.go41
3 files changed, 63 insertions, 29 deletions
diff --git a/matrix/matrix.go b/matrix/matrix.go
index 92fcabe..c41975e 100644
--- a/matrix/matrix.go
+++ b/matrix/matrix.go
@@ -83,8 +83,8 @@ func (c *Container) InitClient() error {
}
var mxid, accessToken string
- if c.config.Session != nil {
- accessToken = c.config.Session.AccessToken
+ if len(c.config.AccessToken) > 0 {
+ accessToken = c.config.AccessToken
mxid = c.config.UserID
}
@@ -96,7 +96,7 @@ func (c *Container) InitClient() error {
c.stop = make(chan bool, 1)
- if c.config.Session != nil && len(accessToken) > 0 {
+ if len(accessToken) > 0 {
go c.Start()
}
return nil
@@ -120,12 +120,9 @@ func (c *Container) Login(user, password string) error {
}
c.client.SetCredentials(resp.UserID, resp.AccessToken)
c.config.UserID = resp.UserID
+ c.config.AccessToken = resp.AccessToken
c.config.Save()
- c.config.Session = c.config.NewSession(resp.UserID)
- c.config.Session.AccessToken = resp.AccessToken
- c.config.Session.Save()
-
go c.Start()
return nil
@@ -156,25 +153,26 @@ func (c *Container) UpdatePushRules() {
if err != nil {
debug.Print("Failed to fetch push rules:", err)
}
- c.config.Session.PushRules = resp
+ c.config.PushRules = resp
+ c.config.SavePushRules()
}
// PushRules returns the push notification rules. If no push rules are cached, UpdatePushRules() will be called first.
func (c *Container) PushRules() *pushrules.PushRuleset {
- if c.config.Session.PushRules == nil {
+ if c.config.PushRules == nil {
c.UpdatePushRules()
}
- return c.config.Session.PushRules
+ return c.config.PushRules
}
// OnLogin initializes the syncer and updates the room list.
func (c *Container) OnLogin() {
c.ui.OnLogin()
- c.client.Store = c.config.Session
+ c.client.Store = c.config
debug.Print("Initializing syncer")
- c.syncer = NewGomuksSyncer(c.config.Session)
+ c.syncer = NewGomuksSyncer(c.config)
c.syncer.OnEventType("m.room.message", c.HandleMessage)
c.syncer.OnEventType("m.room.member", c.HandleMembership)
c.syncer.OnEventType("m.receipt", c.HandleReadReceipt)
@@ -183,14 +181,15 @@ func (c *Container) OnLogin() {
c.syncer.OnEventType("m.push_rules", c.HandlePushRules)
c.syncer.OnEventType("m.tag", c.HandleTag)
c.syncer.InitDoneCallback = func() {
- c.config.Session.InitialSyncDone = true
+ c.config.AuthCache.InitialSyncDone = true
+ c.config.SaveAuthCache()
c.ui.MainView().InitialSyncDone()
c.ui.Render()
}
c.client.Syncer = c.syncer
debug.Print("Setting existing rooms")
- c.ui.MainView().SetRooms(c.config.Session.Rooms)
+ c.ui.MainView().SetRooms(c.config.Rooms)
debug.Print("OnLogin() done.")
}
@@ -268,7 +267,7 @@ func (c *Container) parseReadReceipt(evt *gomatrix.Event) (largestTimestampEvent
continue
}
- myInfo, ok := mRead[c.config.Session.UserID].(map[string]interface{})
+ myInfo, ok := mRead[c.config.UserID].(map[string]interface{})
if !ok {
continue
}
@@ -322,7 +321,7 @@ func (c *Container) parseDirectChatInfo(evt *gomatrix.Event) (map[*rooms.Room]bo
func (c *Container) HandleDirectChatInfo(source EventSource, evt *gomatrix.Event) {
directChats := c.parseDirectChatInfo(evt)
- for _, room := range c.config.Session.Rooms {
+ for _, room := range c.config.Rooms {
shouldBeDirect := directChats[room]
if shouldBeDirect != room.IsDirect {
room.IsDirect = shouldBeDirect
@@ -335,15 +334,17 @@ func (c *Container) HandleDirectChatInfo(source EventSource, evt *gomatrix.Event
func (c *Container) HandlePushRules(source EventSource, evt *gomatrix.Event) {
debug.Print("Received updated push rules")
var err error
- c.config.Session.PushRules, err = pushrules.EventToPushRules(evt)
+ c.config.PushRules, err = pushrules.EventToPushRules(evt)
if err != nil {
debug.Print("Failed to convert event to push rules:", err)
+ return
}
+ c.config.SavePushRules()
}
// HandleTag is the event handler for the m.tag account data event.
func (c *Container) HandleTag(source EventSource, evt *gomatrix.Event) {
- room := c.config.Session.GetRoom(evt.RoomID)
+ room := c.config.GetRoom(evt.RoomID)
tags, _ := evt.Content["tags"].(map[string]interface{})
newTags := make([]rooms.RoomTag, len(tags))
@@ -396,11 +397,11 @@ func (c *Container) HandleMembership(source EventSource, evt *gomatrix.Event) {
isLeave := source&EventSourceLeave != 0
isTimeline := source&EventSourceTimeline != 0
isNonTimelineLeave := isLeave && !isTimeline
- if !c.config.Session.InitialSyncDone && isNonTimelineLeave {
+ if !c.config.AuthCache.InitialSyncDone && isNonTimelineLeave {
return
- } else if evt.StateKey != nil && *evt.StateKey == c.config.Session.UserID {
+ } else if evt.StateKey != nil && *evt.StateKey == c.config.UserID {
c.processOwnMembershipChange(evt)
- } else if !isTimeline && (!c.config.Session.InitialSyncDone || isLeave) {
+ } else if !isTimeline && (!c.config.AuthCache.InitialSyncDone || isLeave) {
// We don't care about other users' membership events in the initial sync or chats we've left.
return
}
@@ -563,7 +564,7 @@ func (c *Container) GetHistory(roomID, prevBatch string, limit int) ([]gomatrix.
// GetRoom gets the room instance stored in the session.
func (c *Container) GetRoom(roomID string) *rooms.Room {
- return c.config.Session.GetRoom(roomID)
+ return c.config.GetRoom(roomID)
}
var mxcRegex = regexp.MustCompile("mxc://(.+)/(.+)")
diff --git a/matrix/pushrules/rule.go b/matrix/pushrules/rule.go
index ec51d24..ddb264e 100644
--- a/matrix/pushrules/rule.go
+++ b/matrix/pushrules/rule.go
@@ -19,8 +19,14 @@ package pushrules
import (
"maunium.net/go/gomuks/lib/glob"
"maunium.net/go/gomatrix"
+ "encoding/gob"
)
+func init() {
+ gob.Register(PushRuleArray{})
+ gob.Register(PushRuleMap{})
+}
+
type PushRuleCollection interface {
GetActions(room Room, event *gomatrix.Event) PushActionArray
}
diff --git a/matrix/rooms/room.go b/matrix/rooms/room.go
index 17bf21b..8a543e8 100644
--- a/matrix/rooms/room.go
+++ b/matrix/rooms/room.go
@@ -24,12 +24,19 @@ import (
"maunium.net/go/gomatrix"
"maunium.net/go/gomuks/debug"
+ "os"
+ "encoding/gob"
)
+func init() {
+ gob.Register([]interface{}{})
+ gob.Register(map[string]interface{}{})
+}
+
type RoomNameSource int
const (
- ExplicitRoomName RoomNameSource = iota
+ ExplicitRoomName RoomNameSource = iota
CanonicalAliasRoomName
AliasRoomName
MemberRoomName
@@ -44,8 +51,8 @@ type RoomTag struct {
}
type UnreadMessage struct {
- EventID string
- Counted bool
+ EventID string
+ Counted bool
Highlight bool
}
@@ -63,9 +70,9 @@ type Room struct {
SessionUserID string
// The number of unread messages that were notified about.
- UnreadMessages []UnreadMessage
+ UnreadMessages []UnreadMessage
unreadCountCache *int
- highlightCache *bool
+ highlightCache *bool
// Whether or not this room is marked as a direct chat.
IsDirect bool
@@ -113,6 +120,26 @@ func (room *Room) UnlockHistory() {
}
}
+func (room *Room) Load(path string) error {
+ file, err := os.OpenFile(path, os.O_RDONLY, 0600)
+ if err != nil {
+ return err
+ }
+ defer file.Close()
+ dec := gob.NewDecoder(file)
+ return dec.Decode(room)
+}
+
+func (room *Room) Save(path string) error {
+ file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE, 0600)
+ if err != nil {
+ return err
+ }
+ defer file.Close()
+ enc := gob.NewEncoder(file)
+ return enc.Encode(room)
+}
+
// MarkRead clears the new message statuses on this room.
func (room *Room) MarkRead(eventID string) {
readToIndex := -1
@@ -159,8 +186,8 @@ func (room *Room) HasNewMessages() bool {
func (room *Room) AddUnread(eventID string, counted, highlight bool) {
room.UnreadMessages = append(room.UnreadMessages, UnreadMessage{
- EventID: eventID,
- Counted: counted,
+ EventID: eventID,
+ Counted: counted,
Highlight: highlight,
})
if counted {