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

V4: State and Cache providers #2251

Open
wants to merge 12 commits into
base: 4.0
Choose a base branch
from
111 changes: 111 additions & 0 deletions src/Discord.Net.Core/Cache/CacheableEntityExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord
{
internal static class CacheableEntityExtensions
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason why these methods arent returning the generic type parameters? We could downcast it later.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, its just whats there atm. it could change once I implement the rest of the entities

{
public static IActivityModel ToModel<TModel>(this RichGame richGame) where TModel : IActivityModel, new()
{
return new TModel()
{
ApplicationId = richGame.ApplicationId,
SmallImage = richGame.SmallAsset?.ImageId,
SmallText = richGame.SmallAsset?.Text,
LargeImage = richGame.LargeAsset?.ImageId,
LargeText = richGame.LargeAsset?.Text,
Details = richGame.Details,
Flags = richGame.Flags,
Name = richGame.Name,
Type = richGame.Type,
JoinSecret = richGame.Secrets?.Join,
SpectateSecret = richGame.Secrets?.Spectate,
MatchSecret = richGame.Secrets?.Match,
State = richGame.State,
PartyId = richGame.Party?.Id,
PartySize = richGame.Party?.Members != null && richGame.Party?.Capacity != null
? new long[] { richGame.Party.Members, richGame.Party.Capacity }
: null,
TimestampEnd = richGame.Timestamps?.End,
TimestampStart = richGame.Timestamps?.Start
};
}

public static IActivityModel ToModel<TModel>(this SpotifyGame spotify) where TModel : IActivityModel, new()
{
return new TModel()
{
Name = spotify.Name,
SessionId = spotify.SessionId,
SyncId = spotify.TrackId,
LargeText = spotify.AlbumTitle,
Details = spotify.TrackTitle,
State = string.Join(";", spotify.Artists),
TimestampEnd = spotify.EndsAt,
TimestampStart = spotify.StartedAt,
LargeImage = spotify.AlbumArt,
Type = ActivityType.Listening,
Flags = spotify.Flags,
};
}

public static IActivityModel ToModel<TModel, TEmoteModel>(this CustomStatusGame custom)
where TModel : IActivityModel, new()
where TEmoteModel : IEmojiModel, new()
{
return new TModel
{
Id = "custom",
Type = ActivityType.CustomStatus,
Name = custom.Name,
State = custom.State,
Emoji = custom.Emote.ToModel<TEmoteModel>(),
CreatedAt = custom.CreatedAt
};
}

public static IActivityModel ToModel<TModel>(this StreamingGame stream) where TModel : IActivityModel, new()
{
return new TModel
{
Name = stream.Name,
Url = stream.Url,
Flags = stream.Flags,
Details = stream.Details
};
}

public static IEmojiModel ToModel<TModel>(this IEmote emote) where TModel : IEmojiModel, new()
{
if (emote == null)
return null;

var model = new TModel()
{
Name = emote.Name
};

if(emote is GuildEmote guildEmote)
{
model.Id = guildEmote.Id;
model.IsAnimated = guildEmote.Animated;
model.IsAvailable = guildEmote.IsAvailable;
model.IsManaged = guildEmote.IsManaged;
model.CreatorId = guildEmote.CreatorId;
model.RequireColons = guildEmote.RequireColons;
model.Roles = guildEmote.RoleIds.ToArray();
}

if(emote is Emote e)
{
model.IsAnimated = e.Animated;
model.Id = e.Id;
}

return model;
}
}
}
22 changes: 22 additions & 0 deletions src/Discord.Net.Core/Cache/ICached.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord
{
internal interface ICached<TType> : ICached, IDisposable
{
void Update(TType model);

TType ToModel();

TResult ToModel<TResult>() where TResult : TType, new();
}

public interface ICached
{
bool IsFreed { get; }
}
}
21 changes: 21 additions & 0 deletions src/Discord.Net.Core/Cache/Models/Emoji/IEmojiModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord
{
public interface IEmojiModel
{
ulong? Id { get; set; }
string Name { get; set; }
ulong[] Roles { get; set; }
bool RequireColons { get; set; }
bool IsManaged { get; set; }
bool IsAnimated { get; set; }
bool IsAvailable { get; set; }

ulong? CreatorId { get; set; }
}
}
13 changes: 13 additions & 0 deletions src/Discord.Net.Core/Cache/Models/IEntityModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord
{
public interface IEntityModel<TId> where TId : IEquatable<TId>
{
TId Id { get; set; }
}
}
48 changes: 48 additions & 0 deletions src/Discord.Net.Core/Cache/Models/Presense/IActivityModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord
{
public interface IActivityModel
{
string Id { get; set; }
string Url { get; set; }
string Name { get; set; }
ActivityType Type { get; set; }
string Details { get; set; }
string State { get; set; }
ActivityProperties Flags { get; set; }
DateTimeOffset CreatedAt { get; set; }
IEmojiModel Emoji { get; set; }
ulong? ApplicationId { get; set; }
string SyncId { get; set; }
string SessionId { get; set; }


#region Assets
string LargeImage { get; set; }
string LargeText { get; set; }
string SmallImage { get; set; }
string SmallText { get; set; }
#endregion

#region Party
string PartyId { get; set; }
long[] PartySize { get; set; }
#endregion

#region Secrets
string JoinSecret { get; set; }
string SpectateSecret { get; set; }
string MatchSecret { get; set; }
#endregion

#region Timestamps
DateTimeOffset? TimestampStart { get; set; }
DateTimeOffset? TimestampEnd { get; set; }
#endregion
}
}
17 changes: 17 additions & 0 deletions src/Discord.Net.Core/Cache/Models/Presense/IPresenceModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord
{
public interface IPresenceModel : IEntityModel<ulong>
{
ulong UserId { get; set; }
ulong? GuildId { get; set; }
UserStatus Status { get; set; }
ClientType[] ActiveClients { get; set; }
IActivityModel[] Activities { get; set; }
}
}
19 changes: 19 additions & 0 deletions src/Discord.Net.Core/Cache/Models/Users/ICurrentUserModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord
{
public interface ICurrentUserModel : IUserModel
{
bool? IsVerified { get; set; }
string Email { get; set; }
bool? IsMfaEnabled { get; set; }
UserProperties Flags { get; set; }
PremiumType PremiumType { get; set; }
string Locale { get; set; }
UserProperties PublicFlags { get; set; }
}
}
22 changes: 22 additions & 0 deletions src/Discord.Net.Core/Cache/Models/Users/IMemberModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord
{
public interface IMemberModel : IEntityModel<ulong>
{
//IUserModel User { get; set; }
string Nickname { get; set; }
string GuildAvatar { get; set; }
ulong[] Roles { get; set; }
DateTimeOffset JoinedAt { get; set; }
DateTimeOffset? PremiumSince { get; set; }
bool IsDeaf { get; set; }
bool IsMute { get; set; }
bool? IsPending { get; set; }
DateTimeOffset? CommunicationsDisabledUntil { get; set; }
}
}
15 changes: 15 additions & 0 deletions src/Discord.Net.Core/Cache/Models/Users/IThreadMemberModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord
{
public interface IThreadMemberModel : IEntityModel<ulong>
{
ulong? ThreadId { get; set; }
ulong? UserId { get; set; }
DateTimeOffset JoinedAt { get; set; }
}
}
16 changes: 16 additions & 0 deletions src/Discord.Net.Core/Cache/Models/Users/IUserModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord
{
public interface IUserModel : IEntityModel<ulong>
{
string Username { get; set; }
string Discriminator { get; set; }
bool? IsBot { get; set; }
string Avatar { get; set; }
}
}
2 changes: 2 additions & 0 deletions src/Discord.Net.Core/Entities/Activities/SpotifyGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ public class SpotifyGame : Game
/// </returns>
public string TrackUrl { get; internal set; }

internal string AlbumArt { get; set; }

internal SpotifyGame() { }

/// <summary>
Expand Down
10 changes: 9 additions & 1 deletion src/Discord.Net.Core/Entities/Emotes/GuildEmote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ public class GuildEmote : Emote
/// </returns>
public bool RequireColons { get; }
/// <summary>
/// Gets whether or not the emote is available.
/// </summary>
/// <remarks>
/// An emote can be unavailable if the guild has lost its boost status.
/// </remarks>
public bool IsAvailable { get; }
/// <summary>
/// Gets the roles that are allowed to use this emoji.
/// </summary>
/// <returns>
Expand All @@ -39,12 +46,13 @@ public class GuildEmote : Emote
/// </returns>
public ulong? CreatorId { get; }

internal GuildEmote(ulong id, string name, bool animated, bool isManaged, bool requireColons, IReadOnlyList<ulong> roleIds, ulong? userId) : base(id, name, animated)
internal GuildEmote(ulong id, string name, bool animated, bool isManaged, bool isAvailable, bool requireColons, IReadOnlyList<ulong> roleIds, ulong? userId) : base(id, name, animated)
{
IsManaged = isManaged;
RequireColons = requireColons;
RoleIds = roleIds;
CreatorId = userId;
IsAvailable = isAvailable;
}

private string DebuggerDisplay => $"{Name} ({Id})";
Expand Down
Loading