Skip to content
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

Add IsInvitable and CreatedAt to threads #2153

Merged
merged 1 commit into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Discord.Net.Core/Entities/Channels/IThreadChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ public interface IThreadChannel : ITextChannel
/// </summary>
int MessageCount { get; }

/// <summary>
/// Gets whether non-moderators can add other non-moderators to a thread.
/// </summary>
/// <remarks>
/// This property is only available on private threads.
/// </remarks>
bool? IsInvitable { get; }

/// <summary>
/// Gets when the thread was created.
/// </summary>
/// <remarks>
/// This property is only populated for threads created after 2022-01-09, hence the default date of this
/// property will be that date.
/// </remarks>
new DateTimeOffset CreatedAt { get; }

/// <summary>
/// Joins the current thread.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions src/Discord.Net.Rest/API/Common/ThreadMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,11 @@ internal class ThreadMetadata

[JsonProperty("locked")]
public Optional<bool> Locked { get; set; }

[JsonProperty("invitable")]
public Optional<bool> Invitable { get; set; }

[JsonProperty("create_timestamp")]
public Optional<DateTimeOffset> CreatedAt { get; set; }
}
}
2 changes: 1 addition & 1 deletion src/Discord.Net.Rest/Entities/Channels/RestChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class RestChannel : RestEntity<ulong>, IChannel, IUpdateable
{
#region RestChannel
/// <inheritdoc />
public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id);
public virtual DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id);

internal RestChannel(BaseDiscordClient discord, ulong id)
: base(discord, id)
Expand Down
16 changes: 13 additions & 3 deletions src/Discord.Net.Rest/Entities/Channels/RestThreadChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,26 @@ public class RestThreadChannel : RestTextChannel, IThreadChannel
/// <inheritdoc/>
public int MessageCount { get; private set; }

/// <inheritdoc/>
public bool? IsInvitable { get; private set; }

/// <inheritdoc cref="IThreadChannel.CreatedAt"/>
public override DateTimeOffset CreatedAt { get; }

/// <summary>
/// Gets the parent text channel id.
/// </summary>
public ulong ParentChannelId { get; private set; }

internal RestThreadChannel(BaseDiscordClient discord, IGuild guild, ulong id)
: base(discord, guild, id) { }
internal RestThreadChannel(BaseDiscordClient discord, IGuild guild, ulong id, DateTimeOffset? createdAt)
: base(discord, guild, id)
{
CreatedAt = createdAt ?? new DateTimeOffset(2022, 1, 9, 0, 0, 0, TimeSpan.Zero);
}

internal new static RestThreadChannel Create(BaseDiscordClient discord, IGuild guild, Model model)
{
var entity = new RestThreadChannel(discord, guild, model.Id);
var entity = new RestThreadChannel(discord, guild, model.Id, model.ThreadMetadata.GetValueOrDefault()?.CreatedAt.GetValueOrDefault());
entity.Update(model);
return entity;
}
Expand All @@ -57,6 +66,7 @@ internal override void Update(Model model)

if (model.ThreadMetadata.IsSpecified)
{
IsInvitable = model.ThreadMetadata.Value.Invitable.ToNullable();
IsArchived = model.ThreadMetadata.Value.Archived;
AutoArchiveDuration = model.ThreadMetadata.Value.AutoArchiveDuration;
ArchiveTimestamp = model.ThreadMetadata.Value.ArchiveTimestamp;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public abstract class SocketChannel : SocketEntity<ulong>, IChannel
/// <summary>
/// Gets when the channel is created.
/// </summary>
public DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id);
public virtual DateTimeOffset CreatedAt => SnowflakeUtils.FromSnowflake(Id);
/// <summary>
/// Gets a collection of users from the WebSocket cache.
/// </summary>
Expand Down
17 changes: 13 additions & 4 deletions src/Discord.Net.WebSocket/Entities/Channels/SocketThreadChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public bool IsPrivateThread
/// <summary>
/// Gets the parent channel this thread resides in.
/// </summary>
public SocketTextChannel ParentChannel { get; private set; }
public SocketGuildChannel ParentChannel { get; private set; }

/// <inheritdoc/>
public int MessageCount { get; private set; }
Expand All @@ -64,6 +64,12 @@ public bool IsPrivateThread
/// <inheritdoc/>
public bool IsLocked { get; private set; }

/// <inheritdoc/>
public bool? IsInvitable { get; private set; }

/// <inheritdoc cref="IThreadChannel.CreatedAt"/>
public override DateTimeOffset CreatedAt { get; }

/// <summary>
/// Gets a collection of cached users within this thread.
/// </summary>
Expand All @@ -78,17 +84,19 @@ public bool IsPrivateThread

private readonly object _downloadLock = new object();

internal SocketThreadChannel(DiscordSocketClient discord, SocketGuild guild, ulong id, SocketTextChannel parent)
internal SocketThreadChannel(DiscordSocketClient discord, SocketGuild guild, ulong id, SocketGuildChannel parent,
DateTimeOffset? createdAt)
: base(discord, id, guild)
{
ParentChannel = parent;
_members = new ConcurrentDictionary<ulong, SocketThreadUser>();
CreatedAt = createdAt ?? new DateTimeOffset(2022, 1, 9, 0, 0, 0, TimeSpan.Zero);
}

internal new static SocketThreadChannel Create(SocketGuild guild, ClientState state, Model model)
{
var parent = (SocketTextChannel)guild.GetChannel(model.CategoryId.Value);
var entity = new SocketThreadChannel(guild.Discord, guild, model.Id, parent);
var parent = guild.GetChannel(model.CategoryId.Value);
var entity = new SocketThreadChannel(guild.Discord, guild, model.Id, parent, model.ThreadMetadata.GetValueOrDefault()?.CreatedAt.ToNullable());
entity.Update(state, model);
return entity;
}
Expand All @@ -103,6 +111,7 @@ internal override void Update(ClientState state, Model model)

if (model.ThreadMetadata.IsSpecified)
{
IsInvitable = model.ThreadMetadata.Value.Invitable.ToNullable();
IsArchived = model.ThreadMetadata.Value.Archived;
ArchiveTimestamp = model.ThreadMetadata.Value.ArchiveTimestamp;
AutoArchiveDuration = model.ThreadMetadata.Value.AutoArchiveDuration;
Expand Down