optimize reply message
This commit is contained in:
parent
ac88478189
commit
944878eb48
|
@ -51,6 +51,11 @@ func (mq *MessageQuery) GetByMXID(mxid id.EventID) *Message {
|
|||
"FROM message WHERE mxid=$1", mxid)
|
||||
}
|
||||
|
||||
func (mq *MessageQuery) GetByID(id string) *Message {
|
||||
return mq.get("SELECT id, chat_jid, chat_receiver, jid, mxid, sender, timestamp, content " +
|
||||
"FROM message WHERE id=$1", id)
|
||||
}
|
||||
|
||||
func (mq *MessageQuery) GetLastInChat(chat PortalKey) *Message {
|
||||
msg := mq.get("SELECT id, chat_jid, chat_receiver, jid, mxid, sender, timestamp, content " +
|
||||
"FROM message WHERE chat_jid=$1 AND chat_receiver=$2 ORDER BY timestamp DESC LIMIT 1", chat.JID, chat.Receiver)
|
||||
|
|
|
@ -96,8 +96,8 @@ func (formatter *Formatter) getMatrixInfoByJID(jid types.SkypeID) (mxid id.UserI
|
|||
return
|
||||
}
|
||||
|
||||
func (formatter *Formatter) ParseSkype(content *event.MessageEventContent) {
|
||||
// parse a tag
|
||||
func (formatter *Formatter) ParseSkype(content *event.MessageEventContent, RoomMXID id.RoomID) {
|
||||
// parse '<a><a/>' tag
|
||||
reg:= regexp.MustCompile(`(?U)(<a .*>(.*)</a>)`)
|
||||
bodyMatch := reg.FindAllStringSubmatch(content.Body, -1)
|
||||
for _, match := range bodyMatch {
|
||||
|
@ -113,7 +113,7 @@ func (formatter *Formatter) ParseSkype(content *event.MessageEventContent) {
|
|||
}
|
||||
content.Body = html.UnescapeString(content.Body)
|
||||
|
||||
// parse @user message
|
||||
// parse mention user message
|
||||
r := regexp.MustCompile(`<at[^>]+\bid="([^"]+)"(.*?)</at>*`)
|
||||
matches := r.FindAllStringSubmatch(content.Body, -1)
|
||||
displayname := ""
|
||||
|
@ -130,30 +130,37 @@ func (formatter *Formatter) ParseSkype(content *event.MessageEventContent) {
|
|||
if output != content.Body {
|
||||
output = strings.Replace(output, "\n", "<br/>", -1)
|
||||
content.FormattedBody = output
|
||||
content.Format = event.FormatHTML
|
||||
|
||||
// parse quote message
|
||||
// parse quote message(set reply)
|
||||
content.Body = strings.ReplaceAll(content.Body, "\n", "")
|
||||
quoteReg := regexp.MustCompile(`<quote[^>]+\bauthor="([^"]+)" authorname="([^"]+)" timestamp="([^"]+)".*>.*?</legacyquote>(.*?)<legacyquote>.*?</legacyquote></quote>(.*)`)
|
||||
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 {
|
||||
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)
|
||||
//href1 := fmt.Sprintf(`https://matrix.to/#/!kpouCkfhzvXgbIJmkP:oliver.matrix.host/$fHQNRydqqqAVS8usHRmXn0nIBM_FC-lo2wI2Uol7wu8?via=oliver.matrix.host`)
|
||||
href1 := ""
|
||||
//mxid `@skype&8-live-xxxxxx:name.matrix.server`
|
||||
href2 := fmt.Sprintf(`https://%s/#/%s`, formatter.bridge.Config.Homeserver.Domain, mxid)
|
||||
newContent := fmt.Sprintf(`<mx-reply><blockquote><a href="%s"></a> <a href="%s">%s</a><br>%s</blockquote></mx-reply>%s`,
|
||||
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`,
|
||||
href1,
|
||||
href2,
|
||||
mxid,
|
||||
match[4],
|
||||
match[5])
|
||||
match[5],
|
||||
match[6])
|
||||
content.FormattedBody = newContent
|
||||
content.Body = match[4] + "\n" + match[5]
|
||||
content.Body = fmt.Sprintf("> <%s> %s\n\n%s", mxid, match[5], match[6])
|
||||
inRelateTo := &event.RelatesTo{
|
||||
Type: event.RelReply,
|
||||
EventID: id.EventID(msgMXID),
|
||||
}
|
||||
content.SetRelatesTo(inRelateTo)
|
||||
}
|
||||
}
|
||||
|
||||
content.Format = event.FormatHTML
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -87,7 +87,7 @@ func TestFormatter_ParseSkype(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
testFormatter.ParseSkype(tt.args.content)
|
||||
testFormatter.ParseSkype(tt.args.content, "")
|
||||
if !reflect.DeepEqual(tt.args.content, tt.expect.content) {
|
||||
t.Errorf("content = %v, wanted %v", tt.args.content, tt.expect.content)
|
||||
}
|
||||
|
|
20
portal.go
20
portal.go
|
@ -1317,6 +1317,16 @@ func (portal *Portal) SetReplySkype(content *event.MessageEventContent, info sky
|
|||
portal.log.Warnln("Failed to get reply target:", err)
|
||||
return
|
||||
}
|
||||
if evt.Type == event.EventEncrypted {
|
||||
_ = evt.Content.ParseRaw(evt.Type)
|
||||
decryptedEvt, err := portal.bridge.Crypto.Decrypt(evt)
|
||||
if err != nil {
|
||||
portal.log.Warnln("Failed to decrypt reply target:", err)
|
||||
} else {
|
||||
evt = decryptedEvt
|
||||
}
|
||||
}
|
||||
_ = evt.Content.ParseRaw(evt.Type)
|
||||
content.SetReply(evt)
|
||||
}
|
||||
return
|
||||
|
@ -1450,8 +1460,8 @@ func (portal *Portal) HandleTextMessage(source *User, message skype.Resource) {
|
|||
MsgType: event.MsgText,
|
||||
}
|
||||
|
||||
portal.bridge.Formatter.ParseSkype(content)
|
||||
portal.SetReplySkype(content, message)
|
||||
portal.bridge.Formatter.ParseSkype(content, portal.MXID)
|
||||
// portal.SetReplySkype(content, message)
|
||||
|
||||
fmt.Println()
|
||||
fmt.Printf("portal HandleTextMessage2: %+v", content)
|
||||
|
@ -1492,7 +1502,7 @@ func (portal *Portal) HandleLocationMessageSkype(source *User, message skype.Res
|
|||
GeoURI: geo,
|
||||
}
|
||||
|
||||
portal.SetReplySkype(content, message)
|
||||
// portal.SetReplySkype(content, message)
|
||||
|
||||
_, _ = intent.UserTyping(portal.MXID, false, 0)
|
||||
resp, err := portal.sendMessage(intent, event.EventMessage, content, message.Timestamp * 1000)
|
||||
|
@ -1522,7 +1532,7 @@ func (portal *Portal) HandleContactMessageSkype(source *User, message skype.Reso
|
|||
MsgType: event.MsgText,
|
||||
}
|
||||
|
||||
portal.SetReplySkype(content, message)
|
||||
// portal.SetReplySkype(content, message)
|
||||
|
||||
_, _ = intent.UserTyping(portal.MXID, false, 0)
|
||||
resp, err := portal.sendMessage(intent, event.EventMessage, content, message.Timestamp * 1000)
|
||||
|
@ -1659,7 +1669,7 @@ func (portal *Portal) HandleMediaMessageSkype(source *User, download func(conn *
|
|||
} else {
|
||||
content.URL = uploaded.ContentURI.CUString()
|
||||
}
|
||||
portal.SetReplySkype(content, info)
|
||||
// portal.SetReplySkype(content, info)
|
||||
|
||||
fmt.Println()
|
||||
fmt.Println("mediaMessage.UrlThumbnail", mediaMessage.UrlThumbnail)
|
||||
|
|
Loading…
Reference in New Issue