optimize reply message

This commit is contained in:
zhaoYangguang 2021-01-15 18:57:11 +08:00
parent 944878eb48
commit 71b2319989
3 changed files with 77 additions and 77 deletions

View File

@ -4,8 +4,6 @@ import (
"bytes" "bytes"
"database/sql" "database/sql"
"encoding/json" "encoding/json"
skype "github.com/kelaresg/go-skypeapi"
log "maunium.net/go/maulogger/v2" log "maunium.net/go/maulogger/v2"
"github.com/kelaresg/matrix-skype/types" "github.com/kelaresg/matrix-skype/types"
@ -84,7 +82,7 @@ type Message struct {
MXID id.EventID MXID id.EventID
Sender types.SkypeID Sender types.SkypeID
Timestamp uint64 Timestamp uint64
Content *skype.Resource Content string
} }
func (msg *Message) Scan(row Scannable) *Message { func (msg *Message) Scan(row Scannable) *Message {
@ -103,7 +101,8 @@ func (msg *Message) Scan(row Scannable) *Message {
} }
func (msg *Message) decodeBinaryContent(content []byte) { func (msg *Message) decodeBinaryContent(content []byte) {
msg.Content = &skype.Resource{} //msg.Content = &skype.Resource{}
msg.Content = ""
reader := bytes.NewReader(content) reader := bytes.NewReader(content)
dec := json.NewDecoder(reader) dec := json.NewDecoder(reader)
err := dec.Decode(&msg.Content) err := dec.Decode(&msg.Content)

View File

@ -113,55 +113,65 @@ func (formatter *Formatter) ParseSkype(content *event.MessageEventContent, RoomM
} }
content.Body = html.UnescapeString(content.Body) content.Body = html.UnescapeString(content.Body)
// parse mention user message var backStr string
r := regexp.MustCompile(`<at[^>]+\bid="([^"]+)"(.*?)</at>*`)
matches := r.FindAllStringSubmatch(content.Body, -1)
displayname := ""
var mxid id.UserID
if len(matches) > 0 {
for _, match := range matches {
mxid, displayname = formatter.getMatrixInfoByJID(match[1] + skypeExt.NewUserSuffix)
number := "@" + strings.Replace(match[1], skypeExt.NewUserSuffix, "", 1)
output = strings.ReplaceAll(content.Body, match[0], fmt.Sprintf(`<a href="https://%s/#/%s">%s</a>:`, formatter.bridge.Config.Homeserver.Domain, mxid, displayname))
content.Body = strings.Replace(content.Body, number, displayname, -1)
}
}
if output != content.Body { if output != content.Body {
output = strings.Replace(output, "\n", "<br/>", -1) output = strings.Replace(output, "\n", "<br/>", -1)
content.FormattedBody = output content.FormattedBody = output
content.Format = event.FormatHTML content.Format = event.FormatHTML
var mxid id.UserID
// parse quote message(set reply) // parse quote message(set reply)
content.Body = strings.ReplaceAll(content.Body, "\n", "") content.Body = strings.ReplaceAll(content.Body, "\n", "")
quoteReg := regexp.MustCompile(`<quote[^>]+\bauthor="([^"]+)" authorname="([^"]+)" timestamp="([^"]+)" conversation.* messageid="([^"]+)".*>.*?</legacyquote>(.*?)<legacyquote>.*?</legacyquote></quote>(.*)`) quoteReg := regexp.MustCompile(`<quote[^>]+\bauthor="([^"]+)" authorname="([^"]+)" timestamp="([^"]+)" conversation.* messageid="([^"]+)".*>.*?</legacyquote>(.*?)<legacyquote>.*?</legacyquote></quote>(.*)`)
quoteMatches := quoteReg.FindAllStringSubmatch(content.Body, -1) quoteMatches := quoteReg.FindAllStringSubmatch(content.Body, -1)
if len(quoteMatches) > 0 { if len(quoteMatches) > 0 {
for _, match := range quoteMatches { for _, match := range quoteMatches {
for index, a := range match {
fmt.Println("index: ", index)
fmt.Println("ParseSkype quoteMatches a:", a)
fmt.Println()
}
msgMXID := "" msgMXID := ""
msg := formatter.bridge.DB.Message.GetByID(match[4]) msg := formatter.bridge.DB.Message.GetByID(match[4])
if msg != nil { if msg != nil {
msgMXID = string(msg.MXID) msgMXID = string(msg.MXID)
} }
mxid, displayname = formatter.getMatrixInfoByJID("8:" + match[1] + skypeExt.NewUserSuffix) mxid, _ = formatter.getMatrixInfoByJID("8:" + match[1] + skypeExt.NewUserSuffix)
href1 := fmt.Sprintf(`https://%s/#/room/%s/%s?via=%s`, formatter.bridge.Config.Homeserver.Domain, RoomMXID, msgMXID, formatter.bridge.Config.Homeserver.Domain) href1 := fmt.Sprintf(`https://%s/#/room/%s/%s?via=%s`, formatter.bridge.Config.Homeserver.Domain, RoomMXID, msgMXID, formatter.bridge.Config.Homeserver.Domain)
href2 := fmt.Sprintf(`https://%s/#/user/%s`, formatter.bridge.Config.Homeserver.Domain, mxid) href2 := fmt.Sprintf(`https://%s/#/user/%s`, formatter.bridge.Config.Homeserver.Domain, mxid)
newContent := fmt.Sprintf(`<mx-reply><blockquote><a href="%s">In reply to</a> <a href="%s">%s</a><br>%s</blockquote></mx-reply>%s`, newContent := fmt.Sprintf(`<mx-reply><blockquote><a href="%s">In reply to</a> <a href="%s">%s</a><br>%s</blockquote></mx-reply>`,
href1, href1,
href2, href2,
mxid, mxid,
match[5], match[5])
match[6])
content.FormattedBody = newContent content.FormattedBody = newContent
content.Body = fmt.Sprintf("> <%s> %s\n\n%s", mxid, match[5], match[6]) content.Body = fmt.Sprintf("> <%s> %s\n\n", mxid, match[5])
inRelateTo := &event.RelatesTo{ inRelateTo := &event.RelatesTo{
Type: event.RelReply, Type: event.RelReply,
EventID: id.EventID(msgMXID), EventID: id.EventID(msgMXID),
} }
content.SetRelatesTo(inRelateTo) content.SetRelatesTo(inRelateTo)
backStr = match[6]
} }
} }
} }
// parse mention user message
r := regexp.MustCompile(`<at[^>]+\bid="([^"]+)"(.*?)</at>*`)
matches := r.FindAllStringSubmatch(backStr, -1)
if len(matches) > 0 {
for _, match := range matches {
mxid, displayname := formatter.getMatrixInfoByJID(match[1] + skypeExt.NewUserSuffix)
number := "@" + strings.Replace(match[1], skypeExt.NewUserSuffix, "", 1)
output = strings.ReplaceAll(backStr, match[0], fmt.Sprintf(`<a href="https://%s/#/%s">%s</a>:`, formatter.bridge.Config.Homeserver.Domain, mxid, displayname))
content.FormattedBody = content.FormattedBody + output
content.Body = content.Body + strings.Replace(backStr, number, displayname, -1)
}
} else {
content.Body = content.Body + backStr
content.FormattedBody = content.FormattedBody + backStr
}
} }
func (formatter *Formatter) ParseMatrix(html string) string { func (formatter *Formatter) ParseMatrix(html string) string {

View File

@ -5,7 +5,6 @@ import (
"maunium.net/go/mautrix/patch" "maunium.net/go/mautrix/patch"
"encoding/hex" "encoding/hex"
"encoding/json"
"encoding/xml" "encoding/xml"
"fmt" "fmt"
skype "github.com/kelaresg/go-skypeapi" skype "github.com/kelaresg/go-skypeapi"
@ -293,7 +292,8 @@ func (portal *Portal) markHandledSkype(source *User, message *skype.Resource, mx
// msg.Sender = message.Jid // msg.Sender = message.Jid
//} //}
} }
msg.Content = message
msg.Content = message.Content
if len(message.Id)>0 { if len(message.Id)>0 {
msg.ID = message.Id msg.ID = message.Id
} }
@ -1890,9 +1890,6 @@ func (portal *Portal) convertMatrixMessageSkype(sender *User, evt *event.Event)
return nil, sender, content return nil, sender, content
} }
//ts := uint64(evt.Timestamp / 1000)
//status := waProto.WebMessageInfo_ERROR
//fromMe := true
currentTimeNanoStr := strconv.FormatInt(time.Now().UnixNano(), 10) currentTimeNanoStr := strconv.FormatInt(time.Now().UnixNano(), 10)
currentTimeNanoStr = currentTimeNanoStr[:len(currentTimeNanoStr)-3] currentTimeNanoStr = currentTimeNanoStr[:len(currentTimeNanoStr)-3]
clientMessageId := currentTimeNanoStr + fmt.Sprintf("%04v", rand.New(rand.NewSource(time.Now().UnixNano())).Intn(10000)) clientMessageId := currentTimeNanoStr + fmt.Sprintf("%04v", rand.New(rand.NewSource(time.Now().UnixNano())).Intn(10000))
@ -1902,53 +1899,51 @@ func (portal *Portal) convertMatrixMessageSkype(sender *User, evt *event.Event)
Timestamp: time.Now().Unix(), Timestamp: time.Now().Unix(),
} }
//ctxInfo := &waProto.ContextInfo{} //ctxInfo := &waProto.ContextInfo{}
//replyToID := content.GetReplyTo() replyToID := content.GetReplyTo()
var newContent string var newContent string
//if len(replyToID) > 0 {
rQuote := regexp.MustCompile(`<mx-reply><blockquote><a[^>]+\bhref="(.*?)://` + portal.bridge.Config.Homeserver.Domain + `/#/@([^"]+):(.*?)">(.*?)<br>([^"]+)</blockquote></mx-reply>(.*)`) if len(replyToID) > 0 {
rQuote := regexp.MustCompile(`<mx-reply>(.*?)</mx-reply>(.*)`)
quoteMatches := rQuote.FindAllStringSubmatch(content.FormattedBody, -1) quoteMatches := rQuote.FindAllStringSubmatch(content.FormattedBody, -1)
fmt.Println("matches0: ", content.FormattedBody) backStr := ""
fmt.Println("matches1: ", quoteMatches)
if len(quoteMatches) > 0 { if len(quoteMatches) > 0 {
for _, match := range quoteMatches { if len(quoteMatches[0]) > 2 {
if len(match) > 2 { backStr = quoteMatches[0][2]
var skyId string
if strings.Index(match[4], "@skype") > -1 {
skyId = patch.ParseLocalPart(html.UnescapeString(match[2]), false)
skyId = strings.ReplaceAll(skyId, "skype&", "")
skyId = strings.ReplaceAll(skyId, "-", ":")
} else {
skyId = strings.ReplaceAll(sender.JID, skypeExt.NewUserSuffix, "")
} }
if len(skyId) < 2 {
continue
} }
skypeUsername := strings.Replace(skyId, "8:", "", 1) content.RemoveReplyFallback()
puppet := sender.bridge.GetPuppetByJID(skyId + skypeExt.NewUserSuffix) msg := portal.bridge.DB.Message.GetByMXID(replyToID)
time.Now().Unix() if msg != nil && len(msg.Content) > 0 {
newContent = fmt.Sprintf(`<quote author="%s" authorname="%s↵" timestamp="%s" conversation="%s" messageid="%s" cuid="%s"><legacyquote>[%s] %s↵: </legacyquote>%s<legacyquote>↵↵&lt;&lt;&lt; </legacyquote></quote>%s`, messageId := msg.ID
skypeUsername, if len(messageId) < 1 {
messageId = strconv.FormatInt(time.Now().UnixNano()/1e6, 10)
}
author := strings.Replace(msg.Sender, skypeExt.NewUserSuffix, "", 1)
author = strings.Replace(author, "8:", "", 1)
conversation := msg.Chat.Receiver
cuid := msg.JID
r := []rune(messageId)
timestamp := string(r[:len(r) - 3])
quoteMessage := msg.Content
puppet := sender.bridge.GetPuppetByJID(msg.Sender)
newContent = fmt.Sprintf(`<quote author="%s" authorname="%s" timestamp="%s" conversation="%s" messageid="%s" cuid="%s"><legacyquote>[%s] %s: </legacyquote>%s<legacyquote>\n\n&lt;&lt;&lt; </legacyquote></quote>%s`,
author,
puppet.Displayname, puppet.Displayname,
strconv.Itoa(int(time.Now().Unix())), timestamp,
portal.Key.JID, conversation,
time.Now().UnixNano() / 1e6, messageId,
strconv.Itoa(int(time.Now().UnixNano())) + "1", cuid,
time.Now().Unix(), timestamp,
puppet.Displayname, puppet.Displayname,
match[5], quoteMessage,
match[6]) backStr)
content.FormattedBody = newContent content.FormattedBody = newContent
} }
} }
}
content.RemoveReplyFallback()
//if len(newContent) > 0 {
// newContent = content.Body
//}
//content.FormattedBody = newContent
//msg := portal.bridge.DB.Message.GetByMXID(replyToID)
//}
relaybotFormatted := false relaybotFormatted := false
if sender.NeedsRelaybot(portal) { if sender.NeedsRelaybot(portal) {
if !portal.HasRelaybot() { if !portal.HasRelaybot() {
@ -2115,24 +2110,20 @@ func (portal *Portal) HandleMatrixMessage(sender *User, evt *event.Event) {
fmt.Println("portal HandleMatrixMessage info is nil: ") fmt.Println("portal HandleMatrixMessage info is nil: ")
return return
} }
infoRaw, err := json.Marshal(info)
if err != nil {
fmt.Println("portal HandleMatrixMessage Marshal info err: ", err)
return
}
fmt.Println("portal HandleMatrixMessage start markHandledSkype: ") fmt.Println("portal HandleMatrixMessage start markHandledSkype: ")
portal.markHandledSkype(sender, &skype.Resource{ portal.markHandledSkype(sender, &skype.Resource{
ClientMessageId: info.ClientMessageId, ClientMessageId: info.ClientMessageId,
Jid: portal.Key.JID,//receiver id(conversation id) Jid: portal.Key.JID,//receiver id(conversation id)
Timestamp: time.Now().Unix(), Timestamp: time.Now().Unix(),
Content: string(infoRaw), Content: info.Content,
}, evt.ID) }, evt.ID)
portal.log.Debugln("Sending event", evt.ID, "to Skype") portal.log.Debugln("Sending event", evt.ID, "to Skype")
errChan := make(chan error, 1) errChan := make(chan error, 1)
//go sender.Conn.Conn.SendMsg(portal.Key.JID, info.Content, info.ClientMessageId, errChan) //go sender.Conn.Conn.SendMsg(portal.Key.JID, info.Content, info.ClientMessageId, errChan)
go SendMsg(sender, portal.Key.JID, info, errChan) go SendMsg(sender, portal.Key.JID, info, errChan)
var err error
var errorEventID id.EventID var errorEventID id.EventID
select { select {
case err = <-errChan: case err = <-errChan: