Пример #1
0
        protected async Task SendMessage()
        {
            if (RecipientPublicData == null)
            {
                throw new Exception("no user selected");
            }
            ParseObject message = await CreateNewMessage();

            if (Conversation == null)
            {
                Conversation = await FindOrCreateNewConversation();
            }
            ParseRelation <ParseObject> messages = Conversation.GetRelation <ParseObject>("messages");

            messages.Add(message);
            Conversation["lastSent"] = DateTime.Now;
            await Conversation.SaveAsync();

            IDictionary <string, object> parameters = new Dictionary <string, object>
            {
                { "senderBaseUserId", PublicUserData.BaseUserId },
                { "receiverBaseUserId", RecipientPublicData.Get <string>("baseUserId") },
                { "senderName", PublicUserData.DisplayName },
                { "messageText", tbMessage.Text }
            };
            await ParseCloud.CallFunctionAsync <string>("sendMessageNotification", parameters);

            tbMessage.Text = "";
            tbType.Focus();
        }
Пример #2
0
        protected async Task <ParseObject> CreateNewMessage()
        {
            ParseObject message = new ParseObject("Message");

            message["text"] = tbMessage.Text;
            message["receiverBaseUserId"] = RecipientPublicData.Get <string>("baseUserId");
            message["senderBaseUserId"]   = UserID;
            message["sentAt"]             = DateTime.UtcNow;
            await message.SaveAsync();

            return(message);
        }
Пример #3
0
        protected async Task <ParseObject> FindOrCreateNewConversation()
        {
            ParseObject conversation = await GetThisConversation(RecipientPublicData.Get <string>("baseUserId"));

            if (conversation != null)
            {
                return(conversation);
            }
            else
            {
                conversation = new ParseObject("Conversation");
                conversation["publicUserData1"] = PublicUserData;
                conversation["publicUserData2"] = RecipientPublicData;
                conversation["baseUserId1"]     = PublicUserData.Get <string>("baseUserId");
                conversation["baseUserId2"]     = RecipientPublicData.Get <string>("baseUserId");
                await conversation.SaveAsync();

                return(conversation);
            }
        }
Пример #4
0
        private async Task LoadEverything()
        {
            //ScriptManager.RegisterStartupScript(this, this.GetType(), "scrollPanel", "scrollPanel();", true);
            IEnumerable <ParseObject> conversations = await GetConversations();

            DataTable dt = InitConversationTable();

            foreach (ParseObject conv in conversations)
            {
                //ConversationPanel pnl = (ConversationPanel)LoadControl("~/UserControls/ConversationPanel.ascx");
                //string theirNumber = conv.Get<string>("baseUserId1") == UserID ? "2" : "1";
                //ParseObject tempTheirPublicData = conv.Get<ParseObject>("publicUserData" + theirNumber);
                //await tempTheirPublicData.FetchIfNeededAsync();
                //pnl.TheirName = tempTheirPublicData.Get<string>("displayName");
                //pnl.LastMessage = await GetFirstMessageText(conv);
                //pnl.TheirUserID = tempTheirPublicData.ObjectId;
                //pnlConversations.ContentTemplateContainer.Controls.Add(pnl);
                //pnlConversations.Controls.Add(pnl);

                DataRow     dr                  = dt.NewRow();
                string      theirNumber         = conv.Get <string>("baseUserId1") == UserID ? "2" : "1";
                ParseObject tempTheirPublicData = conv.Get <ParseObject>("publicUserData" + theirNumber);
                await tempTheirPublicData.FetchIfNeededAsync();

                dr["TheirName"] = tempTheirPublicData.Get <string>("displayName");
                string lastMessage = await GetFirstMessageText(conv);

                dr["LastMessage"] = lastMessage.Length <= 30 ? lastMessage : lastMessage.Substring(0, 30) + "...";
                dr["TheirUserID"] = tempTheirPublicData.ObjectId;
                ParseFile profilePic = tempTheirPublicData.Get <ParseFile>("profilePic");
                dr["ProfilePicUrl"] = profilePic == null ? "Images/default_prof_pic.png" : profilePic.Url.ToString();
                dt.Rows.Add(dr);
            }
            repConversations.DataSource = dt;
            repConversations.DataBind();
            if (Session["ConversationUserId"] != null)
            {
                RecipientPublicData = await GetStudentPublicData(Session["ConversationUserId"].ToString());

                Conversation = await GetThisConversation(RecipientPublicData.Get <string>("baseUserId"));

                if (Conversation == null)
                {
                }
                else
                {
                    ParseRelation <ParseObject> relation = Conversation.GetRelation <ParseObject>("messages");
                    IEnumerable <ParseObject>   messages = await relation.Query.OrderBy("createdAt").FindAsync();

                    DataTable messageData = InitMessageTable();
                    foreach (ParseObject mes in messages)
                    {
                        DataRow dr = messageData.NewRow();
                        dr["Text"]        = mes.Get <string>("text");
                        dr["WasSentByMe"] = mes.Get <string>("senderBaseUserId") == UserID;
                        dr["SentAt"]      = mes.Get <DateTime>("sentAt").ToString();
                        messageData.Rows.Add(dr);
                    }
                    repMessages.DataSource = messageData;
                    repMessages.DataBind();
                    await RemoveNotificationsForConversation();
                }
                tbType.Enabled  = true;
                btnSend.Enabled = true;
            }
            else
            {
                tbType.Enabled  = false;
                btnSend.Enabled = false;
            }
        }