From a65a81610ba30c54bdf1f2045b56b89fe469a140 Mon Sep 17 00:00:00 2001
From: Wim <wim@42.be>
Date: Mon, 30 Dec 2019 19:46:48 +0100
Subject: [PATCH] Support threading from other bridges to msteams

---
 bridge/msteams/msteams.go | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/bridge/msteams/msteams.go b/bridge/msteams/msteams.go
index 4f72bd29..8c9fa265 100644
--- a/bridge/msteams/msteams.go
+++ b/bridge/msteams/msteams.go
@@ -2,6 +2,7 @@ package bmsteams
 
 import (
 	"context"
+	"fmt"
 	"os"
 	"regexp"
 	"strings"
@@ -9,6 +10,8 @@ import (
 
 	"github.com/42wim/matterbridge/bridge"
 	"github.com/42wim/matterbridge/bridge/config"
+
+	//	"github.com/davecgh/go-spew/spew"
 	"github.com/mattn/godown"
 	msgraph "github.com/yaegashi/msgraph.go/beta"
 	"github.com/yaegashi/msgraph.go/msauth"
@@ -75,6 +78,13 @@ func (b *Bmsteams) JoinChannel(channel config.ChannelInfo) error {
 
 func (b *Bmsteams) Send(msg config.Message) (string, error) {
 	b.Log.Debugf("=> Receiving %#v", msg)
+	if msg.ParentID != "" && msg.ParentID != "msg-parent-not-found" {
+		return b.sendReply(msg)
+	}
+	if msg.ParentID == "msg-parent-not-found" {
+		msg.ParentID = ""
+		msg.Text = fmt.Sprintf("[thread]: %s", msg.Text)
+	}
 	ct := b.gc.Teams().ID(b.GetString("TeamID")).Channels().ID(msg.Channel).Messages().Request()
 	text := msg.Username + msg.Text
 	content := &msgraph.ItemBody{Content: &text}
@@ -86,6 +96,20 @@ func (b *Bmsteams) Send(msg config.Message) (string, error) {
 	return *res.ID, nil
 }
 
+func (b *Bmsteams) sendReply(msg config.Message) (string, error) {
+	ct := b.gc.Teams().ID(b.GetString("TeamID")).Channels().ID(msg.Channel).Messages().ID(msg.ParentID).Replies().Request()
+	// Handle prefix hint for unthreaded messages.
+
+	text := msg.Username + msg.Text
+	content := &msgraph.ItemBody{Content: &text}
+	rmsg := &msgraph.ChatMessage{Body: content}
+	res, err := ct.Add(b.ctx, rmsg)
+	if err != nil {
+		return "", err
+	}
+	return *res.ID, nil
+}
+
 func (b *Bmsteams) getMessages(channel string) ([]msgraph.ChatMessage, error) {
 	ct := b.gc.Teams().ID(b.GetString("TeamID")).Channels().ID(channel).Messages().Request()
 	rct, err := ct.Get(b.ctx)