Ejemplo n.º 1
0
        /// <summary>
        /// Gets a list of IFriend models from any function that is an async Task<List<string>>
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="ListSource"></param>
        /// <returns></returns>
        private async Task <List <IFriendModel> > GetFriendModelList(string Id, Func <string, Task <List <string> > > ListSource)
        {
            List <IFriendModel> friends = new();

            // make sure the id is valid
            if (Helpers.FullVerifyGuid(ref Id, logger) is false)
            {
                return(friends);
            }

            var friendIds = await ListSource(Id);


            if (friendIds?.Count is null or 0)
            {
                return(friends);
            }

            foreach (var item in friendIds)
            {
                IFriendModel foundFriend = await GetFriend(item);

                if (foundFriend != null)
                {
                    friends.Add(foundFriend);
                }
            }

            return(friends);
        }
Ejemplo n.º 2
0
        public async Task <bool> SetDisplayName(string Id, string newDisplayName)
        {
            // make sure the user isn't trying to inject via the id
            if (Helpers.FullVerifyGuid(ref Id, logger) is false)
            {
                return(false);
            }

            // clean any possible sql
            Helpers.CleanInputBasic(ref newDisplayName);

            if (string.IsNullOrEmpty(newDisplayName))
            {
                return(false);
            }

            if (string.IsNullOrWhiteSpace(newDisplayName))
            {
                return(false);
            }

            // make sure the display name has no leading or trailing whitespace
            newDisplayName = newDisplayName.Trim();

            // get the old display name to check to see if we need to update or set the name
            IFriendModel myFriendInfo = await friendHandler.GetFriend(Id);

            bool alreadyAdded = myFriendInfo != default;

            string storedProcedure;

            dynamic parameters = new { Id, DisplayName = newDisplayName };

            if (alreadyAdded)
            {
                logger.LogInformation("Changing displayname from {OldDisplayName} to {NewDisplayName} for {Id}", myFriendInfo.DisplayName, newDisplayName, Id);
                storedProcedure = ChangeDisplayNameProcedure;
            }
            else
            {
                logger.LogInformation("Setting new display name for {Id} Name: {NewDisplayName}", Id, newDisplayName);
                storedProcedure = SetDisplayNameProcedure;
            }

            try
            {
                if (myFriendInfo != null)
                {
                    // check to make sure the unique id for this person isn't taken for the next username they are switching to
                    if (await UniqueIdentifierAvailable(newDisplayName, myFriendInfo.UniqueIdentifier) is false)
                    {
                        await SetRandomUniqueIdentifier(Id, newDisplayName);
                    }
                }
                else
                {
                    await SetRandomUniqueIdentifier(Id, newDisplayName);
                }

                // change or set the name
                await db.ExecuteProcedure <dynamic, dynamic>(storedProcedure, parameters);

                // update the name in cache
                await Cache.UpdateOrCache(GetDisplayNameProcedure, new { Id }, newDisplayName);

                return(true);
            }
            catch (Exception e)
            {
                logger.LogError("Failed to change for {Id} Name: {NewDisplayName} null unique Id retrieved Error: {Error}", Id, newDisplayName, e);

                return(false);
            }
        }