refactor(chat): add isMe helper to dedupe self-author checks

This commit is contained in:
ayn2op 2026-05-24 19:25:27 -04:00
parent 83f9c049cf
commit 15f95fb617
3 changed files with 11 additions and 11 deletions

View file

@ -1273,8 +1273,7 @@ func (ml *messagesList) editSelectedMessage() tview.Cmd {
return nil
}
me, _ := ml.chat.state.Cabinet.Me()
if selectedMessage.Author.ID != me.ID {
if !ml.chat.isMe(selectedMessage.Author.ID) {
slog.Error("failed to edit message; not the author", "channel_id", selectedMessage.ChannelID, "message_id", selectedMessage.ID)
return nil
}
@ -1309,8 +1308,7 @@ func (ml *messagesList) deleteSelectedMessage() tview.Cmd {
return func() tview.Msg {
if selectedMessage.GuildID.IsValid() {
me, _ := ml.chat.state.Cabinet.Me()
if selectedMessage.Author.ID != me.ID && !ml.chat.state.HasPermissions(selectedMessage.ChannelID, discord.PermissionManageMessages) {
if !ml.chat.isMe(selectedMessage.Author.ID) && !ml.chat.state.HasPermissions(selectedMessage.ChannelID, discord.PermissionManageMessages) {
slog.Error("failed to delete message; missing relevant permissions", "channel_id", selectedMessage.ChannelID, "message_id", selectedMessage.ID)
return nil
}
@ -1402,8 +1400,7 @@ func (ml *messagesList) ShortHelp() []keybind.Keybind {
}
if selectedMessage, ok := ml.selectedMessage(); ok {
me, _ := ml.chat.state.Cabinet.Me()
if selectedMessage.Author.ID != me.ID {
if !ml.chat.isMe(selectedMessage.Author.ID) {
help = append(help, cfg.Reply.Keybind)
}
}
@ -1423,9 +1420,8 @@ func (ml *messagesList) FullHelp() [][]keybind.Keybind {
canSelectReply = selectedMessage.ReferencedMessage != nil
canOpen = len(messageURLs(*selectedMessage)) != 0 || len(selectedMessage.Attachments) != 0
me, _ := ml.chat.state.Cabinet.Me()
canReply = selectedMessage.Author.ID != me.ID
canEdit = selectedMessage.Author.ID == me.ID
canEdit = ml.chat.isMe(selectedMessage.Author.ID)
canReply = !canEdit
canDelete = canEdit
if !canDelete {
selectedChannel, ok := ml.chat.SelectedChannel()

View file

@ -122,6 +122,11 @@ func (m *Model) SetSelectedChannel(channel *discord.Channel) {
m.selectedChannelMu.Unlock()
}
func (m *Model) isMe(id discord.UserID) bool {
me, _ := m.state.Cabinet.Me()
return me != nil && id == me.ID
}
func (m *Model) buildLayout() {
m.Clear()
m.rightFlex.Clear()

View file

@ -180,8 +180,7 @@ func (m *Model) onTypingStart(event *gateway.TypingStartEvent) {
return
}
me, _ := m.state.Cabinet.Me()
if event.UserID == me.ID {
if m.isMe(event.UserID) {
return
}