Implement NIP04: Encrypted Direct Messages

Closes #5

This adds encrypted direct message support to damus

Changelog-Added: Implement NIP04: Encrypted Direct Messages
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2022-06-30 07:16:48 -07:00
parent 0744156c0c
commit c122035851
24 changed files with 892 additions and 228 deletions

View File

@@ -20,7 +20,7 @@ enum Highlight {
}
return false
}
var is_none: Bool {
if case .none = self {
return true
@@ -41,9 +41,34 @@ struct EventView: View {
let highlight: Highlight
let has_action_bar: Bool
let damus: DamusState
let pubkey: String
@EnvironmentObject var action_bar: ActionBarModel
init(event: NostrEvent, highlight: Highlight, has_action_bar: Bool, damus: DamusState) {
self.event = event
self.highlight = highlight
self.has_action_bar = has_action_bar
self.damus = damus
self.pubkey = event.pubkey
}
init(damus: DamusState, event: NostrEvent) {
self.event = event
self.highlight = .none
self.has_action_bar = false
self.damus = damus
self.pubkey = event.pubkey
}
init(damus: DamusState, event: NostrEvent, pubkey: String) {
self.event = event
self.highlight = .none
self.has_action_bar = false
self.damus = damus
self.pubkey = pubkey
}
var body: some View {
return Group {
if event.known_kind == .boost, let inner_ev = event.inner_event {
@@ -51,7 +76,7 @@ struct EventView: View {
HStack {
Label("", systemImage: "arrow.2.squarepath")
.foregroundColor(Color.gray)
ProfileName(pubkey: event.pubkey, profile: damus.profiles.lookup(id: event.pubkey))
ProfileName(pubkey: pubkey, profile: damus.profiles.lookup(id: pubkey))
.foregroundColor(Color.gray)
Text(" Boosted")
.foregroundColor(Color.gray)
@@ -63,16 +88,17 @@ struct EventView: View {
}
}
}
func TextEvent(_ event: NostrEvent) -> some View {
let content = event.get_content(damus.keypair.privkey)
return HStack(alignment: .top) {
let profile = damus.profiles.lookup(id: event.pubkey)
let profile = damus.profiles.lookup(id: pubkey)
VStack {
let pmodel = ProfileModel(pubkey: event.pubkey, damus: damus)
let pv = ProfileView(damus_state: damus, profile: pmodel, followers: FollowersModel(damus_state: damus, target: event.pubkey))
let pmodel = ProfileModel(pubkey: pubkey, damus: damus)
let pv = ProfileView(damus_state: damus, profile: pmodel, followers: FollowersModel(damus_state: damus, target: pubkey))
NavigationLink(destination: pv) {
ProfilePicView(pubkey: event.pubkey, size: PFP_SIZE, highlight: highlight, image_cache: damus.image_cache, profiles: damus.profiles)
ProfilePicView(pubkey: pubkey, size: PFP_SIZE, highlight: highlight, image_cache: damus.image_cache, profiles: damus.profiles)
}
Spacer()
@@ -80,19 +106,19 @@ struct EventView: View {
VStack(alignment: .leading) {
HStack(alignment: .center) {
ProfileName(pubkey: event.pubkey, profile: profile)
ProfileName(pubkey: pubkey, profile: profile)
Text("\(format_relative_time(event.created_at))")
.foregroundColor(.gray)
}
if event.is_reply {
if event.is_reply(damus.keypair.privkey) {
Text("\(reply_desc(profiles: damus.profiles, event: event))")
.font(.footnote)
.foregroundColor(.gray)
.frame(maxWidth: .infinity, alignment: .leading)
}
NoteContentView(event: event, profiles: damus.profiles, content: event.content)
NoteContentView(privkey: damus.keypair.privkey, event: event, profiles: damus.profiles, content: content)
.frame(maxWidth: .infinity, alignment: .leading)
.textSelection(.enabled)
@@ -122,7 +148,7 @@ extension View {
} label: {
Label("Copy Text", systemImage: "doc.on.doc")
}
Button {
UIPasteboard.general.string = "@" + event.pubkey
} label: {
@@ -134,20 +160,20 @@ extension View {
} label: {
Label("Copy Note ID", systemImage: "tag")
}
Button {
UIPasteboard.general.string = event_to_json(ev: event)
} label: {
Label("Copy Note", systemImage: "note")
}
Button {
NotificationCenter.default.post(name: .broadcast_event, object: event)
} label: {
Label("Broadcast", systemImage: "globe")
}
}
}
}
@@ -160,16 +186,16 @@ func reply_desc(profiles: Profiles, event: NostrEvent) -> String {
let desc = make_reply_description(event.tags)
let pubkeys = desc.pubkeys
let n = desc.others
if desc.pubkeys.count == 0 {
return "Reply to self"
}
let names: [String] = pubkeys.map {
let prof = profiles.lookup(id: $0)
return Profile.displayName(profile: prof, pubkey: $0)
}
if names.count == 2 {
if n > 2 {
let and_other = reply_others_desc(n: n, n_pubkeys: pubkeys.count)
@@ -177,7 +203,7 @@ func reply_desc(profiles: Profiles, event: NostrEvent) -> String {
}
return "Replying to \(names[0]) & \(names[1])"
}
let and_other = reply_others_desc(n: n, n_pubkeys: pubkeys.count)
return "Replying to \(names[0])\(and_other)"
}
@@ -197,7 +223,7 @@ func make_actionbar_model(ev: NostrEvent, damus: DamusState) -> ActionBarModel {
let our_like = damus.likes.our_events[ev.id]
let our_boost = damus.boosts.our_events[ev.id]
let our_tip = damus.tips.our_tips[ev.id]
return ActionBarModel(likes: likes ?? 0,
boosts: boosts ?? 0,
tips: tips ?? 0,