feature:`asyncGetTextURLsToUnfurl` (#12780)

This commit is contained in:
Igor Sirotin 2023-11-21 15:07:10 +00:00 committed by GitHub
parent 8721f2a5d9
commit 842b56be2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 75 additions and 5 deletions

View File

@ -30,6 +30,8 @@ type
linkPreviewPersistentSetting: UrlUnfurlingMode
linkPreviewCurrentMessageSetting: UrlUnfurlingMode
unfurlRequests: HashSet[string]
unfurlingPlanActiveRequest: string
unfurlingPlanActiveRequestUnfurlAfter: bool
unfurlingPlan: UrlsUnfurlingPlan
proc newController*(
@ -59,6 +61,7 @@ proc newController*(
result.linkPreviewPersistentSetting = settingsService.urlUnfurlingMode()
result.linkPreviewCurrentMessageSetting = result.linkPreviewPersistentSetting
result.unfurlRequests = initHashSet[string]()
result.unfurlingPlanActiveRequest = ""
result.unfurlingPlan = initUrlsUnfurlingPlan()
proc onUnfurlingModeChanged(self: Controller, value: UrlUnfurlingMode)
@ -111,6 +114,14 @@ proc init*(self: Controller) =
let args = UrlUnfurlingModeArgs(e)
self.onUnfurlingModeChanged(args.value)
self.events.on(SIGNAL_URLS_UNFURLING_PLAN_READY) do(e: Args):
let args = UrlsUnfurlingPlanDataArgs(e)
if self.unfurlingPlanActiveRequest != args.requestUuid:
return
self.unfurlingPlan = args.plan
self.unfurlingPlanActiveRequest = ""
self.handleUnfurlingPlan(self.unfurlingPlanActiveRequestUnfurlAfter)
proc getChatId*(self: Controller): string =
return self.chatId
@ -215,8 +226,8 @@ proc setText*(self: Controller, text: string, unfurlNewUrls: bool) =
self.delegate.setUrls(@[])
return
self.unfurlingPlan = self.messageService.getTextURLsToUnfurl(text)
self.handleUnfurlingPlan(unfurlNewUrls)
self.unfurlingPlanActiveRequestUnfurlAfter = unfurlNewUrls
self.unfurlingPlanActiveRequest = self.messageService.asyncGetTextURLsToUnfurl(text)
proc handleUnfurlingPlan*(self: Controller, unfurlNewUrls: bool) =
var allUrls = newSeq[string]() # Used for URLs syntax highlighting only

View File

@ -196,6 +196,32 @@ const asyncGetFirstUnseenMessageIdForTaskArg: Task = proc(argEncoded: string) {.
arg.finish(responseJson)
#################################################
# Async get text URLs to unfurl
#################################################
type
AsyncGetTextURLsToUnfurlTaskArg = ref object of QObjectTaskArg
text*: string
requestUuid*: string
const asyncGetTextURLsToUnfurlTask: Task = proc(argEncoded: string) {.gcsafe, nimcall.} =
let arg = decode[AsyncGetTextURLsToUnfurlTaskArg](argEncoded)
var output = %*{
"error": "",
"response": "",
"requestUuid": arg.requestUuid
}
try:
let response = status_go.getTextURLsToUnfurl(arg.text)
if response.error != nil:
output["error"] = %*response.error.message
output["response"] = %*response.result
except Exception as e:
error "asyncGetTextURLsToUnfurlTask failed:", msg = e.msg
output["error"] = %*e.msg
arg.finish(output)
#################################################
# Async unfurl urls

View File

@ -33,9 +33,9 @@ proc toUrlUnfurlingMetadata*(jsonObj: JsonNode): UrlUnfurlingMetadata =
warn "node is not an object", source = "toUrlUnfurlingMetadata"
return
result.url = jsonObj["url"].getStr()
result.permission = toUrlUnfurlingPermission(jsonObj["permission"].getInt)
result.isStatusSharedUrl = jsonObj["isStatusSharedURL"].getBool()
result.url = jsonObj{"url"}.getStr()
result.permission = toUrlUnfurlingPermission(jsonObj{"permission"}.getInt)
result.isStatusSharedUrl = jsonObj{"isStatusSharedURL"}.getBool()
proc toUrlUnfurlingPlan*(jsonObj: JsonNode): UrlsUnfurlingPlan =
result = UrlsUnfurlingPlan()

View File

@ -61,6 +61,7 @@ const SIGNAL_ENVELOPE_EXPIRED* = "envelopeExpired"
const SIGNAL_RELOAD_MESSAGES* = "reloadMessages"
const SIGNAL_URLS_UNFURLED* = "urlsUnfurled"
const SIGNAL_GET_MESSAGE_FINISHED* = "getMessageFinished"
const SIGNAL_URLS_UNFURLING_PLAN_READY* = "urlsUnfurlingPlanReady"
include async_tasks
@ -120,6 +121,10 @@ type
chatId*: string
message*: MessageDto
UrlsUnfurlingPlanDataArgs* = ref object of Args
plan*: UrlsUnfurlingPlan
requestUuid*: string
LinkPreviewDataArgs* = ref object of Args
linkPreviews*: Table[string, LinkPreview]
requestUuid*: string
@ -829,6 +834,34 @@ QtObject:
except Exception as e:
error "getTextURLsToUnfurl failed", errName = e.name, errDesription = e.msg
proc onAsyncGetTextURLsToUnfurl*(self: Service, responseString: string) {.slot.} =
let response = responseString.parseJson()
if response.kind != JObject:
warn "expected response is not a json object", methodName = "onAsyncGetTextURLsToUnfurl"
return
let errMessage = response{"error"}.getStr()
if errMessage != "":
error "asyncGetTextURLsToUnfurl failed", errMessage
return
let args = UrlsUnfurlingPlanDataArgs(
plan: toUrlUnfurlingPlan(response{"response"}),
requestUuid: response{"requestUuid"}.getStr
)
self.events.emit(SIGNAL_URLS_UNFURLING_PLAN_READY, args)
proc asyncGetTextURLsToUnfurl*(self: Service, text: string): string =
let uuid = $genUUID()
let arg = AsyncGetTextURLsToUnfurlTaskArg(
tptr: cast[ByteAddress](asyncGetTextURLsToUnfurlTask),
vptr: cast[ByteAddress](self.vptr),
slot: "onAsyncGetTextURLsToUnfurl",
text: text,
requestUuid: uuid,
)
self.threadpool.start(arg)
return uuid
proc onAsyncUnfurlUrlsFinished*(self: Service, response: string) {.slot.}=
let responseObj = response.parseJson
if responseObj.kind != JObject: