// this is for webpage reads from database. CounterPartyUsername is available. public static ChatDto GetDtoFromChat(wx_chat chat) { string ownerUsername = MvcApplication.UserIdUsernameCache[chat.OwnerID]; string ownerUserNickname = MvcApplication.UsernameNicknameCache[ownerUsername]; string counterPartyUsername = MvcApplication.UserIdUsernameCache[chat.CounterPartyID]; string counterPartyUserNickname = MvcApplication.UsernameNicknameCache[counterPartyUsername]; string fromUsername; if (chat.IsFromOwner) { fromUsername = ownerUsername; } else { fromUsername = string.IsNullOrEmpty(chat.FromUsername) ? counterPartyUsername : chat.FromUsername; } var dto = new ChatDto { ID = chat.ID, OwnerID = chat.OwnerID, OwnerUsername = ownerUsername, OwnerUserNickname = ownerUserNickname, FromUsername = fromUsername, FromUserNickname = MvcApplication.UsernameNicknameCache[fromUsername], IsFromOwner = chat.IsFromOwner, CounterPartyUsername = counterPartyUsername, CounterPartyUserNickname = counterPartyUserNickname, Content = chat.Content, ImageID = chat.ImageID, VoiceID = chat.VoiceID, CreateTime = chat.CreateTime, }; return(dto); }
// this is for server reads messages from webpage ajax post. ToUsername is available. public static wx_chat GetChatFromDto(ChatDto dto) { bool isFromOwner = false; string counterPartyUsername; string fromUsername = string.Empty; if (dto.OwnerUsername == dto.FromUsername) { counterPartyUsername = dto.ToUsername; isFromOwner = true; } else { // check if dto.FromUsername is a chatroom if (IsChatRoom(dto.FromUsername)) { fromUsername = GetFromUsername(dto.Content); dto.Content = GetNewContent(dto.Content); } counterPartyUsername = dto.FromUsername; isFromOwner = false; } var chat = new wx_chat { Content = dto.Content, ImageID = dto.ImageID, VoiceID = dto.VoiceID, CreateTime = dto.CreateTime, OwnerID = MvcApplication.UsernameUserIdCache[dto.OwnerUsername], FromUsername = fromUsername, CounterPartyID = MvcApplication.UsernameUserIdCache[counterPartyUsername], IsFromOwner = isFromOwner }; return(chat); }