feat: implement service layer for Phase 2 - admin management, timezone, soft delete
BountyService: - Add is_admin(), add_admin(), remove_admin(), list_admins() - Add set_timezone(), get_timezone() - Add check_link_unique(), list_deleted_bounties() - Modify add_bounty() to check link uniqueness and require admin - Modify update_bounty() to require admin permission (not creator) - Modify delete_bounty() to perform soft delete (set deleted_at) - get_bounty() now filters out soft-deleted bounties - list_bounties() uses storage.list_bounties() which excludes soft-deleted TrackingService: - get_tracked_bounties() now filters out soft-deleted bounties Tests updated to reflect new admin-only permissions and soft delete behavior.
This commit is contained in:
143
core/services.py
143
core/services.py
@@ -15,11 +15,103 @@ class BountyService:
|
||||
- Positive room_id: DM/personal context (user's Telegram ID)
|
||||
|
||||
This service handles both group and personal bounties through room_id.
|
||||
|
||||
Permissions:
|
||||
- /add, /edit, /delete: admin only
|
||||
- /admin, /admin add, /admin remove: admin only
|
||||
- /bounty, /show, /track, /untrack, /my: everyone
|
||||
"""
|
||||
|
||||
def __init__(self, storage: RoomStorage):
|
||||
self._storage = storage
|
||||
|
||||
def is_admin(self, room_id: int, user_id: int) -> bool:
|
||||
"""Check if user is admin in a room."""
|
||||
room_data = self._storage.load(room_id)
|
||||
if room_data is None:
|
||||
return False
|
||||
return user_id in (room_data.admin_user_ids or [])
|
||||
|
||||
def add_admin(
|
||||
self, room_id: int, admin_user_id: int, requesting_user_id: int
|
||||
) -> None:
|
||||
"""Add an admin to a room. Requires admin permission."""
|
||||
if not self.is_admin(room_id, requesting_user_id):
|
||||
raise PermissionError("Only admins can add admins.")
|
||||
|
||||
room_data = self._storage.load(room_id)
|
||||
if room_data is None:
|
||||
room_data = RoomData(
|
||||
room_id=room_id, bounties=[], next_id=1, admin_user_ids=[]
|
||||
)
|
||||
|
||||
if admin_user_id not in (room_data.admin_user_ids or []):
|
||||
(room_data.admin_user_ids or []).append(admin_user_id)
|
||||
self._storage.save(room_data)
|
||||
|
||||
def remove_admin(
|
||||
self, room_id: int, admin_user_id: int, requesting_user_id: int
|
||||
) -> None:
|
||||
"""Remove an admin from a room. Requires admin permission."""
|
||||
if not self.is_admin(room_id, requesting_user_id):
|
||||
raise PermissionError("Only admins can remove admins.")
|
||||
|
||||
room_data = self._storage.load(room_id)
|
||||
if room_data is None:
|
||||
return
|
||||
|
||||
if admin_user_id in (room_data.admin_user_ids or []):
|
||||
(room_data.admin_user_ids or []).remove(admin_user_id)
|
||||
self._storage.save(room_data)
|
||||
|
||||
def list_admins(self, room_id: int) -> list[int]:
|
||||
"""List all admin user IDs in a room."""
|
||||
room_data = self._storage.load(room_id)
|
||||
if room_data is None:
|
||||
return []
|
||||
return list(room_data.admin_user_ids or [])
|
||||
|
||||
def set_timezone(
|
||||
self, room_id: int, timezone: str, requesting_user_id: int
|
||||
) -> None:
|
||||
"""Set the timezone for a room. Requires admin permission."""
|
||||
if not self.is_admin(room_id, requesting_user_id):
|
||||
raise PermissionError("Only admins can set timezone.")
|
||||
|
||||
room_data = self._storage.load(room_id)
|
||||
if room_data is None:
|
||||
room_data = RoomData(
|
||||
room_id=room_id, bounties=[], next_id=1, admin_user_ids=[]
|
||||
)
|
||||
|
||||
room_data.timezone = timezone
|
||||
self._storage.save(room_data)
|
||||
|
||||
def get_timezone(self, room_id: int) -> str:
|
||||
"""Get the timezone for a room. Returns UTC+0 if not set."""
|
||||
room_data = self._storage.load(room_id)
|
||||
if room_data is None:
|
||||
return "UTC+0"
|
||||
return room_data.timezone or "UTC+0"
|
||||
|
||||
def check_link_unique(
|
||||
self, room_id: int, link: str | None, exclude_bounty_id: int | None = None
|
||||
) -> bool:
|
||||
"""Check if a link is unique within a room (not used by another bounty)."""
|
||||
if not link:
|
||||
return True
|
||||
|
||||
room_data = self._storage.load(room_id)
|
||||
if room_data is None:
|
||||
return True
|
||||
|
||||
for bounty in room_data.bounties:
|
||||
if bounty.deleted_at is not None:
|
||||
continue
|
||||
if bounty.link == link and bounty.id != exclude_bounty_id:
|
||||
return False
|
||||
return True
|
||||
|
||||
def add_bounty(
|
||||
self,
|
||||
room_id: int,
|
||||
@@ -28,7 +120,13 @@ class BountyService:
|
||||
link: Optional[str] = None,
|
||||
due_date_ts: Optional[int] = None,
|
||||
) -> Bounty:
|
||||
"""Add a new bounty to the room."""
|
||||
"""Add a new bounty to the room. Requires admin permission."""
|
||||
if not self.is_admin(room_id, user_id):
|
||||
raise PermissionError("Only admins can add bounties.")
|
||||
|
||||
if not self.check_link_unique(room_id, link):
|
||||
raise ValueError("A bounty with this link already exists in this room.")
|
||||
|
||||
room_data = self._storage.load(room_id)
|
||||
if room_data is None:
|
||||
room_data = RoomData(room_id=room_id, bounties=[], next_id=1)
|
||||
@@ -47,15 +145,20 @@ class BountyService:
|
||||
return bounty
|
||||
|
||||
def list_bounties(self, room_id: int) -> list[Bounty]:
|
||||
"""List all bounties in a room."""
|
||||
room_data = self._storage.load(room_id)
|
||||
if room_data is None:
|
||||
return []
|
||||
return room_data.bounties
|
||||
"""List all non-deleted bounties in a room."""
|
||||
return self._storage.list_bounties(room_id)
|
||||
|
||||
def list_deleted_bounties(self, room_id: int) -> list[Bounty]:
|
||||
"""List all soft-deleted bounties in a room. For /recover functionality."""
|
||||
all_bounties = self._storage.list_all_bounties(room_id, include_deleted=True)
|
||||
return [b for b in all_bounties if b.deleted_at is not None]
|
||||
|
||||
def get_bounty(self, room_id: int, bounty_id: int) -> Bounty | None:
|
||||
"""Get a specific bounty by ID."""
|
||||
return self._storage.get_bounty(room_id, bounty_id)
|
||||
"""Get a specific bounty by ID. Excludes soft-deleted bounties."""
|
||||
bounty = self._storage.get_bounty(room_id, bounty_id)
|
||||
if bounty and bounty.deleted_at is not None:
|
||||
return None
|
||||
return bounty
|
||||
|
||||
def update_bounty(
|
||||
self,
|
||||
@@ -68,12 +171,17 @@ class BountyService:
|
||||
clear_link: bool = False,
|
||||
clear_due: bool = False,
|
||||
) -> bool:
|
||||
"""Update a bounty. Only creator can update."""
|
||||
"""Update a bounty. Only admins can update."""
|
||||
bounty = self._storage.get_bounty(room_id, bounty_id)
|
||||
if not bounty:
|
||||
return False
|
||||
if bounty.created_by_user_id != user_id:
|
||||
raise PermissionError("Only the creator can edit this bounty.")
|
||||
if not self.is_admin(room_id, user_id):
|
||||
raise PermissionError("Only admins can edit bounties.")
|
||||
|
||||
if link and not self.check_link_unique(
|
||||
room_id, link, exclude_bounty_id=bounty_id
|
||||
):
|
||||
raise ValueError("A bounty with this link already exists in this room.")
|
||||
|
||||
updated = Bounty(
|
||||
id=bounty.id,
|
||||
@@ -84,19 +192,22 @@ class BountyService:
|
||||
if clear_due
|
||||
else (due_date_ts if due_date_ts is not None else bounty.due_date_ts),
|
||||
created_at=bounty.created_at,
|
||||
deleted_at=bounty.deleted_at,
|
||||
created_by_username=bounty.created_by_username,
|
||||
)
|
||||
self._storage.update_bounty(room_id, updated)
|
||||
return True
|
||||
|
||||
def delete_bounty(self, room_id: int, bounty_id: int, user_id: int) -> bool:
|
||||
"""Delete a bounty. Only creator can delete."""
|
||||
"""Soft delete a bounty. Only admins can delete."""
|
||||
bounty = self._storage.get_bounty(room_id, bounty_id)
|
||||
if not bounty:
|
||||
return False
|
||||
if bounty.created_by_user_id != user_id:
|
||||
raise PermissionError("Only the creator can delete this bounty.")
|
||||
if not self.is_admin(room_id, user_id):
|
||||
raise PermissionError("Only admins can delete bounties.")
|
||||
|
||||
self._storage.delete_bounty(room_id, bounty_id)
|
||||
bounty.deleted_at = int(time.time())
|
||||
self._storage.update_bounty(room_id, bounty)
|
||||
return True
|
||||
|
||||
|
||||
@@ -147,7 +258,7 @@ class TrackingService:
|
||||
if room_data is None:
|
||||
return []
|
||||
|
||||
bounty_map = {b.id: b for b in room_data.bounties}
|
||||
bounty_map = {b.id: b for b in room_data.bounties if b.deleted_at is None}
|
||||
return [
|
||||
bounty_map[t.bounty_id]
|
||||
for t in tracking_data.tracked
|
||||
|
||||
Reference in New Issue
Block a user