Exemple #1
0
        private void AddGroup(GroupModel group)
        {
            if (this.Contains(group))
            {
                return;
            }

            char firstLetter = char.ToLower(group.Name[0]);

            // show # for numbers
            if (firstLetter >= '0' && firstLetter <= '9')
            {
                firstLetter = '#';
            }

            bool found = false;
            foreach (ContactGroup<GroupModel> alphaGroup in this.groups)
            {
                // create group for letter if it doesn't exist
                if (alphaGroup.FirstLetter == firstLetter)
                {
                    found = true;
                    alphaGroup.Add(group);
                }
            }

            if (!found)
            {
                var alphaGroup = new ContactGroup<GroupModel>(firstLetter);
                this.groups.Add(alphaGroup);

                // create a contact for item and add it to the relevant
                alphaGroup.Add(group);
            }
        }
Exemple #2
0
 public void RemoveGroupMember(GroupModel group, UserModel user)
 {
     YapperServiceProxy.Instance.RemoveGroupMember(group, user, this.RemoveGroupMemberCompleted);
 }
Exemple #3
0
        private void RemoveGroupMemberCompleted(GroupModel gp, UserModel user)
        {
            if (user != null)
            {
                lock (this)
                {
                    var existing = from m in this.dataContext.Table<GroupMemberModel>()
                                   where m.GroupId == gp.Id && m.MemberId == user.Id
                                   select m;

                    if (existing.Count<GroupMemberModel>() != 0)
                    {
                        this.dataContext.Table<GroupMemberModel>().DeleteOnSubmit(existing.First());
                    }

                    this.dataContext.SubmitChanges();
                }
            }

            Messenger.Default.Send<GroupMemberRemovedEvent>(new GroupMemberRemovedEvent() { Success = user != null, User = user });
        }
Exemple #4
0
        private void GroupCreated(GroupModel createdGroup)
        {
            if (createdGroup != null)
            {
                lock (this)
                {
                    this.AddOrUpdateGroup(createdGroup, null);
                    this.dataContext.SubmitChanges();
                }
            }

            Messenger.Default.Send<NewGroupEvent>(new NewGroupEvent() { GroupCreated = createdGroup});
        }
Exemple #5
0
        private void AddOrUpdateGroup(GroupModel createdGroup, List<UserModel> users)
        {
            lock (this)
            {
                Debug.WriteLine(string.Format("{0} {1} {2}", DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt"), "DataSync::AddOrUpdateGroup", "start"));
                createdGroup.UserType = UserType.Group;
                var existing = from u in this.dataContext.Table<UserModel>()
                               where u.Id == createdGroup.Id
                               select u;

                // If the group doesn't exist add it
                if (existing.Count<UserModel>() == 0)
                {
                    this.dataContext.Table<UserModel>().InsertOnSubmit(createdGroup);
                }
                else if (existing.First().UserType == UserType.User)
                {
                    // IF the group exists but is of wrong type, fix it.
                    existing.First().UserType = UserType.Group;
                }

                Debug.WriteLine(string.Format("{0} {1} {2}", DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt"), "DataSync::AddOrUpdateGroup", "start find members"));
                var existingMember = from member in this.dataContext.Table<GroupMemberModel>()
                                     where member.GroupId == createdGroup.Id
                                     select member;
                Debug.WriteLine(string.Format("{0} {1} {2}", DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt"), "DataSync::AddOrUpdateGroup", "end find members"));

                Dictionary<int, GroupMemberModel> members = new Dictionary<int, GroupMemberModel>();

                // Get existing member list
                foreach (GroupMemberModel g in existingMember)
                {
                    members.Add(g.MemberId, g);
                }

                // Add or remove members
                for (int i = 0; i < createdGroup.Members.Count; i++)
                {
                    if (createdGroup.Members[i] == null)
                    {
                        continue;
                    }

                    // Add new members
                    if (!members.ContainsKey(createdGroup.Members[i].Id))
                    {
                        Debug.WriteLine(string.Format("{0} {1} {2}", DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt"), "DataSync::AddOrUpdateGroup", "start find new member in user table"));
                        var existingMemberInUserTable = from u in this.dataContext.Table<UserModel>()
                                                        where u.Id == createdGroup.Members[i].Id
                                                        select u;
                        Debug.WriteLine(string.Format("{0} {1} {2}", DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt"), "DataSync::AddOrUpdateGroup", "end find new member in user table"));

                        // If the group doesn't exist add it
                        if (existingMemberInUserTable.Count<UserModel>() == 0 &&
                            users != null &&
                            !users.Contains(createdGroup.Members[i]))
                        {
                            Debug.WriteLine(string.Format("{0} {1} {2}", DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt"), "DataSync::AddOrUpdateGroup", "start add new member to user table"));

                            this.dataContext.Table<UserModel>().InsertOnSubmit(
                                new UserModel()
                                {
                                    Id = createdGroup.Members[i].Id,
                                    Name = createdGroup.Members[i].Name,
                                    UserType = createdGroup.Members[i].UserType,
                                    PhoneNumber = createdGroup.Members[i].PhoneNumber,
                                    PublicKey = createdGroup.Members[i].PublicKey
                                });
                            users.Add(createdGroup.Members[i]);

                            Debug.WriteLine(string.Format("{0} {1} {2}", DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt"), "DataSync::AddOrUpdateGroup", "end add new member to user table"));
                        }

                        this.dataContext.Table<GroupMemberModel>().InsertOnSubmit(new GroupMemberModel() { GroupId = createdGroup.Id, MemberId = createdGroup.Members[i].Id });
                    }
                    else
                    {
                        members.Remove(createdGroup.Members[i].Id);
                    }
                }

                Debug.WriteLine(string.Format("{0} {1} {2}", DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt"), "DataSync::AddOrUpdateGroup", "start remove members"));
                // Delete existing members that are no longer members.
                foreach (int m in members.Keys)
                {
                    this.dataContext.Table<GroupMemberModel>().DeleteOnSubmit(members[m]);
                }

                Debug.WriteLine(string.Format("{0} {1} {2}", DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt"), "DataSync::AddOrUpdateGroup", "end remove members"));

                Debug.WriteLine(string.Format("{0} {1} {2}", DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt"), "DataSync::AddOrUpdateGroup", "end"));
            }
        }
Exemple #6
0
        private void AddGroupMemberCompleted(GroupModel group, UserModel user)
        {
            if (user != null)
            {
                lock (this)
                {
                    this.dataContext.Table<GroupMemberModel>().InsertOnSubmit(new GroupMemberModel() { GroupId = group.Id, MemberId = user.Id });
                    this.dataContext.SubmitChanges();
                }
            }

            Messenger.Default.Send<GroupMemberAddedEvent>(new GroupMemberAddedEvent() { Success = user != null, User = user });
        }
Exemple #7
0
        public void RemoveGroupMember(GroupModel group, UserModel user, Action<GroupModel, UserModel> callback)
        {
            Debug.WriteLine(string.Format("{0} {1} {2}", DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt"), "YapperServiceProxy::RemoveGroupMember ", "start"));
            Uri uri = new Uri(
                string.Format(
                    YapperServiceProxy.RemoveGroupMemberRelativeUrl,
                    YapperServiceProxy.ServicePrefix,
                    group.Id,
                    HttpUtility.UrlEncode(user.PhoneNumber)));

            WebClient webClient = new WebClient();
            webClient.Headers["AuthTokenCookie"] = this.GetAuthTokenCookie();
            webClient.Headers["Content-Type"] = "application/json; Charset=UTF-8";

            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(this.RemoveGroupMemberCompleted);
            webClient.UploadStringAsync(
                uri,
                null,
                string.Empty,
                new YapperChat.Common.Tuple<Action<GroupModel, UserModel>, YapperChat.Common.Tuple<GroupModel, UserModel>>
                    (
                        callback,
                        new YapperChat.Common.Tuple<GroupModel, UserModel>(group, user)
                    ));
        }
Exemple #8
0
        public void CreateGroup(string name, List<UserModel> members, Action<GroupModel> callback)
        {
            Debug.WriteLine(string.Format("{0} {1} {2}", DateTime.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt"), "YapperServiceProxy::CreateGroup ", "start"));
            if (!members.Contains(UserSettingsModel.Instance.Me))
            {
                members.Add(UserSettingsModel.Instance.Me);
            }

            GroupModel newGroup = new GroupModel() { Members = members, Name = name };
            MemoryStream stream = new MemoryStream();
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(GroupModel));
            serializer.WriteObject(stream, newGroup);
            string jsonString = System.Text.Encoding.UTF8.GetString(stream.ToArray(), 0, stream.ToArray().Length);

            Uri uri = new Uri(
                string.Format(
                    YapperServiceProxy.CreateGroupRelativeUrl,
                    YapperServiceProxy.ServicePrefix));
            WebClient webClient = new WebClient();
            webClient.Headers["AuthTokenCookie"] = this.GetAuthTokenCookie();
            webClient.Headers["Content-Type"] = "application/json; Charset=UTF-8";

            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(this.CreateGroupCompleted);
            webClient.UploadStringAsync(uri, null, jsonString, callback);
        }
Exemple #9
0
        private bool Contains(GroupModel group)
        {
            char firstLetter = char.ToLower(group.Name[0]);
            foreach (ContactGroup<GroupModel> alphaGroup in this.groups)
            {
                if (alphaGroup.FirstLetter == firstLetter)
                {
                    foreach (UserModel user in alphaGroup)
                    {
                        if (user.Id == group.Id)
                        {
                            return true;
                        }
                    }
                }
            }

            return false;
        }
Exemple #10
0
        private void GroupCreated(NewGroupEvent groupEvent)
        {
            if (groupEvent.GroupCreated != null)
            {
                this.createdGroup = groupEvent.GroupCreated;
            }
            else
            {
                foreach (var b in this.ApplicationBar.Buttons)
                {
                    ((ApplicationBarIconButton)b).IsEnabled = true;
                }

                this.ApplicationBar.IsVisible = true;
            }
        }