Convert everyone/here into room and block other room mentions

This commit is contained in:
Tulir Asokan
2023-01-29 15:59:47 +02:00
parent 0f32f966b3
commit 254148f870
3 changed files with 115 additions and 6 deletions

View File

@@ -31,13 +31,9 @@ import (
"maunium.net/go/mautrix/util/variationselector" "maunium.net/go/mautrix/util/variationselector"
) )
var discordExtensions = goldmark.WithExtensions(mdext.SimpleSpoiler, mdext.DiscordUnderline) var discordExtensions = goldmark.WithExtensions(mdext.SimpleSpoiler, mdext.DiscordUnderline, &DiscordEveryone{})
var escapeFixer = regexp.MustCompile(`\\(__[^_]|\*\*[^*])`) var escapeFixer = regexp.MustCompile(`\\(__[^_]|\*\*[^*])`)
func (portal *Portal) renderDiscordMarkdown(text string) event.MessageEventContent {
return format.HTMLToContent(portal.renderDiscordMarkdownOnlyHTML(text))
}
func (portal *Portal) renderDiscordMarkdownOnlyHTML(text string) string { func (portal *Portal) renderDiscordMarkdownOnlyHTML(text string) string {
text = escapeFixer.ReplaceAllStringFunc(text, func(s string) string { text = escapeFixer.ReplaceAllStringFunc(text, func(s string) string {
return s[:2] + `\` + s[2:] return s[:2] + `\` + s[2:]

108
formatter_everyone.go Normal file
View File

@@ -0,0 +1,108 @@
// mautrix-discord - A Matrix-Discord puppeting bridge.
// Copyright (C) 2023 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"fmt"
"regexp"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/parser"
"github.com/yuin/goldmark/renderer"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)
type astDiscordEveryone struct {
ast.BaseInline
onlyHere bool
}
var _ ast.Node = (*astDiscordEveryone)(nil)
var astKindDiscordEveryone = ast.NewNodeKind("DiscordEveryone")
func (n *astDiscordEveryone) Dump(source []byte, level int) {
ast.DumpHelper(n, source, level, nil, nil)
}
func (n *astDiscordEveryone) Kind() ast.NodeKind {
return astKindDiscordEveryone
}
func (n *astDiscordEveryone) String() string {
if n.onlyHere {
return "@here"
}
return "@everyone"
}
type discordEveryoneParser struct{}
var discordEveryoneRegex = regexp.MustCompile(`@(everyone|here)`)
var defaultDiscordEveryoneParser = &discordEveryoneParser{}
func (s *discordEveryoneParser) Trigger() []byte {
return []byte{'@'}
}
func (s *discordEveryoneParser) Parse(parent ast.Node, block text.Reader, pc parser.Context) ast.Node {
line, _ := block.PeekLine()
match := discordEveryoneRegex.FindSubmatch(line)
if match == nil {
return nil
}
block.Advance(len(match[0]))
return &astDiscordEveryone{
onlyHere: string(match[1]) == "here",
}
}
func (s *discordEveryoneParser) CloseBlock(parent ast.Node, pc parser.Context) {
// nothing to do
}
type discordEveryoneHTMLRenderer struct{}
func (r *discordEveryoneHTMLRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {
reg.Register(astKindDiscordEveryone, r.renderDiscordEveryone)
}
func (r *discordEveryoneHTMLRenderer) renderDiscordEveryone(w util.BufWriter, source []byte, n ast.Node, entering bool) (status ast.WalkStatus, err error) {
status = ast.WalkContinue
if !entering {
return
}
mention, _ := n.(*astDiscordEveryone)
class := "everyone"
if mention != nil && mention.onlyHere {
class = "here"
}
_, _ = fmt.Fprintf(w, `<span class="discord-mention-%s">@room</span>`, class)
return
}
type DiscordEveryone struct{}
func (e *DiscordEveryone) Extend(m goldmark.Markdown) {
m.Parser().AddOptions(parser.WithInlineParsers(
util.Prioritized(defaultDiscordEveryoneParser, 600),
))
m.Renderer().AddOptions(renderer.WithNodeRenderers(
util.Prioritized(&discordEveryoneHTMLRenderer{}, 600),
))
}

View File

@@ -951,7 +951,12 @@ func (portal *Portal) convertDiscordTextMessage(intent *appservice.IntentAPI, ms
return nil return nil
} }
content := format.HTMLToContent(strings.Join(htmlParts, "\n")) fullHTML := strings.Join(htmlParts, "\n")
if !msg.MentionEveryone {
fullHTML = strings.ReplaceAll(fullHTML, "@room", "@\u2063ro\u2063om")
}
content := format.HTMLToContent(fullHTML)
if relation != nil { if relation != nil {
content.RelatesTo = relation.Copy() content.RelatesTo = relation.Copy()
} }