From 76cff9554001ca3727e2ba11b790e9bba27d6b77 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Thu, 17 May 2018 16:29:15 +0300 Subject: Move all cache to ~/.cache/gomuks Now `rm -rf ~/.cache/gomuks` has the same effect as `/clearcache` --- matrix/rooms/room.go | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) (limited to 'matrix/rooms/room.go') 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 { -- cgit v1.2.3