@@ 233,12 233,16 @@ func NewApp(client *mautrix.Client) *App {
return app
}
-func (app *App) AddMessage(msg *Message) {
+func (app *App) addMessage(msg *Message) {
app.Messages[msg.RoomID] = append(app.Messages[msg.RoomID], msg)
if msg.Media != nil {
msg.Media.ID = len(app.Uploads[msg.RoomID]) + 1
app.Uploads[msg.RoomID] = append(app.Uploads[msg.RoomID], msg.Media)
}
+}
+
+func (app *App) AddMessage(msg *Message) {
+ app.addMessage(msg)
if app.Screen == msg.RoomID {
app.UI.Feed.Entries = append(app.UI.Feed.Entries, msg)
@@ 572,11 576,60 @@ func (app *App) alive() {
app.ReadReceipts[app.RoomID] = eid
_ = app.Client.MarkRead(app.RoomID, eid)
app.UI.StatusBar.Blink = false
+ app.Unread[app.RoomID] = 0
+ app.Unseen[app.RoomID] = false
}
}
}
}
+// based off DefaultSyncer.processSyncEvent
+func processMessageEvent(roomID id.RoomID, evt *event.Event) {
+ evt.RoomID = roomID
+ evt.Type.Class = event.MessageEventType
+ _ = evt.Content.ParseRaw(evt.Type)
+}
+
+func (app *App) pullRecentMessages() error {
+ var fid string
+ {
+ resp, err := app.Client.CreateFilter(&mautrix.Filter{
+ Room: mautrix.RoomFilter{
+ Timeline: mautrix.FilterPart{
+ Limit: 50,
+ Types: []event.Type{event.EventMessage},
+ },
+ },
+ })
+
+ if err != nil {
+ return err
+ }
+ fid = resp.FilterID
+ }
+ resp, err := app.Client.FullSyncRequest(mautrix.ReqSync{
+ Timeout: 30000,
+ Since: "",
+ FilterID: fid,
+ FullState: false,
+ SetPresence: app.Client.SyncPresence,
+ Context: context.Background(),
+ StreamResponse: false,
+ })
+ if err != nil {
+ return err
+ }
+
+ for rid, room := range resp.Rooms.Join {
+ for _, ev := range room.Timeline.Events {
+ processMessageEvent(rid, ev)
+ app.addMessage(normalMessage(ev))
+ }
+ }
+
+ return nil
+}
+
func (app *App) Run() int {
app.UI.Ansi.Screen(ansi.Alt)
defer app.UI.Ansi.Screen(ansi.Normal)
@@ 588,6 641,13 @@ func (app *App) Run() int {
fatal("Failed to capture terminal input: " + err.Error())
}
+ if err := app.pullRecentMessages(); err != nil {
+ fatal(err.Error())
+ }
+ for rid := range app.Messages {
+ app.Unread[rid] = app.unread(rid)
+ app.Unseen[rid] = app.unseen(rid) > 0
+ }
updates := app.sync()
app.roomsScreen()