| @@ -541,7 +541,7 @@ namespace Discord | |||||
| uint denyValue = deny?.RawValue ?? 0; | uint denyValue = deny?.RawValue ?? 0; | ||||
| bool changed = false; | 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) | if (allowValue != 0 || denyValue != 0) | ||||
| { | { | ||||
| await _api.SetChannelPermissions(channel.Id, targetId, targetType, allowValue, denyValue); | await _api.SetChannelPermissions(channel.Id, targetId, targetType, allowValue, denyValue); | ||||
| @@ -567,7 +567,7 @@ namespace Discord | |||||
| await _api.DeleteChannelPermissions(channel.Id, targetId); | await _api.DeleteChannelPermissions(channel.Id, targetId); | ||||
| if (perms != null) | 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; | changed = true; | ||||
| } | } | ||||
| } | } | ||||
| @@ -614,11 +614,11 @@ namespace Discord | |||||
| try | 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); | await _api.DeleteChannelPermissions(channel.Id, userOrRoleId).ConfigureAwait(false); | ||||
| if (perms != null) | 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) | if (idType == PermissionTarget.Role) | ||||
| channel.InvalidatePermissionsCache(); | channel.InvalidatePermissionsCache(); | ||||
| @@ -9,14 +9,14 @@ namespace Discord | |||||
| { | { | ||||
| public sealed class PermissionOverwrite | public sealed class PermissionOverwrite | ||||
| { | { | ||||
| public string Type { get; } | |||||
| public string TargetType { get; } | |||||
| public string TargetId { get; } | public string TargetId { get; } | ||||
| public PackedChannelPermissions Allow { get; } | public PackedChannelPermissions Allow { get; } | ||||
| public PackedChannelPermissions Deny { get; } | public PackedChannelPermissions Deny { get; } | ||||
| internal PermissionOverwrite(string type, string targetId, uint allow, uint deny) | internal PermissionOverwrite(string type, string targetId, uint allow, uint deny) | ||||
| { | { | ||||
| Type = type; | |||||
| TargetType = type; | |||||
| TargetId = targetId; | TargetId = targetId; | ||||
| Allow = new PackedChannelPermissions(allow); | Allow = new PackedChannelPermissions(allow); | ||||
| Deny = new PackedChannelPermissions( deny); | Deny = new PackedChannelPermissions( deny); | ||||
| @@ -176,9 +176,10 @@ namespace Discord | |||||
| PackedChannelPermissions permissions; | PackedChannelPermissions permissions; | ||||
| if (!_permissions.TryGetValue(channelId, out permissions)) return; | if (!_permissions.TryGetValue(channelId, out permissions)) return; | ||||
| uint newPermissions = 0x0; | uint newPermissions = 0x0; | ||||
| uint oldPermissions = permissions.RawValue; | |||||
| if (UserId == server.OwnerId) | if (UserId == server.OwnerId) | ||||
| newPermissions = PackedChannelPermissions.Mask; | |||||
| newPermissions = PackedChannelPermissions.All.RawValue; | |||||
| else | else | ||||
| { | { | ||||
| if (channel == null) return; | if (channel == null) return; | ||||
| @@ -187,24 +188,26 @@ namespace Discord | |||||
| var orderedRoles = Roles.OrderBy(x => x.Id); | var orderedRoles = Roles.OrderBy(x => x.Id); | ||||
| foreach (var serverRole in orderedRoles) | foreach (var serverRole in orderedRoles) | ||||
| newPermissions |= serverRole.Permissions.RawValue; | 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; | 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; | 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; | 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; | 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(); | channel.InvalidMembersCache(); | ||||
| } | |||||
| } | } | ||||
| //TODO: Add GetServerPermissions | //TODO: Add GetServerPermissions | ||||
| public PackedChannelPermissions GetPermissions(Channel channel) | public PackedChannelPermissions GetPermissions(Channel channel) | ||||
| @@ -4,8 +4,15 @@ namespace Discord | |||||
| { | { | ||||
| public sealed class PackedServerPermissions : PackedPermissions | 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) { } | public PackedServerPermissions(uint rawValue = 0) : base(rawValue) { } | ||||
| @@ -25,8 +32,15 @@ namespace Discord | |||||
| public sealed class PackedChannelPermissions : PackedPermissions | 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) { } | public PackedChannelPermissions(uint rawValue = 0) : base(rawValue) { } | ||||