Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve DM design #816

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions damus/Nostr/NostrEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,29 @@ func first_eref_mention(ev: NostrEvent, privkey: Privkey?) -> Mention<NoteId>? {
return nil
}

func separate_images(ev: NostrEvent, privkey: Privkey?) -> [MediaUrl]? {
let urlBlocks: [URL] = ev.blocks(privkey).blocks.reduce(into: []) { urls, block in
guard case .url(let url) = block else {
return
}
if classify_url(url).is_img != nil {
urls.append(url)
}
}
let mediaUrls = urlBlocks.map { MediaUrl.image($0) }
return mediaUrls.isEmpty ? nil : mediaUrls
}

func separate_invoices(ev: NostrEvent, privkey: Privkey?) -> [Invoice]? {
let invoiceBlocks: [Invoice] = ev.blocks(privkey).blocks.reduce(into: []) { invoices, block in
guard case .invoice(let invoice) = block else {
return
}
invoices.append(invoice)
}
return invoiceBlocks.isEmpty ? nil : invoiceBlocks
}

/**
Transforms a `NostrEvent` of known kind `NostrKind.like`to a human-readable emoji.
If the known kind is not a `NostrKind.like`, it will return `nil`.
Expand Down
10 changes: 5 additions & 5 deletions damus/Util/EventCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ func is_animated_image(url: URL) -> Bool {
return ext == "gif"
}

func preload_event(plan: PreloadPlan, state: DamusState) async {
func preload_event(plan: PreloadPlan, state: DamusState, only_text: Bool = false) async {
var artifacts: NoteArtifacts? = plan.data.artifacts.artifacts
let settings = state.settings
let profiles = state.profiles
Expand All @@ -420,7 +420,7 @@ func preload_event(plan: PreloadPlan, state: DamusState) async {
print("Preloading event \(plan.event.content)")

if artifacts == nil && plan.load_artifacts {
let arts = render_note_content(ev: plan.event, profiles: profiles, privkey: our_keypair.privkey)
let arts = render_note_content(ev: plan.event, profiles: profiles, privkey: our_keypair.privkey, only_text: only_text)
artifacts = arts

// we need these asap
Expand All @@ -441,7 +441,7 @@ func preload_event(plan: PreloadPlan, state: DamusState) async {
}

if plan.load_preview, note_artifact_is_separated(kind: plan.event.known_kind) {
let arts = artifacts ?? render_note_content(ev: plan.event, profiles: profiles, privkey: our_keypair.privkey)
let arts = artifacts ?? render_note_content(ev: plan.event, profiles: profiles, privkey: our_keypair.privkey, only_text: only_text)

// only separated artifacts have previews
if case .separated(let sep) = arts {
Expand Down Expand Up @@ -479,7 +479,7 @@ func preload_event(plan: PreloadPlan, state: DamusState) async {

}

func preload_events(state: DamusState, events: [NostrEvent]) {
func preload_events(state: DamusState, events: [NostrEvent], only_text: Bool = false) {
let event_cache = state.events
let our_keypair = state.keypair
let settings = state.settings
Expand All @@ -494,7 +494,7 @@ func preload_events(state: DamusState, events: [NostrEvent]) {

Task {
for plan in plans {
await preload_event(plan: plan, state: state)
await preload_event(plan: plan, state: state, only_text: only_text)
}
}
}
Expand Down
Loading