Coverage for dao/admin_attention_dao.py: 52.00%

25 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2026-11-30 14:44 +0000

1""" 

2AdminAttentionDAO — single-shot aggregator for the admin nav badge. 

3 

4Counts items across four "needs admin attention" queues: 

5- Invite Requests in 'pending' status 

6- Channel Requests with any platform in 'pending' (telegram or discord) 

7- Support Inbox threads with unread messages where status is 'new' or 'awaiting_admin' 

8 (delegates to EmailThreadsDAO.unread_count_for_attention) 

9- Chat content reports in 'pending' status (SB-1309) 

10 

11Result is cached for 30s via @dao_cache when Redis is enabled; otherwise the 

12decorator no-ops and every call hits the DB (still fine — these are tiny 

13count queries). 

14""" 

15 

16from __future__ import annotations 

17 

18from typing import Any 

19 

20import structlog 

21 

22from dao.base_dao import BaseDAO, dao_cache 

23from dao.email_threads_dao import EmailThreadsDAO 

24 

25logger = structlog.get_logger() 

26 

27# 30s TTL: short enough that admins see updates promptly, long enough that 

28# multiple admin sessions share a single fan-out per 30s window. 

29_CACHE_TTL = 30 

30 

31 

32class AdminAttentionDAO(BaseDAO): 

33 """Aggregator for the admin top-nav attention badge.""" 

34 

35 @dao_cache("admin_attention:counts", ttl=_CACHE_TTL) 

36 def get_counts(self) -> dict[str, Any]: 

37 """Return per-queue pending counts plus a total. 

38 

39 Shape: 

40 { 

41 "invite_requests": int, 

42 "channel_requests": int, 

43 "support_inbox": int, 

44 "content_reports": int, 

45 "total": int, 

46 } 

47 

48 Each count is the number of items needing admin attention — not a 

49 full record count. invite_requests counts rows with status='pending'; 

50 channel_requests counts distinct rows with either telegram_status 

51 or discord_status in 'pending'; support_inbox sums unread messages 

52 across threads in 'new' or 'awaiting_admin'. 

53 """ 

54 invite_requests = self._count_pending_invite_requests() 

55 channel_requests = self._count_pending_channel_requests() 

56 support_inbox = EmailThreadsDAO(self.connection_holder).unread_count_for_attention() 

57 content_reports = self._count_pending_content_reports() 

58 

59 total = invite_requests + channel_requests + support_inbox + content_reports 

60 return { 

61 "invite_requests": invite_requests, 

62 "channel_requests": channel_requests, 

63 "support_inbox": support_inbox, 

64 "content_reports": content_reports, 

65 "total": total, 

66 } 

67 

68 def _count_pending_invite_requests(self) -> int: 

69 response = ( 

70 self.client.table("invite_requests") 

71 .select("id", count="exact") 

72 .eq("status", "pending") 

73 .execute() 

74 ) 

75 return response.count or 0 

76 

77 def _count_pending_content_reports(self) -> int: 

78 """Chat reports nobody has acted on yet (SB-1309).""" 

79 response = ( 

80 self.client.table("content_reports") 

81 .select("id", count="exact") 

82 .eq("status", "pending") 

83 .execute() 

84 ) 

85 return response.count or 0 

86 

87 def _count_pending_channel_requests(self) -> int: 

88 """Count distinct channel-request rows with any platform pending. 

89 

90 A single row tracks both Telegram and Discord status, and a request 

91 is "pending" if either side is in 'pending'. We count rows (not 

92 platform-pending instances) so the badge mirrors the inbox tab's 

93 notion of work-to-do. 

94 """ 

95 # PostgREST `or` filter: telegram_status.eq.pending,discord_status.eq.pending 

96 response = ( 

97 self.client.table("channel_access_requests") 

98 .select("id", count="exact") 

99 .or_("telegram_status.eq.pending,discord_status.eq.pending") 

100 .execute() 

101 ) 

102 return response.count or 0