Ejemplo n.º 1
0
        /// <summary>
        /// Removes user's device token from database to stop push notifications.
        /// </summary>
        /// <param name="userID">A string containing phone number as user id.</param>
        /// <returns>true if the device token is successfully removed; otherwise, false.</returns>

        /*public bool RemoveUserDeviceToken(string userID)
         * {
         *  if (!NeeoUtility.IsNullOrEmpty(userID))
         *  {
         *      NeeoUser user = new NeeoUser(userID);
         *      try
         *      {
         *          if (user.UpdateUserDeviceToken(null))
         *          {
         *              return true;
         *          }
         *          else
         *          {
         *              SetServiceResponseHeaders(CustomHttpStatusCode.InvalidUser);
         *              return false;
         *          }
         *      }
         *      catch (ApplicationException appExp)
         *      {
         *          SetServiceResponseHeaders((CustomHttpStatusCode)(Convert.ToInt32(appExp.Message)));
         *          return false;
         *      }
         *      catch (Exception exp)
         *      {
         *          LogManager.CurrentInstance.ErrorLogger.LogError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, exp.Message, exp);
         *          SetServiceResponseHeaders(CustomHttpStatusCode.UnknownError);
         *          return false;
         *      }
         *  }
         *  else
         *  {
         *      SetServiceResponseHeaders(CustomHttpStatusCode.InvalidArguments);
         *      return false;
         *  }
         * }
         */
        #endregion

        #endregion

        #region Checking Application Operatibility

        /// <summary>
        /// Checks application operatibility and update the user device information in the database for a register user.
        /// </summary>
        /// <param name="uID">A string containing phone number as user id.</param>
        /// <param name="client">An object containing the client information.</param>
        public string CheckAppCompatibility(string uID, UserAgent client)
        {
            uID = (uID != null) ? uID.Trim() : uID;
            #region log user request and response

            /***********************************************
            *  To log user request and response
            ***********************************************/
            if (_logRequestResponse)
            {
                LogManager.CurrentInstance.InfoLogger.LogInfo(
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, System.Reflection.MethodBase.GetCurrentMethod().Name + " ===> " +
                    "Request ===> uID : " + uID + ", client : " + JsonConvert.SerializeObject(client));
            }

            #endregion

            //  #region Verify User
            //var request = OperationContext.Current.IncomingMessageProperties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
            //string keyFromClient = request.Headers["key"];
            //if (NeeoUtility.AuthenticateUserRequest(uID, keyFromClient))
            //{
            //#endregion
            if (client != null)
            {
                ulong tempPhoneNumber = 0;
                if (Enum.IsDefined(typeof(DevicePlatform), client.DP) && !NeeoUtility.IsNullOrEmpty(client.OsVer) &&
                    !NeeoUtility.IsNullOrEmpty(client.AppVer) && !NeeoUtility.IsNullOrEmpty(client.DVenID))
                {
                    switch (CheckCompatibility(client))
                    {
                    case AppCompatibility.Incompatible:
                        NeeoUtility.SetServiceResponseHeaders(CustomHttpStatusCode.Incompatible);
                        break;

                    case AppCompatibility.IncompatibleAppVersion:
                        NeeoUtility.SetServiceResponseHeaders(CustomHttpStatusCode.Incompatible);
                        break;

                    case AppCompatibility.IncompatibleOsVersion:
                        NeeoUtility.SetServiceResponseHeaders(CustomHttpStatusCode.Incompatible);
                        break;

                    case AppCompatibility.Compatible:

                        if (!NeeoUtility.IsNullOrEmpty(uID))
                        {
                            if (ulong.TryParse(uID, out tempPhoneNumber))
                            {
                                NeeoUser user = new NeeoUser(uID);

                                try
                                {
                                    if (!user.UpdateUserDeviceInfo(client.AppVer,
                                                                   new DeviceInfo()
                                    {
                                        DevicePlatform = (DevicePlatform)client.DP,
                                        DeviceVenderID = client.DVenID,
                                        OsVersion = client.OsVer
                                    }, false))
                                    {
                                        NeeoUtility.SetServiceResponseHeaders(CustomHttpStatusCode.InvalidUser);
                                    }
                                }
                                catch (ApplicationException appExp)
                                {
                                    NeeoUtility.SetServiceResponseHeaders(
                                        (CustomHttpStatusCode)(Convert.ToInt32(appExp.Message)));
                                }
                                catch (Exception exp)
                                {
                                    LogManager.CurrentInstance.ErrorLogger.LogError(
                                        System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, exp.Message,
                                        exp);
                                    NeeoUtility.SetServiceResponseHeaders(CustomHttpStatusCode.UnknownError);
                                }
                            }
                            else
                            {
                                NeeoUtility.SetServiceResponseHeaders(CustomHttpStatusCode.InvalidArguments);
                            }
                        }
                        break;
                    }
                }
                else
                {
                    NeeoUtility.SetServiceResponseHeaders(CustomHttpStatusCode.InvalidArguments);
                }
            }
            else
            {
                NeeoUtility.SetServiceResponseHeaders(CustomHttpStatusCode.InvalidArguments);
            }

            //}
            //else
            //{
            //    NeeoUtility.SetServiceResponseHeaders((CustomHttpStatusCode)HttpStatusCode.Unauthorized);

            //}
            return("OK");
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Updates user's device token into the database for push notifications.
        /// </summary>
        /// <param name="opID">an short integer specifying the type of the operation to be performed.</param>
        /// <param name="uID">A string containing phone number as user id.</param>
        /// <param name="deviceToken">A 64-character string indicating the user's device token.</param>
        /// <returns>true if the device token is successfully updated in database; otherwise, false.</returns>
        public bool DeviceToken(short opID, string uID, UserAgent client)
        {
            uID = (uID != null) ? uID.Trim() : uID;
            const string deletionValue = "-1";

            #region log user request and response

            /***********************************************
            *  To log user request and response
            ***********************************************/
            if (_logRequestResponse)
            {
                LogManager.CurrentInstance.InfoLogger.LogInfo(
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, System.Reflection.MethodBase.GetCurrentMethod().Name + " ===> " +
                    "Request ===> opID : " + opID + ", userID : " + uID + ", client : " + JsonConvert.SerializeObject(client));
            }

            #endregion

            // #region Verify User
            //var request = OperationContext.Current.IncomingMessageProperties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
            //string keyFromClient = request.Headers["key"];
            //if (NeeoUtility.AuthenticateUserRequest(uID, keyFromClient))
            //{
            //#endregion
            if (client != null)
            {
                OperationType operationType = (OperationType)opID;
                if ((Enum.IsDefined(typeof(DevicePlatform), client.DP) &&
                     Enum.IsDefined(typeof(PushNotificationSource), client.Pns) &&
                     !NeeoUtility.IsNullOrEmpty(client.DToken) && !NeeoUtility.IsNullOrEmpty(uID) &&
                     !NeeoUtility.IsNullOrEmpty(client.DVenID) && Enum.IsDefined(typeof(OperationType), opID) &&
                     operationType == OperationType.Update))
                {
                    //do nothing
                }
                else if ((Enum.IsDefined(typeof(DevicePlatform), client.DP) &&
                          Enum.IsDefined(typeof(PushNotificationSource), client.Pns) && !NeeoUtility.IsNullOrEmpty(uID) &&
                          !NeeoUtility.IsNullOrEmpty(client.DVenID) && Enum.IsDefined(typeof(OperationType), opID) &&
                          operationType == OperationType.Delete))
                {
                    client.DToken = deletionValue;
                }
                else
                {
                    NeeoUtility.SetServiceResponseHeaders(CustomHttpStatusCode.InvalidArguments);
                    return(false);
                }
                NeeoUser user = new NeeoUser(uID.Trim());
                try
                {
                    return(user.UpdateUserDeviceInfo(null,
                                                     new DeviceInfo()
                    {
                        DeviceToken = client.DToken?.Trim(),
                        DeviceTokenVoIP = client.DTokenVoIP?.Trim(),
                        PushNotificationSource = (PushNotificationSource)client.Pns,
                        DeviceVenderID = client.DVenID,
                        DevicePlatform = (DevicePlatform)client.DP
                    }, true));
                }
                catch (ApplicationException appExp)
                {
                    NeeoUtility.SetServiceResponseHeaders((CustomHttpStatusCode)(Convert.ToInt32(appExp.Message)));
                    return(false);
                }
                catch (Exception exp)
                {
                    LogManager.CurrentInstance.ErrorLogger.LogError(
                        System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, exp.Message, exp);
                    NeeoUtility.SetServiceResponseHeaders(CustomHttpStatusCode.ServerInternalError);
                    return(false);
                }
            }
            NeeoUtility.SetServiceResponseHeaders(CustomHttpStatusCode.InvalidArguments);
            return(false);
            //}
            //else
            //{
            //    NeeoUtility.SetServiceResponseHeaders((CustomHttpStatusCode)HttpStatusCode.Unauthorized);
            //    return false;
            //}
        }