| | 1 | | using System; |
| | 2 | | using System.Linq; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | namespace 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 | | private const string FEATURE_FLAG_FOR_PROMOTE_CHANNELS_TOAST = "promote_channels_toast"; |
| | 13 | |
|
| 5 | 14 | | private BaseVariable<FeatureFlag> featureFlags => dataStore.featureFlags.flags; |
| | 15 | |
|
| | 16 | | private readonly DataStore dataStore; |
| | 17 | | private readonly IUserProfileBridge userProfileBridge; |
| | 18 | |
|
| | 19 | | public event Action<bool> OnAllowedToCreateChannelsChanged; |
| | 20 | |
|
| 669 | 21 | | public ChannelsFeatureFlagService(DataStore dataStore, IUserProfileBridge userProfileBridge) |
| | 22 | | { |
| 669 | 23 | | this.dataStore = dataStore; |
| 669 | 24 | | this.userProfileBridge = userProfileBridge; |
| 669 | 25 | | } |
| | 26 | |
|
| | 27 | | public void Dispose() |
| | 28 | | { |
| 669 | 29 | | userProfileBridge.GetOwn().OnUpdate -= OnUserProfileUpdate; |
| 669 | 30 | | } |
| | 31 | |
|
| | 32 | | public void Initialize() |
| | 33 | | { |
| 668 | 34 | | userProfileBridge.GetOwn().OnUpdate += OnUserProfileUpdate; |
| 668 | 35 | | } |
| | 36 | |
|
| 2 | 37 | | public bool IsChannelsFeatureEnabled() => featureFlags.Get().IsFeatureEnabled(FEATURE_FLAG_FOR_CHANNELS_FEATURE) |
| | 38 | |
|
| | 39 | | private void OnUserProfileUpdate(UserProfile profile) |
| | 40 | | { |
| 7 | 41 | | if (string.IsNullOrEmpty(profile.userId)) |
| 1 | 42 | | return; |
| | 43 | |
|
| 6 | 44 | | OnAllowedToCreateChannelsChanged?.Invoke(IsAllowedToCreateChannels()); |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | public bool IsAllowedToCreateChannels() |
| | 48 | | { |
| 1 | 49 | | if (!featureFlags.Get().IsFeatureEnabled(FEATURE_FLAG_FOR_USERS_ALLOWED_TO_CREATE_CHANNELS)) |
| 0 | 50 | | return false; |
| | 51 | |
|
| 1 | 52 | | FeatureFlagVariantPayload usersAllowedToCreateChannelsPayload = featureFlags |
| | 53 | | .Get() |
| | 54 | | .GetFeatureFlagVariantPayload($"{FEATURE_FLAG_FOR_USERS_ALLOWED_TO_CREATE_CHANNELS}:{VARIANT_FOR_USERS_A |
| | 55 | |
|
| 1 | 56 | | if (usersAllowedToCreateChannelsPayload == null) |
| 0 | 57 | | return false; |
| | 58 | |
|
| 1 | 59 | | UsersAllowedToCreateChannelsVariantPayload allowedUsersData = JsonUtility.FromJson<UsersAllowedToCreateChann |
| 1 | 60 | | UserProfile ownUserProfile = userProfileBridge.GetOwn(); |
| | 61 | |
|
| 1 | 62 | | switch (allowedUsersData.mode) |
| | 63 | | { |
| | 64 | | case AllowChannelsCreationMode.ALLOWLIST: |
| 1 | 65 | | return allowedUsersData.allowList != null |
| 0 | 66 | | && allowedUsersData.allowList.Any(userId => userId?.ToLower() == ownUserProfile.userId.ToLowe |
| | 67 | | case AllowChannelsCreationMode.NAMES: |
| 0 | 68 | | return ownUserProfile.hasClaimedName; |
| | 69 | | case AllowChannelsCreationMode.WALLET: |
| 0 | 70 | | return !ownUserProfile.isGuest; |
| | 71 | | } |
| | 72 | |
|
| 0 | 73 | | return true; |
| | 74 | | } |
| | 75 | |
|
| 1 | 76 | | public bool IsPromoteChannelsToastEnabled() => featureFlags.Get().IsFeatureEnabled(FEATURE_FLAG_FOR_PROMOTE_CHAN |
| | 77 | | } |
| | 78 | | } |