예제 #1
0
        private static async Task AddGroupToDb(Group group)
        {
            using (var db = new CacheDbContext())
            {
                await db.VkPeers.AddAsync(new VkPeer
                {
                    Avatar       = group.Photo50 == null ? null : await ImageUtils.UriToByte(group.Photo50),
                    PeerId       = -group.Id,
                    ReadableName = group.Name,
                    VkMessages   = new List <VkMessage>()
                });

                await db.SaveChangesAsync();
            }
        }
예제 #2
0
        private static async Task AddUserToDb(User user)
        {
            using (var db = new CacheDbContext())
            {
                await db.VkPeers.AddAsync(new VkPeer
                {
                    Avatar       = user.Photo50 == null ? null : await ImageUtils.UriToByte(user.Photo50),
                    PeerId       = user.Id,
                    ReadableName = user.FirstName + " " + user.LastName,
                    VkMessages   = new List <VkMessage>()
                });

                await db.SaveChangesAsync();
            }
        }
        public static async Task <ObservableCollection <ChatsListItemData> > GetChatsListAsItemData(ulong?offset = null)
        {
            var chats = await VkObjects.Api.Messages.GetConversationsAsync(new GetConversationsParams
            {
                Count  = 20,
                Offset = offset
            });

            var chatsListDatas = new ObservableCollection <ChatsListItemData>();

            foreach (var chat in chats.Items)
            {
                var peerName     = "";
                Uri peerImageUri = null;
                if (chat.Conversation.Peer.Type == ConversationPeerType.User)
                {
                    var user = (await VkObjects.Api.Users.GetAsync(new[] { chat.Conversation.Peer.Id },
                                                                   ProfileFields.Photo50 | ProfileFields.FirstName | ProfileFields.LastName))[0];
                    peerName     = user.FirstName + ' ' + user.LastName;
                    peerImageUri = user.Photo50;
                }
                else if (chat.Conversation.Peer.Type == ConversationPeerType.Chat)
                {
                    var chatSettings = chat.Conversation.ChatSettings;
                    peerName     = chatSettings.Title;
                    peerImageUri = chatSettings.Photo?.Photo50;
                }
                else if (chat.Conversation.Peer.Type == ConversationPeerType.Group)
                {
                    //method requires all params bug in library
                    var group = (await VkObjects.Api.Groups.GetByIdAsync(
                                     new[] { (-chat.Conversation.Peer.Id).ToString() },
                                     "", GroupsFields.IsVerified))[0];
                    peerName     = group.Name;
                    peerImageUri = group.Photo50;
                }

                var peerImage = await ImageUtils.UriToByte(peerImageUri);

                using (var db = new CacheDbContext())
                {
                    if (await db.VkPeers.FindAsync(chat.Conversation.Peer.Id) == null)
                    {
                        await db.VkPeers.AddAsync(new VkPeer
                        {
                            PeerId       = chat.Conversation.Peer.Id,
                            ReadableName = peerName,
                            Avatar       = peerImage,
                            VkMessages   = new List <VkMessage>()
                        });

                        await db.SaveChangesAsync();
                    }
                }

                var chatsListItemData = new ChatsListItemData
                {
                    PeerId     = chat.Conversation.Peer.Id,
                    IsThisUser = false,
                    Message    = chat.LastMessage.Text,
                    Name       = peerName,
                    Avatar     = await ImageUtils.ConvertToImageSource(peerImage)
                };
                chatsListDatas.Add(chatsListItemData);
            }

            return(chatsListDatas);
        }
        public static async Task <ObservableCollection <ChatsListItemData> > GetInboxesAsItemData()
        {
            var thisUser = await VkObjects.Api.Users.GetAsync(new long[] { },
                                                              ProfileFields.Photo50 | ProfileFields.FirstName | ProfileFields.LastName);

            //this user is first, groups will go after
            //extract to another method
            using (var db = new CacheDbContext())
            {
                db.SessionInfo.First().UserId       = thisUser.First().Id;
                db.SessionInfo.First().ReadableName = thisUser.First().FirstName + " " + thisUser.First().LastName;
                await db.SaveChangesAsync();
            }
            var groupsNavigationViewData = new List <ChatsListItemData>
            {
                await thisUser.Select(async sthisUser =>
                {
                    var avatar = await ImageUtils.ConvertToImageSource(await ImageUtils.UriToByte(sthisUser.Photo50));
                    return(new ChatsListItemData
                    {
                        Name = sthisUser.FirstName + " " + sthisUser.LastName,
                        Avatar = avatar
                    });
                }).First(),
            };
            var groupsVkCollection = await VkObjects.Api.Groups.GetAsync(new GroupsGetParams
            {
                Filter   = GroupsFilters.Moderator,
                Extended = true,
                Count    = 1000
            });

            foreach (var vkGroup in groupsVkCollection)
            {
                var avatar = await ImageUtils.ConvertToImageSource(await ImageUtils.UriToByte(vkGroup.Photo50));

                var groupItem = new ChatsListItemData
                {
                    Name   = vkGroup.Name,
                    Avatar = avatar
                };

                groupsNavigationViewData.Add(groupItem);
            }

            using (var db = new CacheDbContext())
            {
                foreach (var vkGroup in groupsVkCollection)
                {
                    //todo update, not add
                    if (db.Inboxes.Find(vkGroup.Id) == null)
                    {
                        await db.Inboxes.AddAsync(new VkInbox
                        {
                            //todo double download to single
                            Avatar       = await ImageUtils.UriToByte(vkGroup.Photo50),
                            Id           = vkGroup.Id,
                            Type         = InboxType.Group,
                            ReadableName = vkGroup.Name
                        });
                    }
                }
                if (db.Inboxes.Find(thisUser.First().Id) == null)
                {
                    await db.Inboxes.AddAsync(new VkInbox
                    {
                        Avatar       = await ImageUtils.UriToByte(thisUser.First().Photo50),
                        Id           = thisUser.First().Id,
                        Type         = InboxType.User,
                        ReadableName = thisUser.First().FirstName + " " + thisUser.First().LastName
                    });
                }
                await db.SaveChangesAsync();
            }

            return(new ObservableCollection <ChatsListItemData>(groupsNavigationViewData));
        }
예제 #5
0
        private async void OnNewMessageReceived(Message message)
        {
            // var forwardedMessages = message.ForwardedMessages?.ToArray() ?? new Message[] { };
            var attachments = message.Attachments?.ToArray() ?? new Attachment[] { };

            // var replyMessage = message.ReplyMessage;
            // var actionObject = message.Action;

            using (var db = new CacheDbContext())
            {
                if (message.FromId == null || message.PeerId == null)
                {
                    return;
                }
                string readableName;
                var    foundPeerByFromId = await db.VkPeers.FindAsync(message.FromId);

                if (foundPeerByFromId != null)
                {
                    readableName = foundPeerByFromId.ReadableName;
                }
                else
                {
                    var peer = (await VkUtils.GetAndCacheNewPeers(new[] { (long)message.FromId })).First();
                    readableName = message.FromId > 0
                        ? ((User)peer).FirstName + " " + ((User)peer).LastName
                        : ((Group)peer).Name;
                    foundPeerByFromId = await db.VkPeers.FindAsync(message.FromId);
                }
                var foundPeerByPeerId = await db.VkPeers.FindAsync(message.PeerId);

                var indexToUpdate = 0;
                ChatsListItemData itemToUpdate = null;
                for (var i = 0; i < _viewModel.ChatsList.Count; i++)
                {
                    var item = _viewModel.ChatsList[i];
                    if (item.PeerId != message.PeerId)
                    {
                        continue;
                    }
                    indexToUpdate = i;
                    itemToUpdate  = item;
                    (await db.VkPeers.FindAsync(item.PeerId)).LastMessage = message.Text;
                    break;
                }

                _viewModel.ChatsList.RemoveAt(indexToUpdate);
                _viewModel.ChatsList.Insert(0, new ChatsListItemData
                {
                    Avatar     = itemToUpdate?.Avatar,
                    Message    = message.Text,
                    Name       = itemToUpdate?.Name,
                    PeerId     = message.PeerId ?? 0,
                    IsThisUser = itemToUpdate?.IsThisUser ?? false
                });

                await db.VkMessages.AddAsync(new VkMessage
                {
                    ConversationMessageId = message.ConversationMessageId ?? 0,
                    Text        = message.Text,
                    FromId      = message.FromId ?? 0,
                    PeerId      = message.PeerId ?? 0,
                    VkPeer      = foundPeerByPeerId,
                    ReceiveTime = DateTime.Now
                });

                await db.SaveChangesAsync();

                _viewModel.MessageDict[(long)message.PeerId].Add(new ChatsListItemData
                {
                    Name       = readableName,
                    Message    = message.Text,
                    IsThisUser = message.FromId == db.SessionInfo.First().UserId
                });
                foreach (var attachment in attachments)
                {
                    if (attachment.Type == typeof(Photo))
                    {
                        await ViewAttachmentInWindow(attachment);
                    }
                }
            }
        }