fix edit message

This commit is contained in:
zhaoYangguang 2021-01-20 18:07:04 +08:00
parent 949fbc7ba8
commit e1c5d2db21
4 changed files with 85 additions and 17 deletions

View File

@ -186,6 +186,16 @@ func (formatter *Formatter) ParseSkype(content *event.MessageEventContent, RoomM
content.Body = content.Body + backStr
content.FormattedBody = content.FormattedBody + backStr
}
//filter edit tag
e := regexp.MustCompile(`(<e_m a=".*></e_m>)`)
editMatches := e.FindAllStringSubmatch(content.Body, -1)
if len(editMatches) > 0 {
for _, match := range editMatches {
content.Body = strings.ReplaceAll(content.Body, match[0], "")
content.FormattedBody = strings.ReplaceAll(content.FormattedBody, match[0], "")
}
}
}
func (formatter *Formatter) ParseMatrix(html string) string {

2
go.mod
View File

@ -5,7 +5,7 @@ go 1.14
require (
github.com/chai2010/webp v1.1.0
github.com/gorilla/websocket v1.4.2
github.com/kelaresg/go-skypeapi v0.1.2-0.20201211120317-8651f9f08575
github.com/kelaresg/go-skypeapi v0.1.2-0.20210120095455-33c3f50415c4
github.com/lib/pq v1.7.0
github.com/mattn/go-sqlite3 v2.0.3+incompatible
github.com/pkg/errors v0.9.1

6
go.sum
View File

@ -156,6 +156,10 @@ github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfV
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kelaresg/go-skypeapi v0.1.2-0.20201126103218-226d1ec92858 h1:dHavsrpbaOA/fqwB39zndM1uHIy84EDKhldWajIcfJg=
github.com/kelaresg/go-skypeapi v0.1.2-0.20201126103218-226d1ec92858/go.mod h1:2Mxwa2Flo+PKlVYEM4QBNgs408C/mUSBXRzAb6+B+Zg=
github.com/kelaresg/go-skypeapi v0.1.2-0.20201211120317-8651f9f08575 h1:4M0mtPS+W+In2sjh/g+/gfagn7mMG6igvMOLl2zgiKc=
github.com/kelaresg/go-skypeapi v0.1.2-0.20201211120317-8651f9f08575/go.mod h1:2Mxwa2Flo+PKlVYEM4QBNgs408C/mUSBXRzAb6+B+Zg=
github.com/kelaresg/go-skypeapi v0.1.2-0.20210120095455-33c3f50415c4 h1:8tif5ndoTRHAOj4j2Ump9uyDaKoQ/W17dJ2K36G418U=
github.com/kelaresg/go-skypeapi v0.1.2-0.20210120095455-33c3f50415c4/go.mod h1:2Mxwa2Flo+PKlVYEM4QBNgs408C/mUSBXRzAb6+B+Zg=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6CZQHDETBtE9HaSEkGmuNXF86RwHhHUvq4=
@ -214,6 +218,8 @@ github.com/pidongqianqian/mautrix-go v0.7.3-0.20201126040927-dbde233d88f6 h1:A2W
github.com/pidongqianqian/mautrix-go v0.7.3-0.20201126040927-dbde233d88f6/go.mod h1:TtVePxoEaw6+RZDKVajw66Yaj1lqLjH8l4FF3krsqWY=
github.com/pidongqianqian/mautrix-go v0.8.0-rc.4.0.20201126070406-7b13ac473bcc h1:wXc6kSKzzYrLgqJwaCgJvKZc4Qfq85/3TivSItUFuNk=
github.com/pidongqianqian/mautrix-go v0.8.0-rc.4.0.20201126070406-7b13ac473bcc/go.mod h1:TtVePxoEaw6+RZDKVajw66Yaj1lqLjH8l4FF3krsqWY=
github.com/pidongqianqian/mautrix-go v0.8.0-rc.4.0.20201208081810-787323a21113 h1:tetxcECwIGgb7Gl4AyY/9+MXmuUfp33FweuGU/Bb7AM=
github.com/pidongqianqian/mautrix-go v0.8.0-rc.4.0.20201208081810-787323a21113/go.mod h1:TtVePxoEaw6+RZDKVajw66Yaj1lqLjH8l4FF3krsqWY=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=

View File

@ -216,9 +216,6 @@ func (portal *Portal) handleMessage(msg PortalMessage) {
default:
portal.log.Warnln("Unknown message type:", reflect.TypeOf(msg.data))
}
if data.MessageType == "RichText" || data.MessageType == "Text" {
portal.HandleTextMessage(msg.source, data)
}
} else {
portal.log.Warnln("Unknown message type:", reflect.TypeOf(msg.data))
}
@ -1461,6 +1458,23 @@ func (portal *Portal) HandleTextMessage(source *User, message skype.Resource) {
}
portal.bridge.Formatter.ParseSkype(content, portal.MXID)
// reedit message
if len(message.SkypeEditedId) > 0 {
message.ClientMessageId = message.SkypeEditedId + message.Id
msg := source.bridge.DB.Message.GetByJID(portal.Key, message.SkypeEditedId)
inRelateTo := &event.RelatesTo{
Type: event.RelReplace,
EventID: msg.MXID,
}
content.SetRelatesTo(inRelateTo)
content.NewContent = &event.MessageEventContent{
MsgType: content.MsgType,
Body: content.Body,
FormattedBody: content.FormattedBody,
Format: content.Format,
}
}
// portal.SetReplySkype(content, message)
fmt.Println()
@ -1898,8 +1912,52 @@ func (portal *Portal) convertMatrixMessageSkype(sender *User, evt *event.Event)
Jid: portal.Key.JID,//receiver id(conversation id)
Timestamp: time.Now().Unix(),
}
//ctxInfo := &waProto.ContextInfo{}
replyToID := content.GetReplyTo()
// reedit message
if content.NewContent != nil {
a := strings.Replace(sender.JID, skypeExt.NewUserSuffix, "", 1)
a = strings.Replace(a, "8:", "", 1)
tsMs := strconv.FormatInt(time.Now().UnixNano()/1e6, 10)
r := []rune(tsMs)
ts := string(r[:len(r) - 3])
msg := portal.bridge.DB.Message.GetByMXID(content.RelatesTo.EventID)
if msg != nil && len(msg.JID) > 0 {
info.SkypeEditedId = msg.JID
//info.ClientMessageId = info.ClientMessageId + info.SkypeEditedId
content.Body = content.Body + fmt.Sprintf("<e_m a=\"%s\" ts_ms=\"%s\" ts=\"%s\" t=\"61\"></e_m>", a, tsMs, ts)
content.Body = strings.TrimPrefix(content.Body, " * ")
if len(content.FormattedBody) > 0 {
content.FormattedBody = content.FormattedBody + fmt.Sprintf("<e_m a=\"%s\" ts_ms=\"%s\" ts=\"%s\" t=\"61\"></e_m>", a, tsMs, ts)
content.FormattedBody = strings.TrimPrefix(content.FormattedBody, " * ")
}
}
// in reedit message we can't obtain the "relayId" from RelatesTo.EventID cause the matrix message doesn't put it in "RelatesTo".
// so i get relayId with use regexp, but it's not a good way,
// now there is no way to get relayId if reedit message with add a mention user
// TODO maybe we can record the relayId to DB
rQuote := regexp.MustCompile(`<mx-reply><blockquote><a href=".*#/room/` + string(portal.MXID) + `/(.*)\?via=.*">In reply to.*</blockquote></mx-reply>(.*)`)
quoteMatches := rQuote.FindAllStringSubmatch(content.FormattedBody, -1)
if len(replyToID) < 1 {
if len(quoteMatches) > 0 {
if len(quoteMatches[0]) > 0 {
replyToID = id.EventID(quoteMatches[0][1])
}
//Filter out the " * " in the matrix editing message (i don't why the matrix need a * in the edit message body)
if len(quoteMatches[0]) > 1 {
needReplace := quoteMatches[0][2]
afterReplace := strings.TrimPrefix(needReplace, " * ")
content.Body = strings.Replace(content.Body, needReplace, afterReplace, 1)
content.FormattedBody = strings.Replace(content.FormattedBody, needReplace, afterReplace, 1)
}
}
}
}
// reply message
var newContent string
backStr := ""
if len(replyToID) > 0 {
@ -1959,8 +2017,10 @@ func (portal *Portal) convertMatrixMessageSkype(sender *User, evt *event.Event)
if evt.Type == event.EventSticker {
content.MsgType = event.MsgImage
}
fmt.Println("convertMatrixMessage content.MsgType: ", content.MsgType)
fmt.Println("convertMatrixMessage content.Body: ", content.Body)
fmt.Println("convertMatrixMessage content.NewBody: ", content.NewContent)
fmt.Println("convertMatrixMessage content.FormattedBody: ", content.FormattedBody)
info.Type = string(content.MsgType)
switch content.MsgType {
@ -1977,6 +2037,8 @@ func (portal *Portal) convertMatrixMessageSkype(sender *User, evt *event.Event)
if len(backStr) > 0 {
matchStr = backStr
}
// mention user message
r := regexp.MustCompile(`(?m)<a[^>]+\bhref="(.*?)://` + portal.bridge.Config.Homeserver.Domain + `/#/@([^"]+):(.*?)">(.*?)</a>`)
matches := r.FindAllStringSubmatch(matchStr, -1)
fmt.Println("matches: ", matches)
@ -2001,7 +2063,9 @@ func (portal *Portal) convertMatrixMessageSkype(sender *User, evt *event.Event)
content.FormattedBody = content.FormattedBody + backStr
}
}
}
if len(content.FormattedBody) > 0 {
info.SendTextMessage = &skype.SendTextMessage{
Content : content.FormattedBody,
}
@ -2010,18 +2074,6 @@ func (portal *Portal) convertMatrixMessageSkype(sender *User, evt *event.Event)
Content : content.Body,
}
}
//ctxInfo.MentionedJid = mentionRegex.FindAllString(text, -1)
//for index, mention := range ctxInfo.MentionedJid {
// ctxInfo.MentionedJid[index] = mention[1:] + whatsappExt.NewUserSuffix
//}
//if ctxInfo.StanzaId != nil || ctxInfo.MentionedJid != nil {
// info.Message.ExtendedTextMessage = &waProto.ExtendedTextMessage{
// Text: &text,
// ContextInfo: ctxInfo,
// }
//} else {
// info.Message.Conversation = &text
//}
case event.MsgImage:
caption, fileSize , data := portal.preprocessMatrixMediaSkype(relaybotFormatted, content, evt.ID)
//if media == nil {