-
-
Notifications
You must be signed in to change notification settings - Fork 36
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 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 fcf9029
Add Invite.Roles property
KubaZ2 4f33699
Fix the names of invite RestClient methods
KubaZ2 7421bd9
Fix filename for RestInvite
KubaZ2 398de2d
Update invite receive objects
KubaZ2 d49346a
Add a missing flags attribute
KubaZ2 93af2e8
Do not rent a buffer for UserIdsStream
KubaZ2 8db6130
Only allow for sending InviteTargetUsersProperties once
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
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,7 @@ | ||
| namespace NetCord; | ||
|
|
||
| [Flags] | ||
| public enum InviteFlags | ||
| { | ||
| IsGuestInvite = 1 << 0, | ||
| } | ||
|
KubaZ2 marked this conversation as resolved.
|
||
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,140 @@ | ||
| using System.Buffers.Text; | ||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| namespace NetCord.Rest; | ||
|
|
||
| [GenerateMethodsForProperties] | ||
| public partial class InviteTargetUsersProperties : IHttpSerializable | ||
| { | ||
| private readonly Stream _stream; | ||
| private byte _read; | ||
|
|
||
| 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() | ||
| { | ||
| if (Interlocked.Exchange(ref _read, 1) is 1) | ||
| ThrowAlreadySent(); | ||
|
|
||
| return new StreamContent(_stream); | ||
| } | ||
|
|
||
| [DoesNotReturn] | ||
| private static void ThrowAlreadySent() | ||
| { | ||
| throw new InvalidOperationException("The invite target users have already been sent."); | ||
| } | ||
|
|
||
|
KubaZ2 marked this conversation as resolved.
|
||
| private sealed class UserIdsStream(IEnumerable<ulong> userIds) : Stream | ||
| { | ||
| private readonly IEnumerator<ulong> _enumerator = userIds.GetEnumerator(); | ||
| private readonly byte[] _buffer = new byte[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) | ||
| { | ||
|
KubaZ2 marked this conversation as resolved.
KubaZ2 marked this conversation as resolved.
|
||
| 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(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
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
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.