~taiite/public-inbox

senpai: Switch to the buffer of a new user-requested channel join v2 APPLIED

delthas: 1
 Switch to the buffer of a new user-requested channel join

 5 files changed, 50 insertions(+), 30 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~taiite/public-inbox/patches/23817/mbox | git am -3
Learn more about email & git
View this thread in the archives

[PATCH senpai v2] Switch to the buffer of a new user-requested channel join Export this patch

---
 app.go         |  5 ++++-
 irc/events.go  |  3 ++-
 irc/session.go | 51 +++++++++++++++++++++++++++++---------------------
 ui/buffers.go  |  9 ++++-----
 ui/ui.go       | 12 ++++++++++--
 5 files changed, 50 insertions(+), 30 deletions(-)

diff --git a/app.go b/app.go
index 57c12de..abfc65f 100644
--- a/app.go
+++ b/app.go
@@ -510,10 +510,13 @@ func (app *App) handleIRCEvent(ev interface{}) {
			})
		}
	case irc.SelfJoinEvent:
		app.win.AddBuffer(ev.Channel)
		i := app.win.AddBuffer(ev.Channel)
		app.s.NewHistoryRequest(ev.Channel).
			WithLimit(200).
			Before(msg.TimeOrNow())
		if ev.Requested {
			app.win.JumpBufferIndex(i)
		}
	case irc.UserJoinEvent:
		body := new(ui.StyledStringBuilder)
		body.Grow(len(ev.User) + 1)
diff --git a/irc/events.go b/irc/events.go
index bdd6914..0451a80 100644
--- a/irc/events.go
+++ b/irc/events.go
@@ -22,7 +22,8 @@ type UserNickEvent struct {
}

type SelfJoinEvent struct {
	Channel string
	Channel   string
	Requested bool // whether we recently requested to join that channel
}

type UserJoinEvent struct {
diff --git a/irc/session.go b/irc/session.go
index dabec03..c46f0b2 100644
--- a/irc/session.go
+++ b/irc/session.go
@@ -128,30 +128,33 @@ type Session struct {
	channels  map[string]Channel      // joined channels.
	chBatches map[string]HistoryEvent // channel history batches being processed.
	chReqs    map[string]struct{}     // set of targets for which history is currently requested.

	pendingChannels map[string]time.Time // set of join requests stamps for channels.
}

func NewSession(out chan<- Message, params SessionParams) *Session {
	s := &Session{
		out:           out,
		typings:       NewTypings(),
		typingStamps:  map[string]time.Time{},
		nick:          params.Nickname,
		nickCf:        CasemapASCII(params.Nickname),
		user:          params.Username,
		real:          params.RealName,
		auth:          params.Auth,
		availableCaps: map[string]string{},
		enabledCaps:   map[string]struct{}{},
		casemap:       CasemapRFC1459,
		chantypes:     "#&",
		linelen:       512,
		historyLimit:  100,
		prefixSymbols: "@+",
		prefixModes:   "ov",
		users:         map[string]*User{},
		channels:      map[string]Channel{},
		chBatches:     map[string]HistoryEvent{},
		chReqs:        map[string]struct{}{},
		out:             out,
		typings:         NewTypings(),
		typingStamps:    map[string]time.Time{},
		nick:            params.Nickname,
		nickCf:          CasemapASCII(params.Nickname),
		user:            params.Username,
		real:            params.RealName,
		auth:            params.Auth,
		availableCaps:   map[string]string{},
		enabledCaps:     map[string]struct{}{},
		casemap:         CasemapRFC1459,
		chantypes:       "#&",
		linelen:         512,
		historyLimit:    100,
		prefixSymbols:   "@+",
		prefixModes:     "ov",
		users:           map[string]*User{},
		channels:        map[string]Channel{},
		chBatches:       map[string]HistoryEvent{},
		chReqs:          map[string]struct{}{},
		pendingChannels: map[string]time.Time{},
	}

	s.out <- NewMessage("CAP", "LS", "302")
@@ -270,6 +273,8 @@ func (s *Session) SendRaw(raw string) {
}

func (s *Session) Join(channel, key string) {
	channelCf := s.Casemap(channel)
	s.pendingChannels[channelCf] = time.Now()
	if key == "" {
		s.out <- NewMessage("JOIN", channel)
	} else {
@@ -658,9 +663,13 @@ func (s *Session) handleRegistered(msg Message) Event {
		if c, ok := s.channels[channelCf]; ok && !c.complete {
			c.complete = true
			s.channels[channelCf] = c
			return SelfJoinEvent{
			ev := SelfJoinEvent{
				Channel: c.Name,
			}
			if stamp, ok := s.pendingChannels[channelCf]; ok && time.Now().Sub(stamp) < 5*time.Second {
				ev.Requested = true
			}
			return ev
		}
	case rplTopic:
		channelCf := s.Casemap(msg.Params[1])
diff --git a/ui/buffers.go b/ui/buffers.go
index 297f70e..6de3923 100644
--- a/ui/buffers.go
+++ b/ui/buffers.go
@@ -224,17 +224,16 @@ func (bs *BufferList) Previous() {
	bs.list[bs.current].unread = false
}

func (bs *BufferList) Add(title string) (ok bool) {
func (bs *BufferList) Add(title string) int {
	lTitle := strings.ToLower(title)
	for _, b := range bs.list {
	for i, b := range bs.list {
		if strings.ToLower(b.title) == lTitle {
			return
			return i
		}
	}

	ok = true
	bs.list = append(bs.list, buffer{title: title})
	return
	return len(bs.list) - 1
}

func (bs *BufferList) Remove(title string) (ok bool) {
diff --git a/ui/ui.go b/ui/ui.go
index 0b45a3c..484acd8 100644
--- a/ui/ui.go
+++ b/ui/ui.go
@@ -128,8 +128,8 @@ func (ui *UI) IsAtTop() bool {
	return ui.bs.IsAtTop()
}

func (ui *UI) AddBuffer(title string) {
	_ = ui.bs.Add(title)
func (ui *UI) AddBuffer(title string) int {
	return ui.bs.Add(title)
}

func (ui *UI) RemoveBuffer(title string) {
@@ -156,6 +156,14 @@ func (ui *UI) JumpBuffer(sub string) bool {
	return false
}

func (ui *UI) JumpBufferIndex(i int) bool {
	if i >= 0 && i < len(ui.bs.list) {
		ui.bs.To(i)
		return true
	}
	return false
}

func (ui *UI) SetStatus(status string) {
	ui.status = status
}
-- 
2.30.0
Pushed, thanks!