portal_convert.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. // mautrix-discord - A Matrix-Discord puppeting bridge.
  2. // Copyright (C) 2023 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. "context"
  19. "fmt"
  20. "html"
  21. "strconv"
  22. "strings"
  23. "time"
  24. "github.com/bwmarrin/discordgo"
  25. "github.com/rs/zerolog"
  26. "golang.org/x/exp/slices"
  27. "maunium.net/go/mautrix/id"
  28. "maunium.net/go/mautrix"
  29. "maunium.net/go/mautrix/appservice"
  30. "maunium.net/go/mautrix/event"
  31. "maunium.net/go/mautrix/format"
  32. )
  33. type ConvertedMessage struct {
  34. AttachmentID string
  35. Type event.Type
  36. Content *event.MessageEventContent
  37. Extra map[string]any
  38. }
  39. func (portal *Portal) createMediaFailedMessage(bridgeErr error) *event.MessageEventContent {
  40. return &event.MessageEventContent{
  41. Body: fmt.Sprintf("Failed to bridge media: %v", bridgeErr),
  42. MsgType: event.MsgNotice,
  43. }
  44. }
  45. const DiscordStickerSize = 160
  46. func (portal *Portal) convertDiscordFile(ctx context.Context, typeName string, intent *appservice.IntentAPI, id, url string, content *event.MessageEventContent) *event.MessageEventContent {
  47. meta := AttachmentMeta{AttachmentID: id, MimeType: content.Info.MimeType}
  48. if typeName == "sticker" && content.Info.MimeType == "application/json" {
  49. meta.Converter = portal.bridge.convertLottie
  50. }
  51. dbFile, err := portal.bridge.copyAttachmentToMatrix(intent, url, portal.Encrypted, meta)
  52. if err != nil {
  53. zerolog.Ctx(ctx).Err(err).Msg("Failed to copy attachment to Matrix")
  54. return portal.createMediaFailedMessage(err)
  55. }
  56. if typeName == "sticker" && content.Info.MimeType == "application/json" {
  57. content.Info.MimeType = dbFile.MimeType
  58. }
  59. content.Info.Size = dbFile.Size
  60. if content.Info.Width == 0 && content.Info.Height == 0 {
  61. content.Info.Width = dbFile.Width
  62. content.Info.Height = dbFile.Height
  63. }
  64. if dbFile.DecryptionInfo != nil {
  65. content.File = &event.EncryptedFileInfo{
  66. EncryptedFile: *dbFile.DecryptionInfo,
  67. URL: dbFile.MXC.CUString(),
  68. }
  69. } else {
  70. content.URL = dbFile.MXC.CUString()
  71. }
  72. return content
  73. }
  74. func (portal *Portal) cleanupConvertedStickerInfo(content *event.MessageEventContent) {
  75. if content.Info.Width == 0 && content.Info.Height == 0 {
  76. content.Info.Width = DiscordStickerSize
  77. content.Info.Height = DiscordStickerSize
  78. } else if content.Info.Width > DiscordStickerSize || content.Info.Height > DiscordStickerSize {
  79. if content.Info.Width > content.Info.Height {
  80. content.Info.Height /= content.Info.Width / DiscordStickerSize
  81. content.Info.Width = DiscordStickerSize
  82. } else if content.Info.Width < content.Info.Height {
  83. content.Info.Width /= content.Info.Height / DiscordStickerSize
  84. content.Info.Height = DiscordStickerSize
  85. } else {
  86. content.Info.Width = DiscordStickerSize
  87. content.Info.Height = DiscordStickerSize
  88. }
  89. }
  90. }
  91. func (portal *Portal) convertDiscordSticker(ctx context.Context, intent *appservice.IntentAPI, sticker *discordgo.Sticker) *ConvertedMessage {
  92. var mime, ext string
  93. switch sticker.FormatType {
  94. case discordgo.StickerFormatTypePNG:
  95. mime = "image/png"
  96. ext = "png"
  97. case discordgo.StickerFormatTypeAPNG:
  98. mime = "image/apng"
  99. ext = "png"
  100. case discordgo.StickerFormatTypeLottie:
  101. mime = "application/json"
  102. ext = "json"
  103. case discordgo.StickerFormatTypeGIF:
  104. mime = "image/gif"
  105. ext = "gif"
  106. default:
  107. zerolog.Ctx(ctx).Warn().
  108. Int("sticker_format", int(sticker.FormatType)).
  109. Str("sticker_id", sticker.ID).
  110. Msg("Unknown sticker format")
  111. }
  112. content := &event.MessageEventContent{
  113. Body: sticker.Name, // TODO find description from somewhere?
  114. Info: &event.FileInfo{
  115. MimeType: mime,
  116. },
  117. }
  118. mxc := portal.bridge.Config.Bridge.MediaPatterns.Sticker(sticker.ID, ext)
  119. if mxc.IsEmpty() {
  120. content = portal.convertDiscordFile(ctx, "sticker", intent, sticker.ID, sticker.URL(), content)
  121. } else {
  122. content.URL = mxc.CUString()
  123. }
  124. portal.cleanupConvertedStickerInfo(content)
  125. return &ConvertedMessage{
  126. AttachmentID: sticker.ID,
  127. Type: event.EventSticker,
  128. Content: content,
  129. }
  130. }
  131. func (portal *Portal) convertDiscordAttachment(ctx context.Context, intent *appservice.IntentAPI, att *discordgo.MessageAttachment) *ConvertedMessage {
  132. content := &event.MessageEventContent{
  133. Body: att.Filename,
  134. Info: &event.FileInfo{
  135. Height: att.Height,
  136. MimeType: att.ContentType,
  137. Width: att.Width,
  138. // This gets overwritten later after the file is uploaded to the homeserver
  139. Size: att.Size,
  140. },
  141. }
  142. if att.Description != "" {
  143. content.Body = att.Description
  144. content.FileName = att.Filename
  145. }
  146. var extra map[string]any
  147. switch strings.ToLower(strings.Split(att.ContentType, "/")[0]) {
  148. case "audio":
  149. content.MsgType = event.MsgAudio
  150. if att.Waveform != nil {
  151. // TODO convert waveform
  152. extra = map[string]any{
  153. "org.matrix.1767.audio": map[string]any{
  154. "duration": int(att.DurationSeconds * 1000),
  155. },
  156. "org.matrix.msc3245.voice": map[string]any{},
  157. }
  158. }
  159. case "image":
  160. content.MsgType = event.MsgImage
  161. case "video":
  162. content.MsgType = event.MsgVideo
  163. default:
  164. content.MsgType = event.MsgFile
  165. }
  166. mxc := portal.bridge.Config.Bridge.MediaPatterns.Attachment(portal.Key.ChannelID, att.ID, att.Filename)
  167. if mxc.IsEmpty() {
  168. content = portal.convertDiscordFile(ctx, "attachment", intent, att.ID, att.URL, content)
  169. } else {
  170. content.URL = mxc.CUString()
  171. }
  172. return &ConvertedMessage{
  173. AttachmentID: att.ID,
  174. Type: event.EventMessage,
  175. Content: content,
  176. Extra: extra,
  177. }
  178. }
  179. func (portal *Portal) convertDiscordVideoEmbed(ctx context.Context, intent *appservice.IntentAPI, embed *discordgo.MessageEmbed) *ConvertedMessage {
  180. attachmentID := fmt.Sprintf("video_%s", embed.URL)
  181. dbFile, err := portal.bridge.copyAttachmentToMatrix(intent, embed.Video.ProxyURL, portal.Encrypted, NoMeta)
  182. if err != nil {
  183. zerolog.Ctx(ctx).Err(err).Msg("Failed to copy video embed to Matrix")
  184. return &ConvertedMessage{
  185. AttachmentID: attachmentID,
  186. Type: event.EventMessage,
  187. Content: portal.createMediaFailedMessage(err),
  188. }
  189. }
  190. content := &event.MessageEventContent{
  191. MsgType: event.MsgVideo,
  192. Body: embed.URL,
  193. Info: &event.FileInfo{
  194. Width: embed.Video.Width,
  195. Height: embed.Video.Height,
  196. MimeType: dbFile.MimeType,
  197. Size: dbFile.Size,
  198. },
  199. }
  200. if content.Info.Width == 0 && content.Info.Height == 0 {
  201. content.Info.Width = dbFile.Width
  202. content.Info.Height = dbFile.Height
  203. }
  204. if dbFile.DecryptionInfo != nil {
  205. content.File = &event.EncryptedFileInfo{
  206. EncryptedFile: *dbFile.DecryptionInfo,
  207. URL: dbFile.MXC.CUString(),
  208. }
  209. } else {
  210. content.URL = dbFile.MXC.CUString()
  211. }
  212. extra := map[string]any{}
  213. if embed.Type == discordgo.EmbedTypeGifv {
  214. extra["info"] = map[string]any{
  215. "fi.mau.discord.gifv": true,
  216. "fi.mau.loop": true,
  217. "fi.mau.autoplay": true,
  218. "fi.mau.hide_controls": true,
  219. "fi.mau.no_audio": true,
  220. }
  221. }
  222. return &ConvertedMessage{
  223. AttachmentID: attachmentID,
  224. Type: event.EventMessage,
  225. Content: content,
  226. Extra: extra,
  227. }
  228. }
  229. func (portal *Portal) convertDiscordMessage(ctx context.Context, intent *appservice.IntentAPI, msg *discordgo.Message) []*ConvertedMessage {
  230. predictedLength := len(msg.Attachments) + len(msg.StickerItems)
  231. if msg.Content != "" {
  232. predictedLength++
  233. }
  234. parts := make([]*ConvertedMessage, 0, predictedLength)
  235. if textPart := portal.convertDiscordTextMessage(ctx, intent, msg); textPart != nil {
  236. parts = append(parts, textPart)
  237. }
  238. log := zerolog.Ctx(ctx)
  239. handledIDs := make(map[string]struct{})
  240. for _, att := range msg.Attachments {
  241. if _, handled := handledIDs[att.ID]; handled {
  242. continue
  243. }
  244. handledIDs[att.ID] = struct{}{}
  245. log := log.With().Str("attachment_id", att.ID).Logger()
  246. if part := portal.convertDiscordAttachment(log.WithContext(ctx), intent, att); part != nil {
  247. parts = append(parts, part)
  248. }
  249. }
  250. for _, sticker := range msg.StickerItems {
  251. if _, handled := handledIDs[sticker.ID]; handled {
  252. continue
  253. }
  254. handledIDs[sticker.ID] = struct{}{}
  255. log := log.With().Str("sticker_id", sticker.ID).Logger()
  256. if part := portal.convertDiscordSticker(log.WithContext(ctx), intent, sticker); part != nil {
  257. parts = append(parts, part)
  258. }
  259. }
  260. for i, embed := range msg.Embeds {
  261. // Ignore non-video embeds, they're handled in convertDiscordTextMessage
  262. if getEmbedType(embed) != EmbedVideo {
  263. continue
  264. }
  265. // Discord deduplicates embeds by URL. It makes things easier for us too.
  266. if _, handled := handledIDs[embed.URL]; handled {
  267. continue
  268. }
  269. handledIDs[embed.URL] = struct{}{}
  270. log := log.With().
  271. Str("computed_embed_type", "video").
  272. Str("embed_type", string(embed.Type)).
  273. Int("embed_index", i).
  274. Logger()
  275. part := portal.convertDiscordVideoEmbed(log.WithContext(ctx), intent, embed)
  276. if part != nil {
  277. parts = append(parts, part)
  278. }
  279. }
  280. return parts
  281. }
  282. const (
  283. embedHTMLWrapper = `<blockquote class="discord-embed">%s</blockquote>`
  284. embedHTMLWrapperColor = `<blockquote class="discord-embed" background-color="#%06X">%s</blockquote>`
  285. embedHTMLAuthorWithImage = `<p class="discord-embed-author"><img data-mx-emoticon height="24" src="%s" title="Author icon" alt="">&nbsp;<span>%s</span></p>`
  286. embedHTMLAuthorPlain = `<p class="discord-embed-author"><span>%s</span></p>`
  287. embedHTMLAuthorLink = `<a href="%s">%s</a>`
  288. embedHTMLTitleWithLink = `<p class="discord-embed-title"><a href="%s"><strong>%s</strong></a></p>`
  289. embedHTMLTitlePlain = `<p class="discord-embed-title"><strong>%s</strong></p>`
  290. embedHTMLDescription = `<p class="discord-embed-description">%s</p>`
  291. embedHTMLFieldName = `<th>%s</th>`
  292. embedHTMLFieldValue = `<td>%s</td>`
  293. embedHTMLFields = `<table class="discord-embed-fields"><tr>%s</tr><tr>%s</tr></table>`
  294. embedHTMLLinearField = `<p class="discord-embed-field" x-inline="%s"><strong>%s</strong><br><span>%s</span></p>`
  295. embedHTMLImage = `<p class="discord-embed-image"><img src="%s" alt="" title="Embed image"></p>`
  296. embedHTMLFooterWithImage = `<p class="discord-embed-footer"><sub><img data-mx-emoticon height="20" src="%s" title="Footer icon" alt="">&nbsp;<span>%s</span>%s</sub></p>`
  297. embedHTMLFooterPlain = `<p class="discord-embed-footer"><sub><span>%s</span>%s</sub></p>`
  298. embedHTMLFooterOnlyDate = `<p class="discord-embed-footer"><sub>%s</sub></p>`
  299. embedHTMLDate = `<time datetime="%s">%s</time>`
  300. embedFooterDateSeparator = ` • `
  301. )
  302. func (portal *Portal) convertDiscordRichEmbed(ctx context.Context, intent *appservice.IntentAPI, embed *discordgo.MessageEmbed, msgID string, index int) string {
  303. log := zerolog.Ctx(ctx)
  304. var htmlParts []string
  305. if embed.Author != nil {
  306. var authorHTML string
  307. authorNameHTML := html.EscapeString(embed.Author.Name)
  308. if embed.Author.URL != "" {
  309. authorNameHTML = fmt.Sprintf(embedHTMLAuthorLink, embed.Author.URL, authorNameHTML)
  310. }
  311. authorHTML = fmt.Sprintf(embedHTMLAuthorPlain, authorNameHTML)
  312. if embed.Author.ProxyIconURL != "" {
  313. dbFile, err := portal.bridge.copyAttachmentToMatrix(intent, embed.Author.ProxyIconURL, false, NoMeta)
  314. if err != nil {
  315. log.Warn().Err(err).Msg("Failed to reupload author icon in embed")
  316. } else {
  317. authorHTML = fmt.Sprintf(embedHTMLAuthorWithImage, dbFile.MXC, authorNameHTML)
  318. }
  319. }
  320. htmlParts = append(htmlParts, authorHTML)
  321. }
  322. if embed.Title != "" {
  323. var titleHTML string
  324. baseTitleHTML := portal.renderDiscordMarkdownOnlyHTML(embed.Title, false)
  325. if embed.URL != "" {
  326. titleHTML = fmt.Sprintf(embedHTMLTitleWithLink, html.EscapeString(embed.URL), baseTitleHTML)
  327. } else {
  328. titleHTML = fmt.Sprintf(embedHTMLTitlePlain, baseTitleHTML)
  329. }
  330. htmlParts = append(htmlParts, titleHTML)
  331. }
  332. if embed.Description != "" {
  333. htmlParts = append(htmlParts, fmt.Sprintf(embedHTMLDescription, portal.renderDiscordMarkdownOnlyHTML(embed.Description, true)))
  334. }
  335. for i := 0; i < len(embed.Fields); i++ {
  336. item := embed.Fields[i]
  337. if portal.bridge.Config.Bridge.EmbedFieldsAsTables {
  338. splitItems := []*discordgo.MessageEmbedField{item}
  339. if item.Inline && len(embed.Fields) > i+1 && embed.Fields[i+1].Inline {
  340. splitItems = append(splitItems, embed.Fields[i+1])
  341. i++
  342. if len(embed.Fields) > i+1 && embed.Fields[i+1].Inline {
  343. splitItems = append(splitItems, embed.Fields[i+1])
  344. i++
  345. }
  346. }
  347. headerParts := make([]string, len(splitItems))
  348. contentParts := make([]string, len(splitItems))
  349. for j, splitItem := range splitItems {
  350. headerParts[j] = fmt.Sprintf(embedHTMLFieldName, portal.renderDiscordMarkdownOnlyHTML(splitItem.Name, false))
  351. contentParts[j] = fmt.Sprintf(embedHTMLFieldValue, portal.renderDiscordMarkdownOnlyHTML(splitItem.Value, true))
  352. }
  353. htmlParts = append(htmlParts, fmt.Sprintf(embedHTMLFields, strings.Join(headerParts, ""), strings.Join(contentParts, "")))
  354. } else {
  355. htmlParts = append(htmlParts, fmt.Sprintf(embedHTMLLinearField,
  356. strconv.FormatBool(item.Inline),
  357. portal.renderDiscordMarkdownOnlyHTML(item.Name, false),
  358. portal.renderDiscordMarkdownOnlyHTML(item.Value, true),
  359. ))
  360. }
  361. }
  362. if embed.Image != nil {
  363. dbFile, err := portal.bridge.copyAttachmentToMatrix(intent, embed.Image.ProxyURL, false, NoMeta)
  364. if err != nil {
  365. log.Warn().Err(err).Msg("Failed to reupload image in embed")
  366. } else {
  367. htmlParts = append(htmlParts, fmt.Sprintf(embedHTMLImage, dbFile.MXC))
  368. }
  369. }
  370. var embedDateHTML string
  371. if embed.Timestamp != "" {
  372. formattedTime := embed.Timestamp
  373. parsedTS, err := time.Parse(time.RFC3339, embed.Timestamp)
  374. if err != nil {
  375. log.Warn().Err(err).Msg("Failed to parse timestamp in embed")
  376. } else {
  377. formattedTime = parsedTS.Format(discordTimestampStyle('F').Format())
  378. }
  379. embedDateHTML = fmt.Sprintf(embedHTMLDate, embed.Timestamp, formattedTime)
  380. }
  381. if embed.Footer != nil {
  382. var footerHTML string
  383. var datePart string
  384. if embedDateHTML != "" {
  385. datePart = embedFooterDateSeparator + embedDateHTML
  386. }
  387. footerHTML = fmt.Sprintf(embedHTMLFooterPlain, html.EscapeString(embed.Footer.Text), datePart)
  388. if embed.Footer.ProxyIconURL != "" {
  389. dbFile, err := portal.bridge.copyAttachmentToMatrix(intent, embed.Footer.ProxyIconURL, false, NoMeta)
  390. if err != nil {
  391. log.Warn().Err(err).Msg("Failed to reupload footer icon in embed")
  392. } else {
  393. footerHTML = fmt.Sprintf(embedHTMLFooterWithImage, dbFile.MXC, html.EscapeString(embed.Footer.Text), datePart)
  394. }
  395. }
  396. htmlParts = append(htmlParts, footerHTML)
  397. } else if embed.Timestamp != "" {
  398. htmlParts = append(htmlParts, fmt.Sprintf(embedHTMLFooterOnlyDate, embedDateHTML))
  399. }
  400. if len(htmlParts) == 0 {
  401. return ""
  402. }
  403. compiledHTML := strings.Join(htmlParts, "")
  404. if embed.Color != 0 {
  405. compiledHTML = fmt.Sprintf(embedHTMLWrapperColor, embed.Color, compiledHTML)
  406. } else {
  407. compiledHTML = fmt.Sprintf(embedHTMLWrapper, compiledHTML)
  408. }
  409. return compiledHTML
  410. }
  411. type BeeperLinkPreview struct {
  412. mautrix.RespPreviewURL
  413. MatchedURL string `json:"matched_url"`
  414. ImageEncryption *event.EncryptedFileInfo `json:"beeper:image:encryption,omitempty"`
  415. }
  416. func (portal *Portal) convertDiscordLinkEmbedImage(ctx context.Context, intent *appservice.IntentAPI, url string, width, height int, preview *BeeperLinkPreview) {
  417. dbFile, err := portal.bridge.copyAttachmentToMatrix(intent, url, portal.Encrypted, NoMeta)
  418. if err != nil {
  419. zerolog.Ctx(ctx).Warn().Err(err).Msg("Failed to reupload image in URL preview")
  420. return
  421. }
  422. if width != 0 || height != 0 {
  423. preview.ImageWidth = width
  424. preview.ImageHeight = height
  425. } else {
  426. preview.ImageWidth = dbFile.Width
  427. preview.ImageHeight = dbFile.Height
  428. }
  429. preview.ImageSize = dbFile.Size
  430. preview.ImageType = dbFile.MimeType
  431. if dbFile.Encrypted {
  432. preview.ImageEncryption = &event.EncryptedFileInfo{
  433. EncryptedFile: *dbFile.DecryptionInfo,
  434. URL: dbFile.MXC.CUString(),
  435. }
  436. } else {
  437. preview.ImageURL = dbFile.MXC.CUString()
  438. }
  439. }
  440. func (portal *Portal) convertDiscordLinkEmbedToBeeper(ctx context.Context, intent *appservice.IntentAPI, embed *discordgo.MessageEmbed) *BeeperLinkPreview {
  441. var preview BeeperLinkPreview
  442. preview.MatchedURL = embed.URL
  443. preview.Title = embed.Title
  444. preview.Description = embed.Description
  445. if embed.Image != nil {
  446. portal.convertDiscordLinkEmbedImage(ctx, intent, embed.Image.ProxyURL, embed.Image.Width, embed.Image.Height, &preview)
  447. } else if embed.Thumbnail != nil {
  448. portal.convertDiscordLinkEmbedImage(ctx, intent, embed.Thumbnail.ProxyURL, embed.Thumbnail.Width, embed.Thumbnail.Height, &preview)
  449. }
  450. return &preview
  451. }
  452. const msgInteractionTemplateHTML = `<blockquote>
  453. <a href="https://matrix.to/#/%s">%s</a> used <font color="#3771bb">/%s</font>
  454. </blockquote>`
  455. const msgComponentTemplateHTML = `<p>This message contains interactive elements. Use the Discord app to interact with the message.</p>`
  456. type BridgeEmbedType int
  457. const (
  458. EmbedUnknown BridgeEmbedType = iota
  459. EmbedRich
  460. EmbedLinkPreview
  461. EmbedVideo
  462. )
  463. func isActuallyLinkPreview(embed *discordgo.MessageEmbed) bool {
  464. // Sending YouTube links creates a video embed, but we want to bridge it as a URL preview,
  465. // so this is a hacky way to detect those.
  466. return embed.Video != nil && embed.Video.ProxyURL == ""
  467. }
  468. func getEmbedType(embed *discordgo.MessageEmbed) BridgeEmbedType {
  469. switch embed.Type {
  470. case discordgo.EmbedTypeLink, discordgo.EmbedTypeArticle:
  471. return EmbedLinkPreview
  472. case discordgo.EmbedTypeVideo:
  473. if isActuallyLinkPreview(embed) {
  474. return EmbedLinkPreview
  475. }
  476. return EmbedVideo
  477. case discordgo.EmbedTypeGifv:
  478. return EmbedVideo
  479. case discordgo.EmbedTypeRich, discordgo.EmbedTypeImage:
  480. return EmbedRich
  481. default:
  482. return EmbedUnknown
  483. }
  484. }
  485. func isPlainGifMessage(msg *discordgo.Message) bool {
  486. return len(msg.Embeds) == 1 && msg.Embeds[0].Video != nil && msg.Embeds[0].URL == msg.Content && msg.Embeds[0].Type == discordgo.EmbedTypeGifv
  487. }
  488. func (portal *Portal) convertDiscordMentions(msg *discordgo.Message, replySender id.UserID, syncGhosts bool) *event.Mentions {
  489. var matrixMentions event.Mentions
  490. for _, mention := range msg.Mentions {
  491. puppet := portal.bridge.GetPuppetByID(mention.ID)
  492. if syncGhosts {
  493. puppet.UpdateInfo(nil, mention)
  494. }
  495. user := portal.bridge.GetUserByID(mention.ID)
  496. if user != nil {
  497. matrixMentions.UserIDs = append(matrixMentions.UserIDs, user.MXID)
  498. } else {
  499. matrixMentions.UserIDs = append(matrixMentions.UserIDs, puppet.MXID)
  500. }
  501. }
  502. if replySender != "" {
  503. matrixMentions.UserIDs = append(matrixMentions.UserIDs, replySender)
  504. }
  505. slices.Sort(matrixMentions.UserIDs)
  506. matrixMentions.UserIDs = slices.Compact(matrixMentions.UserIDs)
  507. if msg.MentionEveryone {
  508. matrixMentions.Room = true
  509. }
  510. return &matrixMentions
  511. }
  512. func (portal *Portal) convertDiscordTextMessage(ctx context.Context, intent *appservice.IntentAPI, msg *discordgo.Message) *ConvertedMessage {
  513. log := zerolog.Ctx(ctx)
  514. if msg.Type == discordgo.MessageTypeCall {
  515. return &ConvertedMessage{Type: event.EventMessage, Content: &event.MessageEventContent{
  516. MsgType: event.MsgEmote,
  517. Body: "started a call",
  518. }}
  519. } else if msg.Type == discordgo.MessageTypeGuildMemberJoin {
  520. return &ConvertedMessage{Type: event.EventMessage, Content: &event.MessageEventContent{
  521. MsgType: event.MsgEmote,
  522. Body: "joined the server",
  523. }}
  524. }
  525. var htmlParts []string
  526. if msg.Interaction != nil {
  527. puppet := portal.bridge.GetPuppetByID(msg.Interaction.User.ID)
  528. puppet.UpdateInfo(nil, msg.Interaction.User)
  529. htmlParts = append(htmlParts, fmt.Sprintf(msgInteractionTemplateHTML, puppet.MXID, puppet.Name, msg.Interaction.Name))
  530. }
  531. if msg.Content != "" && !isPlainGifMessage(msg) {
  532. htmlParts = append(htmlParts, portal.renderDiscordMarkdownOnlyHTML(msg.Content, false))
  533. }
  534. previews := make([]*BeeperLinkPreview, 0)
  535. for i, embed := range msg.Embeds {
  536. if i == 0 && msg.MessageReference == nil && isReplyEmbed(embed) {
  537. continue
  538. }
  539. with := log.With().
  540. Str("embed_type", string(embed.Type)).
  541. Int("embed_index", i)
  542. switch getEmbedType(embed) {
  543. case EmbedRich:
  544. log := with.Str("computed_embed_type", "rich").Logger()
  545. htmlParts = append(htmlParts, portal.convertDiscordRichEmbed(log.WithContext(ctx), intent, embed, msg.ID, i))
  546. case EmbedLinkPreview:
  547. log := with.Str("computed_embed_type", "link preview").Logger()
  548. previews = append(previews, portal.convertDiscordLinkEmbedToBeeper(log.WithContext(ctx), intent, embed))
  549. case EmbedVideo:
  550. // Ignore video embeds, they're handled as separate messages
  551. default:
  552. log := with.Logger()
  553. log.Warn().Msg("Unknown embed type in message")
  554. }
  555. }
  556. if len(msg.Components) > 0 {
  557. htmlParts = append(htmlParts, msgComponentTemplateHTML)
  558. }
  559. if len(htmlParts) == 0 {
  560. return nil
  561. }
  562. fullHTML := strings.Join(htmlParts, "\n")
  563. if !msg.MentionEveryone {
  564. fullHTML = strings.ReplaceAll(fullHTML, "@room", "@\u2063ro\u2063om")
  565. }
  566. content := format.HTMLToContent(fullHTML)
  567. extraContent := map[string]any{
  568. "com.beeper.linkpreviews": previews,
  569. }
  570. return &ConvertedMessage{Type: event.EventMessage, Content: &content, Extra: extraContent}
  571. }