


from snek.system.service import BaseService


class ChatService(BaseService):

    async def send(self,user_uid, channel_uid, message):
        channel_message = await self.services.channel_message.create(
            channel_uid, 
            user_uid, 
            message
        )
        channel_message_uid = channel_message["uid"]
        
        user = await self.services.user.get(uid=user_uid)
        await self.services.notification.create_channel_message(channel_message_uid)
        sent_to_count = await self.services.socket.broadcast(channel_uid, dict(
            message=channel_message["message"],
            html=channel_message["html"],
            user_uid=user_uid,
            color=user['color'],
            channel_uid=channel_uid,
            created_at=channel_message["created_at"], 
            updated_at=None,
            username=user['username'],
            uid=channel_message['uid'],
            user_nick=user['nick']
        ))
        return sent_to_count