Skip to content

Commit

Permalink
Added media channel, restructure forum channel, refactor forum tag ->…
Browse files Browse the repository at this point in the history
… channel tag
  • Loading branch information
AnotherZane committed Jul 28, 2024
1 parent 2f2d8b0 commit da1e01a
Show file tree
Hide file tree
Showing 16 changed files with 168 additions and 126 deletions.
Original file line number Diff line number Diff line change
@@ -1,38 +1,10 @@
using System;
using System.Collections.Generic;

namespace Disqord;

/// <summary>
/// Represents a guild forum channel.
/// </summary>
public interface IForumChannel : IThreadParentChannel, ITopicChannel, ISlowmodeChannel
{
/// <summary>
/// Gets the ID of the last thread created in this channel.
/// </summary>
Snowflake? LastThreadId { get; }

/// <summary>
/// Gets the available tags that can be applied to threads in this channel.
/// </summary>
IReadOnlyList<IForumTag> Tags { get; }

/// <summary>
/// Gets the emoji that can be reacted with by default to threads in this channel.
/// </summary>
IEmoji? DefaultReactionEmoji { get; }

/// <summary>
/// Gets the default slowmode applied to threads upon their creation in this channel.
/// </summary>
TimeSpan DefaultThreadSlowmode { get; }

/// <summary>
/// Gets the default sort order of posts in this channel.
/// </summary>
ForumSortOrder? DefaultSortOrder { get; }

/// <summary>
/// Gets the default layout of posts in this channel.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Disqord;

/// <summary>
/// Represents a tag that can be applied to threads in a forum channel.
/// Represents a tag that can be applied to threads in a forum or media channel.
/// </summary>
public interface IForumTag : IIdentifiableEntity, INamableEntity, IJsonUpdatable<ForumTagJsonModel>
public interface IChannelTag : IIdentifiableEntity, INamableEntity, IJsonUpdatable<ChannelTagJsonModel>
{
/// <summary>
/// Gets whether this tag can only be applied to threads
Expand Down
35 changes: 35 additions & 0 deletions src/Disqord.Core/Entities/Core/Channels/Guild/IMediaChannel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;

namespace Disqord;

/// <summary>
/// Represents a guild media channel.
/// </summary>
public interface IMediaChannel : IThreadParentChannel, ITopicChannel, ISlowmodeChannel
{
/// <summary>
/// Gets the ID of the last thread created in this channel.
/// </summary>
Snowflake? LastThreadId { get; }

/// <summary>
/// Gets the available tags that can be applied to threads in this channel.
/// </summary>
IReadOnlyList<IChannelTag> Tags { get; }

/// <summary>
/// Gets the emoji that can be reacted with by default to threads in this channel.
/// </summary>
IEmoji? DefaultReactionEmoji { get; }

/// <summary>
/// Gets the default slowmode applied to threads upon their creation in this channel.
/// </summary>
TimeSpan DefaultThreadSlowmode { get; }

/// <summary>
/// Gets the default sort order of posts in this channel.
/// </summary>
ForumSortOrder? DefaultSortOrder { get; }
}
8 changes: 4 additions & 4 deletions src/Disqord.Core/Entities/Local/Guild/Forums/LocalForumTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Disqord;
/// you must provide the previous tags with their original values.
/// You can use <see cref="CreateFrom"/> for that purpose.
/// </remarks>
public class LocalForumTag : ILocalConstruct<LocalForumTag>, IJsonConvertible<ForumTagJsonModel>
public class LocalForumTag : ILocalConstruct<LocalForumTag>, IJsonConvertible<ChannelTagJsonModel>
{
/// <summary>
/// Gets or sets the ID of this tag.
Expand Down Expand Up @@ -68,11 +68,11 @@ public virtual LocalForumTag Clone()
}

/// <inheritdoc/>
public ForumTagJsonModel ToModel()
public ChannelTagJsonModel ToModel()
{
OptionalGuard.HasValue(Name);

var model = new ForumTagJsonModel
var model = new ChannelTagJsonModel
{
Id = Id,
Name = Name.Value,
Expand Down Expand Up @@ -107,7 +107,7 @@ public ForumTagJsonModel ToModel()
/// <returns>
/// The output <see cref="LocalForumTag"/>.
/// </returns>
public static LocalForumTag CreateFrom(IForumTag tag)
public static LocalForumTag CreateFrom(IChannelTag tag)
{
return new LocalForumTag
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ protected TransientGuildChannel(IClient client, ChannelJsonModel model)
return new TransientStageChannel(client, model);

case ChannelType.Forum:
return new TransientForumChannel(client, model);

case ChannelType.Media:
return new TransientForumChannel(client, model);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Disqord;

/// <inheritdoc cref="IForumTag"/>
public class TransientForumTag : TransientEntity<ForumTagJsonModel>, IForumTag
/// <inheritdoc cref="IChannelTag"/>
public class TransientChannelTag : TransientEntity<ChannelTagJsonModel>, IChannelTag
{
/// <inheritdoc/>
public Snowflake Id => Model.Id.Value;
Expand Down Expand Up @@ -32,7 +32,7 @@ public IEmoji Emoji

private IEmoji? _emoji;

public TransientForumTag(ForumTagJsonModel model)
public TransientChannelTag(ChannelTagJsonModel model)
: base(model)
{ }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Disqord.Models;
using Qommon;

namespace Disqord;

/// <inheritdoc cref="IForumChannel"/>
public class TransientForumChannel : TransientMediaChannel, IForumChannel
{
/// <inheritdoc/>
public ForumLayout DefaultLayout => Model.DefaultForumLayout.GetValueOrDefault();

public TransientForumChannel(IClient client, ChannelJsonModel model)
: base(client, model)
{ }
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

namespace Disqord;

/// <inheritdoc cref="IForumChannel"/>
public class TransientForumChannel : TransientCategorizableGuildChannel, IForumChannel
/// <inheritdoc cref="IMediaChannel"/>
public class TransientMediaChannel : TransientCategorizableGuildChannel, IMediaChannel
{
/// <inheritdoc/>
public string Topic => Model.Topic.Value;
Expand All @@ -25,20 +25,20 @@ public class TransientForumChannel : TransientCategorizableGuildChannel, IForumC
public Snowflake? LastThreadId => Model.LastMessageId.GetValueOrDefault();

/// <inheritdoc/>
public IReadOnlyList<IForumTag> Tags
public IReadOnlyList<IChannelTag> Tags
{
get
{
if (!Model.AvailableTags.HasValue)
return ReadOnlyList<IForumTag>.Empty;
return ReadOnlyList<IChannelTag>.Empty;

if (_availableTags != null)
return _availableTags;

return _availableTags = Model.AvailableTags.Value.ToReadOnlyList(model => new TransientForumTag(model));
return _availableTags = Model.AvailableTags.Value.ToReadOnlyList(model => new TransientChannelTag(model));
}
}
private IReadOnlyList<IForumTag>? _availableTags;
private IReadOnlyList<IChannelTag>? _availableTags;

/// <inheritdoc/>
public IEmoji? DefaultReactionEmoji
Expand Down Expand Up @@ -72,10 +72,7 @@ public IEmoji? DefaultReactionEmoji
/// <inheritdoc/>
public ForumSortOrder? DefaultSortOrder => Model.DefaultSortOrder.GetValueOrDefault();

/// <inheritdoc/>
public ForumLayout DefaultLayout => Model.DefaultForumLayout.GetValueOrDefault();

public TransientForumChannel(IClient client, ChannelJsonModel model)
public TransientMediaChannel(IClient client, ChannelJsonModel model)
: base(client, model)
{ }
}
2 changes: 1 addition & 1 deletion src/Disqord.Core/Models/ChannelJsonModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public class ChannelJsonModel : JsonModel
public Optional<GuildChannelFlags> Flags;

[JsonProperty("available_tags")]
public Optional<ForumTagJsonModel[]> AvailableTags;
public Optional<ChannelTagJsonModel[]> AvailableTags;

[JsonProperty("applied_tags")]
public Optional<Snowflake[]> AppliedTags;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Disqord.Models;

public class ForumTagJsonModel : JsonModel
public class ChannelTagJsonModel : JsonModel
{
[JsonProperty("id")]
public Optional<Snowflake> Id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,12 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Disqord.Models;
using Qommon;
using Qommon.Collections.ReadOnly;

namespace Disqord.Gateway;

/// <inheritdoc cref="IForumChannel"/>
public class CachedForumChannel : CachedCategorizableGuildChannel, IForumChannel
public class CachedForumChannel : CachedMediaChannel, IForumChannel
{
/// <inheritdoc/>
public string? Topic { get; private set; }

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

/// <inheritdoc/>
public TimeSpan Slowmode { get; private set; }

/// <inheritdoc/>
public TimeSpan DefaultAutomaticArchiveDuration { get; private set; }

/// <inheritdoc/>
public Snowflake? LastThreadId { get; private set; }

/// <inheritdoc/>
public IEmoji? DefaultReactionEmoji { get; private set; }

/// <inheritdoc/>
public IReadOnlyList<IForumTag> Tags { get; private set; } = ReadOnlyList<IForumTag>.Empty;

/// <inheritdoc/>
public TimeSpan DefaultThreadSlowmode { get; private set; }

/// <inheritdoc/>
public ForumSortOrder? DefaultSortOrder { get; private set; }

/// <inheritdoc/>
public ForumLayout DefaultLayout { get; private set; }

Expand All @@ -48,49 +18,6 @@ public CachedForumChannel(IGatewayClient client, ChannelJsonModel model)
public override void Update(ChannelJsonModel model)
{
base.Update(model);

if (model.Topic.HasValue)
Topic = model.Topic.Value;

if (model.Nsfw.HasValue)
IsAgeRestricted = model.Nsfw.Value;

if (model.RateLimitPerUser.HasValue)
Slowmode = TimeSpan.FromSeconds(model.RateLimitPerUser.Value);

DefaultAutomaticArchiveDuration = TimeSpan.FromMinutes(model.DefaultAutoArchiveDuration.GetValueOrDefault(1440));

if (model.LastMessageId.HasValue)
LastThreadId = model.LastMessageId.Value;

if (model.DefaultReactionEmoji.HasValue)
{
IEmoji? defaultReactionEmoji;
var defaultReactionEmojiModel = model.DefaultReactionEmoji.GetValueOrDefault();
if (defaultReactionEmojiModel != null)
{
if (defaultReactionEmojiModel.EmojiId != null)
{
defaultReactionEmoji = new TransientCustomEmoji(defaultReactionEmojiModel.EmojiId.Value);
}
else
{
defaultReactionEmoji = new TransientEmoji(defaultReactionEmojiModel.EmojiName!);
}
}
else
{
defaultReactionEmoji = null;
}

DefaultReactionEmoji = defaultReactionEmoji;
}

if (model.AvailableTags.HasValue)
Tags = model.AvailableTags.Value.ToReadOnlyList(model => new TransientForumTag(model));

DefaultThreadSlowmode = TimeSpan.FromSeconds(model.DefaultThreadRateLimitPerUser.GetValueOrDefault(0));
DefaultSortOrder = model.DefaultSortOrder.GetValueOrDefault();
DefaultLayout = model.DefaultForumLayout.GetValueOrDefault();
}
}
Loading

0 comments on commit da1e01a

Please sign in to comment.