< 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:21
Uncovered lines:12
Coverable lines:33
Total lines:94
Line coverage:63.6% (21 of 33)
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%3.143075%
IsAllowedToCreateChannels()0%12.638058.33%
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
 66824        public ChannelsFeatureFlagService(DataStore dataStore, IUserProfileBridge userProfileBridge)
 25        {
 66826            this.dataStore = dataStore;
 66827            this.userProfileBridge = userProfileBridge;
 66828        }
 29
 30        public void Dispose()
 31        {
 66832            userProfileBridge.GetOwn().OnUpdate -= OnUserProfileUpdate;
 66833        }
 34
 35        public void Initialize()
 36        {
 66737            userProfileBridge.GetOwn().OnUpdate += OnUserProfileUpdate;
 66738        }
 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        {
 757            if (string.IsNullOrEmpty(profile.userId))
 158                return;
 59
 660            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            FeatureFlagVariantPayload usersAllowedToCreateChannelsPayload = featureFlags
 69                .Get()
 70                .GetFeatureFlagVariantPayload($"{FEATURE_FLAG_FOR_USERS_ALLOWED_TO_CREATE_CHANNELS}:{VARIANT_FOR_USERS_A
 71
 172            if (usersAllowedToCreateChannelsPayload == null)
 073                return false;
 74
 175            UsersAllowedToCreateChannelsVariantPayload allowedUsersData = JsonUtility.FromJson<UsersAllowedToCreateChann
 176            UserProfile ownUserProfile = userProfileBridge.GetOwn();
 77
 178            switch (allowedUsersData.mode)
 79            {
 80                case AllowChannelsCreationMode.ALLOWLIST:
 181                    return allowedUsersData.allowList != null
 082                           && allowedUsersData.allowList.Any(userId => userId?.ToLower() == ownUserProfile.userId.ToLowe
 83                case AllowChannelsCreationMode.NAMES:
 084                    return ownUserProfile.hasClaimedName;
 85                case AllowChannelsCreationMode.WALLET:
 086                    return !ownUserProfile.isGuest;
 87            }
 88
 089            return true;
 90        }
 91
 192        public bool IsPromoteChannelsToastEnabled() => featureFlags.Get().IsFeatureEnabled(FEATURE_FLAG_FOR_PROMOTE_CHAN
 93    }
 94}