Browse Source

Correct channel permissions calculation, cleanup channel tests

pull/116/head
andrei 6 years ago
parent
commit
75b60b6e17
  1. 27
      disco/types/channel.py
  2. 1
      disco/types/guild.py
  3. 36
      tests/types/channel.py

27
disco/types/channel.py

@ -162,18 +162,27 @@ class Channel(SlottedModel, Permissible):
member = self.guild.get_member(user)
base = self.guild.get_permissions(member)
ow_everyone = self.overwrites.get(self.guild_id)
if ow_everyone:
base += ow_everyone.compiled
# First grab and apply the everyone overwrite
everyone = self.overwrites.get(self.guild_id)
if everyone:
base -= everyone.deny
base += everyone.allow
denies = 0
allows = 0
for role_id in member.roles:
ow_role = self.overwrites.get(role_id)
if ow_role:
base += ow_role.compiled
overwrite = self.overwrites.get(role_id)
if overwrite:
denies |= overwrite.deny
allows |= overwrite.allow
base -= denies
base += allows
ow_member = self.overwrites.get(member.user.id)
if ow_member:
base += ow_member.compiled
base -= ow_member.deny
base += ow_member.allow
return base
@ -200,7 +209,7 @@ class Channel(SlottedModel, Permissible):
"""
Whether this channel is an NSFW channel.
"""
return self.type == ChannelType.GUILD_TEXT and (self.nsfw or NSFW_RE.match(self.name))
return bool(self.type == ChannelType.GUILD_TEXT and (self.nsfw or NSFW_RE.match(self.name)))
@property
def is_voice(self):

1
disco/types/guild.py

@ -357,6 +357,7 @@ class Guild(SlottedModel, Permissible):
if self.owner_id == member.id:
return PermissionValue(Permissions.ADMINISTRATOR)
# Our value starts with the guilds default (@everyone) role permissions
value = PermissionValue(self.roles.get(self.id).permissions)
# Iterate over all roles the user has (plus the @everyone role)

36
tests/types/channel.py

@ -1,25 +1,23 @@
from unittest import TestCase
from disco.types.channel import Channel, ChannelType
class TestChannel(TestCase):
def test_deprecated_nsfw_channel(self):
channel = Channel(
name='nsfw-testing',
type=ChannelType.GUILD_TEXT)
self.assertTrue(channel.is_nsfw)
def test_deprecated_nsfw_channel():
channel = Channel(
name='nsfw-testing',
type=ChannelType.GUILD_TEXT)
assert channel.is_nsfw
channel = Channel(
name='nsfw-testing',
type=ChannelType.GUILD_VOICE)
assert not channel.is_nsfw
channel = Channel(
name='nsfw-testing',
type=ChannelType.GUILD_VOICE)
self.assertFalse(channel.is_nsfw)
channel = Channel(
name='nsfw_testing',
type=ChannelType.GUILD_TEXT)
assert not channel.is_nsfw
channel = Channel(
name='nsfw_testing',
type=ChannelType.GUILD_TEXT)
self.assertFalse(channel.is_nsfw)
def test_nsfw_channel(self):
channel = Channel(name='test', nsfw=True, type=ChannelType.GUILD_TEXT)
assert channel.is_nsfw
def test_nsfw_channel():
channel = Channel(name='test', nsfw=True, type=ChannelType.GUILD_TEXT)
assert channel.is_nsfw

Loading…
Cancel
Save