Skip to content

Commit

Permalink
feat: Unfurl images
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-sirotin committed Aug 18, 2023
1 parent 33b8ca8 commit f7da81c
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 39 deletions.
35 changes: 14 additions & 21 deletions src/app/modules/shared_models/link_preview_model.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,12 @@ type
Hostname
Title
Description
LinkType
ThumbnailWidth
ThumbnailHeight
ThumbnailUrl
ThumbnailDataUri

#
# TODO:
#
# Consider adding a `LinkType` property, which would probably follow the way of unfurling:
# - basic (maybe divide OpenGraph/oEmbed/...)
# - image (contantType == "image/.*")
# - status link (hostname == "status.app")
#
# In future, we can also unfurl other types, like PDF/audio/etc !
#
# This is needed on receiver side to know how to render the preview
# and what to do on click.

QtObject:
type
Model* = ref object of QAbstractListModel
Expand Down Expand Up @@ -79,6 +67,7 @@ QtObject:
ModelRole.Hostname.int:"hostname",
ModelRole.Title.int:"title",
ModelRole.Description.int:"description",
ModelRole.LinkType.int:"linkType",
ModelRole.ThumbnailWidth.int:"thumbnailWidth",
ModelRole.ThumbnailHeight.int:"thumbnailHeight",
ModelRole.ThumbnailUrl.int:"thumbnailUrl",
Expand All @@ -87,14 +76,16 @@ QtObject:

method allLinkPreviewRoles(self: Model): seq[int] =
return @[
Unfurled.int,
Hostname.int,
Title.int,
Description.int,
ThumbnailWidth.int,
ThumbnailHeight.int,
ThumbnailUrl.int,
ThumbnailDataUri.int,
ModelRole.Url.int,
ModelRole.Unfurled.int,
ModelRole.Hostname.int,
ModelRole.Title.int,
ModelRole.Description.int,
ModelRole.LinkType.int,
ModelRole.ThumbnailWidth.int,
ModelRole.ThumbnailHeight.int,
ModelRole.ThumbnailUrl.int,
ModelRole.ThumbnailDataUri.int,
]

method data(self: Model, index: QModelIndex, role: int): QVariant =
Expand All @@ -118,6 +109,8 @@ QtObject:
result = newQVariant(item.linkPreview.title)
of ModelRole.Description:
result = newQVariant(item.linkPreview.description)
of ModelRole.LinkType:
result = newQVariant(item.linkPreview.linkType.int)
of ModelRole.ThumbnailWidth:
result = newQVariant(item.linkPreview.thumbnail.width)
of ModelRole.ThumbnailHeight:
Expand Down
29 changes: 27 additions & 2 deletions src/app_service/service/message/dto/link_preview.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import json, strformat, tables
include ../../../common/json_utils

type
LinkType* {.pure.} = enum
Link = 0
Image

proc toLinkType*(value: int): LinkType =
try:
return LinkType(value)
except RangeDefect:
return LinkType.Link

type
LinkPreviewThumbnail* = object
width*: int
Expand All @@ -15,6 +26,7 @@ type
title*: string
description*: string
thumbnail*: LinkPreviewThumbnail
linkType*: LinkType

proc delete*(self: LinkPreview) =
discard
Expand All @@ -37,6 +49,7 @@ proc toLinkPreview*(jsonObj: JsonNode): LinkPreview =
discard jsonObj.getProp("title", result.title)
discard jsonObj.getProp("description", result.description)
discard jsonObj.getProp("hostname", result.hostname)
result.linkType = toLinkType(jsonObj["type"].getInt)

var thumbnail: JsonNode
if jsonObj.getProp("thumbnail", thumbnail):
Expand All @@ -46,15 +59,27 @@ proc `$`*(self: LinkPreviewThumbnail): string =
result = fmt"""LinkPreviewThumbnail(
width: {self.width},
height: {self.height},
url: {self.url},
dataUri: {self.dataUri}
urlLength: {self.url.len},
dataUriLength: {self.dataUri.len}
)"""

proc `$`*(self: LinkPreview): string =
result = fmt"""LinkPreview(
type: {self.linkType},
url: {self.url},
hostname: {self.hostname},
title: {self.title},
description: {self.description},
thumbnail: {self.thumbnail}
)"""

# Custom JSON converter to force `linkType` integer instead of string
proc `%`*(self: LinkPreview): JsonNode =
result = %* {
"type": self.linkType.int,
"url": self.url,
"hostname": self.hostname,
"title": self.title,
"description": self.description,
"thumbnail": %self.thumbnail,
}
29 changes: 14 additions & 15 deletions ui/imports/shared/views/chat/LinksMessageView.qml
Original file line number Diff line number Diff line change
Expand Up @@ -32,36 +32,35 @@ ColumnLayout {
id: linkMessageLoader

// properties from the model
required property string url
required property bool unfurled
required property string hostname
required property string title
required property string description
required property string hostname
required property int linkType
required property int thumbnailWidth
required property int thumbnailHeight
required property string thumbnailUrl
required property string thumbnailDataUri

asynchronous: true
sourceComponent: unfurledLinkComponent

StateGroup {
//Using StateGroup as a warkardound for https://bugreports.qt.io/browse/QTBUG-47796
states: [
State {
name: "loadLinkPreview"
when: !linkMessageLoader.isImage && !linkMessageLoader.isStatusDeepLink
when: linkMessageLoader.linkType === Constants.LinkPreviewType.Link
PropertyChanges { target: linkMessageLoader; sourceComponent: unfurledLinkComponent }
},
State {
name: "loadImage"
when: linkMessageLoader.linkType === Constants.LinkPreviewType.Image
PropertyChanges { target: linkMessageLoader; sourceComponent: unfurledImageComponent }
}
// NOTE: New unfurling not yet suppport images and status links.
// NOTE: New unfurling not yet suppport status links.
// Uncomment code below when implemented:
// - https://github.com/status-im/status-go/issues/3761
// - https://github.com/status-im/status-go/issues/3762

// State {
// name: "loadImage"
// when: linkMessageLoader.isImage
// PropertyChanges { target: linkMessageLoader; sourceComponent: unfurledImageComponent }
// },
// State {
// name: "statusInvitation"
// when: linkMessageLoader.isStatusDeepLink
Expand All @@ -88,13 +87,13 @@ ColumnLayout {

objectName: "LinksMessageView_unfurledImageComponent_linkImage"
anchors.centerIn: parent
source: result.thumbnailUrl
source: thumbnailUrl
imageWidth: 300
isCurrentUser: root.isCurrentUser
playing: globalAnimationEnabled && localAnimationEnabled
isOnline: root.store.mainModuleInst.isOnline
asynchronous: true
isAnimated: result.contentType ? result.contentType.toLowerCase().endsWith("gif") : false
isAnimated: false // FIXME: GIFs are not supported with new unfurling yet
onClicked: {
if (isAnimated && !playing)
localAnimationEnabled = true
Expand Down Expand Up @@ -190,7 +189,7 @@ ColumnLayout {
isOnline: root.store.mainModuleInst.isOnline
asynchronous: true
onClicked: {
Global.openLink(result.address)
Global.openLink(url)
}
}

Expand Down Expand Up @@ -227,7 +226,7 @@ ColumnLayout {
anchors.bottom: linkSite.bottom
cursorShape: Qt.PointingHandCursor
onClicked: {
Global.openLink(link)
Global.openLink(url)
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions ui/imports/utils/Constants.qml
Original file line number Diff line number Diff line change
Expand Up @@ -1180,4 +1180,9 @@ QtObject {
For1min = 6,
Unmuted = 7
}

enum LinkPreviewType {
Link = 0,
Image = 1
}
}
2 changes: 1 addition & 1 deletion vendor/status-go
Submodule status-go updated 37 files
+3 −0 .gitignore
+7 −2 Makefile
+1 −1 VERSION
+4 −1 _assets/ci/Jenkinsfile.tests
+86 −86 appdatabase/migrations/bindata.go
+54 −54 appdatabase/migrationsprevnodecfg/bindata.go
+3 −2 images/adjust.go
+15 −15 images/decode.go
+3 −0 logutils/override.go
+3 −3 mailserver/migrations/bindata.go
+13 −13 multiaccounts/migrations/bindata.go
+2 −0 params/config.go
+3 −3 protocol/anonmetrics/migrations/migrations.go
+19 −8 protocol/common/message.go
+5 −0 protocol/common/message_test.go
+109 −175 protocol/communities/community.go
+46 −72 protocol/communities/community_categories.go
+3 −3 protocol/communities/community_categories_test.go
+5 −5 protocol/communities/community_test.go
+9 −14 protocol/communities/manager.go
+2 −3 protocol/communities/roles_authorization.go
+20 −20 protocol/encryption/migrations/migrations.go
+100 −3 protocol/linkpreview/linkpreview.go
+82 −3 protocol/linkpreview/linkpreview_test.go
+2 −0 protocol/messenger_test.go
+102 −102 protocol/migrations/migrations.go
+2 −0 protocol/persistence_test.go
+136 −99 protocol/protobuf/chat_message.pb.go
+6 −0 protocol/protobuf/chat_message.proto
+7 −7 protocol/pushnotificationclient/migrations/migrations.go
+5 −5 protocol/pushnotificationserver/migrations/migrations.go
+6 −6 protocol/transport/migrations/migrations.go
+43 −36 rpc/network/network.go
+1 −0 server/handlers_test.go
+1 −1 shell.nix
+20 −20 static/bindata.go
+3 −3 t/bindata.go

0 comments on commit f7da81c

Please sign in to comment.