Wrap events in custom struct to add gomuks-specific fields

This commit is contained in:
Tulir Asokan 2019-06-17 12:27:31 +03:00
parent 754f8e493d
commit fe439f076a
16 changed files with 167 additions and 113 deletions

2
go.mod
View File

@ -25,7 +25,7 @@ require (
gopkg.in/russross/blackfriday.v2 v2.0.1 gopkg.in/russross/blackfriday.v2 v2.0.1
gopkg.in/toast.v1 v1.0.0-20180812000517-0a84660828b2 gopkg.in/toast.v1 v1.0.0-20180812000517-0a84660828b2
gopkg.in/yaml.v2 v2.2.2 gopkg.in/yaml.v2 v2.2.2
maunium.net/go/mautrix v0.1.0-alpha.3.0.20190616174145-055ff7b0fa6a maunium.net/go/mautrix v0.1.0-alpha.3.0.20190617092349-2e611350d5a9
maunium.net/go/mauview v0.0.0-20190606152754-de9e0a754a5d maunium.net/go/mauview v0.0.0-20190606152754-de9e0a754a5d
maunium.net/go/tcell v0.0.0-20190606152714-9a88fc07b3ed maunium.net/go/tcell v0.0.0-20190606152714-9a88fc07b3ed
) )

2
go.sum
View File

@ -100,6 +100,8 @@ maunium.net/go/mautrix v0.1.0-alpha.3.0.20190616163219-6b4bce05f314 h1:wGlIqs/L+
maunium.net/go/mautrix v0.1.0-alpha.3.0.20190616163219-6b4bce05f314/go.mod h1:O+QWJP3H7BZEzIBSrECKpnpRnEKBwaoWVEu/yZwVwxg= maunium.net/go/mautrix v0.1.0-alpha.3.0.20190616163219-6b4bce05f314/go.mod h1:O+QWJP3H7BZEzIBSrECKpnpRnEKBwaoWVEu/yZwVwxg=
maunium.net/go/mautrix v0.1.0-alpha.3.0.20190616174145-055ff7b0fa6a h1:DenEIDOOumsU8zDzj5ePQOSaKsxjAxW7UESJ2xo1lxM= maunium.net/go/mautrix v0.1.0-alpha.3.0.20190616174145-055ff7b0fa6a h1:DenEIDOOumsU8zDzj5ePQOSaKsxjAxW7UESJ2xo1lxM=
maunium.net/go/mautrix v0.1.0-alpha.3.0.20190616174145-055ff7b0fa6a/go.mod h1:O+QWJP3H7BZEzIBSrECKpnpRnEKBwaoWVEu/yZwVwxg= maunium.net/go/mautrix v0.1.0-alpha.3.0.20190616174145-055ff7b0fa6a/go.mod h1:O+QWJP3H7BZEzIBSrECKpnpRnEKBwaoWVEu/yZwVwxg=
maunium.net/go/mautrix v0.1.0-alpha.3.0.20190617092349-2e611350d5a9 h1:GB0c9wXX4JrFUuPft1KGvUU1RKjM04XYQp0Uaj2K3bU=
maunium.net/go/mautrix v0.1.0-alpha.3.0.20190617092349-2e611350d5a9/go.mod h1:O+QWJP3H7BZEzIBSrECKpnpRnEKBwaoWVEu/yZwVwxg=
maunium.net/go/mauview v0.0.0-20190606152754-de9e0a754a5d h1:H4wZ4vMVnOh5QFsb4xZtssgpv3DDEkBRzQ8iyEg2fX0= maunium.net/go/mauview v0.0.0-20190606152754-de9e0a754a5d h1:H4wZ4vMVnOh5QFsb4xZtssgpv3DDEkBRzQ8iyEg2fX0=
maunium.net/go/mauview v0.0.0-20190606152754-de9e0a754a5d/go.mod h1:GL+akv58wNFzzX4IKLvryKx0F/AcYKHql35DiBzBc/w= maunium.net/go/mauview v0.0.0-20190606152754-de9e0a754a5d/go.mod h1:GL+akv58wNFzzX4IKLvryKx0F/AcYKHql35DiBzBc/w=
maunium.net/go/tcell v0.0.0-20190606152714-9a88fc07b3ed h1:sAcUrUZG2LFWBTkTtLKPQvHPHFM5d6huAhr5ZZuxtbQ= maunium.net/go/tcell v0.0.0-20190606152714-9a88fc07b3ed h1:sAcUrUZG2LFWBTkTtLKPQvHPHFM5d6huAhr5ZZuxtbQ=

View File

@ -17,6 +17,7 @@
package ifc package ifc
import ( import (
"maunium.net/go/gomuks/matrix/event"
"maunium.net/go/mautrix" "maunium.net/go/mautrix"
"maunium.net/go/gomuks/matrix/rooms" "maunium.net/go/gomuks/matrix/rooms"
@ -34,16 +35,16 @@ type MatrixContainer interface {
Logout() Logout()
SendPreferencesToMatrix() SendPreferencesToMatrix()
PrepareMarkdownMessage(roomID string, msgtype mautrix.MessageType, message string) *mautrix.Event PrepareMarkdownMessage(roomID string, msgtype mautrix.MessageType, message string) *event.Event
SendEvent(event *mautrix.Event) (string, error) SendEvent(evt *event.Event) (string, error)
SendTyping(roomID string, typing bool) SendTyping(roomID string, typing bool)
MarkRead(roomID, eventID string) MarkRead(roomID, eventID string)
JoinRoom(roomID, server string) (*rooms.Room, error) JoinRoom(roomID, server string) (*rooms.Room, error)
LeaveRoom(roomID string) error LeaveRoom(roomID string) error
CreateRoom(req *mautrix.ReqCreateRoom) (*rooms.Room, error) CreateRoom(req *mautrix.ReqCreateRoom) (*rooms.Room, error)
GetHistory(room *rooms.Room, limit int) ([]*mautrix.Event, error) GetHistory(room *rooms.Room, limit int) ([]*event.Event, error)
GetEvent(room *rooms.Room, eventID string) (*mautrix.Event, error) GetEvent(room *rooms.Room, eventID string) (*event.Event, error)
GetRoom(roomID string) *rooms.Room GetRoom(roomID string) *rooms.Room
GetOrCreateRoom(roomID string) *rooms.Room GetOrCreateRoom(roomID string) *rooms.Room

View File

@ -19,9 +19,9 @@ package ifc
import ( import (
"time" "time"
"maunium.net/go/gomuks/matrix/event"
"maunium.net/go/gomuks/matrix/pushrules" "maunium.net/go/gomuks/matrix/pushrules"
"maunium.net/go/gomuks/matrix/rooms" "maunium.net/go/gomuks/matrix/rooms"
"maunium.net/go/mautrix"
) )
type UIProvider func(gmx Gomuks) GomuksUI type UIProvider func(gmx Gomuks) GomuksUI
@ -60,7 +60,7 @@ type RoomView interface {
SetTyping(users []string) SetTyping(users []string)
UpdateUserList() UpdateUserList()
ParseEvent(evt *mautrix.Event) Message ParseEvent(evt *event.Event) Message
GetEvent(eventID string) Message GetEvent(eventID string) Message
AddMessage(message Message) AddMessage(message Message)
AddServiceMessage(message string) AddServiceMessage(message string)

43
matrix/event/event.go Normal file
View File

@ -0,0 +1,43 @@
// gomuks - A terminal Matrix client written in Go.
// Copyright (C) 2019 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package event
import (
"maunium.net/go/mautrix"
)
type Event struct {
*mautrix.Event
Gomuks GomuksContent `json:"-"`
}
func Wrap(event *mautrix.Event) *Event {
return &Event{Event: event}
}
type OutgoingState int
const (
StateDefault OutgoingState = iota
StateLocalEcho
StateSendFail
)
type GomuksContent struct {
OutgoingState OutgoingState
Edits []*Event
}

View File

@ -26,14 +26,11 @@ import (
sync "github.com/sasha-s/go-deadlock" sync "github.com/sasha-s/go-deadlock"
bolt "go.etcd.io/bbolt" bolt "go.etcd.io/bbolt"
"maunium.net/go/gomuks/matrix/event"
"maunium.net/go/gomuks/matrix/rooms" "maunium.net/go/gomuks/matrix/rooms"
"maunium.net/go/mautrix" "maunium.net/go/mautrix"
) )
func init() {
gob.Register(&mautrix.Event{})
}
type HistoryManager struct { type HistoryManager struct {
sync.Mutex sync.Mutex
@ -88,7 +85,7 @@ func (hm *HistoryManager) Close() error {
return hm.db.Close() return hm.db.Close()
} }
func (hm *HistoryManager) Get(room *rooms.Room, eventID string) (event *mautrix.Event, err error) { func (hm *HistoryManager) Get(room *rooms.Room, eventID string) (evt *event.Event, err error) {
err = hm.db.View(func(tx *bolt.Tx) error { err = hm.db.View(func(tx *bolt.Tx) error {
rid := []byte(room.ID) rid := []byte(room.ID)
eventIDs := tx.Bucket(bucketRoomEventIDs).Bucket(rid) eventIDs := tx.Bucket(bucketRoomEventIDs).Bucket(rid)
@ -102,7 +99,7 @@ func (hm *HistoryManager) Get(room *rooms.Room, eventID string) (event *mautrix.
stream := tx.Bucket(bucketRoomStreams).Bucket(rid) stream := tx.Bucket(bucketRoomStreams).Bucket(rid)
eventData := stream.Get(streamIndex) eventData := stream.Get(streamIndex)
var umErr error var umErr error
event, umErr = unmarshalEvent(eventData) evt, umErr = unmarshalEvent(eventData)
return umErr return umErr
}) })
return return
@ -110,7 +107,7 @@ func (hm *HistoryManager) Get(room *rooms.Room, eventID string) (event *mautrix.
var EventNotFoundError = errors.New("event not found") var EventNotFoundError = errors.New("event not found")
func (hm *HistoryManager) Update(room *rooms.Room, eventID string, update func(event *mautrix.Event) error) error { func (hm *HistoryManager) Update(room *rooms.Room, eventID string, update func(evt *event.Event) error) error {
return hm.db.Update(func(tx *bolt.Tx) error { return hm.db.Update(func(tx *bolt.Tx) error {
rid := []byte(room.ID) rid := []byte(room.ID)
eventIDs := tx.Bucket(bucketRoomEventIDs).Bucket(rid) eventIDs := tx.Bucket(bucketRoomEventIDs).Bucket(rid)
@ -127,11 +124,11 @@ func (hm *HistoryManager) Update(room *rooms.Room, eventID string, update func(e
return EventNotFoundError return EventNotFoundError
} }
if event, err := unmarshalEvent(eventData); err != nil { if evt, err := unmarshalEvent(eventData); err != nil {
return err return err
} else if err = update(event); err != nil { } else if err = update(evt); err != nil {
return err return err
} else if eventData, err = marshalEvent(event); err != nil { } else if eventData, err = marshalEvent(evt); err != nil {
return err return err
} else { } else {
return stream.Put(streamIndex, eventData) return stream.Put(streamIndex, eventData)
@ -139,17 +136,18 @@ func (hm *HistoryManager) Update(room *rooms.Room, eventID string, update func(e
}) })
} }
func (hm *HistoryManager) Append(room *rooms.Room, events []*mautrix.Event) error { func (hm *HistoryManager) Append(room *rooms.Room, events []*mautrix.Event) ([]*event.Event, error) {
return hm.store(room, events, true) return hm.store(room, events, true)
} }
func (hm *HistoryManager) Prepend(room *rooms.Room, events []*mautrix.Event) error { func (hm *HistoryManager) Prepend(room *rooms.Room, events []*mautrix.Event) ([]*event.Event, error) {
return hm.store(room, events, false) return hm.store(room, events, false)
} }
func (hm *HistoryManager) store(room *rooms.Room, events []*mautrix.Event, append bool) error { func (hm *HistoryManager) store(room *rooms.Room, events []*mautrix.Event, append bool) ([]*event.Event, error) {
hm.Lock() hm.Lock()
defer hm.Unlock() defer hm.Unlock()
newEvents := make([]*event.Event, len(events))
err := hm.db.Update(func(tx *bolt.Tx) error { err := hm.db.Update(func(tx *bolt.Tx) error {
streamPointers := tx.Bucket(bucketStreamPointers) streamPointers := tx.Bucket(bucketStreamPointers)
rid := []byte(room.ID) rid := []byte(room.ID)
@ -174,8 +172,9 @@ func (hm *HistoryManager) store(room *rooms.Room, events []*mautrix.Event, appen
if err != nil { if err != nil {
return err return err
} }
for i, event := range events { for i, evt := range events {
if err := put(stream, eventIDs, event, ptrStart+uint64(i)); err != nil { newEvents[i] = event.Wrap(evt)
if err := put(stream, eventIDs, newEvents[i], ptrStart+uint64(i)); err != nil {
return err return err
} }
} }
@ -194,8 +193,9 @@ func (hm *HistoryManager) store(room *rooms.Room, events []*mautrix.Event, appen
} }
} }
eventCount := uint64(len(events)) eventCount := uint64(len(events))
for i, event := range events { for i, evt := range events {
if err := put(stream, eventIDs, event, -ptrStart-uint64(i)); err != nil { newEvents[i] = event.Wrap(evt)
if err := put(stream, eventIDs, newEvents[i], -ptrStart-uint64(i)); err != nil {
return err return err
} }
} }
@ -208,10 +208,10 @@ func (hm *HistoryManager) store(room *rooms.Room, events []*mautrix.Event, appen
return nil return nil
}) })
return err return newEvents, err
} }
func (hm *HistoryManager) Load(room *rooms.Room, num int) (events []*mautrix.Event, err error) { func (hm *HistoryManager) Load(room *rooms.Room, num int) (events []*event.Event, err error) {
hm.Lock() hm.Lock()
defer hm.Unlock() defer hm.Unlock()
err = hm.db.View(func(tx *bolt.Tx) error { err = hm.db.View(func(tx *bolt.Tx) error {
@ -232,11 +232,11 @@ func (hm *HistoryManager) Load(room *rooms.Room, num int) (events []*mautrix.Eve
} }
hm.historyLoadPtr[room] = ptrStartFound - 1 hm.historyLoadPtr[room] = ptrStartFound - 1
for ; k != nil && btoi(k) < ptrStart; k, v = c.Next() { for ; k != nil && btoi(k) < ptrStart; k, v = c.Next() {
event, parseError := unmarshalEvent(v) evt, parseError := unmarshalEvent(v)
if parseError != nil { if parseError != nil {
return parseError return parseError
} }
events = append(events, event) events = append(events, evt)
} }
return nil return nil
}) })
@ -261,10 +261,10 @@ func btoi(b []byte) uint64 {
return binary.BigEndian.Uint64(b) return binary.BigEndian.Uint64(b)
} }
func marshalEvent(event *mautrix.Event) ([]byte, error) { func marshalEvent(evt *event.Event) ([]byte, error) {
var buf bytes.Buffer var buf bytes.Buffer
enc := gzip.NewWriter(&buf) enc := gzip.NewWriter(&buf)
if err := gob.NewEncoder(enc).Encode(event); err != nil { if err := gob.NewEncoder(enc).Encode(evt); err != nil {
_ = enc.Close() _ = enc.Close()
return nil, err return nil, err
} else if err := enc.Close(); err != nil { } else if err := enc.Close(); err != nil {
@ -273,21 +273,21 @@ func marshalEvent(event *mautrix.Event) ([]byte, error) {
return buf.Bytes(), nil return buf.Bytes(), nil
} }
func unmarshalEvent(data []byte) (*mautrix.Event, error) { func unmarshalEvent(data []byte) (*event.Event, error) {
event := &mautrix.Event{} evt := &event.Event{}
if cmpReader, err := gzip.NewReader(bytes.NewReader(data)); err != nil { if cmpReader, err := gzip.NewReader(bytes.NewReader(data)); err != nil {
return nil, err return nil, err
} else if err := gob.NewDecoder(cmpReader).Decode(event); err != nil { } else if err := gob.NewDecoder(cmpReader).Decode(evt); err != nil {
_ = cmpReader.Close() _ = cmpReader.Close()
return nil, err return nil, err
} else if err := cmpReader.Close(); err != nil { } else if err := cmpReader.Close(); err != nil {
return nil, err return nil, err
} }
return event, nil return evt, nil
} }
func put(streams, eventIDs *bolt.Bucket, event *mautrix.Event, key uint64) error { func put(streams, eventIDs *bolt.Bucket, evt *event.Event, key uint64) error {
data, err := marshalEvent(event) data, err := marshalEvent(evt)
if err != nil { if err != nil {
return err return err
} }
@ -295,7 +295,7 @@ func put(streams, eventIDs *bolt.Bucket, event *mautrix.Event, key uint64) error
if err = streams.Put(keyBytes, data); err != nil { if err = streams.Put(keyBytes, data); err != nil {
return err return err
} }
if err = eventIDs.Put([]byte(event.ID), keyBytes); err != nil { if err = eventIDs.Put([]byte(evt.ID), keyBytes); err != nil {
return err return err
} }
return nil return nil

View File

@ -33,6 +33,7 @@ import (
"time" "time"
dbg "runtime/debug" dbg "runtime/debug"
"maunium.net/go/gomuks/matrix/event"
"maunium.net/go/mautrix" "maunium.net/go/mautrix"
"maunium.net/go/mautrix/format" "maunium.net/go/mautrix/format"
@ -310,8 +311,8 @@ func (c *Container) SendPreferencesToMatrix() {
func (c *Container) HandleRedaction(source EventSource, evt *mautrix.Event) { func (c *Container) HandleRedaction(source EventSource, evt *mautrix.Event) {
room := c.GetOrCreateRoom(evt.RoomID) room := c.GetOrCreateRoom(evt.RoomID)
var redactedEvt *mautrix.Event var redactedEvt *event.Event
err := c.history.Update(room, evt.Redacts, func(redacted *mautrix.Event) error { err := c.history.Update(room, evt.Redacts, func(redacted *event.Event) error {
redacted.Unsigned.RedactedBy = evt.ID redacted.Unsigned.RedactedBy = evt.ID
redacted.Unsigned.RedactedBecause = evt redacted.Unsigned.RedactedBecause = evt
redactedEvt = redacted redactedEvt = redacted
@ -344,8 +345,8 @@ func (c *Container) HandleRedaction(source EventSource, evt *mautrix.Event) {
} }
// HandleMessage is the event handler for the m.room.message timeline event. // HandleMessage is the event handler for the m.room.message timeline event.
func (c *Container) HandleMessage(source EventSource, evt *mautrix.Event) { func (c *Container) HandleMessage(source EventSource, mxEvent *mautrix.Event) {
room := c.GetOrCreateRoom(evt.RoomID) room := c.GetOrCreateRoom(mxEvent.RoomID)
if source&EventSourceLeave != 0 { if source&EventSourceLeave != 0 {
room.HasLeft = true room.HasLeft = true
return return
@ -353,10 +354,11 @@ func (c *Container) HandleMessage(source EventSource, evt *mautrix.Event) {
return return
} }
err := c.history.Append(room, []*mautrix.Event{evt}) events, err := c.history.Append(room, []*mautrix.Event{mxEvent})
if err != nil { if err != nil {
debug.Printf("Failed to add event %s to history: %v", evt.ID, err) debug.Printf("Failed to add event %s to history: %v", mxEvent.ID, err)
} }
evt := events[0]
if !c.config.AuthCache.InitialSyncDone { if !c.config.AuthCache.InitialSyncDone {
room.LastReceivedMessage = time.Unix(evt.Timestamp/1000, evt.Timestamp%1000*1000) room.LastReceivedMessage = time.Unix(evt.Timestamp/1000, evt.Timestamp%1000*1000)
@ -372,7 +374,7 @@ func (c *Container) HandleMessage(source EventSource, evt *mautrix.Event) {
} }
if !room.Loaded() { if !room.Loaded() {
pushRules := c.PushRules().GetActions(room, evt).Should() pushRules := c.PushRules().GetActions(room, evt.Event).Should()
shouldNotify := pushRules.Notify || !pushRules.NotifySpecified shouldNotify := pushRules.Notify || !pushRules.NotifySpecified
if !shouldNotify { if !shouldNotify {
room.LastReceivedMessage = time.Unix(evt.Timestamp/1000, evt.Timestamp%1000*1000) room.LastReceivedMessage = time.Unix(evt.Timestamp/1000, evt.Timestamp%1000*1000)
@ -388,7 +390,7 @@ func (c *Container) HandleMessage(source EventSource, evt *mautrix.Event) {
roomView.AddMessage(message) roomView.AddMessage(message)
roomView.MxRoom().LastReceivedMessage = message.Time() roomView.MxRoom().LastReceivedMessage = message.Time()
if c.syncer.FirstSyncDone { if c.syncer.FirstSyncDone {
pushRules := c.PushRules().GetActions(roomView.MxRoom(), evt).Should() pushRules := c.PushRules().GetActions(roomView.MxRoom(), evt.Event).Should()
mainView.NotifyMessage(roomView.MxRoom(), message, pushRules) mainView.NotifyMessage(roomView.MxRoom(), message, pushRules)
c.ui.Render() c.ui.Render()
} }
@ -581,7 +583,7 @@ func (c *Container) MarkRead(roomID, eventID string) {
var mentionRegex = regexp.MustCompile("\\[(.+?)]\\(https://matrix.to/#/@.+?:.+?\\)") var mentionRegex = regexp.MustCompile("\\[(.+?)]\\(https://matrix.to/#/@.+?:.+?\\)")
var roomRegex = regexp.MustCompile("\\[.+?]\\(https://matrix.to/#/(#.+?:[^/]+?)\\)") var roomRegex = regexp.MustCompile("\\[.+?]\\(https://matrix.to/#/(#.+?:[^/]+?)\\)")
func (c *Container) PrepareMarkdownMessage(roomID string, msgtype mautrix.MessageType, text string) *mautrix.Event { func (c *Container) PrepareMarkdownMessage(roomID string, msgtype mautrix.MessageType, text string) *event.Event {
content := format.RenderMarkdown(text) content := format.RenderMarkdown(text)
content.MsgType = msgtype content.MsgType = msgtype
@ -590,7 +592,7 @@ func (c *Container) PrepareMarkdownMessage(roomID string, msgtype mautrix.Messag
content.Body = roomRegex.ReplaceAllString(content.Body, "$1") content.Body = roomRegex.ReplaceAllString(content.Body, "$1")
txnID := c.client.TxnID() txnID := c.client.TxnID()
localEcho := &mautrix.Event{ localEcho := event.Wrap(&mautrix.Event{
ID: txnID, ID: txnID,
Sender: c.config.UserID, Sender: c.config.UserID,
Type: mautrix.EventMessage, Type: mautrix.EventMessage,
@ -599,14 +601,14 @@ func (c *Container) PrepareMarkdownMessage(roomID string, msgtype mautrix.Messag
Content: content, Content: content,
Unsigned: mautrix.Unsigned{ Unsigned: mautrix.Unsigned{
TransactionID: txnID, TransactionID: txnID,
OutgoingState: mautrix.EventStateLocalEcho,
}, },
} })
localEcho.Gomuks.OutgoingState = event.StateLocalEcho
return localEcho return localEcho
} }
// SendMarkdownMessage sends a message with the given markdown text to the given room. // SendMarkdownMessage sends a message with the given markdown text to the given room.
func (c *Container) SendEvent(event *mautrix.Event) (string, error) { func (c *Container) SendEvent(event *event.Event) (string, error) {
defer debug.Recover() defer debug.Recover()
c.SendTyping(event.RoomID, false) c.SendTyping(event.RoomID, false)
@ -670,7 +672,7 @@ func (c *Container) LeaveRoom(roomID string) error {
} }
// GetHistory fetches room history. // GetHistory fetches room history.
func (c *Container) GetHistory(room *rooms.Room, limit int) ([]*mautrix.Event, error) { func (c *Container) GetHistory(room *rooms.Room, limit int) ([]*event.Event, error) {
events, err := c.history.Load(room, limit) events, err := c.history.Load(room, limit)
if err != nil { if err != nil {
return nil, err return nil, err
@ -683,30 +685,32 @@ func (c *Container) GetHistory(room *rooms.Room, limit int) ([]*mautrix.Event, e
if err != nil { if err != nil {
return nil, err return nil, err
} }
if len(resp.Chunk) > 0 {
err = c.history.Prepend(room, resp.Chunk)
if err != nil {
return nil, err
}
}
debug.Printf("Loaded %d events for %s from server from %s to %s", len(resp.Chunk), room.ID, resp.Start, resp.End) debug.Printf("Loaded %d events for %s from server from %s to %s", len(resp.Chunk), room.ID, resp.Start, resp.End)
room.PrevBatch = resp.End room.PrevBatch = resp.End
c.config.Rooms.Put(room) c.config.Rooms.Put(room)
return resp.Chunk, nil if len(resp.Chunk) == 0 {
return []*event.Event{}, nil
} }
events, err = c.history.Prepend(room, resp.Chunk)
func (c *Container) GetEvent(room *rooms.Room, eventID string) (*mautrix.Event, error) {
event, err := c.history.Get(room, eventID)
if event != nil || err != nil {
debug.Printf("Found event %s in local cache", eventID)
return event, err
}
event, err = c.client.GetEvent(room.ID, eventID)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return events, nil
}
func (c *Container) GetEvent(room *rooms.Room, eventID string) (*event.Event, error) {
evt, err := c.history.Get(room, eventID)
if evt != nil || err != nil {
debug.Printf("Found event %s in local cache", eventID)
return evt, err
}
mxEvent, err := c.client.GetEvent(room.ID, eventID)
if err != nil {
return nil, err
}
evt = event.Wrap(mxEvent)
debug.Printf("Loaded event %s from server", eventID) debug.Printf("Loaded event %s from server", eventID)
return event, nil return evt, nil
} }
// GetOrCreateRoom gets the room instance stored in the session. // GetOrCreateRoom gets the room instance stored in the session.

View File

@ -22,6 +22,7 @@ import (
"time" "time"
"maunium.net/go/gomuks/config" "maunium.net/go/gomuks/config"
"maunium.net/go/gomuks/matrix/event"
"maunium.net/go/mautrix" "maunium.net/go/mautrix"
"maunium.net/go/mauview" "maunium.net/go/mauview"
"maunium.net/go/tcell" "maunium.net/go/tcell"
@ -50,7 +51,7 @@ type UIMessage struct {
SenderName string SenderName string
DefaultSenderColor tcell.Color DefaultSenderColor tcell.Color
Timestamp time.Time Timestamp time.Time
State mautrix.OutgoingEventState State event.OutgoingState
IsHighlight bool IsHighlight bool
IsService bool IsService bool
Source json.RawMessage Source json.RawMessage
@ -61,25 +62,25 @@ type UIMessage struct {
const DateFormat = "January _2, 2006" const DateFormat = "January _2, 2006"
const TimeFormat = "15:04:05" const TimeFormat = "15:04:05"
func newUIMessage(event *mautrix.Event, displayname string, renderer MessageRenderer) *UIMessage { func newUIMessage(evt *event.Event, displayname string, renderer MessageRenderer) *UIMessage {
msgtype := event.Content.MsgType msgtype := evt.Content.MsgType
if len(msgtype) == 0 { if len(msgtype) == 0 {
msgtype = mautrix.MessageType(event.Type.String()) msgtype = mautrix.MessageType(evt.Type.String())
} }
return &UIMessage{ return &UIMessage{
SenderID: event.Sender, SenderID: evt.Sender,
SenderName: displayname, SenderName: displayname,
Timestamp: unixToTime(event.Timestamp), Timestamp: unixToTime(evt.Timestamp),
DefaultSenderColor: widget.GetHashColor(event.Sender), DefaultSenderColor: widget.GetHashColor(evt.Sender),
Type: msgtype, Type: msgtype,
EventID: event.ID, EventID: evt.ID,
TxnID: event.Unsigned.TransactionID, TxnID: evt.Unsigned.TransactionID,
Relation: *event.Content.GetRelatesTo(), Relation: *evt.Content.GetRelatesTo(),
State: event.Unsigned.OutgoingState, State: evt.Gomuks.OutgoingState,
IsHighlight: false, IsHighlight: false,
IsService: false, IsService: false,
Source: event.Content.VeryRaw, Source: evt.Content.VeryRaw,
Renderer: renderer, Renderer: renderer,
} }
} }
@ -100,9 +101,9 @@ func unixToTime(unix int64) time.Time {
// In any other case, the sender is the display name of the user who sent the message. // In any other case, the sender is the display name of the user who sent the message.
func (msg *UIMessage) Sender() string { func (msg *UIMessage) Sender() string {
switch msg.State { switch msg.State {
case mautrix.EventStateLocalEcho: case event.StateLocalEcho:
return "Sending..." return "Sending..."
case mautrix.EventStateSendFail: case event.StateSendFail:
return "Error" return "Error"
} }
switch msg.Type { switch msg.Type {
@ -124,11 +125,11 @@ func (msg *UIMessage) NotificationContent() string {
func (msg *UIMessage) getStateSpecificColor() tcell.Color { func (msg *UIMessage) getStateSpecificColor() tcell.Color {
switch msg.State { switch msg.State {
case mautrix.EventStateLocalEcho: case event.StateLocalEcho:
return tcell.ColorGray return tcell.ColorGray
case mautrix.EventStateSendFail: case event.StateSendFail:
return tcell.ColorRed return tcell.ColorRed
case mautrix.EventStateDefault: case event.StateDefault:
fallthrough fallthrough
default: default:
return tcell.ColorDefault return tcell.ColorDefault

View File

@ -21,7 +21,7 @@ import (
"time" "time"
ifc "maunium.net/go/gomuks/interface" ifc "maunium.net/go/gomuks/interface"
"maunium.net/go/mautrix" "maunium.net/go/gomuks/matrix/event"
"maunium.net/go/mauview" "maunium.net/go/mauview"
"maunium.net/go/tcell" "maunium.net/go/tcell"
@ -35,8 +35,8 @@ type ExpandedTextMessage struct {
} }
// NewExpandedTextMessage creates a new ExpandedTextMessage object with the provided values and the default state. // NewExpandedTextMessage creates a new ExpandedTextMessage object with the provided values and the default state.
func NewExpandedTextMessage(event *mautrix.Event, displayname string, text tstring.TString) *UIMessage { func NewExpandedTextMessage(evt *event.Event, displayname string, text tstring.TString) *UIMessage {
return newUIMessage(event, displayname, &ExpandedTextMessage{ return newUIMessage(evt, displayname, &ExpandedTextMessage{
Text: text, Text: text,
}) })
} }

View File

@ -27,6 +27,7 @@ import (
"github.com/lucasb-eyer/go-colorful" "github.com/lucasb-eyer/go-colorful"
"golang.org/x/net/html" "golang.org/x/net/html"
"maunium.net/go/gomuks/matrix/event"
"maunium.net/go/mautrix" "maunium.net/go/mautrix"
"maunium.net/go/tcell" "maunium.net/go/tcell"
@ -382,7 +383,7 @@ func (parser *htmlParser) Parse(htmlData string) Entity {
const TabLength = 4 const TabLength = 4
// Parse parses a HTML-formatted Matrix event into a UIMessage. // Parse parses a HTML-formatted Matrix event into a UIMessage.
func Parse(room *rooms.Room, evt *mautrix.Event, senderDisplayname string) Entity { func Parse(room *rooms.Room, evt *event.Event, senderDisplayname string) Entity {
htmlData := evt.Content.FormattedBody htmlData := evt.Content.FormattedBody
if evt.Content.Format != mautrix.FormatHTML { if evt.Content.Format != mautrix.FormatHTML {
htmlData = strings.Replace(html.EscapeString(evt.Content.Body), "\n", "<br/>", -1) htmlData = strings.Replace(html.EscapeString(evt.Content.Body), "\n", "<br/>", -1)

View File

@ -18,7 +18,7 @@ package messages
import ( import (
ifc "maunium.net/go/gomuks/interface" ifc "maunium.net/go/gomuks/interface"
"maunium.net/go/mautrix" "maunium.net/go/gomuks/matrix/event"
"maunium.net/go/mauview" "maunium.net/go/mauview"
"maunium.net/go/tcell" "maunium.net/go/tcell"
@ -33,8 +33,8 @@ type HTMLMessage struct {
focused bool focused bool
} }
func NewHTMLMessage(event *mautrix.Event, displayname string, root html.Entity) *UIMessage { func NewHTMLMessage(evt *event.Event, displayname string, root html.Entity) *UIMessage {
return newUIMessage(event, displayname, &HTMLMessage{ return newUIMessage(evt, displayname, &HTMLMessage{
Root: root, Root: root,
}) })
} }

View File

@ -22,7 +22,7 @@ import (
"image" "image"
"image/color" "image/color"
"maunium.net/go/mautrix" "maunium.net/go/gomuks/matrix/event"
"maunium.net/go/mauview" "maunium.net/go/mauview"
"maunium.net/go/tcell" "maunium.net/go/tcell"
@ -44,8 +44,8 @@ type ImageMessage struct {
} }
// NewImageMessage creates a new ImageMessage object with the provided values and the default state. // NewImageMessage creates a new ImageMessage object with the provided values and the default state.
func NewImageMessage(matrix ifc.MatrixContainer, event *mautrix.Event, displayname string, body, homeserver, fileID string, data []byte) *UIMessage { func NewImageMessage(matrix ifc.MatrixContainer, evt *event.Event, displayname string, body, homeserver, fileID string, data []byte) *UIMessage {
return newUIMessage(event, displayname, &ImageMessage{ return newUIMessage(evt, displayname, &ImageMessage{
Body: body, Body: body,
Homeserver: homeserver, Homeserver: homeserver,
FileID: fileID, FileID: fileID,

View File

@ -20,6 +20,7 @@ import (
"fmt" "fmt"
"strings" "strings"
"maunium.net/go/gomuks/matrix/event"
"maunium.net/go/mautrix" "maunium.net/go/mautrix"
"maunium.net/go/tcell" "maunium.net/go/tcell"
@ -42,7 +43,7 @@ func getCachedEvent(mainView ifc.MainView, roomID, eventID string) *UIMessage {
return nil return nil
} }
func ParseEvent(matrix ifc.MatrixContainer, mainView ifc.MainView, room *rooms.Room, evt *mautrix.Event) *UIMessage { func ParseEvent(matrix ifc.MatrixContainer, mainView ifc.MainView, room *rooms.Room, evt *event.Event) *UIMessage {
msg := directParseEvent(matrix, room, evt) msg := directParseEvent(matrix, room, evt)
if msg == nil { if msg == nil {
return nil return nil
@ -63,7 +64,7 @@ func ParseEvent(matrix ifc.MatrixContainer, mainView ifc.MainView, room *rooms.R
return msg return msg
} }
func directParseEvent(matrix ifc.MatrixContainer, room *rooms.Room, evt *mautrix.Event) *UIMessage { func directParseEvent(matrix ifc.MatrixContainer, room *rooms.Room, evt *event.Event) *UIMessage {
displayname := evt.Sender displayname := evt.Sender
member := room.GetMember(evt.Sender) member := room.GetMember(evt.Sender)
if member != nil { if member != nil {
@ -89,7 +90,7 @@ func directParseEvent(matrix ifc.MatrixContainer, room *rooms.Room, evt *mautrix
return nil return nil
} }
func ParseStateEvent(evt *mautrix.Event, displayname string) *UIMessage { func ParseStateEvent(evt *event.Event, displayname string) *UIMessage {
text := tstring.NewColorTString(displayname, widget.GetHashColor(evt.Sender)) text := tstring.NewColorTString(displayname, widget.GetHashColor(evt.Sender))
switch evt.Type { switch evt.Type {
case mautrix.StateTopic: case mautrix.StateTopic:
@ -122,7 +123,7 @@ func ParseStateEvent(evt *mautrix.Event, displayname string) *UIMessage {
return NewExpandedTextMessage(evt, displayname, text) return NewExpandedTextMessage(evt, displayname, text)
} }
func ParseMessage(matrix ifc.MatrixContainer, room *rooms.Room, evt *mautrix.Event, displayname string) *UIMessage { func ParseMessage(matrix ifc.MatrixContainer, room *rooms.Room, evt *event.Event, displayname string) *UIMessage {
if len(evt.Content.GetReplyTo()) > 0 { if len(evt.Content.GetReplyTo()) > 0 {
evt.Content.RemoveReplyFallback() evt.Content.RemoveReplyFallback()
} }
@ -146,7 +147,7 @@ func ParseMessage(matrix ifc.MatrixContainer, room *rooms.Room, evt *mautrix.Eve
return nil return nil
} }
func getMembershipChangeMessage(evt *mautrix.Event, membership, prevMembership mautrix.Membership, senderDisplayname, displayname, prevDisplayname string) (sender string, text tstring.TString) { func getMembershipChangeMessage(evt *event.Event, membership, prevMembership mautrix.Membership, senderDisplayname, displayname, prevDisplayname string) (sender string, text tstring.TString) {
switch membership { switch membership {
case "invite": case "invite":
sender = "---" sender = "---"
@ -183,7 +184,7 @@ func getMembershipChangeMessage(evt *mautrix.Event, membership, prevMembership m
return return
} }
func getMembershipEventContent(room *rooms.Room, evt *mautrix.Event) (sender string, text tstring.TString) { func getMembershipEventContent(room *rooms.Room, evt *event.Event) (sender string, text tstring.TString) {
member := room.GetMember(evt.Sender) member := room.GetMember(evt.Sender)
senderDisplayname := evt.Sender senderDisplayname := evt.Sender
if member != nil { if member != nil {
@ -220,7 +221,7 @@ func getMembershipEventContent(room *rooms.Room, evt *mautrix.Event) (sender str
return return
} }
func ParseMembershipEvent(room *rooms.Room, evt *mautrix.Event) *UIMessage { func ParseMembershipEvent(room *rooms.Room, evt *event.Event) *UIMessage {
displayname, text := getMembershipEventContent(room, evt) displayname, text := getMembershipEventContent(room, evt)
if len(text) == 0 { if len(text) == 0 {
return nil return nil
@ -229,7 +230,7 @@ func ParseMembershipEvent(room *rooms.Room, evt *mautrix.Event) *UIMessage {
return NewExpandedTextMessage(evt, displayname, text) return NewExpandedTextMessage(evt, displayname, text)
} }
func ParseAliasEvent(evt *mautrix.Event, displayname string) tstring.TString { func ParseAliasEvent(evt *event.Event, displayname string) tstring.TString {
var prevAliases []string var prevAliases []string
if evt.Unsigned.PrevContent != nil { if evt.Unsigned.PrevContent != nil {
prevAliases = evt.Unsigned.PrevContent.Aliases prevAliases = evt.Unsigned.PrevContent.Aliases

View File

@ -18,7 +18,7 @@ package messages
import ( import (
ifc "maunium.net/go/gomuks/interface" ifc "maunium.net/go/gomuks/interface"
"maunium.net/go/mautrix" "maunium.net/go/gomuks/matrix/event"
"maunium.net/go/mauview" "maunium.net/go/mauview"
"maunium.net/go/tcell" "maunium.net/go/tcell"
@ -27,8 +27,8 @@ import (
type RedactedMessage struct{} type RedactedMessage struct{}
func NewRedactedMessage(event *mautrix.Event, displayname string) *UIMessage { func NewRedactedMessage(evt *event.Event, displayname string) *UIMessage {
return newUIMessage(event, displayname, &RedactedMessage{}) return newUIMessage(evt, displayname, &RedactedMessage{})
} }
func (msg *RedactedMessage) Clone() MessageRenderer { func (msg *RedactedMessage) Clone() MessageRenderer {

View File

@ -21,7 +21,7 @@ import (
"time" "time"
ifc "maunium.net/go/gomuks/interface" ifc "maunium.net/go/gomuks/interface"
"maunium.net/go/mautrix" "maunium.net/go/gomuks/matrix/event"
"maunium.net/go/mauview" "maunium.net/go/mauview"
"maunium.net/go/gomuks/config" "maunium.net/go/gomuks/config"
@ -35,8 +35,8 @@ type TextMessage struct {
} }
// NewTextMessage creates a new UITextMessage object with the provided values and the default state. // NewTextMessage creates a new UITextMessage object with the provided values and the default state.
func NewTextMessage(event *mautrix.Event, displayname string, text string) *UIMessage { func NewTextMessage(evt *event.Event, displayname string, text string) *UIMessage {
return newUIMessage(event, displayname, &TextMessage{ return newUIMessage(evt, displayname, &TextMessage{
Text: text, Text: text,
}) })
} }

View File

@ -27,6 +27,7 @@ import (
"github.com/mattn/go-runewidth" "github.com/mattn/go-runewidth"
"maunium.net/go/gomuks/debug" "maunium.net/go/gomuks/debug"
"maunium.net/go/gomuks/matrix/event"
"maunium.net/go/mauview" "maunium.net/go/mauview"
@ -414,7 +415,7 @@ func (view *RoomView) SendMessage(msgtype mautrix.MessageType, text string) {
view.AddMessage(msg) view.AddMessage(msg)
eventID, err := view.parent.matrix.SendEvent(evt) eventID, err := view.parent.matrix.SendEvent(evt)
if err != nil { if err != nil {
msg.State = mautrix.EventStateSendFail msg.State = event.StateSendFail
// Show shorter version if available // Show shorter version if available
if httpErr, ok := err.(mautrix.HTTPError); ok { if httpErr, ok := err.(mautrix.HTTPError); ok {
err = httpErr err = httpErr
@ -427,7 +428,7 @@ func (view *RoomView) SendMessage(msgtype mautrix.MessageType, text string) {
} else { } else {
debug.Print("Event ID received:", eventID) debug.Print("Event ID received:", eventID)
msg.EventID = eventID msg.EventID = eventID
msg.State = mautrix.EventStateDefault msg.State = event.StateDefault
view.MessageView().setMessageID(msg) view.MessageView().setMessageID(msg)
view.parent.parent.Render() view.parent.parent.Render()
} }
@ -465,11 +466,11 @@ func (view *RoomView) AddMessage(message ifc.Message) {
view.content.AddMessage(message, AppendMessage) view.content.AddMessage(message, AppendMessage)
} }
func (view *RoomView) parseEvent(evt *mautrix.Event) *messages.UIMessage { func (view *RoomView) parseEvent(evt *event.Event) *messages.UIMessage {
return messages.ParseEvent(view.parent.matrix, view.parent, view.Room, evt) return messages.ParseEvent(view.parent.matrix, view.parent, view.Room, evt)
} }
func (view *RoomView) ParseEvent(evt *mautrix.Event) ifc.Message { func (view *RoomView) ParseEvent(evt *event.Event) ifc.Message {
msg := view.parseEvent(evt) msg := view.parseEvent(evt)
if msg == nil { if msg == nil {
return nil return nil