Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

Viewing file on branch master

1package bmsteams
2
3import (
4 "encoding/json"
5 "fmt"
6 "io"
7 "strings"
8
9 "github.com/matterbridge-org/matterbridge/bridge/config"
10 "github.com/matterbridge-org/matterbridge/bridge/helper"
11
12 msgraph "github.com/yaegashi/msgraph.go/beta"
13)
14
15func (b *Bmsteams) findFile(weburl string) (string, error) {
16 itemRB, err := b.gc.GetDriveItemByURL(b.ctx, weburl)
17 if err != nil {
18 return "", err
19 }
20 itemRB.Workbook().Worksheets()
21 b.gc.Workbooks()
22 item, err := itemRB.Request().Get(b.ctx)
23 if err != nil {
24 return "", err
25 }
26 if url, ok := item.GetAdditionalData("@microsoft.graph.downloadUrl"); ok {
27 return url.(string), nil
28 }
29 return "", nil
30}
31
32// handleDownloadFile handles file download
33func (b *Bmsteams) handleDownloadFile(rmsg *config.Message, filename, weburl string) error {
34 realURL, err := b.findFile(weburl)
35 if err != nil {
36 return err
37 }
38 // Actually download the file.
39 data, err := helper.DownloadFile(realURL)
40 if err != nil {
41 return fmt.Errorf("download %s failed %#v", weburl, err)
42 }
43
44 // If a comment is attached to the file(s) it is in the 'Text' field of the teams messge event
45 // and should be added as comment to only one of the files. We reset the 'Text' field to ensure
46 // that the comment is not duplicated.
47 comment := rmsg.Text
48 rmsg.Text = ""
49 helper.HandleDownloadData(b.Log, rmsg, filename, comment, weburl, data, b.General)
50 return nil
51}
52
53func (b *Bmsteams) handleAttachments(rmsg *config.Message, msg msgraph.ChatMessage) {
54 for _, a := range msg.Attachments {
55 //remove the attachment tags from the text
56 rmsg.Text = attachRE.ReplaceAllString(rmsg.Text, "")
57
58 //handle a code snippet (code block)
59 if *a.ContentType == "application/vnd.microsoft.card.codesnippet" {
60 b.handleCodeSnippet(rmsg, a)
61 continue
62 }
63
64 //handle the download
65 err := b.handleDownloadFile(rmsg, *a.Name, *a.ContentURL)
66 if err != nil {
67 b.Log.Errorf("download of %s failed: %s", *a.Name, err)
68 }
69 }
70}
71
72type AttachContent struct {
73 Language string `json:"language"`
74 CodeSnippetURL string `json:"codeSnippetUrl"`
75}
76
77func (b *Bmsteams) handleCodeSnippet(rmsg *config.Message, attach msgraph.ChatMessageAttachment) {
78 var content AttachContent
79 err := json.Unmarshal([]byte(*attach.Content), &content)
80 if err != nil {
81 b.Log.Errorf("unmarshal codesnippet failed: %s", err)
82 return
83 }
84 s := strings.Split(content.CodeSnippetURL, "/")
85 if len(s) != 13 {
86 b.Log.Errorf("codesnippetUrl has unexpected size: %s", content.CodeSnippetURL)
87 return
88 }
89 resp, err := b.gc.Teams().Request().Client().Get(content.CodeSnippetURL)
90 if err != nil {
91 b.Log.Errorf("retrieving snippet content failed:%s", err)
92 return
93 }
94 defer resp.Body.Close()
95
96 res, err := io.ReadAll(resp.Body)
97 if err != nil {
98 b.Log.Errorf("reading snippet data failed: %s", err)
99 return
100 }
101 rmsg.Text = rmsg.Text + "\n```" + content.Language + "\n" + string(res) + "\n```\n"
102}
103