Compare commits
1 Commits
fix/issue-
...
fix/issue-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d38d47fb79 |
@@ -48,21 +48,24 @@ def parse_args(args: list[str]) -> tuple[Optional[str], Optional[str], Optional[
|
|||||||
return text, link, due_date_ts
|
return text, link, due_date_ts
|
||||||
|
|
||||||
|
|
||||||
def format_bounty(b, show_id: bool = True) -> str:
|
def format_bounty(b, show_id: bool = True, slice_length: int = 0) -> str:
|
||||||
parts = []
|
parts = []
|
||||||
if show_id:
|
if show_id:
|
||||||
parts.append(f"[#{b.id}]")
|
parts.append(f"[#{b.id}]")
|
||||||
if b.text:
|
if b.text:
|
||||||
parts.append(b.text)
|
text = b.text
|
||||||
|
if slice_length > 0 and len(text) > slice_length:
|
||||||
|
text = text[:slice_length] + "..."
|
||||||
|
parts.append(text)
|
||||||
if b.link:
|
if b.link:
|
||||||
parts.append(f"🔗 {b.link}")
|
parts.append(f"🔗 {b.link}")
|
||||||
if b.due_date_ts:
|
if b.due_date_ts:
|
||||||
due_str = time.strftime("%Y-%m-%d", time.localtime(b.due_date_ts))
|
due_str = time.strftime("%d %b %Y", time.localtime(b.due_date_ts))
|
||||||
days_left = (b.due_date_ts - int(time.time())) // 86400
|
days_left = (b.due_date_ts - int(time.time())) // 86400
|
||||||
if days_left < 0:
|
if days_left < 0:
|
||||||
parts.append(f"⏰ {due_str} (OVERDUE)")
|
parts.append(f"⏰ {due_str} (OVERDUE)")
|
||||||
elif days_left == 0:
|
elif days_left == 0:
|
||||||
parts.append(f"⏰ {due_str} (TODAY)")
|
parts.append(f"⏰ Today (OVERDUE)")
|
||||||
else:
|
else:
|
||||||
parts.append(f"⏰ {due_str} ({days_left}d)")
|
parts.append(f"⏰ {due_str} ({days_left}d)")
|
||||||
if b.created_by_user_id:
|
if b.created_by_user_id:
|
||||||
@@ -95,13 +98,58 @@ def get_room_id(update: Update) -> int:
|
|||||||
|
|
||||||
async def cmd_bounty(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> None:
|
async def cmd_bounty(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> None:
|
||||||
room_id = get_room_id(update)
|
room_id = get_room_id(update)
|
||||||
bounties = BOUNTY_SERVICE.list_bounties(room_id)
|
args = extract_args(update.message.text)
|
||||||
|
|
||||||
if not bounties:
|
show_all = "all" in args
|
||||||
|
args = [a for a in args if a != "all"]
|
||||||
|
|
||||||
|
try:
|
||||||
|
limit = int(args[0]) if args else 5
|
||||||
|
except (ValueError, IndexError):
|
||||||
|
limit = 5
|
||||||
|
|
||||||
|
now = int(time.time())
|
||||||
|
cutoff_24h = now - 86400
|
||||||
|
|
||||||
|
all_bounties = BOUNTY_SERVICE.list_bounties(room_id)
|
||||||
|
|
||||||
|
def is_expired(b) -> bool:
|
||||||
|
return b.due_date_ts is not None and b.due_date_ts < cutoff_24h
|
||||||
|
|
||||||
|
def sort_key(b):
|
||||||
|
if b.due_date_ts is not None:
|
||||||
|
return (0, b.due_date_ts)
|
||||||
|
return (1, b.created_at)
|
||||||
|
|
||||||
|
filtered_bounties = [b for b in all_bounties if not is_expired(b) or show_all]
|
||||||
|
filtered_bounties.sort(key=sort_key)
|
||||||
|
|
||||||
|
total_count = len(filtered_bounties)
|
||||||
|
displayed_bounties = filtered_bounties[:limit]
|
||||||
|
|
||||||
|
if not displayed_bounties:
|
||||||
|
if show_all:
|
||||||
await update.message.reply_text("No bounties yet.")
|
await update.message.reply_text("No bounties yet.")
|
||||||
|
else:
|
||||||
|
await update.message.reply_text(
|
||||||
|
"No active bounties. Use /bounty all to show expired."
|
||||||
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
lines = [format_bounty(b, show_id=True) for b in bounties]
|
lines = []
|
||||||
|
if limit < total_count:
|
||||||
|
lines.append(f"Showing {limit} of {total_count} bounties:")
|
||||||
|
slice_length = 40
|
||||||
|
elif show_all and total_count > limit:
|
||||||
|
lines.append(f"Showing {limit} of {total_count} bounties (including expired):")
|
||||||
|
slice_length = 40
|
||||||
|
else:
|
||||||
|
lines.append(f"Showing {total_count} bounties:")
|
||||||
|
slice_length = 0
|
||||||
|
|
||||||
|
for b in displayed_bounties:
|
||||||
|
lines.append(format_bounty(b, show_id=True, slice_length=slice_length))
|
||||||
|
|
||||||
await update.message.reply_text("\n".join(lines), disable_web_page_preview=True)
|
await update.message.reply_text("\n".join(lines), disable_web_page_preview=True)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
41
cli/main.py
41
cli/main.py
@@ -127,40 +127,6 @@ def cmd_delete(args):
|
|||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
def cmd_recover(args):
|
|
||||||
"""List or recover soft-deleted bounties."""
|
|
||||||
bounty_service, _ = create_services()
|
|
||||||
room_id = args.group_id or args.user_id
|
|
||||||
user_id = args.user_id or 0
|
|
||||||
|
|
||||||
if not args.bounty_ids:
|
|
||||||
deleted = bounty_service.list_deleted_bounties(room_id)
|
|
||||||
if not deleted:
|
|
||||||
print("No recoverable bounties")
|
|
||||||
return
|
|
||||||
print("Recoverable bounties:")
|
|
||||||
for b in deleted:
|
|
||||||
from datetime import datetime
|
|
||||||
|
|
||||||
deleted_str = datetime.fromtimestamp(b.deleted_at).strftime("%d %b %Y")
|
|
||||||
print(f" [#{b.id}] {b.text or '(no text)'} | Deleted {deleted_str}")
|
|
||||||
return
|
|
||||||
|
|
||||||
if not bounty_service.is_admin(room_id, user_id):
|
|
||||||
print("Error: Only admins can recover bounties.", file=sys.stderr)
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
for bounty_id in args.bounty_ids:
|
|
||||||
try:
|
|
||||||
success, msg = bounty_service.recover_bounty(
|
|
||||||
room_id=room_id, bounty_id=bounty_id, user_id=user_id
|
|
||||||
)
|
|
||||||
print(msg)
|
|
||||||
except PermissionError as e:
|
|
||||||
print(f"Error: {e}", file=sys.stderr)
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
|
|
||||||
def cmd_track(args):
|
def cmd_track(args):
|
||||||
"""Track a bounty."""
|
"""Track a bounty."""
|
||||||
_, tracking_service = create_services()
|
_, tracking_service = create_services()
|
||||||
@@ -230,9 +196,6 @@ def main():
|
|||||||
parser_untrack = subparsers.add_parser("untrack", help="Untrack a bounty")
|
parser_untrack = subparsers.add_parser("untrack", help="Untrack a bounty")
|
||||||
parser_untrack.add_argument("bounty_id", type=int, help="Bounty ID to untrack")
|
parser_untrack.add_argument("bounty_id", type=int, help="Bounty ID to untrack")
|
||||||
|
|
||||||
parser_recover = subparsers.add_parser("recover", help="List or recover soft-deleted bounties")
|
|
||||||
parser_recover.add_argument("bounty_ids", nargs="*", type=int, help="Bounty ID(s) to recover (optional)")
|
|
||||||
|
|
||||||
for sp in [
|
for sp in [
|
||||||
parser_add,
|
parser_add,
|
||||||
parser_list,
|
parser_list,
|
||||||
@@ -241,7 +204,6 @@ def main():
|
|||||||
parser_delete,
|
parser_delete,
|
||||||
parser_track,
|
parser_track,
|
||||||
parser_untrack,
|
parser_untrack,
|
||||||
parser_recover,
|
|
||||||
]:
|
]:
|
||||||
sp.add_argument(
|
sp.add_argument(
|
||||||
"--group-id", type=int, help="Group context (use group room ID)"
|
"--group-id", type=int, help="Group context (use group room ID)"
|
||||||
@@ -260,7 +222,7 @@ def main():
|
|||||||
print("Error: either --group-id or --user-id is required", file=sys.stderr)
|
print("Error: either --group-id or --user-id is required", file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
if args.command in ("add", "list", "my", "update", "delete", "recover"):
|
if args.command in ("add", "list", "my", "update", "delete"):
|
||||||
if not (args.group_id or args.user_id):
|
if not (args.group_id or args.user_id):
|
||||||
print("Error: --group-id or --user-id required", file=sys.stderr)
|
print("Error: --group-id or --user-id required", file=sys.stderr)
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
@@ -276,7 +238,6 @@ def main():
|
|||||||
"delete": cmd_delete,
|
"delete": cmd_delete,
|
||||||
"track": cmd_track,
|
"track": cmd_track,
|
||||||
"untrack": cmd_untrack,
|
"untrack": cmd_untrack,
|
||||||
"recover": cmd_recover,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if args.command in command_map:
|
if args.command in command_map:
|
||||||
|
|||||||
@@ -210,33 +210,6 @@ class BountyService:
|
|||||||
self._storage.update_bounty(room_id, bounty)
|
self._storage.update_bounty(room_id, bounty)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def recover_bounty(
|
|
||||||
self, room_id: int, bounty_id: int, user_id: int
|
|
||||||
) -> tuple[bool, str]:
|
|
||||||
"""Recover a soft-deleted bounty. Only admins can recover.
|
|
||||||
|
|
||||||
Returns (success, message) tuple.
|
|
||||||
"""
|
|
||||||
all_bounties = self._storage.list_all_bounties(room_id, include_deleted=True)
|
|
||||||
bounty = None
|
|
||||||
for b in all_bounties:
|
|
||||||
if b.id == bounty_id:
|
|
||||||
bounty = b
|
|
||||||
break
|
|
||||||
|
|
||||||
if not bounty:
|
|
||||||
return False, f"Bounty #{bounty_id} not found."
|
|
||||||
|
|
||||||
if bounty.deleted_at is None:
|
|
||||||
return False, f"Bounty #{bounty_id} is not deleted."
|
|
||||||
|
|
||||||
if not self.is_admin(room_id, user_id):
|
|
||||||
raise PermissionError("Only admins can recover bounties.")
|
|
||||||
|
|
||||||
bounty.deleted_at = None
|
|
||||||
self._storage.update_bounty(room_id, bounty)
|
|
||||||
return True, f"Recovered bounty #{bounty_id}."
|
|
||||||
|
|
||||||
|
|
||||||
class TrackingService:
|
class TrackingService:
|
||||||
"""Service for tracking bounty operations."""
|
"""Service for tracking bounty operations."""
|
||||||
|
|||||||
Reference in New Issue
Block a user