Skip to content

Commit

Permalink
Fix intimacy relation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
gehongyan committed Jan 3, 2025
1 parent 7ce8fc6 commit 9bb31bb
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/Kook.Net.Core/Entities/Users/IIntimacyRelation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ public interface IIntimacyRelation : IEntity<ulong>
/// <param name="removeFriend"> 是否同时解除好友关系。 </param>
/// <param name="options"> 发送请求时要使用的选项。 </param>
/// <returns> 一个表示异步解除操作的任务。 </returns>
Task UnravelAsync(bool removeFriend, RequestOptions? options = null);
Task UnravelAsync(bool removeFriend = false, RequestOptions? options = null);
}
12 changes: 12 additions & 0 deletions src/Kook.Net.Core/Entities/Users/IIntimacyRelationRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Kook;

/// <summary>
/// 表示一个通用的用户之间的亲密关系请求。
/// </summary>
public interface IIntimacyRelationRequest : IFriendRequest
{
/// <summary>
/// 获取此亲密关系的关系类型。
/// </summary>
IntimacyRelationType? IntimacyType { get; }
}
2 changes: 1 addition & 1 deletion src/Kook.Net.Core/Entities/Users/IUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,5 @@ public interface IUser : IEntity<ulong>, IMentionable, IPresence
/// <param name="removeFriend"> 是否同时移除与此用户的好友关系。 </param>
/// <param name="options"> 发送请求时要使用的选项。 </param>
/// <returns> 一个表示异步解除操作的任务。 </returns>
Task UnravelIntimacyRelationAsync(bool removeFriend, RequestOptions? options = null);
Task UnravelIntimacyRelationAsync(bool removeFriend = false, RequestOptions? options = null);
}
5 changes: 2 additions & 3 deletions src/Kook.Net.Rest/API/Rest/FriendState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@ internal class FriendState
public IntimacyRelationType? IntimacyType { get; set; }

[JsonPropertyName("relation_time")]
[JsonConverter(typeof(NullableDateTimeOffsetUnixTimeMillisecondsConverter))]
[JsonConverter(typeof(NullableDateTimeOffsetUnixTimeSecondsConverter))]
public DateTimeOffset? RelationTime { get; set; }

[JsonPropertyName("relation_status")]
[JsonConverter(typeof(IntimacyStateConverter))]
public IntimacyState? IntimacyState { get; set; }

[JsonPropertyName("friend_status")]
[JsonConverter(typeof(FriendStateConverter))]
public int FriendStatus { get; set; }
public Kook.FriendState FriendStatus { get; set; }

[JsonPropertyName("friend_info")]
public required User User { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions src/Kook.Net.Rest/ClientHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ public static async Task<IReadOnlyCollection<RestIntimacyRelation>> GetIntimacyU
return [..models.Relations.Select(x => RestIntimacyRelation.Create(client, x))];
}

public static async Task<IReadOnlyCollection<RestFriendRequest>> GetIntimacyRequestsAsync(BaseKookClient client, RequestOptions? options)
public static async Task<IReadOnlyCollection<RestIntimacyRelationRequest>> GetIntimacyRequestsAsync(BaseKookClient client, RequestOptions? options)
{
GetFriendStatesResponse models = await client.ApiClient.GetFriendStatesAsync(null, options).ConfigureAwait(false);
return [..models.FriendRequests.Select(x => RestFriendRequest.Create(client, x))];
return [..models.RelationRequests.Select(x => RestIntimacyRelationRequest.Create(client, x))];
}

public static async Task<string> CreateAssetAsync(BaseKookClient client, Stream stream, string filename, RequestOptions? options)
Expand Down
30 changes: 14 additions & 16 deletions src/Kook.Net.Rest/Entities/Users/RestFriendRequest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using Model = Kook.API.Rest.FriendState;

namespace Kook.Rest;
Expand All @@ -10,23 +9,24 @@ namespace Kook.Rest;
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class RestFriendRequest : RestEntity<ulong>, IFriendRequest
{
private readonly bool _isIntimacyRelation;

/// <inheritdoc />
public IUser User { get; internal set; }

/// <inheritdoc />
internal RestFriendRequest(BaseKookClient kook, Model model)
: base(kook, model.Id)
internal RestFriendRequest(BaseKookClient kook, ulong id)
: base(kook, id)
{
_isIntimacyRelation = model.IntimacyState is IntimacyState.Pending;
Update(model);
User = null!;
}

internal static RestFriendRequest Create(BaseKookClient kook, Model model) => new(kook, model);
internal static RestFriendRequest Create(BaseKookClient kook, Model model)
{
RestFriendRequest entity = new(kook, model.Id);
entity.Update(model);
return entity;
}

[MemberNotNull(nameof(User))]
internal void Update(Model model)
internal virtual void Update(Model model)
{
User = RestUser.Create(Kook, model.User);
}
Expand All @@ -36,12 +36,10 @@ internal void Update(Model model)
(User.IsBot ?? false ? ", Bot" : "")}{(User.IsSystemUser ? ", System" : "")})";

/// <inheritdoc />
public Task AcceptAsync(RequestOptions? options = null) => _isIntimacyRelation
? UserHelper.HandleIntimacyRequestAsync(this, true, Kook, options)
: UserHelper.HandleFriendRequestAsync(this, true, Kook, options);
public virtual Task AcceptAsync(RequestOptions? options = null) =>
UserHelper.HandleFriendRequestAsync(this, true, Kook, options);

/// <inheritdoc />
public Task DeclineAsync(RequestOptions? options = null) => _isIntimacyRelation
? UserHelper.HandleIntimacyRequestAsync(this, false, Kook, options)
: UserHelper.HandleFriendRequestAsync(this, false, Kook, options);
public virtual Task DeclineAsync(RequestOptions? options = null) =>
UserHelper.HandleFriendRequestAsync(this, false, Kook, options);
}
2 changes: 1 addition & 1 deletion src/Kook.Net.Rest/Entities/Users/RestIntimacyRelation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ internal void Update(API.Rest.FriendState model)
(User.IsBot ?? false ? ", Bot" : "")}{(User.IsSystemUser ? ", System" : "")}, {RelationType})";

/// <inheritdoc />
public Task UnravelAsync(bool removeFriend, RequestOptions? options = null) =>
public Task UnravelAsync(bool removeFriend = false, RequestOptions? options = null) =>
User.UnravelIntimacyRelationAsync(removeFriend, options);
}
35 changes: 35 additions & 0 deletions src/Kook.Net.Rest/Entities/Users/RestIntimacyRelationRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace Kook.Rest;

/// <summary>
/// 表示一个基于 REST 的亲密关系请求。
/// </summary>
public class RestIntimacyRelationRequest : RestFriendRequest, IIntimacyRelationRequest
{
internal RestIntimacyRelationRequest(BaseKookClient kook, ulong id)
: base(kook, id)
{ }

/// <inheritdoc />
public IntimacyRelationType? IntimacyType { get; internal set; }

internal static new RestIntimacyRelationRequest Create(BaseKookClient kook, API.Rest.FriendState model)
{
RestIntimacyRelationRequest entity = new(kook, model.Id);
entity.Update(model);
return entity;
}

internal override void Update(API.Rest.FriendState model)
{
base.Update(model);
IntimacyType = model.IntimacyType;
}

/// <inheritdoc />
public override Task AcceptAsync(RequestOptions? options = null) =>
UserHelper.HandleIntimacyRequestAsync(this, true, Kook, options);

/// <inheritdoc />
public override Task DeclineAsync(RequestOptions? options = null) =>
UserHelper.HandleIntimacyRequestAsync(this, false, Kook, options);
}
2 changes: 1 addition & 1 deletion src/Kook.Net.Rest/Entities/Users/RestUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public Task RequestIntimacyRelationAsync(IntimacyRelationType relationType, Requ
UserHelper.RequestIntimacyRelationAsync(this, relationType, Kook, options);

/// <inheritdoc />
public Task UnravelIntimacyRelationAsync(bool removeFriend, RequestOptions? options = null) =>
public Task UnravelIntimacyRelationAsync(bool removeFriend = false, RequestOptions? options = null) =>
UserHelper.UnravelIntimacyRelationAsync(this, removeFriend, Kook, options);

#endregion
Expand Down
2 changes: 1 addition & 1 deletion src/Kook.Net.Rest/KookRestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public Task<IReadOnlyCollection<RestIntimacyRelation>> GetIntimacyRelationsAsync
/// </summary>
/// <param name="options"> 发送请求时要使用的选项。 </param>
/// <returns> 一个表示异步获取操作的任务。任务的结果是所有请求与当前用户建立亲密关系的用户。 </returns>
public Task<IReadOnlyCollection<RestFriendRequest>> GetIntimacyRelationRequestsAsync(RequestOptions? options = null) =>
public Task<IReadOnlyCollection<RestIntimacyRelationRequest>> GetIntimacyRelationRequestsAsync(RequestOptions? options = null) =>
ClientHelper.GetIntimacyRequestsAsync(this, options);

#endregion
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Kook.Net.Converters;

internal class NullableDateTimeOffsetUnixTimeSecondsConverter : JsonConverter<DateTimeOffset?>
{
/// <inheritdoc />
public override bool HandleNull => true;

public override DateTimeOffset? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
return null;
long tick = reader.GetInt64();
if (tick == 0)
return null;

return DateTimeOffset.FromUnixTimeSeconds(tick);
}

public override void Write(Utf8JsonWriter writer, DateTimeOffset? value, JsonSerializerOptions options)
{
#if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
if (!value.HasValue || value == DateTimeOffset.MinValue || value == DateTimeOffset.UnixEpoch)
#else
if (!value.HasValue || value == DateTimeOffset.MinValue || value == new DateTimeOffset(621355968000000000L, TimeSpan.Zero))
#endif
writer.WriteNullValue();
else
writer.WriteNumberValue(value.Value.ToUnixTimeSeconds());
}
}
2 changes: 1 addition & 1 deletion src/Kook.Net.WebSocket/Entities/Users/SocketUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public Task RequestIntimacyRelationAsync(IntimacyRelationType relationType, Requ
UserHelper.RequestIntimacyRelationAsync(this, relationType, Kook, options);

/// <inheritdoc />
public Task UnravelIntimacyRelationAsync(bool removeFriend, RequestOptions? options = null) =>
public Task UnravelIntimacyRelationAsync(bool removeFriend = false, RequestOptions? options = null) =>
UserHelper.UnravelIntimacyRelationAsync(this, removeFriend, Kook, options);

/// <summary>
Expand Down

0 comments on commit 9bb31bb

Please sign in to comment.