Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 52 additions & 10 deletions aioircd/channel.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
import trio
from functools import partial
from typing import Union, List
from typing import Union, List, Set

import aioircd

Expand All @@ -10,28 +10,70 @@


class Channel:
def __init__(self, name):
"""
The Channel class represents a chat room.

Attributes:
name: str
the channel name.
users: Set[User]
a set of user who is subscribe to the channel.
"""

def __init__(self, name: str) -> None:
"""
Parameters:
name: str
the channel name.
"""
self._name = name
self.users = set()

def __str__(self):
def __str__(self) -> str:
"""
We represents the channel object by his name.

Return: str
the name of the channel.
"""
return self._name

@property
def name(self):
"""
Getter for private attributes channel name.

Returns: str
the name of the channel.
"""
return self._name

async def send(self, messages: Union[str, List[str]], skipusers=[]):
""" Send many messages to each user subcribed in this channel. """
def _log_messages(self, messages: List[str]) -> None:
"""
Log all messages sent to a client.

Parameters: List[str]
A list of messages send to the client.
"""
for message in messages:
logger.log(aioircd.IO, "send to %s: %s", self, message)

async def send(self, messages: Union[str, List[str]], skipusers: Set["aioircd.user.User"] = set()) -> None:
""""
Send many messages to each user subscribed to this channel.

Parameters:
messages: Union[str, List[str]]
Messages to send to the users.
skipusers: Set[User]
A set of users to skip sending messages to.
"""
if isinstance(messages, str):
messages = [messages]

async with trio.open_nursery() as self._nursery:
for message in messages:
logger.log(aioircd.IO, "send to %s: %s", self, message)
for user in self.users:
if user in skipusers:
continue
self._log_messages(messages)
for user in self.users.difference(skipusers):
self._nursery.start_soon(partial(
user.send,
messages,
Expand Down