< Summary

Class:DCL.Chat.Channels.ChannelsFeatureFlagService
Assembly:ChatController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ChatController/ChannelsFeatureFlagService.cs
Covered lines:13
Uncovered lines:13
Coverable lines:26
Total lines:74
Line coverage:50% (13 of 26)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ChannelsFeatureFlagService(...)0%110100%
Dispose()0%110100%
Initialize()0%110100%
IsChannelsFeatureEnabled()0%110100%
OnUserProfileUpdate(...)0%3.143075%
IsAllowedToCreateChannels()0%56700%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/ChatController/ChannelsFeatureFlagService.cs

#LineLine coverage
 1using System;
 2using System.Linq;
 3using UnityEngine;
 4
 5namespace DCL.Chat.Channels
 6{
 7    public class ChannelsFeatureFlagService : IChannelsFeatureFlagService
 8    {
 9        private const string FEATURE_FLAG_FOR_CHANNELS_FEATURE = "matrix_channels_enabled";
 10        private const string FEATURE_FLAG_FOR_USERS_ALLOWED_TO_CREATE_CHANNELS = "users_allowed_to_create_channels";
 11        private const string VARIANT_FOR_USERS_ALLOWED_TO_CREATE_CHANNELS = "allowedUsers";
 12
 213        private BaseVariable<FeatureFlag> featureFlags => dataStore.featureFlags.flags;
 14
 15        private readonly DataStore dataStore;
 16        private readonly IUserProfileBridge userProfileBridge;
 17
 18        public event Action<bool> OnAllowedToCreateChannelsChanged;
 19
 66820        public ChannelsFeatureFlagService(DataStore dataStore, IUserProfileBridge userProfileBridge)
 21        {
 66822            this.dataStore = dataStore;
 66823            this.userProfileBridge = userProfileBridge;
 66824        }
 25
 26        public void Dispose()
 27        {
 66828            userProfileBridge.GetOwn().OnUpdate -= OnUserProfileUpdate;
 66829        }
 30
 31        public void Initialize()
 32        {
 66833            userProfileBridge.GetOwn().OnUpdate += OnUserProfileUpdate;
 66834        }
 35
 236        public bool IsChannelsFeatureEnabled() => featureFlags.Get().IsFeatureEnabled(FEATURE_FLAG_FOR_CHANNELS_FEATURE)
 37
 38        private void OnUserProfileUpdate(UserProfile profile)
 39        {
 740            if (string.IsNullOrEmpty(profile.userId))
 141                return;
 42
 643            OnAllowedToCreateChannelsChanged?.Invoke(IsAllowedToCreateChannels());
 044        }
 45
 46        public bool IsAllowedToCreateChannels()
 47        {
 048            if (!featureFlags.Get().IsFeatureEnabled(FEATURE_FLAG_FOR_USERS_ALLOWED_TO_CREATE_CHANNELS))
 049                return false;
 50
 051            FeatureFlagVariantPayload usersAllowedToCreateChannelsPayload = featureFlags
 52                .Get()
 53                .GetFeatureFlagVariantPayload($"{FEATURE_FLAG_FOR_USERS_ALLOWED_TO_CREATE_CHANNELS}:{VARIANT_FOR_USERS_A
 54
 055            if (usersAllowedToCreateChannelsPayload == null)
 056                return false;
 57
 058            UsersAllowedToCreateChannelsVariantPayload allowedUsersData = JsonUtility.FromJson<UsersAllowedToCreateChann
 059            UserProfile ownUserProfile = userProfileBridge.GetOwn();
 60
 061            switch (allowedUsersData.mode)
 62            {
 63                case AllowChannelsCreationMode.ALLOWLIST:
 064                    return allowedUsersData.allowList.Any(userId => userId.ToLower() == ownUserProfile.userId.ToLower())
 65                case AllowChannelsCreationMode.NAMES:
 066                    return ownUserProfile.hasClaimedName;
 67                case AllowChannelsCreationMode.WALLET:
 068                    return !ownUserProfile.isGuest;
 69            }
 70
 071            return true;
 72        }
 73    }
 74}