Skip to content

chore: simplify unfollow function#209

Open
kasugamirai wants to merge 1 commit intolumehq:legacyfrom
kasugamirai:feat/follow
Open

chore: simplify unfollow function#209
kasugamirai wants to merge 1 commit intolumehq:legacyfrom
kasugamirai:feat/follow

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