♻️ Move the realm service from sphere to the pass
This commit is contained in:
@@ -30,7 +30,7 @@ public class SnChatRoom : ModelBase, IIdentifiedResource
|
||||
[JsonIgnore] public ICollection<SnChatMember> Members { get; set; } = new List<SnChatMember>();
|
||||
|
||||
public Guid? RealmId { get; set; }
|
||||
public SnRealm? Realm { get; set; }
|
||||
[NotMapped] public SnRealm? Realm { get; set; }
|
||||
|
||||
[NotMapped]
|
||||
[JsonPropertyName("members")]
|
||||
|
||||
@@ -64,7 +64,7 @@ public class SnPost : ModelBase, IIdentifiedResource, IActivity
|
||||
public SnPost? ForwardedPost { get; set; }
|
||||
|
||||
public Guid? RealmId { get; set; }
|
||||
public SnRealm? Realm { get; set; }
|
||||
[NotMapped] public SnRealm? Realm { get; set; }
|
||||
|
||||
[Column(TypeName = "jsonb")] public List<SnCloudFileReferenceObject> Attachments { get; set; } = [];
|
||||
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Text.Json.Serialization;
|
||||
using DysonNetwork.Shared.Proto;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using NodaTime;
|
||||
using NodaTime.Serialization.Protobuf;
|
||||
|
||||
namespace DysonNetwork.Shared.Models;
|
||||
|
||||
@@ -31,6 +33,28 @@ public class SnRealm : ModelBase, IIdentifiedResource
|
||||
public Guid AccountId { get; set; }
|
||||
|
||||
public string ResourceIdentifier => $"realm:{Id}";
|
||||
|
||||
public Realm ToProtoValue()
|
||||
{
|
||||
return new Realm
|
||||
{
|
||||
Id = Id.ToString(),
|
||||
Name = Name
|
||||
};
|
||||
}
|
||||
|
||||
public static SnRealm FromProtoValue(Realm proto)
|
||||
{
|
||||
return new SnRealm
|
||||
{
|
||||
Id = Guid.Parse(proto.Id),
|
||||
Name = proto.Name,
|
||||
Slug = "", // Required but not in proto
|
||||
Description = "",
|
||||
IsCommunity = false,
|
||||
IsPublic = false
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class RealmMemberRole
|
||||
@@ -51,4 +75,40 @@ public class SnRealmMember : ModelBase
|
||||
public int Role { get; set; } = RealmMemberRole.Normal;
|
||||
public Instant? JoinedAt { get; set; }
|
||||
public Instant? LeaveAt { get; set; }
|
||||
}
|
||||
|
||||
public Proto.RealmMember ToProtoValue()
|
||||
{
|
||||
var proto = new Proto.RealmMember
|
||||
{
|
||||
AccountId = AccountId.ToString(),
|
||||
RealmId = RealmId.ToString(),
|
||||
Role = Role,
|
||||
JoinedAt = JoinedAt?.ToTimestamp(),
|
||||
LeaveAt = LeaveAt?.ToTimestamp(),
|
||||
Realm = Realm.ToProtoValue()
|
||||
};
|
||||
if (Account != null)
|
||||
{
|
||||
proto.Account = Account.ToProtoValue();
|
||||
}
|
||||
return proto;
|
||||
}
|
||||
|
||||
public static SnRealmMember FromProtoValue(RealmMember proto)
|
||||
{
|
||||
var member = new SnRealmMember
|
||||
{
|
||||
AccountId = Guid.Parse(proto.AccountId),
|
||||
RealmId = Guid.Parse(proto.RealmId),
|
||||
Role = proto.Role,
|
||||
JoinedAt = proto.JoinedAt?.ToInstant(),
|
||||
LeaveAt = proto.LeaveAt?.ToInstant(),
|
||||
Realm = proto.Realm != null ? SnRealm.FromProtoValue(proto.Realm) : new SnRealm() // Provide default or handle null
|
||||
};
|
||||
if (proto.Account != null)
|
||||
{
|
||||
member.Account = SnAccount.FromProtoValue(proto.Account);
|
||||
}
|
||||
return member;
|
||||
}
|
||||
}
|
||||
|
||||
84
DysonNetwork.Shared/Proto/realm.proto
Normal file
84
DysonNetwork.Shared/Proto/realm.proto
Normal file
@@ -0,0 +1,84 @@
|
||||
syntax = "proto3";
|
||||
|
||||
package proto;
|
||||
|
||||
option csharp_namespace = "DysonNetwork.Shared.Proto";
|
||||
|
||||
import "google/protobuf/timestamp.proto";
|
||||
import "google/protobuf/wrappers.proto";
|
||||
import "google/protobuf/empty.proto";
|
||||
|
||||
import 'account.proto';
|
||||
|
||||
// Message Definitions
|
||||
|
||||
message Realm {
|
||||
string id = 1;
|
||||
string name = 2;
|
||||
}
|
||||
|
||||
message RealmMember {
|
||||
string account_id = 1;
|
||||
string realm_id = 2;
|
||||
int32 role = 3;
|
||||
optional google.protobuf.Timestamp joined_at = 4;
|
||||
optional google.protobuf.Timestamp leave_at = 5;
|
||||
optional Account account = 6;
|
||||
optional Realm realm = 7;
|
||||
}
|
||||
|
||||
// Service Definitions
|
||||
|
||||
service RealmService {
|
||||
// Get realm by id or slug
|
||||
rpc GetRealm(GetRealmRequest) returns (Realm) {}
|
||||
// Get realms for a user
|
||||
rpc GetUserRealms(GetUserRealmsRequest) returns (GetUserRealmsResponse) {}
|
||||
// Send invitation notification
|
||||
rpc SendInviteNotify(SendInviteNotifyRequest) returns (google.protobuf.Empty) {}
|
||||
// Check if member has required role
|
||||
rpc IsMemberWithRole(IsMemberWithRoleRequest) returns (google.protobuf.BoolValue) {}
|
||||
// Load account for a member
|
||||
rpc LoadMemberAccount(LoadMemberAccountRequest) returns (RealmMember) {}
|
||||
// Load accounts for members
|
||||
rpc LoadMemberAccounts(LoadMemberAccountsRequest) returns (LoadMemberAccountsResponse) {}
|
||||
}
|
||||
|
||||
// Request/Response Messages
|
||||
|
||||
message GetRealmRequest {
|
||||
oneof query {
|
||||
string id = 1;
|
||||
string slug = 2;
|
||||
}
|
||||
}
|
||||
|
||||
message GetUserRealmsRequest {
|
||||
string account_id = 1;
|
||||
}
|
||||
|
||||
message GetUserRealmsResponse {
|
||||
repeated string realm_ids = 1;
|
||||
}
|
||||
|
||||
message SendInviteNotifyRequest {
|
||||
RealmMember member = 1;
|
||||
}
|
||||
|
||||
message IsMemberWithRoleRequest {
|
||||
string realm_id = 1;
|
||||
string account_id = 2;
|
||||
repeated int32 required_roles = 3;
|
||||
}
|
||||
|
||||
message LoadMemberAccountRequest {
|
||||
RealmMember member = 1;
|
||||
}
|
||||
|
||||
message LoadMemberAccountsRequest {
|
||||
repeated RealmMember members = 1;
|
||||
}
|
||||
|
||||
message LoadMemberAccountsResponse {
|
||||
repeated RealmMember members = 1;
|
||||
}
|
||||
@@ -3,7 +3,7 @@ using DysonNetwork.Shared.Proto;
|
||||
|
||||
namespace DysonNetwork.Shared.Registry;
|
||||
|
||||
public class AccountClientHelper(AccountService.AccountServiceClient accounts)
|
||||
public class RemoteAccountService(AccountService.AccountServiceClient accounts)
|
||||
{
|
||||
public async Task<Account> GetAccount(Guid id)
|
||||
{
|
||||
60
DysonNetwork.Shared/Registry/RemoteRealmService.cs
Normal file
60
DysonNetwork.Shared/Registry/RemoteRealmService.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using DysonNetwork.Shared.Models;
|
||||
using DysonNetwork.Shared.Proto;
|
||||
|
||||
namespace DysonNetwork.Shared.Registry;
|
||||
|
||||
public class RemoteRealmService(RealmService.RealmServiceClient realms)
|
||||
{
|
||||
public async Task<SnRealm> GetRealm(string id)
|
||||
{
|
||||
var request = new GetRealmRequest { Id = id };
|
||||
var response = await realms.GetRealmAsync(request);
|
||||
return SnRealm.FromProtoValue(response);
|
||||
}
|
||||
|
||||
public async Task<SnRealm> GetRealmBySlug(string slug)
|
||||
{
|
||||
var request = new GetRealmRequest { Slug = slug };
|
||||
var response = await realms.GetRealmAsync(request);
|
||||
return SnRealm.FromProtoValue(response);
|
||||
}
|
||||
|
||||
public async Task<List<Guid>> GetUserRealms(Guid accountId)
|
||||
{
|
||||
var request = new GetUserRealmsRequest { AccountId = accountId.ToString() };
|
||||
var response = await realms.GetUserRealmsAsync(request);
|
||||
return response.RealmIds.Select(Guid.Parse).ToList();
|
||||
}
|
||||
|
||||
public async Task SendInviteNotify(SnRealmMember member)
|
||||
{
|
||||
var protoMember = member.ToProtoValue();
|
||||
var request = new SendInviteNotifyRequest { Member = protoMember };
|
||||
await realms.SendInviteNotifyAsync(request);
|
||||
}
|
||||
|
||||
public async Task<bool> IsMemberWithRole(Guid realmId, Guid accountId, List<int> requiredRoles)
|
||||
{
|
||||
var request = new IsMemberWithRoleRequest { RealmId = realmId.ToString(), AccountId = accountId.ToString() };
|
||||
request.RequiredRoles.AddRange(requiredRoles);
|
||||
var response = await realms.IsMemberWithRoleAsync(request);
|
||||
return response.Value;
|
||||
}
|
||||
|
||||
public async Task<SnRealmMember> LoadMemberAccount(SnRealmMember member)
|
||||
{
|
||||
var protoMember = member.ToProtoValue();
|
||||
var request = new LoadMemberAccountRequest { Member = protoMember };
|
||||
var response = await realms.LoadMemberAccountAsync(request);
|
||||
return SnRealmMember.FromProtoValue(response);
|
||||
}
|
||||
|
||||
public async Task<List<SnRealmMember>> LoadMemberAccounts(List<SnRealmMember> members)
|
||||
{
|
||||
var protoMembers = members.Select(m => m.ToProtoValue()).ToList();
|
||||
var request = new LoadMemberAccountsRequest();
|
||||
request.Members.AddRange(protoMembers);
|
||||
var response = await realms.LoadMemberAccountsAsync(request);
|
||||
return response.Members.Select(SnRealmMember.FromProtoValue).ToList();
|
||||
}
|
||||
}
|
||||
@@ -36,11 +36,11 @@ public static class ServiceInjectionHelper
|
||||
public static IServiceCollection AddAccountService(this IServiceCollection services)
|
||||
{
|
||||
services
|
||||
.AddGrpcClient<AccountService.AccountServiceClient>(o => o.Address = new Uri("https://_grpc.pass") )
|
||||
.AddGrpcClient<AccountService.AccountServiceClient>(o => o.Address = new Uri("https://_grpc.pass"))
|
||||
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
|
||||
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
|
||||
);
|
||||
services.AddSingleton<AccountClientHelper>();
|
||||
services.AddSingleton<RemoteAccountService>();
|
||||
|
||||
services
|
||||
.AddGrpcClient<BotAccountReceiverService.BotAccountReceiverServiceClient>(o =>
|
||||
@@ -59,10 +59,17 @@ public static class ServiceInjectionHelper
|
||||
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
|
||||
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
|
||||
);
|
||||
|
||||
services
|
||||
.AddGrpcClient<RealmService.RealmServiceClient>(o => o.Address = new Uri("https://_grpc.pass"))
|
||||
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
|
||||
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
|
||||
);
|
||||
services.AddSingleton<RemoteRealmService>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
|
||||
public static IServiceCollection AddDriveService(this IServiceCollection services)
|
||||
{
|
||||
services.AddGrpcClient<FileService.FileServiceClient>(o => o.Address = new Uri("https://_grpc.drive"))
|
||||
@@ -70,7 +77,8 @@ public static class ServiceInjectionHelper
|
||||
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
|
||||
);
|
||||
|
||||
services.AddGrpcClient<FileReferenceService.FileReferenceServiceClient>(o => o.Address = new Uri("https://_grpc.drive"))
|
||||
services.AddGrpcClient<FileReferenceService.FileReferenceServiceClient>(o =>
|
||||
o.Address = new Uri("https://_grpc.drive"))
|
||||
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
|
||||
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
|
||||
);
|
||||
@@ -80,7 +88,8 @@ public static class ServiceInjectionHelper
|
||||
|
||||
public static IServiceCollection AddPublisherService(this IServiceCollection services)
|
||||
{
|
||||
services.AddGrpcClient<PublisherService.PublisherServiceClient>(o => o.Address = new Uri("https://_grpc.sphere"))
|
||||
services
|
||||
.AddGrpcClient<PublisherService.PublisherServiceClient>(o => o.Address = new Uri("https://_grpc.sphere"))
|
||||
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
|
||||
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
|
||||
);
|
||||
@@ -90,11 +99,12 @@ public static class ServiceInjectionHelper
|
||||
|
||||
public static IServiceCollection AddDevelopService(this IServiceCollection services)
|
||||
{
|
||||
services.AddGrpcClient<CustomAppService.CustomAppServiceClient>(o => o.Address = new Uri("https://_grpc.develop"))
|
||||
services.AddGrpcClient<CustomAppService.CustomAppServiceClient>(o =>
|
||||
o.Address = new Uri("https://_grpc.develop"))
|
||||
.ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler()
|
||||
{ ServerCertificateCustomValidationCallback = (_, _, _, _) => true }
|
||||
);
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user