-
-
Notifications
You must be signed in to change notification settings - Fork 35
Add community invites #297
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
Open
KubaZ2
wants to merge
10
commits into
alpha
Choose a base branch
from
feature/community-invites
base: alpha
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6194575
Add TargetUsers and RoleIds to InviteProperties
KubaZ2 3a7fbbf
Merge branch 'alpha' into feature/community-invites
KubaZ2 fdecec4
Add more invite endpoints
KubaZ2 3a5cd2a
Merge branch 'alpha' into feature/community-invites
KubaZ2 506679b
Move InviteTargetUsersProperties to a separate file and fix its logic
KubaZ2 60c796f
Dispose stream and fix indentation
KubaZ2 1aea9bb
Add tests for InviteTargetUsersProperties.FromEnumerable
KubaZ2 cab1fb6
Rename a parameter of UpdateInviteTargetUsersAsync
KubaZ2 29bd087
Add a test for GetInviteTargetUsersAsync
KubaZ2 2d15d8e
Dispose a response stream in test
KubaZ2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| using NetCord.Rest.JsonModels; | ||
|
|
||
| namespace NetCord.Rest; | ||
|
|
||
| public class InviteTargetUsersJobStatus(JsonInviteTargetUsersJobStatus jsonModel) : IJsonModel<JsonInviteTargetUsersJobStatus> | ||
| { | ||
| JsonInviteTargetUsersJobStatus IJsonModel<JsonInviteTargetUsersJobStatus>.JsonModel => jsonModel; | ||
|
|
||
| public InviteTargetUsersJobStatusCode Status => jsonModel.Status; | ||
|
|
||
| public int TotalUsers => jsonModel.TotalUsers; | ||
|
|
||
| public int ProcessedUsers => jsonModel.ProcessedUsers; | ||
|
|
||
| public DateTimeOffset CreatedAt => jsonModel.CreatedAt; | ||
|
|
||
| public DateTimeOffset? CompletedAt => jsonModel.CompletedAt; | ||
|
|
||
| public string? ErrorMessage => jsonModel.ErrorMessage; | ||
| } | ||
|
|
||
| public enum InviteTargetUsersJobStatusCode | ||
| { | ||
| Unspecified = 0, | ||
| Processing = 1, | ||
| Completed = 2, | ||
| Failed = 3, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| using System.Buffers; | ||
| using System.Buffers.Text; | ||
|
|
||
| namespace NetCord.Rest; | ||
|
|
||
| [GenerateMethodsForProperties] | ||
| public partial class InviteTargetUsersProperties : IHttpSerializable | ||
| { | ||
| private readonly Stream _stream; | ||
|
|
||
| private InviteTargetUsersProperties(Stream stream) | ||
| { | ||
| _stream = stream; | ||
| } | ||
|
|
||
| public static InviteTargetUsersProperties FromStream(Stream stream) => new(stream); | ||
|
|
||
| public static InviteTargetUsersProperties FromEnumerable(IEnumerable<ulong> userIds) => new(new UserIdsStream(userIds)); | ||
|
|
||
| HttpContent IHttpSerializable.Serialize() => Serialize(); | ||
|
|
||
| internal HttpContent Serialize() => new StreamContent(_stream); | ||
|
|
||
KubaZ2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private sealed class UserIdsStream(IEnumerable<ulong> userIds) : Stream | ||
| { | ||
| private readonly IEnumerator<ulong> _enumerator = userIds.GetEnumerator(); | ||
| private byte[] _buffer = ArrayPool<byte>.Shared.Rent(22); | ||
| private int _startPosition; | ||
| private int _endPosition; | ||
|
|
||
| public override bool CanRead => true; | ||
|
|
||
| public override bool CanSeek => false; | ||
|
|
||
| public override bool CanWrite => false; | ||
|
|
||
| public override long Length => throw new NotSupportedException(); | ||
|
|
||
| public override long Position { get => throw new NotSupportedException(); set => throw new NotSupportedException(); } | ||
|
|
||
| public override void Flush() => throw new NotSupportedException(); | ||
|
|
||
| public override int Read(byte[] buffer, int offset, int count) => Read(new Span<byte>(buffer, offset, count)); | ||
|
|
||
| public override int Read(Span<byte> buffer) | ||
| { | ||
| int totalWritten = 0; | ||
|
|
||
| if (_startPosition != _endPosition) | ||
| { | ||
| var bufferedLength = _endPosition - _startPosition; | ||
|
|
||
| var length = Math.Min(bufferedLength, buffer.Length); | ||
|
|
||
| _buffer.AsSpan(_startPosition, length).CopyTo(buffer); | ||
| _startPosition += length; | ||
|
|
||
| if (length != bufferedLength) | ||
| return length; | ||
|
|
||
| totalWritten += length; | ||
| } | ||
|
|
||
| var newLine = "\r\n"u8; | ||
|
|
||
| while (_enumerator.MoveNext()) | ||
| { | ||
| var userId = _enumerator.Current; | ||
| if (!Utf8Formatter.TryFormat(userId, buffer[totalWritten..], out var written)) | ||
| { | ||
| _ = Utf8Formatter.TryFormat(userId, _buffer, out var writtenToBuffer); | ||
|
|
||
| _buffer.AsSpan(0, _startPosition = written = buffer.Length - totalWritten).CopyTo(buffer[totalWritten..]); | ||
|
|
||
| totalWritten += written; | ||
|
|
||
| newLine.CopyTo(_buffer.AsSpan(writtenToBuffer)); | ||
| _endPosition = writtenToBuffer + newLine.Length; | ||
| break; | ||
| } | ||
|
|
||
| totalWritten += written; | ||
|
|
||
| if (!newLine.TryCopyTo(buffer[totalWritten..])) | ||
| { | ||
| var remaining = buffer.Length - totalWritten; | ||
| if (remaining > 0) | ||
| { | ||
| newLine[..remaining].CopyTo(buffer[totalWritten..]); | ||
| totalWritten += remaining; | ||
| } | ||
|
|
||
| newLine[remaining..].CopyTo(_buffer); | ||
| _startPosition = 0; | ||
| _endPosition = newLine.Length - remaining; | ||
|
|
||
| break; | ||
| } | ||
|
|
||
| totalWritten += newLine.Length; | ||
| } | ||
|
|
||
| return totalWritten; | ||
| } | ||
|
|
||
| public override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | ||
| { | ||
| return ReadAsync(new Memory<byte>(buffer, offset, count), cancellationToken).AsTask(); | ||
| } | ||
|
|
||
| public override ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken = default) | ||
| { | ||
| return new(Read(buffer.Span)); | ||
| } | ||
|
|
||
| public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException(); | ||
| public override void SetLength(long value) => throw new NotSupportedException(); | ||
| public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException(); | ||
|
|
||
| protected override void Dispose(bool disposing) | ||
| { | ||
| if (disposing) | ||
| { | ||
| _enumerator.Dispose(); | ||
| if (Interlocked.Exchange(ref _buffer, null!) is { } buffer) | ||
| ArrayPool<byte>.Shared.Return(buffer); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| using System.Text.Json.Serialization; | ||
|
|
||
| namespace NetCord.Rest.JsonModels; | ||
|
|
||
| public class JsonInviteTargetUsersJobStatus | ||
| { | ||
| [JsonPropertyName("status")] | ||
| public InviteTargetUsersJobStatusCode Status { get; set; } | ||
|
|
||
| [JsonPropertyName("total_users")] | ||
| public int TotalUsers { get; set; } | ||
|
|
||
| [JsonPropertyName("processed_users")] | ||
| public int ProcessedUsers { get; set; } | ||
|
|
||
| [JsonPropertyName("created_at")] | ||
| public DateTimeOffset CreatedAt { get; set; } | ||
|
|
||
| [JsonPropertyName("completed_at")] | ||
| public DateTimeOffset? CompletedAt { get; set; } | ||
|
|
||
| [JsonPropertyName("error_message")] | ||
| public string? ErrorMessage { get; set; } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| [assembly: Parallelize(Scope = ExecutionScope.MethodLevel)] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.