コード例 #1
0
        private void ToggleButton_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            ConversationMemberItem user   = button.DataContext as ConversationMemberItem;
            ConfirmDialog          dialog = new ConfirmDialog("Approve user " + user.FullName + " become host of coversation");
            bool?result = dialog.ShowDialog();

            if (result == true)
            {
                using (HttpClient client = new HttpClient())
                {
                    client.BaseAddress = new Uri(Ultils.url);
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AppInstance.getInstance().GetUser().Token);
                    ConversationMember memberinconversation = new ConversationMember()
                    {
                        UserIds = user.Id
                    };
                    HttpResponseMessage response = client.PutAsJsonAsync("/api/Conversations/HostMember/" + ConversationId, memberinconversation).Result;
                    if (response.IsSuccessStatusCode)
                    {
                        LoadUserList();
                    }
                    else
                    {
                        _404Mess err = response.Content.ReadAsAsync <_404Mess>().Result;
                        System.Windows.MessageBox.Show(err.Message);
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Get conversation messages for member.
        /// </summary>
        /// <param name="conversation">Conversation object.</param>
        /// <param name="member">Conversation member to get messages for.</param>
        /// <param name="includePrevious">Value indicates if all previous messages will be shown.</param>
        /// <param name="referentMesasgeId">If defined only messages newer than this one will be returned. </param>
        /// <returns>List of member messages.</returns>
        public IList <Message> GetMemberMessages(Conversation conversation, ConversationMember member, long referentMesasgeId, bool includePrevious = false)
        {
            if (conversation.Members.Contains(member))
            {
                var messages = _messageRepository.Query().Where(x => x.Conversation == conversation);

                if (!includePrevious)
                {
                    messages = messages.Where(x => x.CreatedOn > member.CreatedOn);
                }

                messages = messages.OrderByDescending(x => x.CreatedOn);
                if (referentMesasgeId != 0)
                {
                    var referentMessage = conversation.Messages.SingleOrDefault(x => x.Id == referentMesasgeId);
                    if (referentMessage != null)
                    {
                        messages = messages.Where(x => x.CreatedOn > referentMessage.CreatedOn);
                    }
                }

                return(messages.ToList());
            }

            throw new CmsException("Users is not member of this conversation.");
        }
コード例 #3
0
        /// <summary>
        /// Mark a message as read adding new MessageState or update existing setting its parameter IsRead to true.
        /// </summary>
        /// <param name="message">Message object.</param>
        /// <param name="member">Member object.</param>
        public void SetAsRead(Message message, ConversationMember member)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            if (member == null)
            {
                throw new ArgumentNullException("member");
            }

            var ms =
                _messageStateRepository.Query().SingleOrDefault(x => x.Member == member && x.Message == message);

            if (ms == null)
            {
                ms = new MessageState
                {
                    IsRead   = true,
                    Member   = member,
                    Message  = message,
                    ReadDate = DateTime.Now
                };
            }
            else
            {
                ms.IsRead   = true;
                ms.ReadDate = DateTime.Now;
            }
            _messageStateRepository.Save(ms);
        }
コード例 #4
0
        /// <summary>
        ///   Add member to conversation by creating presisetns or setting its parameter IsLeft to false
        /// </summary>
        /// <param name="conversation"> Conversation object. </param>
        /// <param name="Users"> Users object. </param>
        public void Join(Conversation conversation, Users Users)
        {
            if (conversation == null)
            {
                throw new ArgumentNullException("conversation");
            }

            if (Users == null)
            {
                throw new ArgumentNullException("Users");
            }

            var member = conversation.Members.SingleOrDefault(x => x.Users == Users);

            if (member == null)
            {
                member = new ConversationMember
                {
                    Conversation = conversation,
                    Users        = Users
                };
                conversation.Members.Add(member);
            }
            else
            {
                member.IsLeaved = false;
            }

            _conversationMemberRepository.Save(member);
        }
コード例 #5
0
        /// <summary>
        /// Mark message as unreaded setting its parameter IsRead to false.
        /// </summary>
        /// <param name="message">Message object.</param>
        /// <param name="member">Conversation member object.</param>
        public void SetAsUnread(Message message, ConversationMember member)
        {
            var ms =
                _messageStateRepository.Query().SingleOrDefault(x => x.Member == member && x.Message == message);

            if (ms != null)
            {
                ms.IsRead = false;
                _messageStateRepository.Save(ms);
            }
        }
コード例 #6
0
        public async Task <ConversationViewModel> AddConversation(AddConversationRequest request)
        {
            User currentUser = Feature.CurrentUser(httpContextAccessor, userRepository);

            var firstmember = new ConversationMember()
            {
                DateJoin = DateTime.Now,
                JoinBy   = currentUser.OId,
                MemberId = currentUser.OId,
                Nickname = $"{currentUser.FirstName} {currentUser.LastName}",
                Role     = ConversationRole.Admin
            };


            request.Participants.Add(firstmember);

            Conversation existConversation = new Conversation()
            {
                Participants = new List <ConversationMember>()
            };

            foreach (Conversation conver in conversationRepository.GetAll())
            {
                if (Feature.IsEqual(conver.Participants, request.Participants) == true)
                {
                    existConversation = conver;
                    break;
                }
            }

            if (existConversation.Participants.Count != 0)
            {
                return(mapper.Map <ConversationViewModel>(existConversation));
            }

            Conversation conversation = MessageAdapter.FromRequest(request, httpContextAccessor, userRepository);

            await conversationRepository.AddAsync(conversation);

            ClientGroup clientGroup = new ClientGroup()
            {
                UserIds   = request.Participants.Select(x => x.MemberId).ToList(),
                Name      = conversation.Id.ToString(),
                GroupType = Feature.GetTypeName(conversation)
            };

            await clientGroupRepository.AddAsync(clientGroup);

            Thread.Sleep(1000);
            return(mapper.Map <ConversationViewModel>(conversation));
        }
コード例 #7
0
        public async Task <IEnumerable <MessageViewModel> > AddMember(AddMemberRequest request)
        {
            var conversation = await conversationRepository.GetByIdAsync(ObjectId.Parse(request.ConversationId));

            var currentUser   = Feature.CurrentUser(httpContextAccessor, userRepository);
            var currentMember = conversation.Participants.FirstOrDefault(x => x.MemberId == currentUser.OId);

            if (currentMember.Role != ConversationRole.Admin)
            {
                throw new Exception("Bạn không có quyền thêm người dùng mới vào cuộc trò chuyện. ");
            }

            var result = new List <Message>();

            foreach (var userId in request.UserIds)
            {
                if (conversation.Participants.FirstOrDefault(x => x.MemberId == userId) == null)
                {
                    var user = await userRepository.GetByIdAsync(ObjectId.Parse(userId));

                    var conversationMember = new ConversationMember()
                    {
                        MemberId = user.OId,
                        DateJoin = DateTime.Now,
                        JoinBy   = currentUser.OId,
                        Nickname = $"{user.FirstName} {user.LastName}",
                        Role     = ConversationRole.Admin
                    };
                    conversation.Participants.Add(conversationMember);
                    await conversationRepository.UpdateAsync(conversation, conversation.Id);

                    var message = new Message()
                    {
                        SenderId       = currentUser.OId,
                        ConversationId = conversation.OId,
                        StringContent  = $"{user.FirstName} {user.LastName} đã được thêm vào nhóm",
                        Status         = ItemStatus.Active,
                        IsEdited       = false,
                        CreatedDate    = DateTime.Now,
                        ModifiedDate   = DateTime.Now
                    };
                    await messageRepository.AddAsync(message);

                    result.Add(message);
                }
            }

            return(mapper.Map <IEnumerable <MessageViewModel> >(result));
        }
コード例 #8
0
        /// <summary>
        ///   Create new conversation and assign members.
        /// </summary>
        /// <param name="Users"> List of Users to be assigned as a members. </param>
        /// <returns> Conversation instance </returns>
        public Conversation CreateConversation(IList <Users> users)
        {
            var conversation = new Conversation();

            foreach (var user in users)
            {
                var member = new ConversationMember
                {
                    Conversation = conversation,
                    Users        = user
                };

                conversation.Members.Add(member);
            }
            _conversationRepository.Save(conversation);
            return(conversation);
        }
コード例 #9
0
        /// <summary>
        /// Update the navigation property members in users
        /// <param name="body"></param>
        /// <param name="requestConfiguration">Configuration for the request such as headers, query parameters, and middleware options.</param>
        /// </summary>
        public RequestInformation CreatePatchRequestInformation(ConversationMember body, Action <ConversationMemberItemRequestBuilderPatchRequestConfiguration> requestConfiguration = default)
        {
            _ = body ?? throw new ArgumentNullException(nameof(body));
            var requestInfo = new RequestInformation {
                HttpMethod     = Method.PATCH,
                UrlTemplate    = UrlTemplate,
                PathParameters = PathParameters,
            };

            requestInfo.SetContentFromParsable(RequestAdapter, "application/json", body);
            if (requestConfiguration != null)
            {
                var requestConfig = new ConversationMemberItemRequestBuilderPatchRequestConfiguration();
                requestConfiguration.Invoke(requestConfig);
                requestInfo.AddRequestOptions(requestConfig.Options);
                requestInfo.AddHeaders(requestConfig.Headers);
            }
            return(requestInfo);
        }
コード例 #10
0
        /// <summary>
        ///   Remove member from conversation by setting its parameter IsLeft to true.
        /// </summary>
        /// <param name="conversation"> Conversation object. </param>
        /// <param name="member"> Member object. </param>
        public void Leave(Conversation conversation, ConversationMember member)
        {
            if (conversation == null)
            {
                throw new ArgumentNullException("conversation");
            }

            if (member == null)
            {
                throw new ArgumentNullException("member");
            }

            if (!member.IsLeaved)
            {
                member.IsLeaved = true;
            }

            _conversationMemberRepository.Save(member);
        }
コード例 #11
0
        // DELETE: api/ConversationMember/5
        public void Delete(int Id)
        {
            ConversationMember member = this.repository.FindById(Id);

            this.repository.Delete(member);
        }
コード例 #12
0
 // PUT: api/ConversationMember/5
 public void Put(int Id, [FromBody] ConversationMember value)
 {
     value.Id = Id;
     this.repository.Update(value);
 }
コード例 #13
0
 // POST: api/ConversationMember
 public void Post([FromBody] ConversationMember value)
 {
     this.repository.Create(value);
 }