mirror of https://github.com/lukechilds/damus.git
William Casarin
2 years ago
4 changed files with 183 additions and 0 deletions
@ -0,0 +1,91 @@ |
|||
// |
|||
// Mute.swift |
|||
// damus |
|||
// |
|||
// Created by William Casarin on 2023-01-25. |
|||
// |
|||
|
|||
import Foundation |
|||
|
|||
func create_or_update_mutelist(keypair: FullKeypair, mprev: NostrEvent?, to_add: String) -> NostrEvent? { |
|||
return create_or_update_list_event(keypair: keypair, mprev: mprev, to_add: to_add, list_name: "mute", list_type: "p") |
|||
} |
|||
|
|||
func remove_from_mutelist(keypair: FullKeypair, prev: NostrEvent, to_remove: String) -> NostrEvent? { |
|||
return remove_from_list_event(keypair: keypair, prev: prev, to_remove: to_remove, tag_type: "p") |
|||
} |
|||
|
|||
func create_or_update_list_event(keypair: FullKeypair, mprev: NostrEvent?, to_add: String, list_name: String, list_type: String) -> NostrEvent? { |
|||
let pubkey = keypair.pubkey |
|||
|
|||
if let prev = mprev { |
|||
if let okprev = ensure_list_name(list: prev, name: list_name), prev.pubkey == keypair.pubkey { |
|||
return add_to_list_event(keypair: keypair, prev: okprev, to_add: to_add, tag_type: list_type) |
|||
} |
|||
} |
|||
|
|||
var tags = [["d", list_name], [list_type, to_add]] |
|||
let ev = NostrEvent(content: "", pubkey: pubkey, kind: 30000, tags: tags) |
|||
|
|||
ev.tags = tags |
|||
ev.id = calculate_event_id(ev: ev) |
|||
ev.sig = sign_event(privkey: keypair.privkey, ev: ev) |
|||
|
|||
return ev |
|||
} |
|||
|
|||
func remove_from_list_event(keypair: FullKeypair, prev: NostrEvent, to_remove: String, tag_type: String) -> NostrEvent? { |
|||
var exists = false |
|||
for tag in prev.tags { |
|||
if tag.count >= 2 && tag[0] == tag_type && tag[1] == to_remove { |
|||
exists = true |
|||
} |
|||
} |
|||
|
|||
// make sure we actually have the pubkey to remove |
|||
guard exists else { |
|||
return nil |
|||
} |
|||
|
|||
let new_tags = prev.tags.filter { tag in |
|||
!(tag.count >= 2 && tag[0] == tag_type && tag[1] == to_remove) |
|||
} |
|||
|
|||
let ev = NostrEvent(content: prev.content, pubkey: keypair.pubkey, kind: 30000, tags: new_tags) |
|||
ev.id = calculate_event_id(ev: ev) |
|||
ev.sig = sign_event(privkey: keypair.privkey, ev: ev) |
|||
|
|||
return ev |
|||
} |
|||
|
|||
func add_to_list_event(keypair: FullKeypair, prev: NostrEvent, to_add: String, tag_type: String) -> NostrEvent? { |
|||
for tag in prev.tags { |
|||
// we are already muting this user |
|||
if tag.count >= 2 && tag[0] == tag_type && tag[1] == to_add { |
|||
return nil |
|||
} |
|||
} |
|||
|
|||
let new = NostrEvent(content: prev.content, pubkey: keypair.pubkey, kind: 30000, tags: prev.tags) |
|||
new.tags.append([tag_type, to_add]) |
|||
new.id = calculate_event_id(ev: new) |
|||
new.sig = sign_event(privkey: keypair.privkey, ev: new) |
|||
|
|||
return new |
|||
} |
|||
|
|||
func ensure_list_name(list: NostrEvent, name: String) -> NostrEvent? { |
|||
for tag in list.tags { |
|||
if tag.count >= 2 && tag[0] == "d" { |
|||
if tag[1] != name { |
|||
return nil |
|||
} else { |
|||
return list |
|||
} |
|||
} |
|||
} |
|||
|
|||
list.tags.insert(["d", name], at: 0) |
|||
|
|||
return list |
|||
} |
@ -0,0 +1,71 @@ |
|||
// |
|||
// ListTests.swift |
|||
// damusTests |
|||
// |
|||
// Created by William Casarin on 2023-01-25. |
|||
// |
|||
|
|||
import XCTest |
|||
@testable import damus |
|||
|
|||
final class ListTests: XCTestCase { |
|||
|
|||
override func setUpWithError() throws { |
|||
// Put setup code here. This method is called before the invocation of each test method in the class. |
|||
} |
|||
|
|||
override func tearDownWithError() throws { |
|||
// Put teardown code here. This method is called after the invocation of each test method in the class. |
|||
} |
|||
|
|||
func testCreateMuteList() throws { |
|||
let privkey = "87f313b03f2548e6eaf1c188db47078e08e894252949779b639b28db0891937a" |
|||
let pubkey = "4b0c29bf96496130c1253102f6870c0eee05db38a257315858272aa43fd19685" |
|||
let to_mute = "2fa2630fea3d2c188c49f2799fcd92f0e9879ea6a36ae60770a5428ed6c19edd" |
|||
let keypair = FullKeypair(pubkey: pubkey, privkey: privkey) |
|||
let mutelist = create_or_update_mutelist(keypair: keypair, mprev: nil, to_add: to_mute)! |
|||
|
|||
XCTAssertEqual(mutelist.pubkey, pubkey) |
|||
XCTAssertEqual(mutelist.content, "") |
|||
XCTAssertEqual(mutelist.tags.count, 2) |
|||
XCTAssertEqual(mutelist.tags[0][0], "d") |
|||
XCTAssertEqual(mutelist.tags[0][1], "mute") |
|||
XCTAssertEqual(mutelist.tags[1][0], "p") |
|||
XCTAssertEqual(mutelist.tags[1][1], to_mute) |
|||
} |
|||
|
|||
func testCreateAndRemoveMuteList() throws { |
|||
let privkey = "87f313b03f2548e6eaf1c188db47078e08e894252949779b639b28db0891937a" |
|||
let pubkey = "4b0c29bf96496130c1253102f6870c0eee05db38a257315858272aa43fd19685" |
|||
let to_mute = "2fa2630fea3d2c188c49f2799fcd92f0e9879ea6a36ae60770a5428ed6c19edd" |
|||
let keypair = FullKeypair(pubkey: pubkey, privkey: privkey) |
|||
let mutelist = create_or_update_mutelist(keypair: keypair, mprev: nil, to_add: to_mute)! |
|||
let new = remove_from_mutelist(keypair: keypair, prev: mutelist, to_remove: to_mute)! |
|||
|
|||
XCTAssertEqual(new.pubkey, pubkey) |
|||
XCTAssertEqual(new.content, "") |
|||
XCTAssertEqual(new.tags.count, 1) |
|||
XCTAssertEqual(new.tags[0][0], "d") |
|||
XCTAssertEqual(new.tags[0][1], "mute") |
|||
} |
|||
|
|||
func testAddToExistingMutelist() throws { |
|||
let privkey = "87f313b03f2548e6eaf1c188db47078e08e894252949779b639b28db0891937a" |
|||
let pubkey = "4b0c29bf96496130c1253102f6870c0eee05db38a257315858272aa43fd19685" |
|||
let to_mute = "2fa2630fea3d2c188c49f2799fcd92f0e9879ea6a36ae60770a5428ed6c19edd" |
|||
let to_mute_2 = "976b4ab41f8634119b4f21f57ef5836a4bef65d0bf72c7ced67b8b170ba4a38d" |
|||
let keypair = FullKeypair(pubkey: pubkey, privkey: privkey) |
|||
let mutelist = create_or_update_mutelist(keypair: keypair, mprev: nil, to_add: to_mute)! |
|||
let new = create_or_update_mutelist(keypair: keypair, mprev: mutelist, to_add: to_mute_2)! |
|||
|
|||
XCTAssertEqual(new.pubkey, pubkey) |
|||
XCTAssertEqual(new.content, "") |
|||
XCTAssertEqual(new.tags.count, 3) |
|||
XCTAssertEqual(new.tags[0][0], "d") |
|||
XCTAssertEqual(new.tags[0][1], "mute") |
|||
XCTAssertEqual(new.tags[1][0], "p") |
|||
XCTAssertEqual(new.tags[1][1], to_mute) |
|||
XCTAssertEqual(new.tags[2][0], "p") |
|||
XCTAssertEqual(new.tags[2][1], to_mute_2) |
|||
} |
|||
} |
Loading…
Reference in new issue