urlpreview.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2022 Tulir Asokan
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "bytes"
  19. "context"
  20. "encoding/json"
  21. "image"
  22. "net/http"
  23. "github.com/tidwall/gjson"
  24. "google.golang.org/protobuf/proto"
  25. "maunium.net/go/mautrix/appservice"
  26. "maunium.net/go/mautrix/event"
  27. "maunium.net/go/mautrix/id"
  28. "go.mau.fi/whatsmeow"
  29. waProto "go.mau.fi/whatsmeow/binary/proto"
  30. )
  31. type BeeperLinkPreview struct {
  32. MatchedURL string `json:"matched_url"`
  33. CanonicalURL string `json:"og:url,omitempty"`
  34. Title string `json:"og:title,omitempty"`
  35. Type string `json:"og:type,omitempty"`
  36. Description string `json:"og:description,omitempty"`
  37. Image id.ContentURIString `json:"og:image,omitempty"`
  38. ImageWidth int `json:"og:image:width,omitempty"`
  39. ImageHeight int `json:"og:image:height,omitempty"`
  40. MatchedURLFallback string `json:"matchedUrl"`
  41. }
  42. func (portal *Portal) convertURLPreviewToBeeper(intent *appservice.IntentAPI, source *User, msg *waProto.ExtendedTextMessage) (output *BeeperLinkPreview) {
  43. if msg.GetMatchedText() == "" {
  44. return
  45. }
  46. output = &BeeperLinkPreview{
  47. MatchedURL: msg.GetMatchedText(),
  48. CanonicalURL: msg.GetCanonicalUrl(),
  49. Title: msg.GetTitle(),
  50. Description: msg.GetDescription(),
  51. }
  52. output.MatchedURLFallback = output.MatchedURL
  53. if len(output.CanonicalURL) == 0 {
  54. output.CanonicalURL = output.MatchedURL
  55. }
  56. var thumbnailData []byte
  57. if msg.ThumbnailDirectPath != nil {
  58. var err error
  59. thumbnailData, err = source.Client.DownloadThumbnail(msg)
  60. if err != nil {
  61. portal.log.Warnfln("Failed to download thumbnail for link preview: %v", err)
  62. }
  63. } else if msg.JpegThumbnail != nil {
  64. thumbnailData = msg.JpegThumbnail
  65. }
  66. if thumbnailData != nil {
  67. mxc, err := intent.UploadBytes(thumbnailData, http.DetectContentType(thumbnailData))
  68. if err != nil {
  69. portal.log.Warnfln("Failed to reupload thumbnail for link preview: %v", err)
  70. } else {
  71. output.Image = mxc.ContentURI.CUString()
  72. output.ImageHeight = int(msg.GetThumbnailHeight())
  73. output.ImageWidth = int(msg.GetThumbnailWidth())
  74. if output.ImageHeight == 0 || output.ImageWidth == 0 {
  75. src, _, err := image.Decode(bytes.NewReader(thumbnailData))
  76. if err == nil {
  77. imageBounds := src.Bounds()
  78. output.ImageWidth, output.ImageHeight = imageBounds.Max.X, imageBounds.Max.Y
  79. }
  80. }
  81. }
  82. }
  83. if msg.GetPreviewType() == waProto.ExtendedTextMessage_VIDEO {
  84. output.Type = "video.other"
  85. }
  86. return
  87. }
  88. func (portal *Portal) convertURLPreviewToWhatsApp(sender *User, evt *event.Event, dest *waProto.ExtendedTextMessage) {
  89. rawPreview := gjson.GetBytes(evt.Content.VeryRaw, `com\.beeper\.linkpreview`)
  90. if !rawPreview.Exists() || !rawPreview.IsObject() {
  91. return
  92. }
  93. var preview BeeperLinkPreview
  94. if err := json.Unmarshal([]byte(rawPreview.Raw), &preview); err != nil {
  95. return
  96. }
  97. if len(preview.MatchedURL) == 0 {
  98. if len(preview.MatchedURLFallback) == 0 {
  99. return
  100. } else {
  101. preview.MatchedURL = preview.MatchedURLFallback
  102. }
  103. }
  104. dest.MatchedText = &preview.MatchedURL
  105. if len(preview.CanonicalURL) > 0 {
  106. dest.CanonicalUrl = &preview.CanonicalURL
  107. }
  108. if len(preview.Description) > 0 {
  109. dest.Description = &preview.Description
  110. }
  111. if len(preview.Title) > 0 {
  112. dest.Title = &preview.Title
  113. }
  114. imageMXC := preview.Image.ParseOrIgnore()
  115. if !imageMXC.IsEmpty() {
  116. data, err := portal.MainIntent().DownloadBytes(imageMXC)
  117. if err != nil {
  118. portal.log.Errorfln("Failed to download URL preview image %s: %v", preview.Image, err)
  119. return
  120. }
  121. uploadResp, err := sender.Client.Upload(context.Background(), data, whatsmeow.MediaLinkThumbnail)
  122. if err != nil {
  123. portal.log.Errorfln("Failed to upload URL preview thumbnail in %s: %v", evt.ID, err)
  124. return
  125. }
  126. dest.ThumbnailSha256 = uploadResp.FileSHA256
  127. dest.ThumbnailEncSha256 = uploadResp.FileEncSHA256
  128. dest.ThumbnailDirectPath = &uploadResp.DirectPath
  129. dest.MediaKey = uploadResp.MediaKey
  130. var width, height int
  131. dest.JpegThumbnail, width, height, err = createJPEGThumbnailAndGetSize(data)
  132. if err != nil {
  133. portal.log.Warnfln("Failed to create JPEG thumbnail for URL preview in %s: %v", evt.ID, err)
  134. }
  135. if preview.ImageHeight > 0 && preview.ImageWidth > 0 {
  136. dest.ThumbnailWidth = proto.Uint32(uint32(preview.ImageWidth))
  137. dest.ThumbnailHeight = proto.Uint32(uint32(preview.ImageHeight))
  138. } else if width > 0 && height > 0 {
  139. dest.ThumbnailWidth = proto.Uint32(uint32(width))
  140. dest.ThumbnailHeight = proto.Uint32(uint32(height))
  141. }
  142. }
  143. }