Remove unnecessary user parameter in parseMatrixHTML
This commit is contained in:
21
formatter.go
21
formatter.go
@@ -90,23 +90,21 @@ func (portal *Portal) renderDiscordMarkdownOnlyHTML(text string, allowInlineLink
|
|||||||
return format.UnwrapSingleParagraph(buf.String())
|
return format.UnwrapSingleParagraph(buf.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
const formatterContextUserKey = "fi.mau.discord.user"
|
|
||||||
const formatterContextPortalKey = "fi.mau.discord.portal"
|
const formatterContextPortalKey = "fi.mau.discord.portal"
|
||||||
|
|
||||||
func pillConverter(displayname, mxid, eventID string, ctx format.Context) string {
|
func (br *DiscordBridge) pillConverter(displayname, mxid, eventID string, ctx format.Context) string {
|
||||||
if len(mxid) == 0 {
|
if len(mxid) == 0 {
|
||||||
return displayname
|
return displayname
|
||||||
}
|
}
|
||||||
user := ctx.ReturnData[formatterContextUserKey].(*User)
|
|
||||||
if mxid[0] == '#' {
|
if mxid[0] == '#' {
|
||||||
alias, err := user.bridge.Bot.ResolveAlias(id.RoomAlias(mxid))
|
alias, err := br.Bot.ResolveAlias(id.RoomAlias(mxid))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return displayname
|
return displayname
|
||||||
}
|
}
|
||||||
mxid = alias.RoomID.String()
|
mxid = alias.RoomID.String()
|
||||||
}
|
}
|
||||||
if mxid[0] == '!' {
|
if mxid[0] == '!' {
|
||||||
portal := user.bridge.GetPortalByMXID(id.RoomID(mxid))
|
portal := br.GetPortalByMXID(id.RoomID(mxid))
|
||||||
if portal != nil {
|
if portal != nil {
|
||||||
if eventID == "" {
|
if eventID == "" {
|
||||||
//currentPortal := ctx[formatterContextPortalKey].(*Portal)
|
//currentPortal := ctx[formatterContextPortalKey].(*Portal)
|
||||||
@@ -117,7 +115,7 @@ func pillConverter(displayname, mxid, eventID string, ctx format.Context) string
|
|||||||
//} else {
|
//} else {
|
||||||
// // TODO is mentioning private channels possible at all?
|
// // TODO is mentioning private channels possible at all?
|
||||||
//}
|
//}
|
||||||
} else if msg := user.bridge.DB.Message.GetByMXID(portal.Key, id.EventID(eventID)); msg != nil {
|
} else if msg := br.DB.Message.GetByMXID(portal.Key, id.EventID(eventID)); msg != nil {
|
||||||
guildID := portal.GuildID
|
guildID := portal.GuildID
|
||||||
if guildID == "" {
|
if guildID == "" {
|
||||||
guildID = "@me"
|
guildID = "@me"
|
||||||
@@ -126,11 +124,11 @@ func pillConverter(displayname, mxid, eventID string, ctx format.Context) string
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if mxid[0] == '@' {
|
} else if mxid[0] == '@' {
|
||||||
parsedID, ok := user.bridge.ParsePuppetMXID(id.UserID(mxid))
|
parsedID, ok := br.ParsePuppetMXID(id.UserID(mxid))
|
||||||
if ok {
|
if ok {
|
||||||
return fmt.Sprintf("<@%s>", parsedID)
|
return fmt.Sprintf("<@%s>", parsedID)
|
||||||
}
|
}
|
||||||
mentionedUser := user.bridge.GetUserByMXID(id.UserID(mxid))
|
mentionedUser := br.GetUserByMXID(id.UserID(mxid))
|
||||||
if mentionedUser != nil && mentionedUser.DiscordID != "" {
|
if mentionedUser != nil && mentionedUser.DiscordID != "" {
|
||||||
return fmt.Sprintf("<@%s>", mentionedUser.DiscordID)
|
return fmt.Sprintf("<@%s>", mentionedUser.DiscordID)
|
||||||
}
|
}
|
||||||
@@ -197,14 +195,9 @@ var matrixHTMLParser = &format.HTMLParser{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func (portal *Portal) parseMatrixHTML(content *event.MessageEventContent) string {
|
||||||
matrixHTMLParser.PillConverter = pillConverter
|
|
||||||
}
|
|
||||||
|
|
||||||
func (portal *Portal) parseMatrixHTML(user *User, content *event.MessageEventContent) string {
|
|
||||||
if content.Format == event.FormatHTML && len(content.FormattedBody) > 0 {
|
if content.Format == event.FormatHTML && len(content.FormattedBody) > 0 {
|
||||||
ctx := format.NewContext()
|
ctx := format.NewContext()
|
||||||
ctx.ReturnData[formatterContextUserKey] = user
|
|
||||||
ctx.ReturnData[formatterContextPortalKey] = portal
|
ctx.ReturnData[formatterContextPortalKey] = portal
|
||||||
return variationselector.FullyQualify(matrixHTMLParser.Parse(content.FormattedBody, ctx))
|
return variationselector.FullyQualify(matrixHTMLParser.Parse(content.FormattedBody, ctx))
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -97,6 +97,8 @@ func (br *DiscordBridge) Init() {
|
|||||||
br.CommandProcessor = commands.NewProcessor(&br.Bridge)
|
br.CommandProcessor = commands.NewProcessor(&br.Bridge)
|
||||||
br.RegisterCommands()
|
br.RegisterCommands()
|
||||||
|
|
||||||
|
matrixHTMLParser.PillConverter = br.pillConverter
|
||||||
|
|
||||||
br.DB = database.New(br.Bridge.DB, br.Log.Sub("Database"))
|
br.DB = database.New(br.Bridge.DB, br.Log.Sub("Database"))
|
||||||
discordLog = br.ZLog.With().Str("component", "discordgo").Logger()
|
discordLog = br.ZLog.With().Str("component", "discordgo").Logger()
|
||||||
|
|
||||||
|
|||||||
@@ -1083,7 +1083,7 @@ func (portal *Portal) handleMatrixMessage(sender *User, evt *event.Event) {
|
|||||||
if editMXID := content.GetRelatesTo().GetReplaceID(); editMXID != "" && content.NewContent != nil {
|
if editMXID := content.GetRelatesTo().GetReplaceID(); editMXID != "" && content.NewContent != nil {
|
||||||
edits := portal.bridge.DB.Message.GetByMXID(portal.Key, editMXID)
|
edits := portal.bridge.DB.Message.GetByMXID(portal.Key, editMXID)
|
||||||
if edits != nil {
|
if edits != nil {
|
||||||
discordContent := portal.parseMatrixHTML(sender, content.NewContent)
|
discordContent := portal.parseMatrixHTML(content.NewContent)
|
||||||
// TODO save edit in message table
|
// TODO save edit in message table
|
||||||
_, err := sender.Session.ChannelMessageEdit(edits.DiscordProtoChannelID(), edits.DiscordID, discordContent)
|
_, err := sender.Session.ChannelMessageEdit(edits.DiscordProtoChannelID(), edits.DiscordID, discordContent)
|
||||||
go portal.sendMessageMetrics(evt, err, "Failed to edit")
|
go portal.sendMessageMetrics(evt, err, "Failed to edit")
|
||||||
@@ -1129,7 +1129,7 @@ func (portal *Portal) handleMatrixMessage(sender *User, evt *event.Event) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sendReq.Content = portal.parseMatrixHTML(sender, content)
|
sendReq.Content = portal.parseMatrixHTML(content)
|
||||||
case event.MsgAudio, event.MsgFile, event.MsgImage, event.MsgVideo:
|
case event.MsgAudio, event.MsgFile, event.MsgImage, event.MsgVideo:
|
||||||
data, err := downloadMatrixAttachment(portal.MainIntent(), content)
|
data, err := downloadMatrixAttachment(portal.MainIntent(), content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -1145,7 +1145,7 @@ func (portal *Portal) handleMatrixMessage(sender *User, evt *event.Event) {
|
|||||||
sendReq.Attachments = []*discordgo.MessageAttachment{att}
|
sendReq.Attachments = []*discordgo.MessageAttachment{att}
|
||||||
if content.FileName != "" && content.FileName != content.Body {
|
if content.FileName != "" && content.FileName != content.Body {
|
||||||
att.Filename = content.FileName
|
att.Filename = content.FileName
|
||||||
sendReq.Content = portal.parseMatrixHTML(sender, content)
|
sendReq.Content = portal.parseMatrixHTML(content)
|
||||||
}
|
}
|
||||||
prep, err := sender.Session.ChannelAttachmentCreate(channelID, &discordgo.ReqPrepareAttachments{
|
prep, err := sender.Session.ChannelAttachmentCreate(channelID, &discordgo.ReqPrepareAttachments{
|
||||||
Files: []*discordgo.FilePrepare{{
|
Files: []*discordgo.FilePrepare{{
|
||||||
|
|||||||
Reference in New Issue
Block a user