| @@ -541,7 +541,7 @@ namespace Discord | |||
| uint denyValue = deny?.RawValue ?? 0; | |||
| bool changed = false; | |||
| var perms = channel.PermissionOverwrites.Where(x => x.Type != targetType || x.TargetId != targetId).FirstOrDefault(); | |||
| var perms = channel.PermissionOverwrites.Where(x => x.TargetType != targetType || x.TargetId != targetId).FirstOrDefault(); | |||
| if (allowValue != 0 || denyValue != 0) | |||
| { | |||
| await _api.SetChannelPermissions(channel.Id, targetId, targetType, allowValue, denyValue); | |||
| @@ -567,7 +567,7 @@ namespace Discord | |||
| await _api.DeleteChannelPermissions(channel.Id, targetId); | |||
| if (perms != null) | |||
| { | |||
| channel._permissionOverwrites = channel.PermissionOverwrites.Where(x => x.Type != targetType || x.TargetId != targetId).ToArray(); | |||
| channel._permissionOverwrites = channel.PermissionOverwrites.Where(x => x.TargetType != targetType || x.TargetId != targetId).ToArray(); | |||
| changed = true; | |||
| } | |||
| } | |||
| @@ -614,11 +614,11 @@ namespace Discord | |||
| try | |||
| { | |||
| var perms = channel.PermissionOverwrites.Where(x => x.Type != idType || x.TargetId != userOrRoleId).FirstOrDefault(); | |||
| var perms = channel.PermissionOverwrites.Where(x => x.TargetType != idType || x.TargetId != userOrRoleId).FirstOrDefault(); | |||
| await _api.DeleteChannelPermissions(channel.Id, userOrRoleId).ConfigureAwait(false); | |||
| if (perms != null) | |||
| { | |||
| channel.PermissionOverwrites.Where(x => x.Type != idType || x.TargetId != userOrRoleId).ToArray(); | |||
| channel.PermissionOverwrites.Where(x => x.TargetType != idType || x.TargetId != userOrRoleId).ToArray(); | |||
| if (idType == PermissionTarget.Role) | |||
| channel.InvalidatePermissionsCache(); | |||
| @@ -9,14 +9,14 @@ namespace Discord | |||
| { | |||
| public sealed class PermissionOverwrite | |||
| { | |||
| public string Type { get; } | |||
| public string TargetType { get; } | |||
| public string TargetId { get; } | |||
| public PackedChannelPermissions Allow { get; } | |||
| public PackedChannelPermissions Deny { get; } | |||
| internal PermissionOverwrite(string type, string targetId, uint allow, uint deny) | |||
| { | |||
| Type = type; | |||
| TargetType = type; | |||
| TargetId = targetId; | |||
| Allow = new PackedChannelPermissions(allow); | |||
| Deny = new PackedChannelPermissions( deny); | |||
| @@ -176,9 +176,10 @@ namespace Discord | |||
| PackedChannelPermissions permissions; | |||
| if (!_permissions.TryGetValue(channelId, out permissions)) return; | |||
| uint newPermissions = 0x0; | |||
| uint oldPermissions = permissions.RawValue; | |||
| if (UserId == server.OwnerId) | |||
| newPermissions = PackedChannelPermissions.Mask; | |||
| newPermissions = PackedChannelPermissions.All.RawValue; | |||
| else | |||
| { | |||
| if (channel == null) return; | |||
| @@ -187,24 +188,26 @@ namespace Discord | |||
| var orderedRoles = Roles.OrderBy(x => x.Id); | |||
| foreach (var serverRole in orderedRoles) | |||
| newPermissions |= serverRole.Permissions.RawValue; | |||
| foreach (var denyRole in channelOverwrites.Where(x => x.Type == PermissionTarget.Role && x.Deny.RawValue != 0 && orderedRoles.Any(y => y.Id == x.TargetId))) | |||
| foreach (var denyRole in channelOverwrites.Where(x => x.TargetType == PermissionTarget.Role && x.Deny.RawValue != 0 && orderedRoles.Any(y => y.Id == x.TargetId))) | |||
| newPermissions &= ~denyRole.Deny.RawValue; | |||
| foreach (var allowRole in channelOverwrites.Where(x => x.Type == PermissionTarget.Role && x.Allow.RawValue != 0 && orderedRoles.Any(y => y.Id == x.TargetId))) | |||
| foreach (var allowRole in channelOverwrites.Where(x => x.TargetType == PermissionTarget.Role && x.Allow.RawValue != 0 && orderedRoles.Any(y => y.Id == x.TargetId))) | |||
| newPermissions |= allowRole.Allow.RawValue; | |||
| foreach (var denyMembers in channelOverwrites.Where(x => x.Type == PermissionTarget.Member && x.TargetId == UserId && x.Deny.RawValue != 0)) | |||
| foreach (var denyMembers in channelOverwrites.Where(x => x.TargetType == PermissionTarget.Member && x.TargetId == UserId && x.Deny.RawValue != 0)) | |||
| newPermissions &= ~denyMembers.Deny.RawValue; | |||
| foreach (var allowMembers in channelOverwrites.Where(x => x.Type == PermissionTarget.Member && x.TargetId == UserId && x.Allow.RawValue != 0)) | |||
| foreach (var allowMembers in channelOverwrites.Where(x => x.TargetType == PermissionTarget.Member && x.TargetId == UserId && x.Allow.RawValue != 0)) | |||
| newPermissions |= allowMembers.Allow.RawValue; | |||
| if (((newPermissions >> (PackedChannelPermissions.GlobalBit - 1)) & 1) == 1) | |||
| newPermissions = PackedChannelPermissions.Mask; | |||
| } | |||
| if (permissions.RawValue != newPermissions) | |||
| { | |||
| permissions.SetRawValue(newPermissions); | |||
| permissions.SetRawValue(newPermissions); | |||
| if (permissions.General_ManagePermissions) | |||
| permissions.SetRawValue(PackedChannelPermissions.All.RawValue); | |||
| else if (server.DefaultChannelId == channelId) | |||
| permissions.Text_ReadMessages = true; | |||
| if (permissions.RawValue != oldPermissions) | |||
| channel.InvalidMembersCache(); | |||
| } | |||
| } | |||
| //TODO: Add GetServerPermissions | |||
| public PackedChannelPermissions GetPermissions(Channel channel) | |||
| @@ -4,8 +4,15 @@ namespace Discord | |||
| { | |||
| public sealed class PackedServerPermissions : PackedPermissions | |||
| { | |||
| internal const int GlobalBit = 4; //ManagePermissions implicitly gives all permissions | |||
| public static readonly uint Mask = Convert.ToUInt32("00000011111100111111110000111111", 2); | |||
| public static PackedServerPermissions None { get; } | |||
| public static PackedServerPermissions All { get; } | |||
| static PackedServerPermissions() | |||
| { | |||
| None = new PackedServerPermissions(); | |||
| None.Lock(); | |||
| All = new PackedServerPermissions(Convert.ToUInt32("00000011111100111111110000111111", 2)); | |||
| All.Lock(); | |||
| } | |||
| public PackedServerPermissions(uint rawValue = 0) : base(rawValue) { } | |||
| @@ -25,8 +32,15 @@ namespace Discord | |||
| public sealed class PackedChannelPermissions : PackedPermissions | |||
| { | |||
| internal const int GlobalBit = 4; //ManagePermissions implicitly gives all permissions | |||
| public static readonly uint Mask = Convert.ToUInt32("00000011111100111111110000011001", 2); | |||
| public static PackedChannelPermissions None { get; } | |||
| public static PackedChannelPermissions All { get; } | |||
| static PackedChannelPermissions() | |||
| { | |||
| None = new PackedChannelPermissions(); | |||
| None.Lock(); | |||
| All = new PackedChannelPermissions(Convert.ToUInt32("00000011111100111111110000011001", 2)); | |||
| All.Lock(); | |||
| } | |||
| public PackedChannelPermissions(uint rawValue = 0) : base(rawValue) { } | |||