From e1c5d2db21e9c72e5f78d2876e694c064f96b140 Mon Sep 17 00:00:00 2001 From: zhaoYangguang <1163765691@qq.com> Date: Wed, 20 Jan 2021 18:07:04 +0800 Subject: [PATCH] fix edit message --- formatting.go | 10 ++++++ go.mod | 2 +- go.sum | 6 ++++ portal.go | 84 +++++++++++++++++++++++++++++++++++++++++---------- 4 files changed, 85 insertions(+), 17 deletions(-) diff --git a/formatting.go b/formatting.go index 5d3c0fa..1ef6bd0 100644 --- a/formatting.go +++ b/formatting.go @@ -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(`( 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("", a, tsMs, ts) + content.Body = strings.TrimPrefix(content.Body, " * ") + if len(content.FormattedBody) > 0 { + content.FormattedBody = content.FormattedBody + fmt.Sprintf("", 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(`
In reply to.*
(.*)`) + 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)]+\bhref="(.*?)://` + portal.bridge.Config.Homeserver.Domain + `/#/@([^"]+):(.*?)">(.*?)`) 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 {