Skip to content

Commit

Permalink
Bump protos
Browse files Browse the repository at this point in the history
  • Loading branch information
belak committed Jan 4, 2025
1 parent 57ea27d commit 24c3a0e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 21 deletions.
59 changes: 40 additions & 19 deletions backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func (b *Backend) handleMessageCreate(s *discordgo.Session, m *discordgo.Message
}

func (b *Backend) handleMessageCreateImpl(s *discordgo.Session, m *discordgo.MessageCreate) {
ok, err := ComesFromDM(s, m)
fromDM, err := ComesFromDM(s, m)
if err != nil {
b.logger.Warn().Err(err).Msg("failed to determine if message is private")
return
Expand All @@ -227,22 +227,28 @@ func (b *Backend) handleMessageCreateImpl(s *discordgo.Session, m *discordgo.Mes
return
}

if ok {
if text, ok := ActionText(rawText); ok {
if fromDM {
rootBlock, isAction, err := TextToBlock(rawText)
if err != nil {
b.logger.Warn().Err(err).Msg("failed to convert message to blocks")
return
}

if isAction {
b.writeEvent(&pb.ChatEvent{Inner: &pb.ChatEvent_PrivateAction{PrivateAction: &pb.PrivateActionEvent{
Source: &pb.User{
Id: m.ChannelID,
DisplayName: m.Author.Username,
},
Text: text,
RootBlock: rootBlock,
}}})
} else {
b.writeEvent(&pb.ChatEvent{Inner: &pb.ChatEvent_PrivateMessage{PrivateMessage: &pb.PrivateMessageEvent{
Source: &pb.User{
Id: m.ChannelID,
DisplayName: m.Author.Username,
},
Text: rawText,
RootBlock: rootBlock,
}}})
}
return
Expand All @@ -256,14 +262,8 @@ func (b *Backend) handleMessageCreateImpl(s *discordgo.Session, m *discordgo.Mes
},
}

if text, ok := ActionText(rawText); ok {
b.writeEvent(&pb.ChatEvent{Inner: &pb.ChatEvent_Action{Action: &pb.ActionEvent{
Source: source,
Text: text,
}}})
return
}

// Special case - if the message started with the command prefix, we do much
// less parsing and processing on it.
if strings.HasPrefix(rawText, b.cmdPrefix) {
msgParts := strings.SplitN(rawText, " ", 2)
if len(msgParts) < 2 {
Expand All @@ -281,22 +281,43 @@ func (b *Backend) handleMessageCreateImpl(s *discordgo.Session, m *discordgo.Mes
return
}

// Special case - if the original message started with the bot's user ID,
// make sure we trim that off before processing as a mention event.
mentionPrefix := fmt.Sprintf("<@%s>", s.State.User.ID)
if strings.HasPrefix(m.Content, mentionPrefix) {
msg := *m.Message
msg.Content = strings.TrimSpace(strings.TrimPrefix(m.Content, mentionPrefix))

rootBlock, _, err := TextToBlock(ReplaceMentions(b.logger, s, &msg))
if err != nil {
b.logger.Warn().Err(err).Msg("failed to convert message to blocks")
return
}

b.writeEvent(&pb.ChatEvent{Inner: &pb.ChatEvent_Mention{Mention: &pb.MentionEvent{
Source: source,
Text: ReplaceMentions(b.logger, s, &msg),
Source: source,
RootBlock: rootBlock,
}}})
return
}

b.writeEvent(&pb.ChatEvent{Inner: &pb.ChatEvent_Message{Message: &pb.MessageEvent{
Source: source,
Text: rawText,
}}})
rootBlock, isAction, err := TextToBlock(rawText)
if err != nil {
b.logger.Warn().Err(err).Msg("failed to convert message to blocks")
return
}

if isAction {
b.writeEvent(&pb.ChatEvent{Inner: &pb.ChatEvent_Action{Action: &pb.ActionEvent{
Source: source,
RootBlock: rootBlock,
}}})
} else {
b.writeEvent(&pb.ChatEvent{Inner: &pb.ChatEvent_Message{Message: &pb.MessageEvent{
Source: source,
RootBlock: rootBlock,
}}})
}
}

func (b *Backend) sendJoinNotification(s *discordgo.Session, guildID, userID, channelID string, count int) {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ require (

replace github.com/seabird-chat/seabird-go => ../seabird-go

// This fork is needed because CommonMark allows H4-H6, but Discord doesn't
replace github.com/yuin/goldmark => github.com/belak-forks/goldmark v0.0.0-20250104065338-f2faabf722aa
5 changes: 4 additions & 1 deletion messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import (
"github.com/seabird-chat/seabird-go/pb"
)

// TODO: support timestamps
// TODO: support custom emoji

func maybeContainer(blocks ...*pb.Block) *pb.Block {
if len(blocks) == 1 {
return blocks[0]
Expand All @@ -23,7 +26,7 @@ func maybeContainer(blocks ...*pb.Block) *pb.Block {
return seabird.NewContainerBlock(blocks...)
}

func TextToBlocks(data string) (*pb.Block, bool, error) {
func TextToBlock(data string) (*pb.Block, bool, error) {
var isAction bool

// If the message starts and ends with an underscore, it's an "action"
Expand Down
2 changes: 1 addition & 1 deletion messages_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func TestTextToBlocks(t *testing.T) {

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
blocks, isAction, err := TextToBlocks(testCase.input)
blocks, isAction, err := TextToBlock(testCase.input)
assert.NoError(t, err)
assert.Equal(t, isAction, testCase.isAction)
expected, err := protojson.Marshal(testCase.expected)
Expand Down

0 comments on commit 24c3a0e

Please sign in to comment.