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