A community based topic aggregation platform built on atproto

fix(service): add DID verification and improve error detection

Service layer improvements:

1. Add DID verification in ResolveCommunityIdentifier:
- When a DID is provided, verify the community actually exists
in the AppView database
- Prevents accepting non-existent DIDs (e.g., did:plc:fakefake)
- Provides clearer error messages when community doesn't exist

2. Improve duplicate error detection in BlockCommunity:
- Check for HTTP 409 Conflict status code explicitly
- Added "status 409" check in addition to text-based detection
- More robust across different PDS implementations
- Still maintains fallback checks for compatibility

Both changes improve error handling and user experience while
maintaining backward compatibility.

Addresses: Critical review comment #2, Important review comment #3

Changed files
+17 -3
internal
core
communities
+17 -3
internal/core/communities/service.go
···
// 3. This avoids a race condition where two concurrent requests both pass the check
recordURI, recordCID, err := s.createRecordOnPDSAs(ctx, userDID, "social.coves.community.block", "", blockRecord, userAccessToken)
if err != nil {
-
// Check if this is a duplicate error from PDS
errMsg := err.Error()
-
if strings.Contains(errMsg, "duplicate") || strings.Contains(errMsg, "already exists") {
// Fetch and return existing block from our indexed view
existingBlock, getErr := s.repo.GetBlock(ctx, userDID, communityDID)
if getErr == nil {
···
return "", ErrInvalidInput
}
-
// If it's already a DID, return it
if strings.HasPrefix(identifier, "did:") {
return identifier, nil
}
···
// 3. This avoids a race condition where two concurrent requests both pass the check
recordURI, recordCID, err := s.createRecordOnPDSAs(ctx, userDID, "social.coves.community.block", "", blockRecord, userAccessToken)
if err != nil {
+
// Check if this is a duplicate/conflict error from PDS
+
// PDS should return 409 Conflict for duplicate records, but we also check common error messages
+
// for compatibility with different PDS implementations
errMsg := err.Error()
+
isDuplicate := strings.Contains(errMsg, "status 409") || // HTTP 409 Conflict
+
strings.Contains(errMsg, "duplicate") ||
+
strings.Contains(errMsg, "already exists") ||
+
strings.Contains(errMsg, "AlreadyExists")
+
+
if isDuplicate {
// Fetch and return existing block from our indexed view
existingBlock, getErr := s.repo.GetBlock(ctx, userDID, communityDID)
if getErr == nil {
···
return "", ErrInvalidInput
}
+
// If it's already a DID, verify the community exists
if strings.HasPrefix(identifier, "did:") {
+
_, err := s.repo.GetByDID(ctx, identifier)
+
if err != nil {
+
if IsNotFound(err) {
+
return "", fmt.Errorf("community not found: %w", err)
+
}
+
return "", fmt.Errorf("failed to verify community DID: %w", err)
+
}
return identifier, nil
}