~geb/sup

edf74065e1bc8bb05fc0923f504e765da6caa673 — John Gebbie 1 year, 11 months ago b70759e
pull recent messages
1 files changed, 61 insertions(+), 1 deletions(-)

M main.go
M main.go => main.go +61 -1
@@ 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()