예제 #1
0
        public WallPostData GetAttachmentsPost(ReadOnlyCollection <Attachment> attachments)
        {
            var vkPosts = attachments.Where(attachment => attachment.Instance.GetType() == typeof(Wall))
                          .Select(attachment => (Wall)attachment.Instance) //IEnumerable of Wall posts among Attachments
                          .ToArray();
            var vkPost = vkPosts[0];                                       //InvalidOperationException = wallPost - пустая коллекция.

            //ArgumentNullException = wallpost - null ?
            if (vkPosts.Length > 1)
            {
                throw new ApplicationException("More than 1 post in attachments!"); //More than 1 posts in message
            }
            var    id        = vkPost.FromId;
            string groupName = GroupNameById(id);
            string groupPic  = GroupPicById(id);

            //Console.WriteLine($"vk.com/public{-id}");
            //Console.WriteLine($"vk.com/wall{vkPost.OwnerId}_{vkPost.Id}");
            if (vkPost.CopyHistory.Count() > 1)
            {
                throw new DeepCopyHistoryException(vkPost.CopyHistory.Count());
            }

            WallPostData originalWallPost = null;

            if (vkPost.CopyHistory.Count() > 0)
            {
                var    original          = vkPost.CopyHistory.First();
                var    originalGroupId   = original.FromId;
                string originalGroupName = GroupNameById(originalGroupId);
                string originalGroupPic  = GroupPicById(originalGroupId);
                string originalLink      = null;
                if (original != null && original.OwnerId != null && original.Id != null)
                {
                    originalLink = $"vk.com/wall{original.OwnerId}_{original.Id}";
                }
                originalWallPost = new WallPostData(originalGroupName, originalGroupPic, original.Text, original.Date,
                                                    original.Attachments, null, originalLink);
            }

            string link = null;

            if (vkPost != null && vkPost.OwnerId != null && vkPost.Id != null)
            {
                link = $"vk.com/wall{vkPost.OwnerId}_{vkPost.Id}";
            }
            //WallPostData wallPost = new WallPostData(groupName, vkPost.Text, vkPost.Date, vkPost.Attachments);
            WallPostData wallPost = new WallPostData(groupName, groupPic, vkPost.Text, vkPost.Date, vkPost.Attachments,
                                                     originalWallPost, link);

            return(wallPost);
        }
예제 #2
0
        public MessagesSendParams HandlePost(long userId, WallPostData postData, Message message)
        {
            State st = new State {
                PostData = postData
            };

            usersStateMap[userId] = st;
            return(new MessagesSendParams()
            {
                PeerId = message.PeerId,
                RandomId = new Random().Next(),
                Keyboard = Keyboards.PostHandlingKeyboardBuilder().Build(),
                Message = "Обработка поста."
            });
        }
예제 #3
0
 public bool AddPost(WallPostData wallPostData, string[] tags)
 {
     /*_idsToWallPostData[_index] = wallPostData;
      * _idsToTags[_index] = new List<string>();
      * if (tags != null)
      * {
      *  foreach (var tag in tags)
      *  {
      *      if (_tags.Contains(tag))
      *      {
      *          _idsToTags[_index].Add(tag);
      *      }
      *  }
      * }
      * _index++;
      * return true;*/
     return(AddPost(wallPostData, tags, out int id));
 }
예제 #4
0
        public bool AddPost(WallPostData wallPostData, string[] tags, out int id)
        {
            var json = wallPostData.ToJson();

            /*using (StreamWriter sw = new StreamWriter(Globals.DefaultFolderPath+"jsonfile.txt",false,System.Text.Encoding.UTF8))
             * {
             *  sw.WriteLine(json);
             * }*/

            var d = JsonSerializer.Deserialize <Dictionary <String, Object> >(json);

            Console.WriteLine(d["Text"]);
            Console.WriteLine("json here");


            _idsToWallPostData[_index] = wallPostData;
            _idsToTags[_index]         = new List <string>();

            /*var photoes = wallPostData.Attachments.Where(att => PhotoModel.IsPhoto(att))
             *  .Select(att => new PhotoModel(att));
             * foreach (var photo in photoes)
             * {
             *  photo.PrintUri();
             * }*/
            id = _index; //id of added post
            if (tags != null)
            {
                foreach (var tag in tags)
                {
                    if (_tags.Contains(tag))
                    {
                        _idsToTags[_index].Add(tag);
                    }
                }
            }
            _index++;
            return(true);
        }
예제 #5
0
        public async Task <int> AddPost(WallPostData wallPostData, string[] tags)
        {
            string token = await tokenBotAuthorizer.GetToken();

            Globals.Client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
            int idOriginal = -1;

            if (wallPostData.IsRepost())
            {
                PostDTO original = new PostDTO(wallPostData.Original, new string[] {});
                original.IsShared = true;
                string originalJson     = JsonSerializer.Serialize <PostDTO>(original);
                var    responseOriginal =
                    await Globals.Client.PostAsync(feedUrl, new StringContent(originalJson, Encoding.UTF8, "application/json"));

                string contentOriginal = await responseOriginal.Content.ReadAsStringAsync();

                var dictOriginal = JsonSerializer.Deserialize <Dictionary <string, object> >(contentOriginal);
                idOriginal = ((JsonElement)dictOriginal["id"]).GetInt32();
            }
            PostDTO jsonData = new PostDTO(wallPostData, tags);

            jsonData.OriginalPostId = idOriginal;
            string json = JsonSerializer.Serialize <PostDTO>(jsonData);

            Console.WriteLine(json);
            var response =
                await Globals.Client.PostAsync(feedUrl, new StringContent(json, Encoding.UTF8, "application/json"));

            string content = await response.Content.ReadAsStringAsync();

            //Console.WriteLine(content);
            var dict = JsonSerializer.Deserialize <Dictionary <string, object> >(content);

            return(((JsonElement)dict["id"]).GetInt32());
        }
예제 #6
0
            public PostDTO(WallPostData post, string[] tags)
            {
                Date          = (DateTime)post.Date; //Dangerous!
                Author        = post.Author;
                AuthorPicture = post.AuthorPicUrl;
                Text          = post.Text;
                List <ContentDTO> contentsList = new List <ContentDTO>();

                foreach (var attachment in post.Attachments)
                {
                    ContentDTO contentDto = new ContentDTO()
                    {
                        Type = attachment.AttachmentType,
                        Path = attachment.Uri
                    };
                    contentsList.Add(contentDto);
                }

                Contents       = contentsList;
                Hashtags       = tags.ToList();
                IsShared       = false;
                OriginalPostId = -1;
                VkPostLink     = post.Link;
            }
예제 #7
0
 public State()
 {
     PostData = new WallPostData();
     Tags     = new List <string>();
 }