private ModifyRequest BuildModifyRequest(MParticleUser user, IdentityApiRequest identityApiRequest)
        {
            var modifyRequest = new ModifyRequest()
            {
                IdentityChanges = new List <IdentityChange>()
            };

            PopulateBaseIdentityRequest(modifyRequest);
            var updatedIdentities = identityApiRequest.UserIdentities;
            var currentIdentities = user.UserIdentities;

            foreach (var identity in updatedIdentities)
            {
                var change = new IdentityChange()
                {
                    NewValue     = identity.Value,
                    IdentityType = (IdentityType)identity.Key
                };
                if (currentIdentities.ContainsKey(identity.Key))
                {
                    change.OldValue = currentIdentities[identity.Key];
                }
                (modifyRequest.IdentityChanges as List <IdentityChange>).Add(change);
            }
            return(modifyRequest);
        }
Beispiel #2
0
 public IdentityApiRequestBuilder(MParticleUser user)
 {
     if (user != null)
     {
         this.UserIdentities(user.UserIdentities.ToDictionary(kvp => kvp.Key, kvp => kvp.Value));
     }
 }
        private async Task <IdentityApiResult> PerformIdentityCallAsync(IdentityApiRequest originalRequest, Task <object> identityTask, MParticleUser user = null)
        {
            try
            {
                var response = await identityTask;
                if (response is IdentityResponse)
                {
                    var newUser = user ?? new MParticleUser(long.Parse(((IdentityResponse)response).Mpid), persistenceManager);
                    newUser.UserIdentities = originalRequest.UserIdentities;
                    if (CurrentUser == null || CurrentUser.Mpid != newUser.Mpid)
                    {
                        CurrentUser = newUser;
                        try
                        {
                            await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                                                                                          () =>
                            {
                                originalRequest.UserAliasDelegate?.Invoke(CurrentUser, newUser);
                                this.IdentityStateChange?.Invoke(this, new IdentityStateChangeEventArgs(originalRequest, newUser));
                            });
                        }
                        catch (Exception ex)
                        {
                            this.Logger?.Log(new LogEntry(LoggingEventType.Error, "Error while invoking IdentityStateChange listener" + ex.ToString()));
                        }
                    }

                    return(new IdentityApiResult()
                    {
                        User = newUser
                    });
                }
                else
                {
                    return(new IdentityApiResult()
                    {
                        Error = (ErrorResponse)response
                    });
                }
            }
            catch (Exception ex)
            {
                this.Logger?.Log(new LogEntry(LoggingEventType.Error, "Error while performing identity request: " + ex.ToString()));

                return(new IdentityApiResult()
                {
                    Error = new ErrorResponse()
                    {
                        StatusCode = (int)IdentityApi.UnknownError,
                        Errors = new Error[] { new Error()
                                               {
                                                   Message = ex.Message
                                               } }
                    }
                });
            }
        }
Beispiel #4
0
        public void TestSetUserAttributes()
        {
            PersistenceManager persistenceManager = new PersistenceManager(MParticleOptions.Builder("foo", "bar").Build());

            persistenceManager.Initialize(Package.Current.Id.Version);
            var user = new MParticleUser(5, persistenceManager);

            user.UserAttribute("foo attribute key", "foo value");
            Assert.AreEqual("foo value", user.UserAttributes["foo attribute key"]);
        }
Beispiel #5
0
        public void TestGetUserIdentitiesNoIdentities()
        {
            PersistenceManager persistenceManager = new PersistenceManager(MParticleOptions.Builder("foo", "bar").Build());

            persistenceManager.Initialize(Package.Current.Id.Version);
            var user       = new MParticleUser(5, persistenceManager);
            var identities = user.UserIdentities;

            Assert.AreEqual(0, identities.Count);
        }
Beispiel #6
0
 internal void AddMpid(MParticleUser user)
 {
     if (user != null)
     {
         if (!Mpids.Contains(user.Mpid))
         {
             Mpids.Add(user.Mpid);
         }
     }
 }
 internal void LogPending(MParticleUser user)
 {
     lock (pendingMessages)
     {
         if (pendingMessages.Count > 0)
         {
             eventsApiManager.EnqueueMessage(CreateBatch(pendingMessages.ToArray()));
             pendingMessages.Clear();
         }
     }
 }
Beispiel #8
0
        public void TestSetUserAttributeTag()
        {
            PersistenceManager persistenceManager = new PersistenceManager(MParticleOptions.Builder("foo", "bar").Build());

            persistenceManager.Initialize(Package.Current.Id.Version);
            var user = new MParticleUser(5, persistenceManager);

            user.UserTag("foo user attribute tag");
            Assert.AreEqual(1, user.UserAttributes.Count);
            Assert.IsTrue(user.UserAttributes.ContainsKey("foo user attribute tag"));
        }
Beispiel #9
0
        public void TestSetUserAttributeList()
        {
            PersistenceManager persistenceManager = new PersistenceManager(MParticleOptions.Builder("foo", "bar").Build());

            persistenceManager.Initialize(Package.Current.Id.Version);
            var user = new MParticleUser(5, persistenceManager);
            var attributeValueList = new List <string>();

            attributeValueList.Add("foo item 1");
            user.UserAttribute("foo attribute list key", attributeValueList);
            Assert.AreEqual("foo item 1", ((IList <string>)user.UserAttributes["foo attribute list key"])[0]);
        }
Beispiel #10
0
        public void TestRemoveUserAttribute()
        {
            PersistenceManager persistenceManager = new PersistenceManager(MParticleOptions.Builder("foo", "bar").Build());

            persistenceManager.Initialize(Package.Current.Id.Version);
            var user = new MParticleUser(5, persistenceManager);

            user.UserTag("foo user attribute tag 1");
            user.UserTag("foo user attribute tag 2");
            Assert.AreEqual(2, user.UserAttributes.Count);
            user.RemoveUserAttribute("foo user attribute tag 1");
            Assert.AreEqual(1, user.UserAttributes.Count);
        }
Beispiel #11
0
        public void TestGetUserIdentities()
        {
            PersistenceManager persistenceManager = new PersistenceManager(MParticleOptions.Builder("foo", "bar").Build());

            persistenceManager.Initialize(Package.Current.Id.Version);
            IList <UserIdentity> userIdentities = new List <UserIdentity>();
            UserIdentity         identity       = new UserIdentity();

            identity.DateFirstSet = 123;
            identity.Identity     = "foo identity";
            identity.IsFirstSeen  = true;
            identity.Name         = UserIdentityType.Twitter;
            userIdentities.Add(identity);
            persistenceManager.SetUserIdentities(5, userIdentities);
            var user       = new MParticleUser(5, persistenceManager);
            var identities = user.UserIdentities;

            Assert.AreEqual(1, identities.Count);
            Assert.AreEqual("foo identity", identities[UserIdentityType.Twitter]);
        }
        internal async Task <IdentityApiResult> ModifyAsync(MParticleUser user, IdentityApiRequest identityApiRequest)
        {
            if (identityApiRequest == null || user == null)
            {
                return(new IdentityApiResult()
                {
                    Error = new ErrorResponse()
                    {
                        StatusCode = (int)IdentityApi.BadRequest,
                        Errors = new Error [] { new Error()
                                                {
                                                    Message = "Modify requests must not be empty."
                                                } }
                    }
                });
            }
            ModifyRequest request = BuildModifyRequest(user, identityApiRequest);
            var           task    = identityApiManager.Modify(user.Mpid, request);

            return(await PerformIdentityCallAsync(identityApiRequest, task, user));
        }
Beispiel #13
0
 public static IdentityApiRequestBuilder WithUser(MParticleUser user)
 {
     return(new IdentityApiRequestBuilder(user));
 }
 internal IdentityStateChangeEventArgs(IdentityApiRequest request, MParticleUser user)
 {
     Request = request;
     User    = user;
 }