Group image and video metadata in specialised structs (#3518)

This commit is contained in:
Stefan Ceriu
2024-11-15 15:00:27 +02:00
committed by GitHub
parent 4ea276dc99
commit ef288a9662
27 changed files with 232 additions and 214 deletions
@@ -14,13 +14,9 @@ struct ImageRoomTimelineItemContent: Hashable {
var formattedCaption: AttributedString?
/// The original textual representation of the formatted caption directly from the event (usually HTML code)
var formattedCaptionHTMLString: String?
let source: MediaSourceProxy
var size: CGSize?
var aspectRatio: CGFloat?
let thumbnailSource: MediaSourceProxy?
var thumbnailSize: CGSize?
var thumbnailAspectRatio: CGFloat?
let imageInfo: ImageInfoProxy
let thumbnailInfo: ImageInfoProxy?
var blurhash: String?
var contentType: UTType?
@@ -14,15 +14,9 @@ struct VideoRoomTimelineItemContent: Hashable {
var formattedCaption: AttributedString?
/// The original textual representation of the formatted caption directly from the event (usually HTML code)
var formattedCaptionHTMLString: String?
let duration: TimeInterval
let source: MediaSourceProxy?
var size: CGSize?
var aspectRatio: CGFloat?
let thumbnailSource: MediaSourceProxy?
var thumbnailSize: CGSize?
var thumbnailAspectRatio: CGFloat?
let videoInfo: VideoInfoProxy
let thumbnailInfo: ImageInfoProxy?
var blurhash: String?
var contentType: UTType?
@@ -17,10 +17,8 @@ struct StickerRoomTimelineItem: EventBasedTimelineItemProtocol, Equatable {
let sender: TimelineItemSender
let imageURL: URL
let imageInfo: ImageInfoProxy
var size: CGSize?
var aspectRatio: CGFloat?
var blurhash: String?
var properties = RoomTimelineItemProperties()
@@ -118,20 +118,10 @@ struct RoomTimelineItemFactory: RoomTimelineItemFactoryProtocol {
private func buildStickerTimelineItem(_ eventItemProxy: EventTimelineItemProxy,
_ body: String,
_ imageInfo: ImageInfo,
_ info: MatrixRustSDK.ImageInfo,
_ imageURL: URL,
_ isOutgoing: Bool) -> RoomTimelineItemProtocol {
var imageSize: CGSize?
var imageAspectRatio: CGFloat?
if let height = imageInfo.height,
let width = imageInfo.width {
imageSize = .init(width: CGFloat(width), height: CGFloat(height))
if width > 0, height > 0 {
imageAspectRatio = CGFloat(width) / CGFloat(height)
}
}
let imageInfo = ImageInfoProxy(url: imageURL, width: info.width, height: info.height, mimeType: info.mimetype)
return StickerRoomTimelineItem(id: eventItemProxy.id,
body: body,
@@ -140,10 +130,8 @@ struct RoomTimelineItemFactory: RoomTimelineItemFactoryProtocol {
isEditable: eventItemProxy.isEditable,
canBeRepliedTo: eventItemProxy.canBeRepliedTo,
sender: eventItemProxy.sender,
imageURL: imageURL,
size: imageSize,
aspectRatio: imageAspectRatio,
blurhash: imageInfo.blurhash,
imageInfo: imageInfo,
blurhash: info.blurhash,
properties: RoomTimelineItemProperties(reactions: aggregateReactions(eventItemProxy.reactions),
deliveryStatus: eventItemProxy.deliveryStatus,
orderedReadReceipts: orderReadReceipts(eventItemProxy.readReceipts),
@@ -522,91 +510,47 @@ struct RoomTimelineItemFactory: RoomTimelineItemFactoryProtocol {
let htmlCaption = messageContent.formattedCaption?.format == .html ? messageContent.formattedCaption?.body : nil
let formattedCaption = htmlCaption != nil ? attributedStringBuilder.fromHTML(htmlCaption) : attributedStringBuilder.fromPlain(messageContent.caption)
let thumbnailSource = messageContent.info?.thumbnailSource.map { MediaSourceProxy(source: $0, mimeType: messageContent.info?.thumbnailInfo?.mimetype) }
let thumbnailInfo = ImageInfoProxy(source: messageContent.info?.thumbnailSource,
width: messageContent.info?.thumbnailInfo?.width,
height: messageContent.info?.thumbnailInfo?.height,
mimeType: messageContent.info?.thumbnailInfo?.mimetype)
var thumbnailSize: CGSize?
var thumbnailAspectRatio: CGFloat?
if let thumbnailInfo = messageContent.info?.thumbnailInfo,
let height = thumbnailInfo.height,
let width = thumbnailInfo.width {
thumbnailSize = .init(width: CGFloat(width), height: CGFloat(height))
if width > 0, height > 0 {
thumbnailAspectRatio = CGFloat(width) / CGFloat(height)
}
}
var imageSize: CGSize?
var imageAspectRatio: CGFloat?
if let imageInfo = messageContent.info,
let height = imageInfo.height,
let width = imageInfo.width {
imageSize = .init(width: CGFloat(width), height: CGFloat(height))
if width > 0, height > 0 {
imageAspectRatio = CGFloat(width) / CGFloat(height)
}
}
let imageInfo = ImageInfoProxy(source: messageContent.source,
width: messageContent.info?.width,
height: messageContent.info?.height,
mimeType: messageContent.info?.mimetype)
return .init(filename: messageContent.filename,
caption: messageContent.caption,
formattedCaption: formattedCaption,
formattedCaptionHTMLString: htmlCaption,
source: MediaSourceProxy(source: messageContent.source, mimeType: messageContent.info?.mimetype),
size: imageSize,
aspectRatio: imageAspectRatio,
thumbnailSource: thumbnailSource,
thumbnailSize: thumbnailSize,
thumbnailAspectRatio: thumbnailAspectRatio,
imageInfo: imageInfo,
thumbnailInfo: thumbnailInfo,
blurhash: messageContent.info?.blurhash,
contentType: UTType(mimeType: messageContent.info?.mimetype, fallbackFilename: messageContent.filename))
}
private func buildVideoTimelineItemContent(_ messageContent: VideoMessageContent) -> VideoRoomTimelineItemContent {
let htmlCaption = messageContent.formattedCaption?.format == .html ? messageContent.formattedCaption?.body : nil
let formattedCaption = htmlCaption != nil ? attributedStringBuilder.fromHTML(htmlCaption) : attributedStringBuilder.fromPlain(messageContent.caption)
let thumbnailSource = messageContent.info?.thumbnailSource.map { MediaSourceProxy(source: $0, mimeType: messageContent.info?.thumbnailInfo?.mimetype) }
var thumbnailSize: CGSize?
var thumbnailAspectRatio: CGFloat?
let thumbnailInfo = ImageInfoProxy(source: messageContent.info?.thumbnailSource,
width: messageContent.info?.thumbnailInfo?.width,
height: messageContent.info?.thumbnailInfo?.height,
mimeType: messageContent.info?.thumbnailInfo?.mimetype)
if let thumbnailInfo = messageContent.info?.thumbnailInfo,
let height = thumbnailInfo.height,
let width = thumbnailInfo.width {
thumbnailSize = .init(width: CGFloat(width), height: CGFloat(height))
if width > 0, height > 0 {
thumbnailAspectRatio = CGFloat(width) / CGFloat(height)
}
}
var videoSize: CGSize?
var videoAspectRatio: CGFloat?
if let videoInfo = messageContent.info,
let height = videoInfo.height,
let width = videoInfo.width {
videoSize = .init(width: CGFloat(width), height: CGFloat(height))
if width > 0, height > 0 {
videoAspectRatio = CGFloat(width) / CGFloat(height)
}
}
let videoInfo = VideoInfoProxy(source: messageContent.source,
duration: messageContent.info?.duration ?? 0,
width: messageContent.info?.width,
height: messageContent.info?.height,
mimeType: messageContent.info?.mimetype)
return .init(filename: messageContent.filename,
caption: messageContent.caption,
formattedCaption: formattedCaption,
formattedCaptionHTMLString: htmlCaption,
duration: messageContent.info?.duration ?? 0,
source: MediaSourceProxy(source: messageContent.source, mimeType: messageContent.info?.mimetype),
size: videoSize,
aspectRatio: videoAspectRatio,
thumbnailSource: thumbnailSource,
thumbnailSize: thumbnailSize,
thumbnailAspectRatio: thumbnailAspectRatio,
videoInfo: videoInfo,
thumbnailInfo: thumbnailInfo,
blurhash: messageContent.info?.blurhash,
contentType: UTType(mimeType: messageContent.info?.mimetype, fallbackFilename: messageContent.filename))
}
@@ -821,3 +765,12 @@ private extension RepliedToEventDetails {
}
}
}
private extension Receipt {
var dateTimestamp: Date? {
guard let timestamp else {
return nil
}
return Date(timeIntervalSince1970: TimeInterval(timestamp / 1000))
}
}