commit d7b5669ecf9b3006e2b62247b45a7e89e6058f74
parent 8141a2970c27068d2d969bf6a8ef9077f4b09ee9
Author: William Casarin <jb55@jb55.com>
Date: Fri, 19 Aug 2022 07:21:18 -0700
support kind 42 chat messages
Changelog-Added: Support kind 42 chat messages (ArcadeCity).
Signed-off-by: William Casarin <jb55@jb55.com>
Diffstat:
9 files changed, 36 insertions(+), 7 deletions(-)
diff --git a/damus/ContentView.swift b/damus/ContentView.swift
@@ -187,7 +187,7 @@ struct ContentView: View {
.sheet(item: $active_sheet) { item in
switch item {
case .post:
- PostView(references: [])
+ PostView(replying_to: nil, references: [])
case .reply(let event):
ReplyView(replying_to: event, damus: damus_state!)
}
diff --git a/damus/Models/HomeModel.swift b/damus/Models/HomeModel.swift
@@ -86,6 +86,7 @@ class HomeModel: ObservableObject {
}
switch kind {
+ case .chat: fallthrough
case .text:
handle_text_event(sub_id: sub_id, ev)
case .contacts:
diff --git a/damus/Models/Mentions.swift b/damus/Models/Mentions.swift
@@ -279,7 +279,7 @@ func post_to_event(post: NostrPost, privkey: String, pubkey: String) -> NostrEve
let post_blocks = parse_post_blocks(content: post.content)
let post_tags = make_post_tags(post_blocks: post_blocks, tags: tags)
let content = render_blocks(blocks: post_tags.blocks)
- let new_ev = NostrEvent(content: content, pubkey: pubkey, kind: 1, tags: post_tags.tags)
+ let new_ev = NostrEvent(content: content, pubkey: pubkey, kind: post.kind.rawValue, tags: post_tags.tags)
new_ev.calculate_id()
new_ev.sign(privkey: privkey)
return new_ev
diff --git a/damus/Models/Post.swift b/damus/Models/Post.swift
@@ -8,8 +8,21 @@
import Foundation
struct NostrPost {
+ let kind: NostrKind
let content: String
let references: [ReferencedId]
+
+ init (content: String, references: [ReferencedId]) {
+ self.content = content
+ self.references = references
+ self.kind = .text
+ }
+
+ init (content: String, references: [ReferencedId], kind: NostrKind) {
+ self.content = content
+ self.references = references
+ self.kind = kind
+ }
}
// TODO: parse nostr:{e,p}:pubkey uris as well
diff --git a/damus/Nostr/NostrKind.swift b/damus/Nostr/NostrKind.swift
@@ -16,4 +16,5 @@ enum NostrKind: Int {
case delete = 5
case boost = 6
case like = 7
+ case chat = 42
}
diff --git a/damus/Views/PostView.swift b/damus/Views/PostView.swift
@@ -18,6 +18,7 @@ struct PostView: View {
@State var post: String = POST_PLACEHOLDER
@State var new: Bool = true
+ let replying_to: NostrEvent?
@FocusState var focus: Bool
let references: [ReferencedId]
@@ -37,7 +38,12 @@ struct PostView: View {
}
func send_post() {
- let new_post = NostrPost(content: self.post, references: references)
+ var kind: NostrKind = .text
+ if replying_to?.known_kind == .chat {
+ kind = .chat
+ }
+ let new_post = NostrPost(content: self.post, references: references, kind: kind)
+
NotificationCenter.default.post(name: .post, object: NostrPostResult.post(new_post))
dismiss()
}
diff --git a/damus/Views/ReplyView.swift b/damus/Views/ReplyView.swift
@@ -34,7 +34,7 @@ struct ReplyView: View {
.font(.footnote)
}
EventView(event: replying_to, highlight: .none, has_action_bar: false, damus: damus, show_friend_icon: true)
- PostView(references: gather_reply_ids(our_pubkey: damus.pubkey, from: replying_to))
+ PostView(replying_to: replying_to, references: gather_reply_ids(our_pubkey: damus.pubkey, from: replying_to))
}
.padding()
diff --git a/damus/Views/ThreadView.swift b/damus/Views/ThreadView.swift
@@ -51,7 +51,7 @@ struct ThreadView: View {
return
}
seen_first = true
- is_chatroom = has_hashtag(ev.tags, hashtag: "chat")
+ is_chatroom = should_show_chatroom(ev)
}
}
.onAppear() {
@@ -71,9 +71,17 @@ struct ThreadView_Previews: PreviewProvider {
}
*/
+func should_show_chatroom(_ ev: NostrEvent) -> Bool {
+ if ev.known_kind == .chat {
+ return true
+ }
+
+ return has_hashtag(ev.tags, hashtag: "chat")
+}
+
func has_hashtag(_ tags: [[String]], hashtag: String) -> Bool {
for tag in tags {
- if tag.count >= 2 && tag[0] == "hashtag" && tag[1] == hashtag {
+ if tag.count >= 2 && (tag[0] == "hashtag" || tag[0] == "t") && tag[1] == hashtag {
return true
}
}
diff --git a/damus/Views/TimelineView.swift b/damus/Views/TimelineView.swift
@@ -21,7 +21,7 @@ struct InnerTimelineView: View {
LazyVStack {
ForEach(events, id: \.id) { (ev: NostrEvent) in
let tm = ThreadModel(event: inner_event_or_self(ev: ev), pool: damus.pool, privkey: damus.keypair.privkey)
- let is_chatroom = has_hashtag(ev.tags, hashtag: "chat")
+ let is_chatroom = should_show_chatroom(ev)
let tv = ThreadView(thread: tm, damus: damus, is_chatroom: is_chatroom)
NavigationLink(destination: tv) {