public async Task DeleteChatRoomUserAsync(ChatRoomUser cru){
			var query = ParseObject.GetQuery ("ChatRoomUser").WhereEqualTo ("userId", cru.userID).WhereEqualTo ("chatroomId", cru.chatRoomID);
			IEnumerable<ParseObject> poList = await query.FindAsync ();
			foreach (ParseObject po in poList) {
				await po.DeleteAsync ();
			}
		}
Пример #2
0
        protected void CreateChatRooms()
        {
            chatroomsAdapter = new ChatRoomsAdapter(this);
            var chatRoomsListView = FindViewById <ListView> (Resource.Id.chatroomsListView);

            chatRoomsListView.Adapter    = chatroomsAdapter;
            chatRoomsListView.ItemClick += (object sender, AdapterView.ItemClickEventArgs e) => {
                ChatRoom currChatRoom = chatroomsAdapter.GetChatRoomAt(e.Position);
                var      intent       = new Intent(this, typeof(ChatRoomActivity));
                intent.PutExtra("chatroom", currChatRoom.webID);
                StartActivity(intent);
            };
            chatRoomsListView.ItemLongClick += async(object sender, AdapterView.ItemLongClickEventArgs e) => {
                AlertDialog.Builder alert = new AlertDialog.Builder(this);
                alert.SetTitle("Do you want to delete this chatroom?");
                alert.SetPositiveButton("Yes", (senderAlert, args) => {
                    ChatRoom currChatRoom = chatroomsAdapter.GetChatRoomAt(e.Position);
                    ParsePush.UnsubscribeAsync(currChatRoom.webID);
                    ChatRoomUser cru = DatabaseAccessors.ChatRoomDatabaseAccessor.DeleteChatRoomUser(DatabaseAccessors.CurrentUser().webID, currChatRoom.webID);
                    DatabaseAccessors.ChatRoomDatabaseAccessor.DeleteChatRoom(currChatRoom.webID);
                    ParseChatRoomDatabase pcrd = new ParseChatRoomDatabase();
                    pcrd.DeleteChatRoomUserAsync(cru);
                    Console.WriteLine("ERASED");
                    chatroomsAdapter.NotifyDataSetChanged();
                });
                alert.SetNegativeButton("No", (senderAlert, args) => {
                });
                alert.Show();
            };
        }
		ParseObject ToParseObject(ChatRoomUser cru)
		{
			var po = new ParseObject ("ChatRoomUser");
			po ["userId"] = cru.userID;
			po ["chatroomId"] = cru.chatRoomID;
			return po;
		}
 public ChatRoomUser DeleteChatRoomUser(string userId, string chatroomId)
 {
     lock (chatroomUserLocker) {
         ChatRoomUser cru = chatroomUserDatabase.Table <ChatRoomUser> ().FirstOrDefault(x => x.userID == userId && x.chatRoomID == chatroomId);
         chatroomUserDatabase.Delete <ChatRoomUser> (cru.ID);
         return(cru);
     }
 }
        static ChatRoomUser FromParseObjectChatRoomUser(ParseObject po)
        {
            var t = new ChatRoomUser();

            t.userID     = (string)po ["userId"];
            t.chatRoomID = (string)po ["chatroomId"];
            return(t);
        }
        ParseObject ToParseObject(ChatRoomUser cru)
        {
            var po = new ParseObject("ChatRoomUser");

            po ["userId"]     = cru.userID;
            po ["chatroomId"] = cru.chatRoomID;
            return(po);
        }
        public async Task DeleteChatRoomUserAsync(ChatRoomUser cru)
        {
            var query = ParseObject.GetQuery("ChatRoomUser").WhereEqualTo("userId", cru.userID).WhereEqualTo("chatroomId", cru.chatRoomID);
            IEnumerable <ParseObject> poList = await query.FindAsync();

            foreach (ParseObject po in poList)
            {
                await po.DeleteAsync();
            }
        }
 public void SaveChatRoomUsers(List <User> users, ChatRoom chatroom)
 {
     lock (chatroomUserLocker) {
         foreach (User user in users)
         {
             ChatRoomUser cru = new ChatRoomUser();
             cru.chatRoomID = chatroom.webID;
             cru.userID     = user.webID;
             chatroomUserDatabase.Insert(cru);
         }
     }
 }
 public void SaveChatRoomUsers(List<User> users, ChatRoom chatroom)
 {
     lock (chatroomUserLocker) {
         foreach (User user in users) {
             ChatRoomUser cru = new ChatRoomUser ();
             cru.chatRoomID = chatroom.webID;
             cru.userID = user.webID;
             chatroomUserDatabase.Insert (cru);
         }
     }
 }
		public async Task SaveChatRoomUsersAsync(ChatRoomUser cru){
			ParseObject po = ToParseObject (cru);
			await po.SaveAsync ();
		}
		static ChatRoomUser FromParseObjectChatRoomUser(ParseObject po){
			var t = new ChatRoomUser ();
			t.userID = (string)po ["userId"];
			t.chatRoomID = (string)po ["chatroomId"];
			return t;
		}
        public async Task GetAndSyncChatRoomsAsync()
        {
            var query = ParseObject.GetQuery("ChatRoomUser").WhereEqualTo("userId", ParseUser.CurrentUser.ObjectId).Select("chatroomId");
            IEnumerable <ParseObject> chatRoomIds = await query.FindAsync();

            List <ParseObject> results = new List <ParseObject> ();

            foreach (ParseObject crpo in chatRoomIds)
            {
                var currCRUQuery = ParseObject.GetQuery("ChatRoomUser").WhereEqualTo("chatroomId", crpo ["chatroomId"]);
                foreach (ParseObject cruPO in await currCRUQuery.FindAsync())
                {
                    results.Add(cruPO);
                }
            }
            List <ChatRoomUser> crusToAdd  = new List <ChatRoomUser> ();
            List <String>       crToAdd    = new List <String> ();
            List <String>       usersToAdd = new List <String> ();

            foreach (var cruPO in results)
            {
                ChatRoomUser cru = FromParseObjectChatRoomUser(cruPO);
                if (DatabaseAccessors.ChatRoomDatabaseAccessor.ExistsChatRoomUser(cru.chatRoomID, cru.userID) == false)
                {
                    crusToAdd.Add(cru);
                }
                if (DatabaseAccessors.ChatRoomDatabaseAccessor.ExistsChatRoom(cru.chatRoomID) == false && crToAdd.Contains(cru.chatRoomID) == false)
                {
                    crToAdd.Add(cru.chatRoomID);
                }
                if (DatabaseAccessors.UserDatabaseAccessor.ExistsUser(cru.userID) == false && usersToAdd.Contains(cru.userID) == false)
                {
                    usersToAdd.Add(cru.userID);
                }
            }
            foreach (string userId in usersToAdd)
            {
                User        newUser   = new User();
                var         userQuery = ParseObject.GetQuery("UserInformation").WhereEqualTo("userId", userId);
                ParseObject userPO    = await userQuery.FirstAsync();

                newUser.name  = (string)userPO ["fullName"];
                newUser.webID = userId;
                DatabaseAccessors.UserDatabaseAccessor.SaveUser(newUser);
            }

            foreach (string crid in crToAdd)
            {
                ChatRoom currChatRoom = new ChatRoom();
                currChatRoom.webID = crid;
                DatabaseAccessors.ChatRoomDatabaseAccessor.SaveChatRoom(currChatRoom);
            }


            foreach (ChatRoomUser cru in crusToAdd)
            {
                User        currUser       = DatabaseAccessors.UserDatabaseAccessor.GetUser(cru.userID);
                List <User> currUserAsList = new List <User> ();
                currUserAsList.Add(currUser);
                ChatRoom chatroom = DatabaseAccessors.ChatRoomDatabaseAccessor.GetChatRoom(cru.chatRoomID);
                DatabaseAccessors.ChatRoomDatabaseAccessor.SaveChatRoomUsers(currUserAsList, chatroom);
            }
        }
 public async Task SaveChatRoomUsersAsync(ChatRoomUser cru)
 {
     ParseObject po = ToParseObject(cru);
     await po.SaveAsync();
 }