| | 1 | | using Cysharp.Threading.Tasks; |
| | 2 | | using DCL.Tasks; |
| | 3 | | using SocialFeaturesAnalytics; |
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Text.RegularExpressions; |
| | 7 | | using System.Threading; |
| | 8 | | using UnityEngine; |
| | 9 | |
|
| | 10 | | namespace DCL.Social.Friends |
| | 11 | | { |
| | 12 | | public class FriendsHUDController : IHUD |
| | 13 | | { |
| | 14 | | private const int LOAD_FRIENDS_ON_DEMAND_COUNT = 30; |
| | 15 | | private const int MAX_SEARCHED_FRIENDS = 100; |
| | 16 | | private const string ENABLE_QUICK_ACTIONS_FOR_FRIEND_REQUESTS_FLAG = "enable_quick_actions_on_friend_requests"; |
| | 17 | | private const int GET_FRIENDS_TIMEOUT = 10; |
| | 18 | |
|
| 3 | 19 | | private readonly Dictionary<string, FriendEntryModel> friends = new (); |
| 3 | 20 | | private readonly Dictionary<string, FriendEntryModel> onlineFriends = new (); |
| | 21 | | private readonly DataStore dataStore; |
| | 22 | | private readonly IFriendsController friendsController; |
| | 23 | | private readonly IUserProfileBridge userProfileBridge; |
| | 24 | | private readonly ISocialAnalytics socialAnalytics; |
| | 25 | | private readonly IChatController chatController; |
| | 26 | | private readonly IMouseCatcher mouseCatcher; |
| 3 | 27 | | private readonly Regex ethAddressRegex = new (@"^0x[a-fA-F0-9]{40}$"); |
| | 28 | |
|
| 12 | 29 | | private BaseVariable<HashSet<string>> visibleTaskbarPanels => dataStore.HUDs.visibleTaskbarPanels; |
| 0 | 30 | | private bool isQuickActionsForFriendRequestsEnabled => dataStore.featureFlags.flags.Get().IsFeatureEnabled(ENABL |
| 3 | 31 | | private CancellationTokenSource friendOperationsCancellationToken = new (); |
| 3 | 32 | | private CancellationTokenSource ensureProfilesCancellationToken = new (); |
| | 33 | | private UserProfile ownUserProfile; |
| | 34 | | private bool searchingFriends; |
| | 35 | | private int lastSkipForFriends; |
| | 36 | | private int lastSkipForFriendRequests; |
| | 37 | |
|
| 17 | 38 | | public bool IsVisible { get; private set; } |
| 103 | 39 | | public IFriendsHUDComponentView View { get; private set; } |
| | 40 | |
|
| | 41 | | public event Action<string> OnPressWhisper; |
| | 42 | | public event Action OnOpened; |
| | 43 | | public event Action OnClosed; |
| | 44 | | public event Action OnViewClosed; |
| | 45 | |
|
| 3 | 46 | | public FriendsHUDController(DataStore dataStore, |
| | 47 | | IFriendsController friendsController, |
| | 48 | | IUserProfileBridge userProfileBridge, |
| | 49 | | ISocialAnalytics socialAnalytics, |
| | 50 | | IChatController chatController, |
| | 51 | | IMouseCatcher mouseCatcher) |
| | 52 | | { |
| 3 | 53 | | this.dataStore = dataStore; |
| 3 | 54 | | this.friendsController = friendsController; |
| 3 | 55 | | this.userProfileBridge = userProfileBridge; |
| 3 | 56 | | this.socialAnalytics = socialAnalytics; |
| 3 | 57 | | this.chatController = chatController; |
| 3 | 58 | | this.mouseCatcher = mouseCatcher; |
| 3 | 59 | | } |
| | 60 | |
|
| | 61 | | public void Initialize(IFriendsHUDComponentView view, bool isVisible = true) |
| | 62 | | { |
| 3 | 63 | | View = view; |
| | 64 | |
|
| 3 | 65 | | view.Initialize(chatController, friendsController, socialAnalytics); |
| 3 | 66 | | view.RefreshFriendsTab(); |
| 3 | 67 | | view.OnFriendRequestApproved += HandleRequestAccepted; |
| 3 | 68 | | view.OnCancelConfirmation += HandleRequestCancelled; |
| 3 | 69 | | view.OnRejectConfirmation += HandleRequestRejected; |
| 3 | 70 | | view.OnFriendRequestSent += HandleRequestFriendship; |
| 3 | 71 | | view.OnFriendRequestOpened += OpenFriendRequestDetails; |
| 3 | 72 | | view.OnWhisper += HandleOpenWhisperChat; |
| 3 | 73 | | view.OnClose += HandleViewClosed; |
| 3 | 74 | | view.OnRequireMoreFriends += DisplayMoreFriends; |
| 3 | 75 | | view.OnRequireMoreFriendRequests += DisplayMoreFriendRequests; |
| 3 | 76 | | view.OnSearchFriendsRequested += SearchFriends; |
| 3 | 77 | | view.OnFriendListDisplayed += DisplayFriendsIfAnyIsLoaded; |
| 3 | 78 | | view.OnRequestListDisplayed += DisplayFriendRequestsIfAnyIsLoaded; |
| | 79 | |
|
| 3 | 80 | | if (mouseCatcher != null) |
| 2 | 81 | | mouseCatcher.OnMouseLock += HandleViewClosed; |
| | 82 | |
|
| 3 | 83 | | ownUserProfile = userProfileBridge.GetOwn(); |
| 3 | 84 | | ownUserProfile.OnUpdate -= HandleProfileUpdated; |
| 3 | 85 | | ownUserProfile.OnUpdate += HandleProfileUpdated; |
| | 86 | |
|
| 3 | 87 | | friendsController.OnUpdateFriendship += HandleFriendshipUpdated; |
| 3 | 88 | | friendsController.OnUpdateUserStatus += HandleUserStatusUpdated; |
| 3 | 89 | | friendsController.OnFriendNotFound += OnFriendNotFound; |
| 3 | 90 | | friendsController.OnFriendRequestReceived += ShowFriendRequest; |
| | 91 | |
|
| 3 | 92 | | if (friendsController.IsInitialized) |
| 0 | 93 | | view.HideLoadingSpinner(); |
| | 94 | | else |
| | 95 | | { |
| 3 | 96 | | view.ShowLoadingSpinner(); |
| 3 | 97 | | friendsController.OnInitialized -= HandleFriendsInitialized; |
| 3 | 98 | | friendsController.OnInitialized += HandleFriendsInitialized; |
| | 99 | | } |
| | 100 | |
|
| 3 | 101 | | ShowOrHideMoreFriendsToLoadHint(); |
| 3 | 102 | | ShowOrHideMoreFriendRequestsToLoadHint(); |
| | 103 | |
|
| 3 | 104 | | SetVisibility(isVisible); |
| 3 | 105 | | IsVisible = isVisible; |
| 3 | 106 | | } |
| | 107 | |
|
| | 108 | | private void SetVisiblePanelList(bool visible) |
| | 109 | | { |
| 6 | 110 | | HashSet<string> newSet = visibleTaskbarPanels.Get(); |
| | 111 | |
|
| 6 | 112 | | if (visible) |
| 4 | 113 | | newSet.Add("FriendsPanel"); |
| | 114 | | else |
| 2 | 115 | | newSet.Remove("FriendsPanel"); |
| | 116 | |
|
| 6 | 117 | | visibleTaskbarPanels.Set(newSet, true); |
| 6 | 118 | | } |
| | 119 | |
|
| | 120 | | public void Dispose() |
| | 121 | | { |
| 4 | 122 | | friendOperationsCancellationToken?.SafeCancelAndDispose(); |
| 4 | 123 | | friendOperationsCancellationToken = null; |
| 4 | 124 | | ensureProfilesCancellationToken?.SafeCancelAndDispose(); |
| 4 | 125 | | ensureProfilesCancellationToken = null; |
| | 126 | |
|
| 4 | 127 | | friendsController.OnInitialized -= HandleFriendsInitialized; |
| 4 | 128 | | friendsController.OnUpdateFriendship -= HandleFriendshipUpdated; |
| 4 | 129 | | friendsController.OnUpdateUserStatus -= HandleUserStatusUpdated; |
| | 130 | |
|
| 4 | 131 | | if (View != null) |
| | 132 | | { |
| 4 | 133 | | View.OnFriendRequestApproved -= HandleRequestAccepted; |
| 4 | 134 | | View.OnCancelConfirmation -= HandleRequestCancelled; |
| 4 | 135 | | View.OnRejectConfirmation -= HandleRequestRejected; |
| 4 | 136 | | View.OnFriendRequestSent -= HandleRequestFriendship; |
| 4 | 137 | | View.OnFriendRequestOpened -= OpenFriendRequestDetails; |
| 4 | 138 | | View.OnWhisper -= HandleOpenWhisperChat; |
| 4 | 139 | | View.OnClose -= HandleViewClosed; |
| 4 | 140 | | View.OnRequireMoreFriends -= DisplayMoreFriends; |
| 4 | 141 | | View.OnRequireMoreFriendRequests -= DisplayMoreFriendRequests; |
| 4 | 142 | | View.OnSearchFriendsRequested -= SearchFriends; |
| 4 | 143 | | View.OnFriendListDisplayed -= DisplayFriendsIfAnyIsLoaded; |
| 4 | 144 | | View.OnRequestListDisplayed -= DisplayFriendRequestsIfAnyIsLoaded; |
| 4 | 145 | | View.Dispose(); |
| | 146 | | } |
| | 147 | |
|
| 4 | 148 | | if (ownUserProfile != null) |
| 4 | 149 | | ownUserProfile.OnUpdate -= HandleProfileUpdated; |
| | 150 | |
|
| 4 | 151 | | if (userProfileBridge != null) |
| | 152 | | { |
| 8 | 153 | | foreach (string friendId in friends.Keys) |
| | 154 | | { |
| 0 | 155 | | var profile = userProfileBridge.Get(friendId); |
| 0 | 156 | | if (profile == null) continue; |
| 0 | 157 | | profile.OnUpdate -= HandleFriendProfileUpdated; |
| | 158 | | } |
| | 159 | | } |
| 4 | 160 | | } |
| | 161 | |
|
| | 162 | | public void SetVisibility(bool visible) |
| | 163 | | { |
| 8 | 164 | | if (IsVisible == visible) |
| 2 | 165 | | return; |
| | 166 | |
|
| 6 | 167 | | IsVisible = visible; |
| 6 | 168 | | SetVisiblePanelList(visible); |
| | 169 | |
|
| 6 | 170 | | if (visible) |
| | 171 | | { |
| 4 | 172 | | lastSkipForFriends = 0; |
| 4 | 173 | | lastSkipForFriendRequests = 0; |
| 4 | 174 | | View.ClearAll(); |
| 4 | 175 | | View.Show(); |
| 4 | 176 | | UpdateNotificationsCounter(); |
| | 177 | |
|
| 8 | 178 | | foreach (var friend in onlineFriends) |
| 0 | 179 | | View.Set(friend.Key, friend.Value); |
| | 180 | |
|
| 4 | 181 | | if (View.IsFriendListActive) |
| 1 | 182 | | DisplayMoreFriends(); |
| 3 | 183 | | else if (View.IsRequestListActive) |
| 0 | 184 | | DisplayMoreFriendRequests(); |
| | 185 | |
|
| 4 | 186 | | OnOpened?.Invoke(); |
| | 187 | | } |
| | 188 | | else |
| | 189 | | { |
| 2 | 190 | | View.Hide(); |
| 2 | 191 | | View.DisableSearchMode(); |
| 2 | 192 | | searchingFriends = false; |
| 2 | 193 | | OnClosed?.Invoke(); |
| | 194 | | } |
| 0 | 195 | | } |
| | 196 | |
|
| | 197 | | private void HandleViewClosed() |
| | 198 | | { |
| 0 | 199 | | OnViewClosed?.Invoke(); |
| 0 | 200 | | SetVisibility(false); |
| 0 | 201 | | } |
| | 202 | |
|
| | 203 | | private void HandleFriendsInitialized() |
| | 204 | | { |
| 0 | 205 | | friendsController.OnInitialized -= HandleFriendsInitialized; |
| 0 | 206 | | View.HideLoadingSpinner(); |
| | 207 | |
|
| 0 | 208 | | if (View.IsActive()) |
| | 209 | | { |
| 0 | 210 | | if (View.IsFriendListActive && lastSkipForFriends <= 0) |
| 0 | 211 | | DisplayMoreFriendsAsync(RestartFriendsOperationsCancellationToken()).Forget(); |
| 0 | 212 | | else if (View.IsRequestListActive && lastSkipForFriendRequests <= 0 && !searchingFriends) |
| 0 | 213 | | DisplayMoreFriendRequestsAsync(RestartFriendsOperationsCancellationToken()).Forget(); |
| | 214 | | } |
| | 215 | |
|
| 0 | 216 | | UpdateNotificationsCounter(); |
| 0 | 217 | | } |
| | 218 | |
|
| | 219 | | private void HandleProfileUpdated(UserProfile profile) => |
| 0 | 220 | | UpdateBlockStatus(profile).Forget(); |
| | 221 | |
|
| | 222 | | private async UniTask UpdateBlockStatus(UserProfile profile) |
| | 223 | | { |
| | 224 | | const int ITERATIONS_PER_FRAME = 10; |
| | 225 | |
|
| 0 | 226 | | var iterations = 0; |
| | 227 | |
|
| 0 | 228 | | foreach (var friendPair in friends) |
| | 229 | | { |
| 0 | 230 | | string friendId = friendPair.Key; |
| 0 | 231 | | var model = friendPair.Value; |
| | 232 | |
|
| 0 | 233 | | model.blocked = profile.blocked?.Contains(friendId) ?? false; |
| 0 | 234 | | await UniTask.SwitchToMainThread(); |
| 0 | 235 | | View.UpdateBlockStatus(friendId, model.blocked); |
| | 236 | |
|
| 0 | 237 | | iterations++; |
| | 238 | |
|
| 0 | 239 | | if (iterations > 0 && iterations % ITERATIONS_PER_FRAME == 0) |
| 0 | 240 | | await UniTask.NextFrame(); |
| 0 | 241 | | } |
| 0 | 242 | | } |
| | 243 | |
|
| | 244 | | private void HandleRequestFriendship(string userNameOrId) |
| | 245 | | { |
| | 246 | | async UniTaskVoid HandleRequestFriendshipAsync(string userNameOrId, CancellationToken cancellationToken) |
| | 247 | | { |
| 0 | 248 | | if (AreAlreadyFriends(userNameOrId)) |
| 0 | 249 | | View.ShowRequestSendError(FriendRequestError.AlreadyFriends); |
| | 250 | | else |
| | 251 | | { |
| | 252 | | try |
| | 253 | | { |
| 0 | 254 | | string userId = SolveUserIdFromUserInput(userNameOrId); |
| | 255 | |
|
| 0 | 256 | | if (!string.IsNullOrEmpty(userId)) |
| | 257 | | { |
| 0 | 258 | | FriendRequest request = await friendsController.RequestFriendshipAsync(userId, "", cancellat |
| | 259 | |
|
| 0 | 260 | | ShowFriendRequest(request); |
| | 261 | |
|
| 0 | 262 | | socialAnalytics.SendFriendRequestSent(request.From, request.To, request.MessageBody?.Length |
| | 263 | | PlayerActionSource.FriendsHUD, request.FriendRequestId); |
| | 264 | | } |
| | 265 | | else |
| 0 | 266 | | View.ShowRequestSendError(FriendRequestError.UserNotFound); |
| 0 | 267 | | } |
| 0 | 268 | | catch (Exception e) when (e is not OperationCanceledException) |
| | 269 | | { |
| 0 | 270 | | e.ReportFriendRequestErrorToAnalyticsAsSender(userNameOrId, PlayerActionSource.FriendsHUD.ToStri |
| | 271 | | userProfileBridge, socialAnalytics); |
| | 272 | |
|
| 0 | 273 | | Debug.LogException(e); |
| 0 | 274 | | } |
| | 275 | |
|
| 0 | 276 | | View.ShowRequestSendSuccess(); |
| | 277 | | } |
| 0 | 278 | | } |
| | 279 | |
|
| 0 | 280 | | HandleRequestFriendshipAsync(userNameOrId, RestartFriendsOperationsCancellationToken()).Forget(); |
| 0 | 281 | | } |
| | 282 | |
|
| | 283 | | private string SolveUserIdFromUserInput(string userNameOrId) |
| | 284 | | { |
| 0 | 285 | | Match ethAddressMatch = ethAddressRegex.Match(userNameOrId); |
| | 286 | |
|
| 0 | 287 | | if (ethAddressMatch.Success) |
| 0 | 288 | | return userNameOrId; |
| | 289 | |
|
| | 290 | | // TODO: a request to the catalyst is needed to retrieve a user profile by user name |
| | 291 | | // the user may exist with the name but not loaded in the current catalog |
| 0 | 292 | | UserProfile profile = userProfileBridge.GetByName(userNameOrId, false); |
| 0 | 293 | | return profile?.userId ?? ""; |
| | 294 | | } |
| | 295 | |
|
| | 296 | | private bool AreAlreadyFriends(string userNameOrId) |
| | 297 | | { |
| 0 | 298 | | var userId = userNameOrId; |
| 0 | 299 | | var profile = userProfileBridge.GetByName(userNameOrId); |
| | 300 | |
|
| 0 | 301 | | if (profile != default) |
| 0 | 302 | | userId = profile.userId; |
| | 303 | |
|
| 0 | 304 | | return friendsController != null |
| | 305 | | && friendsController.ContainsStatus(userId, FriendshipStatus.FRIEND); |
| | 306 | | } |
| | 307 | |
|
| | 308 | | private void HandleUserStatusUpdated(string userId, UserStatus status) => |
| 0 | 309 | | UpdateUserStatus(userId, status); |
| | 310 | |
|
| | 311 | | private void UpdateUserStatus(string userId, UserStatus status) |
| | 312 | | { |
| 0 | 313 | | switch (status.friendshipStatus) |
| | 314 | | { |
| | 315 | | case FriendshipStatus.FRIEND: |
| 0 | 316 | | var friend = friends.ContainsKey(userId) |
| | 317 | | ? new FriendEntryModel(friends[userId]) |
| | 318 | | : new FriendEntryModel(); |
| | 319 | |
|
| 0 | 320 | | friend.CopyFrom(status); |
| 0 | 321 | | friend.blocked = IsUserBlocked(userId); |
| 0 | 322 | | friends[userId] = friend; |
| 0 | 323 | | View.Set(userId, friend); |
| | 324 | |
|
| 0 | 325 | | if (status.presence == PresenceStatus.ONLINE) |
| 0 | 326 | | onlineFriends[userId] = friend; |
| | 327 | | else |
| 0 | 328 | | onlineFriends.Remove(userId); |
| | 329 | |
|
| 0 | 330 | | break; |
| | 331 | | case FriendshipStatus.NOT_FRIEND: |
| 0 | 332 | | View.Remove(userId); |
| 0 | 333 | | friends.Remove(userId); |
| 0 | 334 | | onlineFriends.Remove(userId); |
| | 335 | | break; |
| | 336 | | } |
| | 337 | |
|
| 0 | 338 | | UpdateNotificationsCounter(); |
| 0 | 339 | | ShowOrHideMoreFriendsToLoadHint(); |
| 0 | 340 | | ShowOrHideMoreFriendRequestsToLoadHint(); |
| 0 | 341 | | } |
| | 342 | |
|
| | 343 | | private void HandleFriendshipUpdated(string userId, FriendshipAction friendshipAction) |
| | 344 | | { |
| | 345 | | async UniTaskVoid HandleFriendshipUpdatedAsync(string userId, FriendshipAction friendshipAction, Cancellatio |
| | 346 | | { |
| 0 | 347 | | var userProfile = userProfileBridge.Get(userId); |
| | 348 | |
|
| | 349 | | switch (friendshipAction) |
| | 350 | | { |
| | 351 | | case FriendshipAction.NONE: |
| | 352 | | case FriendshipAction.REJECTED: |
| | 353 | | case FriendshipAction.CANCELLED: |
| | 354 | | case FriendshipAction.DELETED: |
| 0 | 355 | | RemoveFriendship(userId); |
| 0 | 356 | | break; |
| | 357 | | case FriendshipAction.APPROVED: |
| 0 | 358 | | userProfile = await EnsureProfileOrShowFallbackFriend(userId, userProfile, cancellationToken); |
| | 359 | |
|
| 0 | 360 | | var approved = friends.ContainsKey(userId) |
| | 361 | | ? new FriendEntryModel(friends[userId]) |
| | 362 | | : new FriendEntryModel(); |
| | 363 | |
|
| 0 | 364 | | approved.CopyFrom(userProfile); |
| 0 | 365 | | approved.blocked = IsUserBlocked(userId); |
| 0 | 366 | | friends[userId] = approved; |
| 0 | 367 | | View.Set(userId, approved); |
| 0 | 368 | | userProfile.OnUpdate -= HandleFriendProfileUpdated; |
| 0 | 369 | | userProfile.OnUpdate += HandleFriendProfileUpdated; |
| 0 | 370 | | break; |
| | 371 | | case FriendshipAction.REQUESTED_FROM: |
| 0 | 372 | | userProfile = await EnsureProfileOrShowFallbackFriend(userId, userProfile, cancellationToken); |
| | 373 | |
|
| 0 | 374 | | var requestReceived = friends.ContainsKey(userId) |
| | 375 | | ? new FriendRequestEntryModel(friends[userId], string.Empty, true, new DateTime(0), isQuickA |
| | 376 | | : new FriendRequestEntryModel { bodyMessage = string.Empty, isReceived = true, timestamp = n |
| | 377 | |
|
| 0 | 378 | | requestReceived.CopyFrom(userProfile); |
| 0 | 379 | | requestReceived.blocked = IsUserBlocked(userId); |
| 0 | 380 | | friends[userId] = requestReceived; |
| 0 | 381 | | View.Set(userId, requestReceived); |
| 0 | 382 | | userProfile.OnUpdate -= HandleFriendProfileUpdated; |
| 0 | 383 | | userProfile.OnUpdate += HandleFriendProfileUpdated; |
| 0 | 384 | | break; |
| | 385 | | case FriendshipAction.REQUESTED_TO: |
| 0 | 386 | | userProfile = await EnsureProfileOrShowFallbackFriend(userId, userProfile, cancellationToken); |
| | 387 | |
|
| 0 | 388 | | var requestSent = friends.ContainsKey(userId) |
| | 389 | | ? new FriendRequestEntryModel(friends[userId], string.Empty, false, new DateTime(0), isQuick |
| | 390 | | : new FriendRequestEntryModel { bodyMessage = string.Empty, isReceived = false, timestamp = |
| | 391 | |
|
| 0 | 392 | | requestSent.CopyFrom(userProfile); |
| 0 | 393 | | requestSent.blocked = IsUserBlocked(userId); |
| 0 | 394 | | friends[userId] = requestSent; |
| 0 | 395 | | View.Set(userId, requestSent); |
| 0 | 396 | | userProfile.OnUpdate -= HandleFriendProfileUpdated; |
| 0 | 397 | | userProfile.OnUpdate += HandleFriendProfileUpdated; |
| | 398 | | break; |
| | 399 | | } |
| | 400 | |
|
| 0 | 401 | | UpdateNotificationsCounter(); |
| 0 | 402 | | ShowOrHideMoreFriendsToLoadHint(); |
| 0 | 403 | | ShowOrHideMoreFriendRequestsToLoadHint(); |
| 0 | 404 | | } |
| | 405 | |
|
| 0 | 406 | | HandleFriendshipUpdatedAsync(userId, friendshipAction, ensureProfilesCancellationToken.Token).Forget(); |
| 0 | 407 | | } |
| | 408 | |
|
| | 409 | | private async UniTask<UserProfile> EnsureProfileOrShowFallbackFriend(string userId, UserProfile userProfile, Can |
| | 410 | | { |
| 0 | 411 | | try { userProfile ??= await userProfileBridge.RequestFullUserProfileAsync(userId, cancellationToken); } |
| 0 | 412 | | catch (Exception e) when (e is not OperationCanceledException) |
| | 413 | | { |
| 0 | 414 | | FriendEntryModel fallbackModel = new () |
| | 415 | | { |
| | 416 | | userId = userId, |
| | 417 | | userName = userId, |
| | 418 | | blocked = IsUserBlocked(userId), |
| | 419 | | }; |
| | 420 | |
|
| 0 | 421 | | friends[userId] = fallbackModel; |
| 0 | 422 | | View.Set(userId, fallbackModel); |
| | 423 | |
|
| 0 | 424 | | throw; |
| | 425 | | } |
| | 426 | |
|
| 0 | 427 | | return userProfile; |
| 0 | 428 | | } |
| | 429 | |
|
| | 430 | | private void RemoveFriendship(string userId) |
| | 431 | | { |
| 0 | 432 | | friends.Remove(userId); |
| 0 | 433 | | View.Remove(userId); |
| 0 | 434 | | onlineFriends.Remove(userId); |
| 0 | 435 | | } |
| | 436 | |
|
| | 437 | | private void HandleFriendProfileUpdated(UserProfile profile) |
| | 438 | | { |
| 0 | 439 | | var userId = profile.userId; |
| 0 | 440 | | if (!friends.ContainsKey(userId)) return; |
| 0 | 441 | | friends[userId].CopyFrom(profile); |
| | 442 | |
|
| 0 | 443 | | var status = friendsController.GetUserStatus(profile.userId); |
| 0 | 444 | | if (status == null) return; |
| | 445 | |
|
| 0 | 446 | | UpdateUserStatus(userId, status); |
| 0 | 447 | | } |
| | 448 | |
|
| | 449 | | private bool IsUserBlocked(string userId) |
| | 450 | | { |
| 0 | 451 | | if (ownUserProfile != null && ownUserProfile.blocked != null) |
| 0 | 452 | | return ownUserProfile.blocked.Contains(userId); |
| | 453 | |
|
| 0 | 454 | | return false; |
| | 455 | | } |
| | 456 | |
|
| | 457 | | private void OnFriendNotFound(string name) |
| | 458 | | { |
| 0 | 459 | | View.DisplayFriendUserNotFound(); |
| 0 | 460 | | } |
| | 461 | |
|
| | 462 | | private void UpdateNotificationsCounter() |
| | 463 | | { |
| 4 | 464 | | if (View.IsActive()) |
| 4 | 465 | | dataStore.friendNotifications.seenFriends.Set(View.FriendCount); |
| | 466 | |
|
| 4 | 467 | | dataStore.friendNotifications.pendingFriendRequestCount.Set(friendsController.ReceivedRequestCount); |
| 4 | 468 | | } |
| | 469 | |
|
| | 470 | | private void HandleOpenWhisperChat(FriendEntryModel entry) => |
| 0 | 471 | | OnPressWhisper?.Invoke(entry.userId); |
| | 472 | |
|
| | 473 | | private void HandleRequestRejected(FriendRequestEntryModel entry) |
| | 474 | | { |
| 0 | 475 | | HandleRequestRejectedAsync(entry.userId, RestartFriendsOperationsCancellationToken()).Forget(); |
| 0 | 476 | | } |
| | 477 | |
|
| | 478 | | private async UniTaskVoid HandleRequestRejectedAsync(string userId, CancellationToken cancellationToken) |
| | 479 | | { |
| | 480 | | try |
| | 481 | | { |
| 0 | 482 | | FriendRequest request = await friendsController.RejectFriendshipAsync(userId, cancellationToken); |
| | 483 | |
|
| 0 | 484 | | socialAnalytics.SendFriendRequestRejected(request.From, request.To, |
| | 485 | | PlayerActionSource.FriendsHUD.ToString(), request.HasBodyMessage, request.FriendRequestId); |
| | 486 | |
|
| 0 | 487 | | RemoveFriendship(userId); |
| 0 | 488 | | } |
| 0 | 489 | | catch (Exception e) when (e is not OperationCanceledException) |
| | 490 | | { |
| 0 | 491 | | e.ReportFriendRequestErrorToAnalyticsByUserId(userId, PlayerActionSource.FriendsHUD.ToString(), |
| | 492 | | friendsController, socialAnalytics); |
| | 493 | |
|
| 0 | 494 | | throw; |
| | 495 | | } |
| | 496 | |
|
| 0 | 497 | | UpdateNotificationsCounter(); |
| 0 | 498 | | } |
| | 499 | |
|
| | 500 | | private async UniTaskVoid HandleRequestCancelledAsync(string userId, CancellationToken cancellationToken) |
| | 501 | | { |
| | 502 | | try |
| | 503 | | { |
| 0 | 504 | | FriendRequest request = await friendsController.CancelRequestByUserIdAsync(userId, cancellationToken); |
| | 505 | |
|
| 0 | 506 | | socialAnalytics.SendFriendRequestCancelled(request.From, request.To, |
| | 507 | | PlayerActionSource.FriendsHUD.ToString(), request.FriendRequestId); |
| | 508 | |
|
| 0 | 509 | | RemoveFriendship(userId); |
| 0 | 510 | | } |
| 0 | 511 | | catch (Exception e) when (e is not OperationCanceledException) |
| | 512 | | { |
| 0 | 513 | | e.ReportFriendRequestErrorToAnalyticsByUserId(userId, PlayerActionSource.FriendsHUD.ToString(), |
| | 514 | | friendsController, socialAnalytics); |
| | 515 | |
|
| 0 | 516 | | throw; |
| | 517 | | } |
| 0 | 518 | | } |
| | 519 | |
|
| | 520 | | private void HandleRequestCancelled(FriendRequestEntryModel entry) |
| | 521 | | { |
| 0 | 522 | | HandleRequestCancelledAsync(entry.userId, RestartFriendsOperationsCancellationToken()).Forget(); |
| 0 | 523 | | } |
| | 524 | |
|
| | 525 | | private void HandleRequestAccepted(FriendRequestEntryModel entry) |
| | 526 | | { |
| 0 | 527 | | HandleRequestAcceptedAsync(entry.userId, RestartFriendsOperationsCancellationToken()).Forget(); |
| 0 | 528 | | } |
| | 529 | |
|
| | 530 | | private async UniTaskVoid HandleRequestAcceptedAsync(string userId, CancellationToken cancellationToken) |
| | 531 | | { |
| | 532 | | try |
| | 533 | | { |
| 0 | 534 | | FriendRequest request = friendsController.GetAllocatedFriendRequestByUser(userId); |
| 0 | 535 | | request = await friendsController.AcceptFriendshipAsync(request.FriendRequestId, cancellationToken); |
| | 536 | |
|
| 0 | 537 | | socialAnalytics.SendFriendRequestApproved(request.From, request.To, |
| | 538 | | PlayerActionSource.FriendsHUD.ToString(), |
| | 539 | | request.HasBodyMessage, |
| | 540 | | request.FriendRequestId); |
| 0 | 541 | | } |
| 0 | 542 | | catch (Exception e) when (e is not OperationCanceledException) |
| | 543 | | { |
| 0 | 544 | | e.ReportFriendRequestErrorToAnalyticsByUserId(userId, PlayerActionSource.FriendsHUD.ToString(), |
| | 545 | | friendsController, socialAnalytics); |
| | 546 | |
|
| 0 | 547 | | throw; |
| | 548 | | } |
| 0 | 549 | | } |
| | 550 | |
|
| | 551 | | private void DisplayFriendsIfAnyIsLoaded() |
| | 552 | | { |
| 0 | 553 | | if (lastSkipForFriends > 0) return; |
| 0 | 554 | | if (!friendsController.IsInitialized) return; |
| 0 | 555 | | DisplayMoreFriendsAsync(RestartFriendsOperationsCancellationToken()).Forget(); |
| 0 | 556 | | } |
| | 557 | |
|
| | 558 | | private void DisplayMoreFriends() |
| | 559 | | { |
| 2 | 560 | | if (!friendsController.IsInitialized) return; |
| 0 | 561 | | DisplayMoreFriendsAsync(RestartFriendsOperationsCancellationToken()).Forget(); |
| 0 | 562 | | } |
| | 563 | |
|
| | 564 | | private async UniTask DisplayMoreFriendsAsync(CancellationToken cancellationToken) |
| | 565 | | { |
| 0 | 566 | | string[] friendsToAdd = await friendsController |
| | 567 | | .GetFriendsAsync(LOAD_FRIENDS_ON_DEMAND_COUNT, lastSkipForFriends, cancellation |
| | 568 | | .Timeout(TimeSpan.FromSeconds(GET_FRIENDS_TIMEOUT)); |
| | 569 | |
|
| 0 | 570 | | for (var i = 0; i < friendsToAdd.Length; i++) |
| 0 | 571 | | HandleFriendshipUpdated(friendsToAdd[i], FriendshipAction.APPROVED); |
| | 572 | |
|
| | 573 | | // We are not handling properly the case when the friends are not fetched correctly from server. |
| | 574 | | // 'lastSkipForFriends' will have an invalid value. |
| | 575 | | // this may happen only on the old flow.. the task operation should throw an exception if anything goes wron |
| 0 | 576 | | lastSkipForFriends += LOAD_FRIENDS_ON_DEMAND_COUNT; |
| | 577 | |
|
| 0 | 578 | | ShowOrHideMoreFriendsToLoadHint(); |
| 0 | 579 | | } |
| | 580 | |
|
| | 581 | | private void DisplayMoreFriendRequests() |
| | 582 | | { |
| 0 | 583 | | if (!friendsController.IsInitialized) return; |
| 0 | 584 | | if (searchingFriends) return; |
| 0 | 585 | | DisplayMoreFriendRequestsAsync(RestartFriendsOperationsCancellationToken()).Forget(); |
| 0 | 586 | | } |
| | 587 | |
|
| | 588 | | private async UniTask DisplayMoreFriendRequestsAsync(CancellationToken cancellationToken) |
| | 589 | | { |
| 0 | 590 | | var allFriendRequests = await friendsController.GetFriendRequestsAsync( |
| | 591 | | LOAD_FRIENDS_ON_DEMAND_COUNT, lastSkipForFriendRequests, |
| | 592 | | LOAD_FRIENDS_ON_DEMAND_COUNT, lastSkipForFriendRequests, |
| | 593 | | cancellationToken); |
| | 594 | |
|
| 0 | 595 | | AddFriendRequests(allFriendRequests); |
| | 596 | |
|
| | 597 | | // We are not handling properly the case when the friend requests are not fetched correctly from server. |
| | 598 | | // 'lastSkipForFriendRequests' will have an invalid value. |
| | 599 | | // this may happen only on the old flow.. the task operation should throw an exception if anything goes wron |
| 0 | 600 | | lastSkipForFriendRequests += LOAD_FRIENDS_ON_DEMAND_COUNT; |
| | 601 | |
|
| 0 | 602 | | ShowOrHideMoreFriendRequestsToLoadHint(); |
| 0 | 603 | | } |
| | 604 | |
|
| | 605 | | private void AddFriendRequests(IEnumerable<FriendRequest> friendRequests) |
| | 606 | | { |
| 0 | 607 | | if (friendRequests == null) |
| 0 | 608 | | return; |
| | 609 | |
|
| 0 | 610 | | foreach (var friendRequest in friendRequests) |
| 0 | 611 | | ShowFriendRequest(friendRequest); |
| 0 | 612 | | } |
| | 613 | |
|
| | 614 | | private void ShowFriendRequest(FriendRequest friendRequest) |
| | 615 | | { |
| | 616 | | async UniTaskVoid ShowFriendRequestAsync(FriendRequest friendRequest, CancellationToken cancellationToken) |
| | 617 | | { |
| 0 | 618 | | bool isReceivedRequest = friendRequest.IsSentTo(ownUserProfile.userId); |
| 0 | 619 | | string userId = isReceivedRequest ? friendRequest.From : friendRequest.To; |
| 0 | 620 | | UserProfile userProfile = userProfileBridge.Get(userId); |
| | 621 | |
|
| 0 | 622 | | try { userProfile ??= await userProfileBridge.RequestFullUserProfileAsync(userId, cancellationToken); } |
| 0 | 623 | | catch (Exception e) when (e is not OperationCanceledException) |
| | 624 | | { |
| 0 | 625 | | FriendRequestEntryModel fallbackModel = new () |
| | 626 | | { |
| | 627 | | bodyMessage = friendRequest.MessageBody, |
| | 628 | | isReceived = isReceivedRequest, |
| | 629 | | timestamp = friendRequest.Timestamp, |
| | 630 | | isShortcutButtonsActive = isQuickActionsForFriendRequestsEnabled, |
| | 631 | | blocked = IsUserBlocked(userId), |
| | 632 | | userId = userId, |
| | 633 | | userName = userId, |
| | 634 | | }; |
| | 635 | |
|
| 0 | 636 | | friends[userId] = fallbackModel; |
| 0 | 637 | | onlineFriends.Remove(userId); |
| 0 | 638 | | View.Set(userId, fallbackModel); |
| | 639 | |
|
| 0 | 640 | | throw; |
| | 641 | | } |
| | 642 | |
|
| 0 | 643 | | var request = friends.ContainsKey(userId) |
| | 644 | | ? new FriendRequestEntryModel(friends[userId], friendRequest.MessageBody, isReceivedRequest, friendR |
| | 645 | | : new FriendRequestEntryModel |
| | 646 | | { |
| | 647 | | bodyMessage = friendRequest.MessageBody, |
| | 648 | | isReceived = isReceivedRequest, |
| | 649 | | timestamp = friendRequest.Timestamp, |
| | 650 | | isShortcutButtonsActive = isQuickActionsForFriendRequestsEnabled |
| | 651 | | }; |
| | 652 | |
|
| 0 | 653 | | request.CopyFrom(userProfile); |
| 0 | 654 | | request.blocked = IsUserBlocked(userId); |
| 0 | 655 | | friends[userId] = request; |
| 0 | 656 | | onlineFriends.Remove(userId); |
| 0 | 657 | | View.Set(userId, request); |
| 0 | 658 | | userProfile.OnUpdate -= HandleFriendProfileUpdated; |
| 0 | 659 | | userProfile.OnUpdate += HandleFriendProfileUpdated; |
| 0 | 660 | | } |
| | 661 | |
|
| 0 | 662 | | ShowFriendRequestAsync(friendRequest, ensureProfilesCancellationToken.Token).Forget(); |
| 0 | 663 | | } |
| | 664 | |
|
| | 665 | | private void DisplayFriendRequestsIfAnyIsLoaded() |
| | 666 | | { |
| 0 | 667 | | if (lastSkipForFriendRequests > 0) return; |
| 0 | 668 | | if (!friendsController.IsInitialized) return; |
| 0 | 669 | | if (searchingFriends) return; |
| 0 | 670 | | DisplayMoreFriendRequestsAsync(RestartFriendsOperationsCancellationToken()).Forget(); |
| 0 | 671 | | } |
| | 672 | |
|
| | 673 | | private void ShowOrHideMoreFriendRequestsToLoadHint() |
| | 674 | | { |
| 3 | 675 | | if (lastSkipForFriendRequests >= friendsController.TotalFriendRequestCount) |
| 3 | 676 | | View.HideMoreRequestsToLoadHint(); |
| | 677 | | else |
| 0 | 678 | | View.ShowMoreRequestsToLoadHint(Mathf.Clamp(friendsController.TotalFriendRequestCount - lastSkipForFrien |
| | 679 | | 0, |
| | 680 | | friendsController.TotalFriendRequestCount)); |
| 0 | 681 | | } |
| | 682 | |
|
| | 683 | | private void ShowOrHideMoreFriendsToLoadHint() |
| | 684 | | { |
| 3 | 685 | | if (lastSkipForFriends >= friendsController.TotalFriendCount || searchingFriends) |
| 3 | 686 | | View.HideMoreFriendsToLoadHint(); |
| | 687 | | else |
| 0 | 688 | | View.ShowMoreFriendsToLoadHint(Mathf.Clamp(friendsController.TotalFriendCount - lastSkipForFriends, |
| | 689 | | 0, |
| | 690 | | friendsController.TotalFriendCount)); |
| 0 | 691 | | } |
| | 692 | |
|
| | 693 | | private void SearchFriends(string search) |
| | 694 | | { |
| 0 | 695 | | SearchFriendsAsync(search, RestartFriendsOperationsCancellationToken()).Forget(); |
| 0 | 696 | | } |
| | 697 | |
|
| | 698 | | private async UniTask SearchFriendsAsync(string search, CancellationToken cancellationToken) |
| | 699 | | { |
| 0 | 700 | | if (string.IsNullOrEmpty(search)) |
| | 701 | | { |
| 0 | 702 | | View.DisableSearchMode(); |
| 0 | 703 | | searchingFriends = false; |
| 0 | 704 | | ShowOrHideMoreFriendsToLoadHint(); |
| 0 | 705 | | return; |
| | 706 | | } |
| | 707 | |
|
| 0 | 708 | | IReadOnlyList<string> friendsToAdd = await friendsController |
| | 709 | | .GetFriendsAsync(search, MAX_SEARCHED_FRIENDS, cancellationToken) |
| | 710 | | .Timeout(TimeSpan.FromSeconds(GET_FRIENDS_TIMEOUT)); |
| | 711 | |
|
| 0 | 712 | | for (int i = 0; i < friendsToAdd.Count; i++) |
| 0 | 713 | | HandleFriendshipUpdated(friendsToAdd[i], FriendshipAction.APPROVED); |
| | 714 | |
|
| 0 | 715 | | View.EnableSearchMode(); |
| 0 | 716 | | View.HideMoreFriendsToLoadHint(); |
| 0 | 717 | | searchingFriends = true; |
| 0 | 718 | | } |
| | 719 | |
|
| | 720 | | private void OpenFriendRequestDetails(string userId) |
| | 721 | | { |
| 0 | 722 | | FriendRequest friendRequest = friendsController.GetAllocatedFriendRequestByUser(userId); |
| | 723 | |
|
| 0 | 724 | | if (friendRequest == null) |
| | 725 | | { |
| 0 | 726 | | Debug.LogError($"Could not find an allocated friend request for user: {userId}"); |
| 0 | 727 | | return; |
| | 728 | | } |
| | 729 | |
|
| 0 | 730 | | if (friendRequest.IsSentTo(userId)) |
| 0 | 731 | | dataStore.HUDs.openSentFriendRequestDetail.Set(friendRequest.FriendRequestId, true); |
| | 732 | | else |
| 0 | 733 | | dataStore.HUDs.openReceivedFriendRequestDetail.Set(friendRequest.FriendRequestId, true); |
| 0 | 734 | | } |
| | 735 | |
|
| | 736 | | private CancellationToken RestartFriendsOperationsCancellationToken() |
| | 737 | | { |
| 0 | 738 | | friendOperationsCancellationToken = friendOperationsCancellationToken.SafeRestart(); |
| 0 | 739 | | return friendOperationsCancellationToken.Token; |
| | 740 | | } |
| | 741 | | } |
| | 742 | | } |