boosts working

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2022-05-10 14:41:34 -07:00
parent bd49c8a9d1
commit 040ffdf9f5
10 changed files with 127 additions and 54 deletions

View File

@@ -37,6 +37,7 @@ class NostrEvent: Codable, Identifiable, CustomStringConvertible {
var id: String
var sig: String
var tags: [[String]]
var boosted_by: String?
// cached field for pow calc
var pow: Int?
@@ -53,6 +54,10 @@ class NostrEvent: Codable, Identifiable, CustomStringConvertible {
return parse_mentions(content: self.content, tags: self.tags)
}()
lazy var inner_event: NostrEvent? = {
return event_from_json(dat: self.content)
}()
lazy var event_refs: [EventRef] = {
return interpret_event_refs(blocks: self.blocks, tags: self.tags)
}()
@@ -335,13 +340,26 @@ func get_referenced_ids(tags: [[String]], key: String) -> [ReferencedId] {
}
}
func make_boost_event(pubkey: String, privkey: String, boosted: NostrEvent) -> NostrEvent {
var tags: [[String]] = boosted.tags.filter { tag in tag.count >= 2 && (tag[0] == "e" || tag[0] == "p") }
tags.append(["e", boosted.id])
tags.append(["p", boosted.pubkey])
let ev = NostrEvent(content: event_to_json(ev: boosted), pubkey: pubkey, kind: 6, tags: tags)
ev.calculate_id()
ev.sign(privkey: privkey)
return ev
}
func make_like_event(pubkey: String, liked: NostrEvent) -> NostrEvent? {
func make_like_event(pubkey: String, privkey: String, liked: NostrEvent) -> NostrEvent {
var tags: [[String]] = liked.tags.filter { tag in tag.count >= 2 && (tag[0] == "e" || tag[0] == "p") }
tags.append(["e", liked.id])
tags.append(["p", liked.pubkey])
let ev = NostrEvent(content: "", pubkey: pubkey, kind: 7, tags: tags)
ev.calculate_id()
ev.sign(privkey: privkey)
return NostrEvent(content: "", pubkey: pubkey, kind: 7, tags: tags)
return ev
}
func gather_reply_ids(our_pubkey: String, from: NostrEvent) -> [ReferencedId] {
@@ -355,6 +373,10 @@ func gather_reply_ids(our_pubkey: String, from: NostrEvent) -> [ReferencedId] {
return ids
}
func event_from_json(dat: String) -> NostrEvent? {
return try? JSONDecoder().decode(NostrEvent.self, from: Data(dat.utf8))
}
func event_to_json(ev: NostrEvent) -> String {
let encoder = JSONEncoder()
guard let res = try? encoder.encode(ev) else {