optimize reply message
This commit is contained in:
parent
944878eb48
commit
71b2319989
|
@ -4,8 +4,6 @@ import (
|
|||
"bytes"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
skype "github.com/kelaresg/go-skypeapi"
|
||||
|
||||
log "maunium.net/go/maulogger/v2"
|
||||
|
||||
"github.com/kelaresg/matrix-skype/types"
|
||||
|
@ -84,7 +82,7 @@ type Message struct {
|
|||
MXID id.EventID
|
||||
Sender types.SkypeID
|
||||
Timestamp uint64
|
||||
Content *skype.Resource
|
||||
Content string
|
||||
}
|
||||
|
||||
func (msg *Message) Scan(row Scannable) *Message {
|
||||
|
@ -103,7 +101,8 @@ func (msg *Message) Scan(row Scannable) *Message {
|
|||
}
|
||||
|
||||
func (msg *Message) decodeBinaryContent(content []byte) {
|
||||
msg.Content = &skype.Resource{}
|
||||
//msg.Content = &skype.Resource{}
|
||||
msg.Content = ""
|
||||
reader := bytes.NewReader(content)
|
||||
dec := json.NewDecoder(reader)
|
||||
err := dec.Decode(&msg.Content)
|
||||
|
|
|
@ -113,55 +113,65 @@ func (formatter *Formatter) ParseSkype(content *event.MessageEventContent, RoomM
|
|||
}
|
||||
content.Body = html.UnescapeString(content.Body)
|
||||
|
||||
// parse mention user message
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
var backStr string
|
||||
if output != content.Body {
|
||||
output = strings.Replace(output, "\n", "<br/>", -1)
|
||||
content.FormattedBody = output
|
||||
content.Format = event.FormatHTML
|
||||
var mxid id.UserID
|
||||
|
||||
// parse quote message(set reply)
|
||||
content.Body = strings.ReplaceAll(content.Body, "\n", "")
|
||||
quoteReg := regexp.MustCompile(`<quote[^>]+\bauthor="([^"]+)" authorname="([^"]+)" timestamp="([^"]+)" conversation.* messageid="([^"]+)".*>.*?</legacyquote>(.*?)<legacyquote>.*?</legacyquote></quote>(.*)`)
|
||||
quoteMatches := quoteReg.FindAllStringSubmatch(content.Body, -1)
|
||||
|
||||
if len(quoteMatches) > 0 {
|
||||
for _, match := range quoteMatches {
|
||||
for index, a := range match {
|
||||
fmt.Println("index: ", index)
|
||||
fmt.Println("ParseSkype quoteMatches a:", a)
|
||||
fmt.Println()
|
||||
}
|
||||
msgMXID := ""
|
||||
msg := formatter.bridge.DB.Message.GetByID(match[4])
|
||||
if msg != nil {
|
||||
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)
|
||||
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,
|
||||
href2,
|
||||
mxid,
|
||||
match[5],
|
||||
match[6])
|
||||
match[5])
|
||||
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{
|
||||
Type: event.RelReply,
|
||||
EventID: id.EventID(msgMXID),
|
||||
}
|
||||
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 {
|
||||
|
|
87
portal.go
87
portal.go
|
@ -5,7 +5,6 @@ import (
|
|||
"maunium.net/go/mautrix/patch"
|
||||
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
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.Content = message
|
||||
|
||||
msg.Content = message.Content
|
||||
if len(message.Id)>0 {
|
||||
msg.ID = message.Id
|
||||
}
|
||||
|
@ -1890,9 +1890,6 @@ func (portal *Portal) convertMatrixMessageSkype(sender *User, evt *event.Event)
|
|||
return nil, sender, content
|
||||
}
|
||||
|
||||
//ts := uint64(evt.Timestamp / 1000)
|
||||
//status := waProto.WebMessageInfo_ERROR
|
||||
//fromMe := true
|
||||
currentTimeNanoStr := strconv.FormatInt(time.Now().UnixNano(), 10)
|
||||
currentTimeNanoStr = currentTimeNanoStr[:len(currentTimeNanoStr)-3]
|
||||
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(),
|
||||
}
|
||||
//ctxInfo := &waProto.ContextInfo{}
|
||||
//replyToID := content.GetReplyTo()
|
||||
replyToID := content.GetReplyTo()
|
||||
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)
|
||||
fmt.Println("matches0: ", content.FormattedBody)
|
||||
fmt.Println("matches1: ", quoteMatches)
|
||||
backStr := ""
|
||||
if len(quoteMatches) > 0 {
|
||||
for _, match := range quoteMatches {
|
||||
if len(match) > 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(quoteMatches[0]) > 2 {
|
||||
backStr = quoteMatches[0][2]
|
||||
}
|
||||
if len(skyId) < 2 {
|
||||
continue
|
||||
}
|
||||
|
||||
skypeUsername := strings.Replace(skyId, "8:", "", 1)
|
||||
puppet := sender.bridge.GetPuppetByJID(skyId + skypeExt.NewUserSuffix)
|
||||
time.Now().Unix()
|
||||
newContent = fmt.Sprintf(`<quote author="%s" authorname="%s↵" timestamp="%s" conversation="%s" messageid="%s" cuid="%s"><legacyquote>[%s] %s↵: </legacyquote>%s<legacyquote>↵↵<<< </legacyquote></quote>%s`,
|
||||
skypeUsername,
|
||||
content.RemoveReplyFallback()
|
||||
msg := portal.bridge.DB.Message.GetByMXID(replyToID)
|
||||
if msg != nil && len(msg.Content) > 0 {
|
||||
messageId := msg.ID
|
||||
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<<< </legacyquote></quote>%s`,
|
||||
author,
|
||||
puppet.Displayname,
|
||||
strconv.Itoa(int(time.Now().Unix())),
|
||||
portal.Key.JID,
|
||||
time.Now().UnixNano() / 1e6,
|
||||
strconv.Itoa(int(time.Now().UnixNano())) + "1",
|
||||
time.Now().Unix(),
|
||||
timestamp,
|
||||
conversation,
|
||||
messageId,
|
||||
cuid,
|
||||
timestamp,
|
||||
puppet.Displayname,
|
||||
match[5],
|
||||
match[6])
|
||||
quoteMessage,
|
||||
backStr)
|
||||
content.FormattedBody = newContent
|
||||
}
|
||||
}
|
||||
}
|
||||
content.RemoveReplyFallback()
|
||||
//if len(newContent) > 0 {
|
||||
// newContent = content.Body
|
||||
//}
|
||||
//content.FormattedBody = newContent
|
||||
//msg := portal.bridge.DB.Message.GetByMXID(replyToID)
|
||||
//}
|
||||
|
||||
relaybotFormatted := false
|
||||
if sender.NeedsRelaybot(portal) {
|
||||
if !portal.HasRelaybot() {
|
||||
|
@ -2115,24 +2110,20 @@ func (portal *Portal) HandleMatrixMessage(sender *User, evt *event.Event) {
|
|||
fmt.Println("portal HandleMatrixMessage info is nil: ")
|
||||
return
|
||||
}
|
||||
infoRaw, err := json.Marshal(info)
|
||||
if err != nil {
|
||||
fmt.Println("portal HandleMatrixMessage Marshal info err: ", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("portal HandleMatrixMessage start markHandledSkype: ")
|
||||
portal.markHandledSkype(sender, &skype.Resource{
|
||||
ClientMessageId: info.ClientMessageId,
|
||||
Jid: portal.Key.JID,//receiver id(conversation id)
|
||||
Timestamp: time.Now().Unix(),
|
||||
Content: string(infoRaw),
|
||||
Content: info.Content,
|
||||
}, evt.ID)
|
||||
portal.log.Debugln("Sending event", evt.ID, "to Skype")
|
||||
|
||||
errChan := make(chan error, 1)
|
||||
//go sender.Conn.Conn.SendMsg(portal.Key.JID, info.Content, info.ClientMessageId, errChan)
|
||||
go SendMsg(sender, portal.Key.JID, info, errChan)
|
||||
|
||||
var err error
|
||||
var errorEventID id.EventID
|
||||
select {
|
||||
case err = <-errChan:
|
||||
|
|
Loading…
Reference in New Issue