refactor(notifications): do not parse markdown in content
This commit is contained in:
parent
7e545793f3
commit
36ebaa6cf9
2 changed files with 4 additions and 107 deletions
|
|
@ -6,14 +6,12 @@ import (
|
|||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/ayn2op/discordo/internal/config"
|
||||
"github.com/ayn2op/discordo/internal/consts"
|
||||
"github.com/diamondburned/arikawa/v3/discord"
|
||||
"github.com/diamondburned/arikawa/v3/gateway"
|
||||
"github.com/diamondburned/ningen/v3"
|
||||
"github.com/diamondburned/ningen/v3/discordmd"
|
||||
)
|
||||
|
||||
func HandleIncomingMessage(state *ningen.State, msg *gateway.MessageCreateEvent, cfg *config.Config) error {
|
||||
|
|
@ -36,21 +34,13 @@ func HandleIncomingMessage(state *ningen.State, msg *gateway.MessageCreateEvent,
|
|||
}
|
||||
}
|
||||
|
||||
// Render message
|
||||
src := []byte(msg.Content)
|
||||
ast := discordmd.ParseWithMessage(src, *state.Cabinet, &msg.Message, false)
|
||||
buff := strings.Builder{}
|
||||
if err := defaultRenderer.Render(&buff, src, ast); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Handle sent files
|
||||
notifContent := buff.String()
|
||||
content := msg.Content
|
||||
if msg.Content == "" && len(msg.Attachments) > 0 {
|
||||
notifContent = "Uploaded " + msg.Message.Attachments[0].Filename
|
||||
content = "Uploaded " + msg.Message.Attachments[0].Filename
|
||||
}
|
||||
|
||||
if msg.Author.DisplayOrTag() == "" || notifContent == "" {
|
||||
if msg.Author.DisplayOrTag() == "" || content == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
@ -74,7 +64,7 @@ func HandleIncomingMessage(state *ningen.State, msg *gateway.MessageCreateEvent,
|
|||
}
|
||||
|
||||
shouldChime := cfg.Notifications.Sound.Enabled && (!cfg.Notifications.Sound.OnlyOnPing || (isChannelDM || state.MessageMentions(&msg.Message) == 3))
|
||||
if err := sendDesktopNotification(notifTitle, notifContent, imagePath, shouldChime, cfg.Notifications.Duration); err != nil {
|
||||
if err := sendDesktopNotification(notifTitle, content, imagePath, shouldChime, cfg.Notifications.Duration); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,93 +0,0 @@
|
|||
package notifications
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/diamondburned/ningen/v3/discordmd"
|
||||
"github.com/yuin/goldmark/ast"
|
||||
gmr "github.com/yuin/goldmark/renderer"
|
||||
)
|
||||
|
||||
// Using a modified version of the discordmd BasicRenderer
|
||||
var defaultRenderer = newRenderer()
|
||||
|
||||
type renderer struct {
|
||||
config *gmr.Config
|
||||
}
|
||||
|
||||
func newRenderer() *renderer {
|
||||
config := gmr.NewConfig()
|
||||
return &renderer{config}
|
||||
}
|
||||
|
||||
func (r *renderer) AddOptions(opts ...gmr.Option) {
|
||||
for _, opt := range opts {
|
||||
opt.SetConfig(r.config)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *renderer) Render(w io.Writer, source []byte, n ast.Node) error {
|
||||
return ast.Walk(n, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
|
||||
switch n := n.(type) {
|
||||
case *ast.Document:
|
||||
// noop
|
||||
case *ast.Blockquote:
|
||||
io.WriteString(w, "\"")
|
||||
case *ast.Heading:
|
||||
io.WriteString(w, "\n")
|
||||
case *ast.FencedCodeBlock:
|
||||
io.WriteString(w, "\n")
|
||||
|
||||
if entering {
|
||||
lines := n.Lines()
|
||||
for i := range lines.Len() {
|
||||
line := lines.At(i)
|
||||
io.WriteString(w, "| ")
|
||||
w.Write(line.Value(source))
|
||||
}
|
||||
}
|
||||
case *ast.AutoLink:
|
||||
if entering {
|
||||
w.Write(n.URL(source))
|
||||
}
|
||||
case *ast.Link:
|
||||
if !entering {
|
||||
io.WriteString(w, " ("+string(n.Destination)+")")
|
||||
}
|
||||
case *discordmd.Inline:
|
||||
if n.Attr&discordmd.AttrSpoiler != 0 {
|
||||
if entering {
|
||||
io.WriteString(w, "*spoiler*")
|
||||
}
|
||||
return ast.WalkSkipChildren, nil
|
||||
}
|
||||
case *ast.Text:
|
||||
if entering {
|
||||
w.Write(n.Segment.Value(source))
|
||||
switch {
|
||||
case n.HardLineBreak():
|
||||
io.WriteString(w, "\n\n")
|
||||
case n.SoftLineBreak():
|
||||
io.WriteString(w, "\n")
|
||||
}
|
||||
}
|
||||
case *discordmd.Mention:
|
||||
if entering {
|
||||
switch {
|
||||
case n.Channel != nil:
|
||||
io.WriteString(w, "#"+n.Channel.Name)
|
||||
case n.GuildUser != nil:
|
||||
io.WriteString(w, "@"+n.GuildUser.Username)
|
||||
case n.GuildRole != nil:
|
||||
io.WriteString(w, "@"+n.GuildRole.Name)
|
||||
}
|
||||
}
|
||||
case *discordmd.Emoji:
|
||||
if entering {
|
||||
io.WriteString(w, ":"+string(n.Name)+":")
|
||||
}
|
||||
}
|
||||
|
||||
return ast.WalkContinue, nil
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue