♻️ I have no idea what am I doing. Might be mixing stuff
This commit is contained in:
@@ -9,6 +9,7 @@ namespace DysonNetwork.Pass.Account;
|
||||
|
||||
public class AccountServiceGrpc(
|
||||
AppDatabase db,
|
||||
RelationshipService relationships,
|
||||
IClock clock,
|
||||
ILogger<AccountServiceGrpc> logger
|
||||
)
|
||||
@@ -19,7 +20,7 @@ public class AccountServiceGrpc(
|
||||
|
||||
private readonly ILogger<AccountServiceGrpc>
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
|
||||
|
||||
public override async Task<Shared.Proto.Account> GetAccount(GetAccountRequest request, ServerCallContext context)
|
||||
{
|
||||
if (!Guid.TryParse(request.Id, out var accountId))
|
||||
@@ -36,7 +37,8 @@ public class AccountServiceGrpc(
|
||||
return account.ToProtoValue();
|
||||
}
|
||||
|
||||
public override async Task<GetAccountBatchResponse> GetAccountBatch(GetAccountBatchRequest request, ServerCallContext context)
|
||||
public override async Task<GetAccountBatchResponse> GetAccountBatch(GetAccountBatchRequest request,
|
||||
ServerCallContext context)
|
||||
{
|
||||
var accountIds = request.Id
|
||||
.Select(id => Guid.TryParse(id, out var accountId) ? accountId : (Guid?)null)
|
||||
@@ -245,7 +247,8 @@ public class AccountServiceGrpc(
|
||||
return new Empty();
|
||||
}
|
||||
|
||||
public override async Task<ListContactsResponse> ListContacts(ListContactsRequest request, ServerCallContext context)
|
||||
public override async Task<ListContactsResponse> ListContacts(ListContactsRequest request,
|
||||
ServerCallContext context)
|
||||
{
|
||||
if (!Guid.TryParse(request.AccountId, out var accountId))
|
||||
throw new RpcException(new Grpc.Core.Status(StatusCode.InvalidArgument, "Invalid account ID format"));
|
||||
@@ -263,7 +266,8 @@ public class AccountServiceGrpc(
|
||||
return response;
|
||||
}
|
||||
|
||||
public override async Task<Shared.Proto.AccountContact> VerifyContact(VerifyContactRequest request, ServerCallContext context)
|
||||
public override async Task<Shared.Proto.AccountContact> VerifyContact(VerifyContactRequest request,
|
||||
ServerCallContext context)
|
||||
{
|
||||
// This is a placeholder implementation. In a real-world scenario, you would
|
||||
// have a more robust verification mechanism (e.g., sending a code to the
|
||||
@@ -343,7 +347,8 @@ public class AccountServiceGrpc(
|
||||
return response;
|
||||
}
|
||||
|
||||
public override async Task<Shared.Proto.AccountProfile> SetActiveBadge(SetActiveBadgeRequest request, ServerCallContext context)
|
||||
public override async Task<Shared.Proto.AccountProfile> SetActiveBadge(SetActiveBadgeRequest request,
|
||||
ServerCallContext context)
|
||||
{
|
||||
if (!Guid.TryParse(request.AccountId, out var accountId))
|
||||
throw new RpcException(new Grpc.Core.Status(StatusCode.InvalidArgument, "Invalid account ID format"));
|
||||
@@ -359,4 +364,55 @@ public class AccountServiceGrpc(
|
||||
|
||||
return profile.ToProtoValue();
|
||||
}
|
||||
|
||||
public override async Task<ListUserRelationshipSimpleResponse> ListFriends(
|
||||
ListUserRelationshipSimpleRequest request, ServerCallContext context)
|
||||
{
|
||||
var accountId = Guid.Parse(request.AccountId);
|
||||
var relationship = await relationships.ListAccountFriends(accountId);
|
||||
var resp = new ListUserRelationshipSimpleResponse();
|
||||
resp.AccountsId.AddRange(relationship.Select(x => x.ToString()));
|
||||
return resp;
|
||||
}
|
||||
|
||||
public override async Task<ListUserRelationshipSimpleResponse> ListBlocked(
|
||||
ListUserRelationshipSimpleRequest request, ServerCallContext context)
|
||||
{
|
||||
var accountId = Guid.Parse(request.AccountId);
|
||||
var relationship = await relationships.ListAccountBlocked(accountId);
|
||||
var resp = new ListUserRelationshipSimpleResponse();
|
||||
resp.AccountsId.AddRange(relationship.Select(x => x.ToString()));
|
||||
return resp;
|
||||
}
|
||||
|
||||
public override async Task<GetRelationshipResponse> GetRelationship(GetRelationshipRequest request,
|
||||
ServerCallContext context)
|
||||
{
|
||||
var relationship = await relationships.GetRelationship(
|
||||
Guid.Parse(request.AccountId),
|
||||
Guid.Parse(request.RelatedId),
|
||||
status: (RelationshipStatus?)request.Status
|
||||
);
|
||||
return new GetRelationshipResponse
|
||||
{
|
||||
Relationship = relationship?.ToProtoValue()
|
||||
};
|
||||
}
|
||||
|
||||
public override async Task<BoolValue> HasRelationship(GetRelationshipRequest request, ServerCallContext context)
|
||||
{
|
||||
var hasRelationship = false;
|
||||
if (!request.HasStatus)
|
||||
hasRelationship = await relationships.HasExistingRelationship(
|
||||
Guid.Parse(request.AccountId),
|
||||
Guid.Parse(request.RelatedId)
|
||||
);
|
||||
else
|
||||
hasRelationship = await relationships.HasRelationshipWithStatus(
|
||||
Guid.Parse(request.AccountId),
|
||||
Guid.Parse(request.RelatedId),
|
||||
(RelationshipStatus)request.Status
|
||||
);
|
||||
return new BoolValue { Value = hasRelationship };
|
||||
}
|
||||
}
|
@@ -1,5 +1,7 @@
|
||||
using DysonNetwork.Shared.Data;
|
||||
using DysonNetwork.Shared.Proto;
|
||||
using NodaTime;
|
||||
using NodaTime.Serialization.Protobuf;
|
||||
|
||||
namespace DysonNetwork.Pass.Account;
|
||||
|
||||
@@ -20,4 +22,15 @@ public class Relationship : ModelBase
|
||||
public Instant? ExpiredAt { get; set; }
|
||||
|
||||
public RelationshipStatus Status { get; set; } = RelationshipStatus.Pending;
|
||||
|
||||
public Shared.Proto.Relationship ToProtoValue() => new()
|
||||
{
|
||||
AccountId = AccountId.ToString(),
|
||||
RelatedId = RelatedId.ToString(),
|
||||
Account = Account.ToProtoValue(),
|
||||
Related = Related.ToProtoValue(),
|
||||
Type = (int)Status,
|
||||
CreatedAt = CreatedAt.ToTimestamp(),
|
||||
UpdatedAt = UpdatedAt.ToTimestamp()
|
||||
};
|
||||
}
|
@@ -154,13 +154,18 @@ public class RelationshipService(AppDatabase db, ICacheService cache)
|
||||
|
||||
public async Task<List<Guid>> ListAccountFriends(Account account)
|
||||
{
|
||||
var cacheKey = $"{UserFriendsCacheKeyPrefix}{account.Id}";
|
||||
return await ListAccountFriends(account.Id);
|
||||
}
|
||||
|
||||
public async Task<List<Guid>> ListAccountFriends(Guid accountId)
|
||||
{
|
||||
var cacheKey = $"{UserFriendsCacheKeyPrefix}{accountId}";
|
||||
var friends = await cache.GetAsync<List<Guid>>(cacheKey);
|
||||
|
||||
if (friends == null)
|
||||
{
|
||||
friends = await db.AccountRelationships
|
||||
.Where(r => r.RelatedId == account.Id)
|
||||
.Where(r => r.RelatedId == accountId)
|
||||
.Where(r => r.Status == RelationshipStatus.Friends)
|
||||
.Select(r => r.AccountId)
|
||||
.ToListAsync();
|
||||
@@ -173,13 +178,18 @@ public class RelationshipService(AppDatabase db, ICacheService cache)
|
||||
|
||||
public async Task<List<Guid>> ListAccountBlocked(Account account)
|
||||
{
|
||||
var cacheKey = $"{UserBlockedCacheKeyPrefix}{account.Id}";
|
||||
return await ListAccountBlocked(account.Id);
|
||||
}
|
||||
|
||||
public async Task<List<Guid>> ListAccountBlocked(Guid accountId)
|
||||
{
|
||||
var cacheKey = $"{UserBlockedCacheKeyPrefix}{accountId}";
|
||||
var blocked = await cache.GetAsync<List<Guid>>(cacheKey);
|
||||
|
||||
if (blocked == null)
|
||||
{
|
||||
blocked = await db.AccountRelationships
|
||||
.Where(r => r.RelatedId == account.Id)
|
||||
.Where(r => r.RelatedId == accountId)
|
||||
.Where(r => r.Status == RelationshipStatus.Blocked)
|
||||
.Select(r => r.AccountId)
|
||||
.ToListAsync();
|
||||
|
@@ -1,50 +0,0 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace DysonNetwork.Pass.Account;
|
||||
|
||||
/// <summary>
|
||||
/// The verification info of a resource
|
||||
/// stands, for it is really an individual or organization or a company in the real world.
|
||||
/// Besides, it can also be use for mark parody or fake.
|
||||
/// </summary>
|
||||
public class VerificationMark
|
||||
{
|
||||
public VerificationMarkType Type { get; set; }
|
||||
[MaxLength(1024)] public string? Title { get; set; }
|
||||
[MaxLength(8192)] public string? Description { get; set; }
|
||||
[MaxLength(1024)] public string? VerifiedBy { get; set; }
|
||||
|
||||
public Shared.Proto.VerificationMark ToProtoValue()
|
||||
{
|
||||
var proto = new Shared.Proto.VerificationMark
|
||||
{
|
||||
Type = Type switch
|
||||
{
|
||||
VerificationMarkType.Official => Shared.Proto.VerificationMarkType.Official,
|
||||
VerificationMarkType.Individual => Shared.Proto.VerificationMarkType.Individual,
|
||||
VerificationMarkType.Organization => Shared.Proto.VerificationMarkType.Organization,
|
||||
VerificationMarkType.Government => Shared.Proto.VerificationMarkType.Government,
|
||||
VerificationMarkType.Creator => Shared.Proto.VerificationMarkType.Creator,
|
||||
VerificationMarkType.Developer => Shared.Proto.VerificationMarkType.Developer,
|
||||
VerificationMarkType.Parody => Shared.Proto.VerificationMarkType.Parody,
|
||||
_ => Shared.Proto.VerificationMarkType.Unspecified
|
||||
},
|
||||
Title = Title ?? string.Empty,
|
||||
Description = Description ?? string.Empty,
|
||||
VerifiedBy = VerifiedBy ?? string.Empty
|
||||
};
|
||||
|
||||
return proto;
|
||||
}
|
||||
}
|
||||
|
||||
public enum VerificationMarkType
|
||||
{
|
||||
Official,
|
||||
Individual,
|
||||
Organization,
|
||||
Government,
|
||||
Creator,
|
||||
Developer,
|
||||
Parody
|
||||
}
|
Reference in New Issue
Block a user