< 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:22
Uncovered lines:13
Coverable lines:35
Total lines:97
Line coverage:62.8% (22 of 35)
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%
GetAutoJoinChannelsList()0%6200%
OnUserProfileUpdate(...)0%4.123050%
IsAllowedToCreateChannels()0%18.3710056.25%
IsPromoteChannelsToastEnabled()0%110100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using UnityEngine;
 5
 6namespace DCL.Chat.Channels
 7{
 8    public class ChannelsFeatureFlagService : IChannelsFeatureFlagService
 9    {
 10        private const string FEATURE_FLAG_FOR_CHANNELS_FEATURE = "matrix_channels_enabled";
 11        private const string FEATURE_FLAG_FOR_USERS_ALLOWED_TO_CREATE_CHANNELS = "users_allowed_to_create_channels";
 12        private const string VARIANT_FOR_USERS_ALLOWED_TO_CREATE_CHANNELS = "allowedUsers";
 13        private const string FEATURE_FLAG_FOR_AUTOMATIC_JOIN_CHANNELS = "automatic_joined_channels";
 14        private const string VARIANT_FOR_AUTOMATIC_JOIN_CHANNELS = "automaticChannels";
 15        private const string FEATURE_FLAG_FOR_PROMOTE_CHANNELS_TOAST = "promote_channels_toast";
 16
 517        private BaseVariable<FeatureFlag> featureFlags => dataStore.featureFlags.flags;
 18
 19        private readonly DataStore dataStore;
 20        private readonly IUserProfileBridge userProfileBridge;
 21
 22        public event Action<bool> OnAllowedToCreateChannelsChanged;
 23
 49424        public ChannelsFeatureFlagService(DataStore dataStore, IUserProfileBridge userProfileBridge)
 25        {
 49426            this.dataStore = dataStore;
 49427            this.userProfileBridge = userProfileBridge;
 49428        }
 29
 30        public void Dispose()
 31        {
 49432            userProfileBridge.GetOwn().OnUpdate -= OnUserProfileUpdate;
 49433        }
 34
 35        public void Initialize()
 36        {
 49337            userProfileBridge.GetOwn().OnUpdate += OnUserProfileUpdate;
 49338        }
 39
 240        public bool IsChannelsFeatureEnabled() => featureFlags.Get().IsFeatureEnabled(FEATURE_FLAG_FOR_CHANNELS_FEATURE)
 41
 42        public AutomaticJoinChannelList GetAutoJoinChannelsList()
 43        {
 044            if (!featureFlags.Get().IsFeatureEnabled(FEATURE_FLAG_FOR_AUTOMATIC_JOIN_CHANNELS))
 045                return null;
 46
 047            FeatureFlagVariantPayload ffChannelsList = featureFlags
 48                .Get()
 49                .GetFeatureFlagVariantPayload($"{FEATURE_FLAG_FOR_AUTOMATIC_JOIN_CHANNELS}:{VARIANT_FOR_AUTOMATIC_JOIN_C
 50
 051            AutomaticJoinChannelList autoJoinChannelsList = JsonUtility.FromJson<AutomaticJoinChannelList>(ffChannelsLis
 052            return autoJoinChannelsList;
 53        }
 54
 55        private void OnUserProfileUpdate(UserProfile profile)
 56        {
 157            if (string.IsNullOrEmpty(profile.userId))
 158                return;
 59
 060            OnAllowedToCreateChannelsChanged?.Invoke(IsAllowedToCreateChannels());
 061        }
 62
 63        public bool IsAllowedToCreateChannels()
 64        {
 165            if (!featureFlags.Get().IsFeatureEnabled(FEATURE_FLAG_FOR_USERS_ALLOWED_TO_CREATE_CHANNELS))
 066                return false;
 67
 168            UserProfile ownUserProfile = userProfileBridge.GetOwn();
 169            if (ownUserProfile == null) return false;
 170            if (string.IsNullOrEmpty(ownUserProfile.userId)) return false;
 71
 172            FeatureFlagVariantPayload usersAllowedToCreateChannelsPayload = featureFlags
 73                .Get()
 74                .GetFeatureFlagVariantPayload($"{FEATURE_FLAG_FOR_USERS_ALLOWED_TO_CREATE_CHANNELS}:{VARIANT_FOR_USERS_A
 75
 176            if (usersAllowedToCreateChannelsPayload == null)
 077                return false;
 78
 179            UsersAllowedToCreateChannelsVariantPayload allowedUsersData = JsonUtility.FromJson<UsersAllowedToCreateChann
 80
 181            switch (allowedUsersData.mode)
 82            {
 83                case AllowChannelsCreationMode.ALLOWLIST:
 184                    return allowedUsersData.allowList != null
 085                           && allowedUsersData.allowList.Any(userId => userId?.ToLower() == ownUserProfile.userId.ToLowe
 86                case AllowChannelsCreationMode.NAMES:
 087                    return ownUserProfile.hasClaimedName;
 88                case AllowChannelsCreationMode.WALLET:
 089                    return !ownUserProfile.isGuest;
 90            }
 91
 092            return true;
 93        }
 94
 195        public bool IsPromoteChannelsToastEnabled() => featureFlags.Get().IsFeatureEnabled(FEATURE_FLAG_FOR_PROMOTE_CHAN
 96    }
 97}