31 lines
1.3 KiB
Python
31 lines
1.3 KiB
Python
|
|
from snek.system.service import BaseService
|
||
|
|
|
||
|
|
class ChannelService(BaseService):
|
||
|
|
mapper_name = "channel"
|
||
|
|
|
||
|
|
async def create(self, label, created_by_uid, description=None, tag=None, is_private=False, is_listed=True):
|
||
|
|
if label[0] != "#" and is_listed:
|
||
|
|
label = f"#{label}"
|
||
|
|
count = await self.count(deleted_at=None)
|
||
|
|
if not tag and not count:
|
||
|
|
tag = "public"
|
||
|
|
model = await self.new()
|
||
|
|
model['label'] = label
|
||
|
|
model['description'] = description
|
||
|
|
model['tag'] = tag
|
||
|
|
model['created_by_uid'] = created_by_uid
|
||
|
|
model['is_private'] = is_private
|
||
|
|
model['is_listed'] = is_listed
|
||
|
|
if await self.save(model):
|
||
|
|
return model
|
||
|
|
raise Exception(f"Failed to create channel: {model.errors}.")
|
||
|
|
|
||
|
|
async def ensure_public_channel(self, created_by_uid):
|
||
|
|
model = await self.get(is_listed=True,tag="public")
|
||
|
|
is_moderator = False
|
||
|
|
if not model:
|
||
|
|
is_moderator = True
|
||
|
|
model = await self.create("public", created_by_uid=created_by_uid, is_listed=True, tag="public")
|
||
|
|
await self.app.services.channel_member.create(model['uid'], created_by_uid, is_moderator=is_moderator, is_read_only=False, is_muted=False, is_banned=False)
|
||
|
|
return model
|
||
|
|
|