| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Linq; |
| | 4 | | using UnityEngine; |
| | 5 | |
|
| | 6 | | namespace 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 | |
|
| 14 | 17 | | 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 | |
|
| 426 | 24 | | public ChannelsFeatureFlagService(DataStore dataStore, IUserProfileBridge userProfileBridge) |
| | 25 | | { |
| 426 | 26 | | this.dataStore = dataStore; |
| 426 | 27 | | this.userProfileBridge = userProfileBridge; |
| 426 | 28 | | } |
| | 29 | |
|
| | 30 | | public void Dispose() |
| | 31 | | { |
| 426 | 32 | | userProfileBridge.GetOwn().OnUpdate -= OnUserProfileUpdate; |
| 426 | 33 | | } |
| | 34 | |
|
| | 35 | | public void Initialize() |
| | 36 | | { |
| 425 | 37 | | userProfileBridge.GetOwn().OnUpdate += OnUserProfileUpdate; |
| 425 | 38 | | } |
| | 39 | |
|
| 11 | 40 | | public bool IsChannelsFeatureEnabled() => featureFlags.Get().IsFeatureEnabled(FEATURE_FLAG_FOR_CHANNELS_FEATURE) |
| | 41 | |
|
| | 42 | | public AutomaticJoinChannelList GetAutoJoinChannelsList() |
| | 43 | | { |
| 0 | 44 | | if (!featureFlags.Get().IsFeatureEnabled(FEATURE_FLAG_FOR_AUTOMATIC_JOIN_CHANNELS)) |
| 0 | 45 | | return null; |
| | 46 | |
|
| 0 | 47 | | FeatureFlagVariantPayload ffChannelsList = featureFlags |
| | 48 | | .Get() |
| | 49 | | .GetFeatureFlagVariantPayload($"{FEATURE_FLAG_FOR_AUTOMATIC_JOIN_CHANNELS}:{VARIANT_FOR_AUTOMATIC_JOIN_C |
| | 50 | |
|
| 0 | 51 | | AutomaticJoinChannelList autoJoinChannelsList = JsonUtility.FromJson<AutomaticJoinChannelList>(ffChannelsLis |
| 0 | 52 | | return autoJoinChannelsList; |
| | 53 | | } |
| | 54 | |
|
| | 55 | | private void OnUserProfileUpdate(UserProfile profile) |
| | 56 | | { |
| 1 | 57 | | if (string.IsNullOrEmpty(profile.userId)) |
| 1 | 58 | | return; |
| | 59 | |
|
| 0 | 60 | | OnAllowedToCreateChannelsChanged?.Invoke(IsAllowedToCreateChannels()); |
| 0 | 61 | | } |
| | 62 | |
|
| | 63 | | public bool IsAllowedToCreateChannels() |
| | 64 | | { |
| 1 | 65 | | if (!featureFlags.Get().IsFeatureEnabled(FEATURE_FLAG_FOR_USERS_ALLOWED_TO_CREATE_CHANNELS)) |
| 0 | 66 | | return false; |
| | 67 | |
|
| 1 | 68 | | UserProfile ownUserProfile = userProfileBridge.GetOwn(); |
| 1 | 69 | | if (ownUserProfile == null) return false; |
| 1 | 70 | | if (string.IsNullOrEmpty(ownUserProfile.userId)) return false; |
| | 71 | |
|
| 1 | 72 | | FeatureFlagVariantPayload usersAllowedToCreateChannelsPayload = featureFlags |
| | 73 | | .Get() |
| | 74 | | .GetFeatureFlagVariantPayload($"{FEATURE_FLAG_FOR_USERS_ALLOWED_TO_CREATE_CHANNELS}:{VARIANT_FOR_USERS_A |
| | 75 | |
|
| 1 | 76 | | if (usersAllowedToCreateChannelsPayload == null) |
| 0 | 77 | | return false; |
| | 78 | |
|
| 1 | 79 | | UsersAllowedToCreateChannelsVariantPayload allowedUsersData = JsonUtility.FromJson<UsersAllowedToCreateChann |
| | 80 | |
|
| 1 | 81 | | switch (allowedUsersData.mode) |
| | 82 | | { |
| | 83 | | case AllowChannelsCreationMode.ALLOWLIST: |
| 1 | 84 | | return allowedUsersData.allowList != null |
| 0 | 85 | | && allowedUsersData.allowList.Any(userId => userId?.ToLower() == ownUserProfile.userId.ToLowe |
| | 86 | | case AllowChannelsCreationMode.NAMES: |
| 0 | 87 | | return ownUserProfile.hasClaimedName; |
| | 88 | | case AllowChannelsCreationMode.WALLET: |
| 0 | 89 | | return !ownUserProfile.isGuest; |
| | 90 | | } |
| | 91 | |
|
| 0 | 92 | | return true; |
| | 93 | | } |
| | 94 | |
|
| 1 | 95 | | public bool IsPromoteChannelsToastEnabled() => featureFlags.Get().IsFeatureEnabled(FEATURE_FLAG_FOR_PROMOTE_CHAN |
| | 96 | | } |
| | 97 | | } |