tomatrix: bridge live photos as videos
This commit is contained in:
@@ -141,10 +141,17 @@ func (tc *TelegramConnector) Download(ctx context.Context, mediaID networkid.Med
|
|||||||
|
|
||||||
switch msgMedia := rawMsgMedia.(type) {
|
switch msgMedia := rawMsgMedia.(type) {
|
||||||
case *tg.MessageMediaPhoto:
|
case *tg.MessageMediaPhoto:
|
||||||
log.Debug().
|
if msgMedia.Video != nil && !info.Thumbnail {
|
||||||
Int64("photo_id", msgMedia.Photo.GetID()).
|
log.Debug().
|
||||||
Msg("downloading photo")
|
Int64("document_id", msgMedia.Video.GetID()).
|
||||||
readyTransferer = transferer.WithPhoto(msgMedia.Photo)
|
Msg("downloading live photo")
|
||||||
|
readyTransferer = transferer.WithDocument(msgMedia.Video, false)
|
||||||
|
} else {
|
||||||
|
log.Debug().
|
||||||
|
Int64("photo_id", msgMedia.Photo.GetID()).
|
||||||
|
Msg("downloading photo")
|
||||||
|
readyTransferer = transferer.WithPhoto(msgMedia.Photo)
|
||||||
|
}
|
||||||
case *tg.MessageMediaDocument:
|
case *tg.MessageMediaDocument:
|
||||||
document, ok := msgMedia.Document.(*tg.Document)
|
document, ok := msgMedia.Document.(*tg.Document)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|||||||
@@ -225,6 +225,12 @@ func (t *Transferer) WithDocument(doc tg.DocumentClass, thumbnail bool) *ReadyTr
|
|||||||
return &ReadyTransferer{t, &documentFileLocation}
|
return &ReadyTransferer{t, &documentFileLocation}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *Transferer) WithLivePhoto(pc tg.PhotoClass, doc tg.DocumentClass) *ReadyTransferer {
|
||||||
|
photo := pc.(*tg.Photo)
|
||||||
|
t.fileInfo.Width, t.fileInfo.Height, _, _ = getLargestPhotoSize(photo.GetSizes())
|
||||||
|
return t.WithDocument(doc, false)
|
||||||
|
}
|
||||||
|
|
||||||
// WithPhoto transforms a [Transferer] to a [ReadyTransferer] by setting the
|
// WithPhoto transforms a [Transferer] to a [ReadyTransferer] by setting the
|
||||||
// given photo as the location that will be downloaded by the
|
// given photo as the location that will be downloaded by the
|
||||||
// [ReadyTransferer].
|
// [ReadyTransferer].
|
||||||
|
|||||||
+44
-10
@@ -61,7 +61,9 @@ func mediaHashID(ctx context.Context, m tg.MessageMediaClass) []byte {
|
|||||||
}
|
}
|
||||||
switch media := m.(type) {
|
switch media := m.(type) {
|
||||||
case *tg.MessageMediaPhoto:
|
case *tg.MessageMediaPhoto:
|
||||||
if media.Photo != nil {
|
if media.Video != nil {
|
||||||
|
return binary.BigEndian.AppendUint64(nil, uint64(media.Video.GetID()))
|
||||||
|
} else if media.Photo != nil {
|
||||||
return binary.BigEndian.AppendUint64(nil, uint64(media.Photo.GetID()))
|
return binary.BigEndian.AppendUint64(nil, uint64(media.Photo.GetID()))
|
||||||
} else {
|
} else {
|
||||||
zerolog.Ctx(ctx).Debug().Msg("Attempted to get hash for nil photo")
|
zerolog.Ctx(ctx).Debug().Msg("Attempted to get hash for nil photo")
|
||||||
@@ -427,6 +429,8 @@ func (c *TelegramClient) convertMediaRequiringUpload(
|
|||||||
var telegramMediaID int64
|
var telegramMediaID int64
|
||||||
var isSticker, isVideo, isVideoGif bool
|
var isSticker, isVideo, isVideoGif bool
|
||||||
extra := map[string]any{}
|
extra := map[string]any{}
|
||||||
|
// FIXME don't use raw map for fields in the FileInfo struct
|
||||||
|
extraInfo := map[string]any{}
|
||||||
|
|
||||||
transferer := media.NewTransferer(c.client.API()).WithRoomID(portal.MXID)
|
transferer := media.NewTransferer(c.client.API()).WithRoomID(portal.MXID)
|
||||||
var mediaTransferer *media.ReadyTransferer
|
var mediaTransferer *media.ReadyTransferer
|
||||||
@@ -482,8 +486,40 @@ func (c *TelegramClient) convertMediaRequiringUpload(
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
telegramMediaID = photo.GetID()
|
if video, ok := msgMedia.Video.(*tg.Document); ok {
|
||||||
mediaTransferer = transferer.WithPhoto(photo)
|
content.MsgType = event.MsgVideo
|
||||||
|
telegramMediaID = video.GetID()
|
||||||
|
mediaTransferer = transferer.WithLivePhoto(photo, video)
|
||||||
|
extraInfo["fi.mau.telegram.live_photo"] = true
|
||||||
|
|
||||||
|
// TODO deduplicate with document thumbnail code
|
||||||
|
var thumbnailURL id.ContentURIString
|
||||||
|
var thumbnailFile *event.EncryptedFileInfo
|
||||||
|
var thumbnailInfo *event.FileInfo
|
||||||
|
var err error
|
||||||
|
|
||||||
|
thumbnailTransferer := media.NewTransferer(c.client.API()).
|
||||||
|
WithRoomID(portal.MXID).
|
||||||
|
WithPhoto(photo)
|
||||||
|
if c.main.useDirectMedia {
|
||||||
|
thumbnailURL, thumbnailInfo, err = thumbnailTransferer.DirectDownloadURL(ctx, c.telegramUserID, portal, msgID, false, photo.ID)
|
||||||
|
if err != nil {
|
||||||
|
log.Err(err).Msg("Failed to create direct download URL for thumbnail")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if thumbnailURL == "" {
|
||||||
|
thumbnailURL, thumbnailFile, thumbnailInfo, err = thumbnailTransferer.Transfer(ctx, c.main.Store, intent)
|
||||||
|
if err != nil {
|
||||||
|
log.Err(err).Msg("Failed to transfer thumbnail")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if thumbnailURL != "" || thumbnailFile != nil {
|
||||||
|
transferer = transferer.WithThumbnail(thumbnailURL, thumbnailFile, thumbnailInfo)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
telegramMediaID = photo.GetID()
|
||||||
|
mediaTransferer = transferer.WithPhoto(photo)
|
||||||
|
}
|
||||||
case *tg.MessageMediaDocument:
|
case *tg.MessageMediaDocument:
|
||||||
document, ok := msgMedia.Document.(*tg.Document)
|
document, ok := msgMedia.Document.(*tg.Document)
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -500,7 +536,6 @@ func (c *TelegramClient) convertMediaRequiringUpload(
|
|||||||
|
|
||||||
content.MsgType = event.MsgFile
|
content.MsgType = event.MsgFile
|
||||||
|
|
||||||
extraInfo := map[string]any{}
|
|
||||||
for _, attr := range document.GetAttributes() {
|
for _, attr := range document.GetAttributes() {
|
||||||
switch a := attr.(type) {
|
switch a := attr.(type) {
|
||||||
case *tg.DocumentAttributeFilename:
|
case *tg.DocumentAttributeFilename:
|
||||||
@@ -593,7 +628,6 @@ func (c *TelegramClient) convertMediaRequiringUpload(
|
|||||||
extraInfo["fi.mau.hide_controls"] = true
|
extraInfo["fi.mau.hide_controls"] = true
|
||||||
extraInfo["fi.mau.no_audio"] = true
|
extraInfo["fi.mau.no_audio"] = true
|
||||||
}
|
}
|
||||||
extra["info"] = extraInfo
|
|
||||||
|
|
||||||
if _, ok := document.GetThumbs(); ok && eventType != event.EventSticker {
|
if _, ok := document.GetThumbs(); ok && eventType != event.EventSticker {
|
||||||
var thumbnailURL id.ContentURIString
|
var thumbnailURL id.ContentURIString
|
||||||
@@ -678,11 +712,11 @@ func (c *TelegramClient) convertMediaRequiringUpload(
|
|||||||
extra["town.robin.msc3725.content_warning"] = map[string]any{
|
extra["town.robin.msc3725.content_warning"] = map[string]any{
|
||||||
"type": "town.robin.msc3725.spoiler",
|
"type": "town.robin.msc3725.spoiler",
|
||||||
}
|
}
|
||||||
if extra["info"] == nil {
|
extra["page.codeberg.everypizza.msc4193.spoiler"] = true
|
||||||
extra["info"] = map[string]any{}
|
extraInfo["fi.mau.telegram.spoiler"] = true
|
||||||
}
|
}
|
||||||
info := extra["info"].(map[string]any)
|
if len(extraInfo) > 0 {
|
||||||
info["fi.mau.telegram.spoiler"] = true
|
extra["info"] = extraInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
converted = &bridgev2.ConvertedMessagePart{
|
converted = &bridgev2.ConvertedMessagePart{
|
||||||
|
|||||||
Reference in New Issue
Block a user