Ejemplo n.º 1
0
        public async Task <IFriendModel> GetFriend(string Id)
        {
            // make sure the id we got is a valid Guid
            if (Helpers.FullVerifyGuid(ref Id, logger) is false)
            {
                return(null);
            }

            // limit sql injections as much as possible
            Helpers.CleanInputBasic(ref Id);

            var parameters = new { Id };

            TFriendModelType result;

            if ((result = await Cache.GetCachedOrDefault(GetFriendProcedure, parameters)) != null)
            {
                return(result);
            }

            result = await db.ExecuteSingleProcedure <TFriendModelType, dynamic>(GetFriendProcedure, parameters);

            await Cache.UpdateOrCache(GetFriendProcedure, parameters, result);

            if (result != null)
            {
                // get the avatar for the user, this is segregated into it's own table incase further more serious segregation is needed
                result.AvatarPath = await avatarHandler.GetAvatar(result.Id) ?? "/Images/DefaultAvatar.webp";
            }

            return(result);
        }
Ejemplo n.º 2
0
        public async Task <bool> SendMessage(string SenderId, string RecipientId, IMessageModel Message)
        {
            if (Helpers.FullVerifyGuid(ref SenderId, logger) is false)
            {
                return(false);
            }

            if (Helpers.FullVerifyGuid(ref RecipientId, logger) is false)
            {
                return(false);
            }

            try
            {
                // get the serialized messages list
                var result = await db.ExecuteSingleProcedure <string, dynamic>(GetMessagesProcedure, new { Id = RecipientId });

                // if the list is null just create a new one
                List <MessageModel> messages = result is null ? new() : Newtonsoft.Json.JsonConvert.DeserializeObject <List <MessageModel> >(result);

                // add it to the list
                if (Message is MessageModel mm)
                {
                    // add the message
                    if (messages.Contains(mm) is false)
                    {
                        messages.Add(mm);
                    }
                }

                // serialize the list for storage
                result = Newtonsoft.Json.JsonConvert.SerializeObject(messages);

                // save the messages
                await db.ExecuteVoidProcedure <dynamic>(SetMessagesProcedure, new { Id = RecipientId, Messages = result });

                return(true);
            }
            catch (Exception e)
            {
                logger.LogError("Failed to send message From {SenderId} to {RecipientId} Error: {Error}", SenderId, RecipientId, e);
                return(false);
            }
        }
Ejemplo n.º 3
0
        public async Task <string> GetOAuth(string Id)
        {
            // make sure to avoid possible Sql Injection
            if (Helpers.FullVerifyGuid(ref Id, logger) is false)
            {
                return(null);
            }

            string OAuth = await db.ExecuteSingleProcedure <string, dynamic>(GetOAuthProcedureName, new { Id });

            logger.LogInformation("Retrieved OAuth for {Id}: {OAuth}", Id, OAuth);

            return(OAuth);
        }
Ejemplo n.º 4
0
        public async Task <string> GetAvatar(string Id)
        {
            if (Helpers.FullVerifyGuid(ref Id, logger) is false)
            {
                return(null);
            }

            try
            {
                return(await db.ExecuteSingleProcedure <string, dynamic>(GetAvatarProcedure, new { Id }));
            }
            catch (Exception e)
            {
                logger.LogError("Failed to get Avatar for {Id} Error: {Error}", Id, e);
                return(null);
            }
        }
Ejemplo n.º 5
0
        private async Task <Dictionary <string, string> > GetAllStates(string Id)
        {
            try
            {
                // states are stored as a dictionary with friend ids and states

                string serializedStates = await db.ExecuteSingleProcedure <string, dynamic>(GetStateProcedureName, new { Id });

                if (string.IsNullOrEmpty(serializedStates))
                {
                    return(new());
                }

                Dictionary <string, string> states = JsonConvert.DeserializeObject <Dictionary <string, string> >(serializedStates);

                return(states ?? new());
            }
            catch (Exception e)
            {
                logger.LogError("Failed to set state for {Id} {Error}", Id, e);
                return(new());
            }
        }
Ejemplo n.º 6
0
        public async Task <string> GetDisplayName(string Id)
        {
            if (string.IsNullOrEmpty(Id))
            {
                return(null);
            }

            // make sure the id given is actuall an id
            if (Helpers.FullVerifyGuid(ref Id, logger) is false)
            {
                return(null);
            }

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

            logger.LogInformation("Fectching username for {UserId}", Id);

            string result;

            dynamic parameters = new { Id };

            if ((result = await Cache.GetCachedOrDefault(GetDisplayNameProcedure, parameters)) != default)
            {
                logger.LogInformation("Fetched cached username {Username} for {Id}", result, Id);
                return(result);
            }

            result = await db.ExecuteSingleProcedure <string, dynamic>(GetDisplayNameProcedure, new { Id });

            if (result != default)
            {
                await Cache.UpdateOrCache(GetDisplayNameProcedure, parameters, result);
            }

            return(result);
        }