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

fix: update contacts compatible with remove im. #242

Open
wants to merge 2 commits into
base: release/1.4.17
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: update contacts compatible with remove im.
  • Loading branch information
felix-zhaolei committed Apr 15, 2024
commit 214edb012ce0e6d6be88381d11754c3426e0ef06
Original file line number Diff line number Diff line change
@@ -20,20 +20,5 @@ public IEnumerable<ValidationResult> Validate(ValidationContext validationContex
{
yield return new ValidationResult("Invalid input.");
}

if (!RelationId.IsNullOrWhiteSpace())
{
if (Addresses is { Count: > 0 })
{
yield return new ValidationResult("Invalid input.");
}
}
else
{
if (Addresses?.Count != 1)
{
yield return new ValidationResult("Invalid input.");
}
}
}
}
51 changes: 44 additions & 7 deletions src/CAServer.Application/Contacts/ContactAppService.cs
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AElf;
using AElf.Types;
using CAServer.Common;
using CAServer.Commons;
@@ -123,6 +124,23 @@ public async Task<ContactResultDto> UpdateAsync(Guid id, CreateUpdateContactDto
}

var contact = contactResult.Data;
if (contact.Addresses?.Count > 1)
{
contact.Addresses.ForEach(t =>
{
var validResult = ValidAddress(t.Address);
if (!validResult)
{
throw new UserFriendlyException("invalid address.");
}
});

var dto = ObjectMapper.Map<ContactGrainDto, ContactDto>(contact);
dto.Addresses = input.Addresses;
dto.Name = input.Name;
return await UpdateContactAsync(id, userId, dto);
}

if (contact.Addresses != null && contact.Addresses.Count > 1 && input.Addresses != null &&
input.Addresses.Count == 1)
{
@@ -145,17 +163,41 @@ public async Task<ContactResultDto> UpdateAsync(Guid id, CreateUpdateContactDto
var contactDto = await GetContactDtoAsync(input, id);
await CheckContactAsync(contactDto);

return await UpdateContactAsync(id, userId, contactDto);
}

private bool ValidAddress(string address)
{
try
{
var isValid = AddressHelper.VerifyFormattedAddress(address);
if (!isValid)
{
Logger.LogInformation("invalid address: {address}", address);
}

return isValid;
}
catch (Exception e)
{
Logger.LogError(e, "valid address error", address);
return false;
}
}

private async Task<ContactResultDto> UpdateContactAsync(Guid contactId, Guid userId, ContactDto contactDto)
{
var contactGrain = _clusterClient.GetGrain<IContactGrain>(contactId);
var result =
await contactGrain.UpdateContactAsync(userId,
ObjectMapper.Map<ContactDto, ContactGrainDto>(contactDto));

if (!result.Success)
{
throw new UserFriendlyException(result.Message);
}

await _distributedEventBus.PublishAsync(ObjectMapper.Map<ContactGrainDto, ContactUpdateEto>(result.Data));
// return ObjectMapper.Map<ContactGrainDto, ContactResultDto>(result.Data);

var contactResultDto = ObjectMapper.Map<ContactGrainDto, ContactResultDto>(result.Data);
var imageMap = _variablesOptions.ImageMap;

@@ -168,11 +210,6 @@ await contactGrain.UpdateContactAsync(userId,
contactAddressDto.Image = imageMap.GetOrDefault(contactAddressDto.ChainName);
}

if (contact.Name != input.Name)
{
await ImRemarkAsync(contactResultDto?.ImInfo?.RelationId, userId, input.Name);
}

return contactResultDto;
}