49 lines
1.4 KiB
Python
Raw Normal View History

2025-01-25 21:24:44 +00:00
from snek.system.service import BaseService
2025-01-25 21:28:33 +00:00
2025-01-25 21:24:44 +00:00
class ChannelService(BaseService):
mapper_name = "channel"
2025-01-25 21:28:33 +00:00
async def create(
self,
label,
created_by_uid,
description=None,
tag=None,
is_private=False,
is_listed=True,
):
2025-01-25 21:24:44 +00:00
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()
2025-01-25 21:28:33 +00:00
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
2025-01-25 21:24:44 +00:00
if await self.save(model):
return model
raise Exception(f"Failed to create channel: {model.errors}.")
2025-01-25 21:28:33 +00:00
2025-01-25 21:24:44 +00:00
async def ensure_public_channel(self, created_by_uid):
2025-01-25 21:28:33 +00:00
model = await self.get(is_listed=True, tag="public")
is_moderator = False
2025-01-25 21:24:44 +00:00
if not model:
2025-01-25 21:28:33 +00:00
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,
)
2025-01-25 21:24:44 +00:00
return model