LCOV - code coverage report
Current view: top level - lib/models - groupmembers.dart (source / functions) Hit Total Coverage
Test: lcov.info Lines: 0 47 0.0 %
Date: 2026-09-01 20:55:37 Functions: 0 0 -

          Line data    Source code
       1             : import 'package:flutter/foundation.dart';
       2             : import 'package:flutter/material.dart';
       3             : import 'package:cwtch/l10n/app_localizations.dart';
       4             : 
       5             : import '../cwtch_icons_icons.dart';
       6             : 
       7             : class GroupMember extends ChangeNotifier {
       8             :   late String _onion;
       9             :   late String _role;
      10             :   late Map<String, bool> _acl;
      11             : 
      12           0 :   GroupMember(String onion, Map<String, dynamic> acl) {
      13           0 :     this._onion = onion;
      14           0 :     this._role = acl["AssignedRole"] ?? "unknown";
      15           0 :     acl.remove("AssignedRole");
      16           0 :     this._acl = acl.cast<String, bool>();
      17             :   }
      18             : 
      19           0 :   String get onion => _onion;
      20           0 :   String get role => _role;
      21           0 :   String get aclWIP => _role + " " + _acl.entries.where((x) => x.value).map((x) => x.key).join(", ");
      22             : 
      23           0 :   static String roleL10n(BuildContext context, String? role) {
      24             :     switch (role) {
      25           0 :       case 'admin':
      26           0 :         return AppLocalizations.of(context)!.roleAdmin;
      27           0 :       case 'op':
      28           0 :         return AppLocalizations.of(context)!.roleOp;
      29           0 :       case 'mod':
      30           0 :         return AppLocalizations.of(context)!.roleMod;
      31           0 :       case 'voice':
      32           0 :         return AppLocalizations.of(context)!.roleVoice;
      33           0 :       case 'user':
      34           0 :         return AppLocalizations.of(context)!.roleUser;
      35           0 :       case 'banned':
      36           0 :         return AppLocalizations.of(context)!.roleBanned;
      37             :       default:
      38           0 :         return AppLocalizations.of(context)!.roleUnknown;
      39             :     }
      40             :   }
      41             : 
      42           0 :   static Widget roleIcon(String? role, double size, {Color fgColor = Colors.white}) {
      43             :     // *  @  %  +  -
      44           0 :     String semantic = (role ?? "unknown role") + " symbol";
      45             :     switch (role) {
      46           0 :       case 'admin':
      47           0 :         return Icon(CwtchIcons.member_admin, color: fgColor, size: size, semanticLabel: semantic);
      48           0 :       case 'op':
      49           0 :         return Icon(CwtchIcons.member_op, color: fgColor, size: size, semanticLabel: semantic);
      50           0 :       case 'mod':
      51           0 :         return Icon(CwtchIcons.member_mod, color: fgColor, size: size, semanticLabel: semantic);
      52           0 :       case 'voice':
      53           0 :         return Icon(CwtchIcons.member_voice, color: fgColor, size: size, semanticLabel: semantic);
      54           0 :       case 'user':
      55           0 :         return Icon(CwtchIcons.member_user, color: fgColor, size: size, semanticLabel: semantic);
      56           0 :       case 'banned':
      57           0 :         return Icon(CwtchIcons.block_peer, color: fgColor, size: size, semanticLabel: semantic);
      58             :       default:
      59           0 :         return Icon(CwtchIcons.member_user, color: fgColor, size: size, semanticLabel: semantic);
      60             :     }
      61             :   }
      62             : 
      63             :   // this logic must perfectly match cwtch/models/roles.go::IsAbleToPromoteTo(...)
      64             :   // hence its code is replicated here 1:1 in comments
      65             :   // this client-side check has no security implications -- it is simply used to
      66             :   // prevent allowing users to perform actions that will end up forbidden by the backend
      67           0 :   static bool isAbleToPromoteTo(String? myRole, String? currentRole, String? targetRole, String modeLine) {
      68           0 :     if (myRole == 'admin') {
      69             :       //        if r.IsEffectiveAdmin() {
      70             :       return true; //           return true // admins can do anything
      71             :     } //        }
      72             :     //
      73             :     //  // no one below admin can promote/demote to admin ever
      74           0 :     if (currentRole == 'admin' || targetRole == 'admin') {
      75             :       //        if currentRole == ROLE_ADMIN || targetRole == ROLE_ADMIN {
      76             :       return false; //          return false
      77             :     } //        }
      78             :     //
      79           0 :     if (myRole == 'op') {
      80             :       //        if r == ROLE_OP {
      81           0 :       if (currentRole == 'op' || targetRole == 'op') {
      82             :         //              if currentRole == ROLE_OP || targetRole == ROLE_OP {
      83           0 :         return modeLine.contains('e'); //                       return mode.IsSet(Equality) // ops can only promote/demote ops if equality
      84             :       } else {
      85             :         //              } else {
      86             :         return true; //                 return true // op can always promote/demote below op
      87             :       } //              }
      88             :     } //        }
      89             :     //
      90             :     //  // no one below op can promote/demote op ever
      91           0 :     if (currentRole == 'op' || targetRole == 'op') {
      92             :       //        if currentRole == ROLE_OP || targetRole == ROLE_OP {
      93             :       return false; //          return false
      94             :     } //        }
      95             :     //
      96           0 :     if (myRole == 'mod') {
      97             :       //        if r == ROLE_MOD {
      98           0 :       if (currentRole == 'mod' || targetRole == 'mod') {
      99             :         //              if currentRole == ROLE_MOD || targetRole == ROLE_MOD {
     100           0 :         return modeLine.contains('e'); //                       return mode.IsSet(Equality) // ops can only promote/demote mod if equality
     101             :       } else {
     102             :         //              } else {
     103             :         return true; //                 return true // mod can always promote/demote below mode
     104             :       } //              }
     105             :     } //        }
     106             :     //
     107             :     return false; //    return false // not an admin, operator, or mod means we can never promote/demote
     108             :   }
     109             : }

Generated by: LCOV version 1.14