upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/sub_commands
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2024-07-24 16:37:10 +0100
committerDanConwayDev <DanConwayDev@protonmail.com>2024-07-24 16:37:10 +0100
commit8638b321fdff94d034ec912ecd0910b6f564ff04 (patch)
tree411e0ca989d8c53be5b11b39461297bf6a92d781 /src/sub_commands
parent95cb9c040dfa8ca18bf907a44a86df35b316b6ca (diff)
refactor: use nip34 kinds from rust-nostr
instead of Kind::Custom(u16) as v33 of rust-nostr introduced them
Diffstat (limited to 'src/sub_commands')
-rw-r--r--src/sub_commands/init.rs4
-rw-r--r--src/sub_commands/list.rs69
-rw-r--r--src/sub_commands/send.rs30
3 files changed, 49 insertions, 54 deletions
diff --git a/src/sub_commands/init.rs b/src/sub_commands/init.rs
index 2a97779..1c51375 100644
--- a/src/sub_commands/init.rs
+++ b/src/sub_commands/init.rs
@@ -16,7 +16,7 @@ use crate::{
16 login, 16 login,
17 repo_ref::{ 17 repo_ref::{
18 extract_pks, get_repo_config_from_yaml, save_repo_config_to_yaml, 18 extract_pks, get_repo_config_from_yaml, save_repo_config_to_yaml,
19 try_and_get_repo_coordinates, RepoRef, REPO_REF_KIND, 19 try_and_get_repo_coordinates, RepoRef,
20 }, 20 },
21 Cli, 21 Cli,
22}; 22};
@@ -336,7 +336,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> {
336 git_repo.save_git_config_item( 336 git_repo.save_git_config_item(
337 "nostr.repo", 337 "nostr.repo",
338 &Coordinate { 338 &Coordinate {
339 kind: Kind::Custom(REPO_REF_KIND), 339 kind: Kind::GitRepoAnnouncement,
340 public_key: user_ref.public_key, 340 public_key: user_ref.public_key,
341 identifier: identifier.clone(), 341 identifier: identifier.clone(),
342 relays: vec![], 342 relays: vec![],
diff --git a/src/sub_commands/list.rs b/src/sub_commands/list.rs
index cc7ac6f..73ef107 100644
--- a/src/sub_commands/list.rs
+++ b/src/sub_commands/list.rs
@@ -2,7 +2,7 @@ use std::{collections::HashSet, io::Write, ops::Add, path::Path};
2 2
3use anyhow::{bail, Context, Result}; 3use anyhow::{bail, Context, Result};
4use nostr::nips::nip01::Coordinate; 4use nostr::nips::nip01::Coordinate;
5use nostr_sdk::PublicKey; 5use nostr_sdk::{Kind, PublicKey};
6 6
7use super::send::event_is_patch_set_root; 7use super::send::event_is_patch_set_root;
8#[cfg(test)] 8#[cfg(test)]
@@ -16,7 +16,7 @@ use crate::{
16 repo_ref::{get_repo_coordinates, RepoRef}, 16 repo_ref::{get_repo_coordinates, RepoRef},
17 sub_commands::send::{ 17 sub_commands::send::{
18 commit_msg_from_patch_oneliner, event_is_cover_letter, event_is_revision_root, 18 commit_msg_from_patch_oneliner, event_is_cover_letter, event_is_revision_root,
19 event_to_cover_letter, patch_supports_commit_ids, PATCH_KIND, 19 event_to_cover_letter, patch_supports_commit_ids,
20 }, 20 },
21}; 21};
22 22
@@ -84,31 +84,31 @@ pub async fn launch() -> Result<()> {
84 .collect::<Vec<&nostr::Event>>() 84 .collect::<Vec<&nostr::Event>>()
85 .first() 85 .first()
86 { 86 {
87 e.kind().as_u16() 87 e.kind()
88 } else { 88 } else {
89 STATUS_KIND_OPEN 89 Kind::GitStatusOpen
90 }; 90 };
91 if status.eq(&STATUS_KIND_OPEN) { 91 if status.eq(&Kind::GitStatusOpen) {
92 open_proposals.push(proposal); 92 open_proposals.push(proposal);
93 } else if status.eq(&STATUS_KIND_CLOSED) { 93 } else if status.eq(&Kind::GitStatusClosed) {
94 closed_proposals.push(proposal); 94 closed_proposals.push(proposal);
95 } else if status.eq(&STATUS_KIND_DRAFT) { 95 } else if status.eq(&Kind::GitStatusDraft) {
96 draft_proposals.push(proposal); 96 draft_proposals.push(proposal);
97 } else if status.eq(&STATUS_KIND_APPLIED) { 97 } else if status.eq(&Kind::GitStatusApplied) {
98 applied_proposals.push(proposal); 98 applied_proposals.push(proposal);
99 } 99 }
100 } 100 }
101 101
102 let mut selected_status = STATUS_KIND_OPEN; 102 let mut selected_status = Kind::GitStatusOpen;
103 103
104 loop { 104 loop {
105 let proposals_for_status = if selected_status == STATUS_KIND_OPEN { 105 let proposals_for_status = if selected_status == Kind::GitStatusOpen {
106 &open_proposals 106 &open_proposals
107 } else if selected_status == STATUS_KIND_DRAFT { 107 } else if selected_status == Kind::GitStatusDraft {
108 &draft_proposals 108 &draft_proposals
109 } else if selected_status == STATUS_KIND_CLOSED { 109 } else if selected_status == Kind::GitStatusClosed {
110 &closed_proposals 110 &closed_proposals
111 } else if selected_status == STATUS_KIND_APPLIED { 111 } else if selected_status == Kind::GitStatusApplied {
112 &applied_proposals 112 &applied_proposals
113 } else { 113 } else {
114 &open_proposals 114 &open_proposals
@@ -116,15 +116,15 @@ pub async fn launch() -> Result<()> {
116 116
117 let prompt = if proposals.len().eq(&open_proposals.len()) { 117 let prompt = if proposals.len().eq(&open_proposals.len()) {
118 "all proposals" 118 "all proposals"
119 } else if selected_status == STATUS_KIND_OPEN { 119 } else if selected_status == Kind::GitStatusOpen {
120 if open_proposals.is_empty() { 120 if open_proposals.is_empty() {
121 "proposals menu" 121 "proposals menu"
122 } else { 122 } else {
123 "open proposals" 123 "open proposals"
124 } 124 }
125 } else if selected_status == STATUS_KIND_DRAFT { 125 } else if selected_status == Kind::GitStatusDraft {
126 "draft proposals" 126 "draft proposals"
127 } else if selected_status == STATUS_KIND_CLOSED { 127 } else if selected_status == Kind::GitStatusClosed {
128 "closed proposals" 128 "closed proposals"
129 } else { 129 } else {
130 "applied proposals" 130 "applied proposals"
@@ -143,16 +143,16 @@ pub async fn launch() -> Result<()> {
143 }) 143 })
144 .collect(); 144 .collect();
145 145
146 if !selected_status.eq(&STATUS_KIND_OPEN) && open_proposals.len().gt(&0) { 146 if !selected_status.eq(&Kind::GitStatusOpen) && open_proposals.len().gt(&0) {
147 choices.push(format!("({}) Open proposals...", open_proposals.len())); 147 choices.push(format!("({}) Open proposals...", open_proposals.len()));
148 } 148 }
149 if !selected_status.eq(&STATUS_KIND_DRAFT) && draft_proposals.len().gt(&0) { 149 if !selected_status.eq(&Kind::GitStatusDraft) && draft_proposals.len().gt(&0) {
150 choices.push(format!("({}) Draft proposals...", draft_proposals.len())); 150 choices.push(format!("({}) Draft proposals...", draft_proposals.len()));
151 } 151 }
152 if !selected_status.eq(&STATUS_KIND_CLOSED) && closed_proposals.len().gt(&0) { 152 if !selected_status.eq(&Kind::GitStatusClosed) && closed_proposals.len().gt(&0) {
153 choices.push(format!("({}) Closed proposals...", closed_proposals.len())); 153 choices.push(format!("({}) Closed proposals...", closed_proposals.len()));
154 } 154 }
155 if !selected_status.eq(&STATUS_KIND_APPLIED) && applied_proposals.len().gt(&0) { 155 if !selected_status.eq(&Kind::GitStatusApplied) && applied_proposals.len().gt(&0) {
156 choices.push(format!( 156 choices.push(format!(
157 "({}) Applied proposals...", 157 "({}) Applied proposals...",
158 applied_proposals.len() 158 applied_proposals.len()
@@ -167,13 +167,13 @@ pub async fn launch() -> Result<()> {
167 167
168 if (selected_index + 1).gt(&proposals_for_status.len()) { 168 if (selected_index + 1).gt(&proposals_for_status.len()) {
169 if choices[selected_index].contains("Open") { 169 if choices[selected_index].contains("Open") {
170 selected_status = STATUS_KIND_OPEN; 170 selected_status = Kind::GitStatusOpen;
171 } else if choices[selected_index].contains("Draft") { 171 } else if choices[selected_index].contains("Draft") {
172 selected_status = STATUS_KIND_DRAFT; 172 selected_status = Kind::GitStatusDraft;
173 } else if choices[selected_index].contains("Closed") { 173 } else if choices[selected_index].contains("Closed") {
174 selected_status = STATUS_KIND_CLOSED; 174 selected_status = Kind::GitStatusClosed;
175 } else if choices[selected_index].contains("Applied") { 175 } else if choices[selected_index].contains("Applied") {
176 selected_status = STATUS_KIND_APPLIED; 176 selected_status = Kind::GitStatusApplied;
177 } 177 }
178 continue; 178 continue;
179 } 179 }
@@ -804,17 +804,12 @@ pub fn get_most_recent_patch_with_ancestors(
804 Ok(res) 804 Ok(res)
805} 805}
806 806
807pub static STATUS_KIND_OPEN: u16 = 1630;
808pub static STATUS_KIND_APPLIED: u16 = 1631;
809pub static STATUS_KIND_CLOSED: u16 = 1632;
810pub static STATUS_KIND_DRAFT: u16 = 1633;
811
812pub fn status_kinds() -> Vec<nostr::Kind> { 807pub fn status_kinds() -> Vec<nostr::Kind> {
813 vec![ 808 vec![
814 nostr::Kind::Custom(STATUS_KIND_OPEN), 809 nostr::Kind::GitStatusOpen,
815 nostr::Kind::Custom(STATUS_KIND_APPLIED), 810 nostr::Kind::GitStatusApplied,
816 nostr::Kind::Custom(STATUS_KIND_CLOSED), 811 nostr::Kind::GitStatusClosed,
817 nostr::Kind::Custom(STATUS_KIND_DRAFT), 812 nostr::Kind::GitStatusDraft,
818 ] 813 ]
819} 814}
820 815
@@ -826,7 +821,7 @@ pub async fn get_proposals_and_revisions_from_cache(
826 git_repo_path, 821 git_repo_path,
827 vec![ 822 vec![
828 nostr::Filter::default() 823 nostr::Filter::default()
829 .kind(nostr::Kind::Custom(PATCH_KIND)) 824 .kind(nostr::Kind::GitPatch)
830 .custom_tag( 825 .custom_tag(
831 nostr::SingleLetterTag::lowercase(nostr_sdk::Alphabet::A), 826 nostr::SingleLetterTag::lowercase(nostr_sdk::Alphabet::A),
832 repo_coordinates 827 repo_coordinates
@@ -855,10 +850,10 @@ pub async fn get_all_proposal_patch_events_from_cache(
855 git_repo_path, 850 git_repo_path,
856 vec![ 851 vec![
857 nostr::Filter::default() 852 nostr::Filter::default()
858 .kind(nostr::Kind::Custom(PATCH_KIND)) 853 .kind(nostr::Kind::GitPatch)
859 .event(*proposal_id), 854 .event(*proposal_id),
860 nostr::Filter::default() 855 nostr::Filter::default()
861 .kind(nostr::Kind::Custom(PATCH_KIND)) 856 .kind(nostr::Kind::GitPatch)
862 .id(*proposal_id), 857 .id(*proposal_id),
863 ], 858 ],
864 ) 859 )
@@ -891,7 +886,7 @@ pub async fn get_all_proposal_patch_events_from_cache(
891 git_repo_path, 886 git_repo_path,
892 vec![ 887 vec![
893 nostr::Filter::default() 888 nostr::Filter::default()
894 .kind(nostr::Kind::Custom(PATCH_KIND)) 889 .kind(nostr::Kind::GitPatch)
895 .events(revision_roots) 890 .events(revision_roots)
896 .authors(permissioned_users.clone()), 891 .authors(permissioned_users.clone()),
897 ], 892 ],
diff --git a/src/sub_commands/send.rs b/src/sub_commands/send.rs
index 9733cfe..73c980b 100644
--- a/src/sub_commands/send.rs
+++ b/src/sub_commands/send.rs
@@ -12,7 +12,7 @@ use nostr::{
12 }, 12 },
13 EventBuilder, FromBech32, Tag, TagKind, ToBech32, UncheckedUrl, 13 EventBuilder, FromBech32, Tag, TagKind, ToBech32, UncheckedUrl,
14}; 14};
15use nostr_sdk::{hashes::sha1::Hash as Sha1Hash, NostrSigner, TagStandard}; 15use nostr_sdk::{hashes::sha1::Hash as Sha1Hash, Kind, NostrSigner, TagStandard};
16 16
17use super::list::tag_value; 17use super::list::tag_value;
18#[cfg(not(test))] 18#[cfg(not(test))]
@@ -28,7 +28,7 @@ use crate::{
28 }, 28 },
29 git::{Repo, RepoActions}, 29 git::{Repo, RepoActions},
30 login, 30 login,
31 repo_ref::{get_repo_coordinates, RepoRef, REPO_REF_KIND}, 31 repo_ref::{get_repo_coordinates, RepoRef},
32 Cli, 32 Cli,
33}; 33};
34 34
@@ -288,7 +288,10 @@ pub async fn send_events(
288) -> Result<()> { 288) -> Result<()> {
289 let fallback = [ 289 let fallback = [
290 client.get_fallback_relays().clone(), 290 client.get_fallback_relays().clone(),
291 if events.iter().any(|e| e.kind().as_u16().eq(&REPO_REF_KIND)) { 291 if events
292 .iter()
293 .any(|e| e.kind().eq(&Kind::GitRepoAnnouncement))
294 {
292 client.get_blaster_relays().clone() 295 client.get_blaster_relays().clone()
293 } else { 296 } else {
294 vec![] 297 vec![]
@@ -573,8 +576,6 @@ async fn get_root_proposal_id_and_mentions_from_in_reply_to(
573 Ok((root_proposal_id, mention_tags)) 576 Ok((root_proposal_id, mention_tags))
574} 577}
575 578
576pub static PATCH_KIND: u16 = 1617;
577
578#[allow(clippy::too_many_lines)] 579#[allow(clippy::too_many_lines)]
579pub async fn generate_cover_letter_and_patch_events( 580pub async fn generate_cover_letter_and_patch_events(
580 cover_letter_title_description: Option<(String, String)>, 581 cover_letter_title_description: Option<(String, String)>,
@@ -593,7 +594,7 @@ pub async fn generate_cover_letter_and_patch_events(
593 594
594 if let Some((title, description)) = cover_letter_title_description { 595 if let Some((title, description)) = cover_letter_title_description {
595 events.push(sign_event(EventBuilder::new( 596 events.push(sign_event(EventBuilder::new(
596 nostr::event::Kind::Custom(PATCH_KIND), 597 nostr::event::Kind::GitPatch,
597 format!( 598 format!(
598 "From {} Mon Sep 17 00:00:00 2001\nSubject: [PATCH 0/{}] {title}\n\n{description}", 599 "From {} Mon Sep 17 00:00:00 2001\nSubject: [PATCH 0/{}] {title}\n\n{description}",
599 commits.last().unwrap(), 600 commits.last().unwrap(),
@@ -601,7 +602,7 @@ pub async fn generate_cover_letter_and_patch_events(
601 ), 602 ),
602 [ 603 [
603 repo_ref.maintainers.iter().map(|m| Tag::coordinate(Coordinate { 604 repo_ref.maintainers.iter().map(|m| Tag::coordinate(Coordinate {
604 kind: nostr::Kind::Custom(REPO_REF_KIND), 605 kind: nostr::Kind::GitRepoAnnouncement,
605 public_key: *m, 606 public_key: *m,
606 identifier: repo_ref.identifier.to_string(), 607 identifier: repo_ref.identifier.to_string(),
607 relays: repo_ref.relays.clone(), 608 relays: repo_ref.relays.clone(),
@@ -789,7 +790,7 @@ pub fn event_is_cover_letter(event: &nostr::Event) -> bool {
789 // TODO: look for Subject:[ PATCH 0/n ] but watch out for: 790 // TODO: look for Subject:[ PATCH 0/n ] but watch out for:
790 // [PATCH v1 0/n ] or 791 // [PATCH v1 0/n ] or
791 // [PATCH subsystem v2 0/n ] 792 // [PATCH subsystem v2 0/n ]
792 event.kind.as_u16().eq(&PATCH_KIND) 793 event.kind.eq(&Kind::GitPatch)
793 && event.iter_tags().any(|t| t.as_vec()[1].eq("root")) 794 && event.iter_tags().any(|t| t.as_vec()[1].eq("root"))
794 && event.iter_tags().any(|t| t.as_vec()[1].eq("cover-letter")) 795 && event.iter_tags().any(|t| t.as_vec()[1].eq("cover-letter"))
795} 796}
@@ -860,16 +861,15 @@ pub fn event_to_cover_letter(event: &nostr::Event) -> Result<CoverLetter> {
860} 861}
861 862
862pub fn event_is_patch_set_root(event: &nostr::Event) -> bool { 863pub fn event_is_patch_set_root(event: &nostr::Event) -> bool {
863 event.kind.as_u16().eq(&PATCH_KIND) && event.iter_tags().any(|t| t.as_vec()[1].eq("root")) 864 event.kind.eq(&Kind::GitPatch) && event.iter_tags().any(|t| t.as_vec()[1].eq("root"))
864} 865}
865 866
866pub fn event_is_revision_root(event: &nostr::Event) -> bool { 867pub fn event_is_revision_root(event: &nostr::Event) -> bool {
867 event.kind.as_u16().eq(&PATCH_KIND) 868 event.kind.eq(&Kind::GitPatch) && event.iter_tags().any(|t| t.as_vec()[1].eq("revision-root"))
868 && event.iter_tags().any(|t| t.as_vec()[1].eq("revision-root"))
869} 869}
870 870
871pub fn patch_supports_commit_ids(event: &nostr::Event) -> bool { 871pub fn patch_supports_commit_ids(event: &nostr::Event) -> bool {
872 event.kind.as_u16().eq(&PATCH_KIND) 872 event.kind.eq(&Kind::GitPatch)
873 && event 873 && event
874 .iter_tags() 874 .iter_tags()
875 .any(|t| t.as_vec()[0].eq("commit-pgp-sig")) 875 .any(|t| t.as_vec()[0].eq("commit-pgp-sig"))
@@ -897,7 +897,7 @@ pub async fn generate_patch_event(
897 897
898 sign_event( 898 sign_event(
899 EventBuilder::new( 899 EventBuilder::new(
900 nostr::event::Kind::Custom(PATCH_KIND), 900 nostr::event::Kind::GitPatch,
901 git_repo 901 git_repo
902 .make_patch_from_commit(commit, &series_count) 902 .make_patch_from_commit(commit, &series_count)
903 .context(format!("cannot make patch for commit {commit}"))?, 903 .context(format!("cannot make patch for commit {commit}"))?,
@@ -907,7 +907,7 @@ pub async fn generate_patch_event(
907 .iter() 907 .iter()
908 .map(|m| { 908 .map(|m| {
909 Tag::coordinate(Coordinate { 909 Tag::coordinate(Coordinate {
910 kind: nostr::Kind::Custom(REPO_REF_KIND), 910 kind: nostr::Kind::GitRepoAnnouncement,
911 public_key: *m, 911 public_key: *m,
912 identifier: repo_ref.identifier.to_string(), 912 identifier: repo_ref.identifier.to_string(),
913 relays: repo_ref.relays.clone(), 913 relays: repo_ref.relays.clone(),
@@ -1238,7 +1238,7 @@ mod tests {
1238 1238
1239 fn generate_cover_letter(title: &str, description: &str) -> Result<nostr::Event> { 1239 fn generate_cover_letter(title: &str, description: &str) -> Result<nostr::Event> {
1240 Ok(nostr::event::EventBuilder::new( 1240 Ok(nostr::event::EventBuilder::new(
1241 nostr::event::Kind::Custom(PATCH_KIND), 1241 nostr::event::Kind::GitPatch,
1242 format!("From ea897e987ea9a7a98e7a987e97987ea98e7a3334 Mon Sep 17 00:00:00 2001\nSubject: [PATCH 0/2] {title}\n\n{description}"), 1242 format!("From ea897e987ea9a7a98e7a987e97987ea98e7a3334 Mon Sep 17 00:00:00 2001\nSubject: [PATCH 0/2] {title}\n\n{description}"),
1243 [ 1243 [
1244 Tag::hashtag("cover-letter"), 1244 Tag::hashtag("cover-letter"),