Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: simplify unfollow function #209

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kasugamirai
Copy link
Contributor

No description provided.

@reyamir
Copy link
Collaborator

reyamir commented Jun 14, 2024

In the PR #208 , I've merged follow and unfollow functions into toggle_contact

pub async fn toggle_contact(
  hex: &str,
  alias: Option<&str>,
  state: State<'_, Nostr>,
) -> Result<String, String> {
  let client = &state.client;

  match client.get_contact_list(None).await {
    Ok(mut contact_list) => {
      let public_key = PublicKey::from_str(hex).unwrap();

      match contact_list.iter().position(|x| x.public_key == public_key) {
        Some(index) => {
          // Remove contact
          contact_list.remove(index);
        }
        None => {
          // TODO: Add relay_url
          let new_contact = Contact::new(public_key, None, alias);
          // Add new contact
          contact_list.push(new_contact);
        }
      }

      // Update local state
      state.contact_list.lock().unwrap().clone_from(&contact_list);

      // Publish
      match client.set_contact_list(contact_list).await {
        Ok(event_id) => Ok(event_id.to_string()),
        Err(err) => Err(err.to_string()),
      }
    }
    Err(err) => Err(err.to_string()),
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants