< Summary

Information
Class: NostrSure.Domain.Entities.ContactEntry
Assembly: NostrSure.Domain
File(s): /home/runner/work/NostrSure/NostrSure/NostrSure.Domain/Entities/ContactEntry.cs
Line coverage
100%
Covered lines: 40
Uncovered lines: 0
Coverable lines: 40
Total lines: 75
Line coverage: 100%
Branch coverage
94%
Covered branches: 17
Total branches: 18
Branch coverage: 94.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
get_ContactPubkey()100%11100%
get_Petname()100%11100%
get_RelayUrl()100%11100%
get_IsValid()75%44100%
FromPTag(...)100%88100%
ToPTag()100%11100%
BuildPTagValues()100%66100%

File(s)

/home/runner/work/NostrSure/NostrSure/NostrSure.Domain/Entities/ContactEntry.cs

#LineLine coverage
 1namespace NostrSure.Domain.Entities;
 2
 3using NostrSure.Domain.ValueObjects;
 4
 5/// <summary>
 6/// Represents a contact entry in a NIP-02 contact list.
 7/// Immutable value object following SOLID principles.
 8/// </summary>
 489public sealed record ContactEntry(
 5310    Pubkey ContactPubkey,
 6211    string? Petname = null,
 11312    string? RelayUrl = null)
 13{
 14    /// <summary>
 15    /// Validates the contact entry according to NIP-02 specifications.
 16    /// </summary>
 1117    public bool IsValid => ContactPubkey.IsValid &&
 1118                          (string.IsNullOrEmpty(RelayUrl) || Uri.TryCreate(RelayUrl, UriKind.Absolute, out _));
 19
 20    /// <summary>
 21    /// Creates a ContactEntry from a NostrTag "p" tag.
 22    /// </summary>
 23    public static ContactEntry FromPTag(NostrTag pTag)
 2324    {
 2325        if (pTag.Name != "p" || pTag.Values.Count == 0)
 226            throw new ArgumentException("Invalid p tag for contact entry", nameof(pTag));
 27
 2128        var pubkeyValue = pTag.Values[0];
 2129        var petname = pTag.Values.ElementAtOrDefault(1);
 2130        var relayUrl = pTag.Values.ElementAtOrDefault(2);
 31
 2132        return new ContactEntry(
 2133            new Pubkey(pubkeyValue),
 2134            string.IsNullOrWhiteSpace(petname) ? null : petname,
 2135            string.IsNullOrWhiteSpace(relayUrl) ? null : relayUrl
 2136        );
 2137    }
 38
 39    /// <summary>
 40    /// Converts this ContactEntry to a NostrTag "p" tag.
 41    /// </summary>
 42    public NostrTag ToPTag()
 2343    {
 2344        var values = BuildPTagValues();
 2345        return new NostrTag("p", values);
 2346    }
 47
 48    /// <summary>
 49    /// Builds the values list for the "p" tag, handling optional fields explicitly.
 50    /// </summary>
 51    private List<string> BuildPTagValues()
 2352    {
 2353        var values = new List<string> { ContactPubkey.Value };
 54
 2355        bool hasPetname = !string.IsNullOrWhiteSpace(Petname);
 2356        bool hasRelayUrl = !string.IsNullOrWhiteSpace(RelayUrl);
 57
 2358        if (hasPetname)
 1859        {
 1860            values.Add(Petname!);
 1861        }
 562        else if (hasRelayUrl)
 263        {
 64            // Petname slot must be present (as empty string) if relay URL is present
 265            values.Add(string.Empty);
 266        }
 67
 2368        if (hasRelayUrl)
 869        {
 870            values.Add(RelayUrl!);
 871        }
 72
 2373        return values;
 2374    }
 75}