From 50ba10bf61c122fe4596fdd7abf3c281f3f5e51c Mon Sep 17 00:00:00 2001 From: diamondburned Date: Thu, 16 Jul 2020 20:12:42 -0700 Subject: [PATCH] Joining a channel now marks it as read --- channel.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/channel.go b/channel.go index 141f04a..d48cc22 100644 --- a/channel.go +++ b/channel.go @@ -237,15 +237,19 @@ func (ch *Channel) JoinServer(ctx context.Context, ct cchat.MessagesContainer) ( } } - // Sort messages chronologically using the ID so that the oldest messages - // (ones with the smallest snowflake) is in front. - sort.Slice(m, func(i, j int) bool { - return m[i].ID < m[j].ID - }) + // Only do all this if we even have any messages. + if len(m) > 0 { + // Sort messages chronologically using the ID so that the oldest messages + // (ones with the smallest snowflake) is in front. + sort.Slice(m, func(i, j int) bool { return m[i].ID < m[j].ID }) - // Iterate from the earliest messages to the latest messages. - for _, m := range m { - ct.CreateMessage(constructor(m)) + // Iterate from the earliest messages to the latest messages. + for _, m := range m { + ct.CreateMessage(constructor(m)) + } + + // Mark this channel as read. + ch.session.ReadState.MarkRead(ch.id, m[len(m)-1].ID) } // Bind the handler.