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