public async Task <IViewComponentResult> InvokeAsync()
        {
            try
            {
                var key = "recent-activity";
                List <ContentViewModel> data;
                if (!_cache.TryGetValue(key, out data))
                {
                    var controller = new ContentManager();
                    data = controller.Search(new SearchRequest()
                    {
                        PageSize = 25
                    });

                    foreach (var content in data)
                    {
                        if (content.Type == "answer")
                        {
                            var parent = ContentApi.GetParent(content.Id).First();
                            content.Title    = parent.Title;
                            content.ParentId = parent.Id;
                        }
                    }
                    _cache.Set(key, data, new MemoryCacheEntryOptions().SetAbsoluteExpiration(TimeSpan.FromMinutes(1)));
                }
                return(View(data));
            }
            catch (Exception e)
            {
                return(View(new List <ContentViewModel>()));
            }
        }
        public async Task <IViewComponentResult> InvokeAsync()
        {
            try
            {
                //if (_cache == null)
                //{
                var controller = new ContentController();
                _cache = ContentApi.Search(25, 1, null, null, null, null);
                //}

                foreach (var content in _cache)
                {
                    if (content.Type == "answer")
                    {
                        content.Title    = parent.Title;
                        content.ParentId = parent.Id;
                    }
                }
                return(View(_cache));
            }
            catch (Exception e)
            {
                return(View(new List <Content>()));
            }
        }
Exemple #3
0
        public ContentViewModel Create(ContentRequest request)
        {
            Authorized();

            var content = new Content();

            content.Title    = request.Title;
            content.Body     = request.Body;
            content.HtmlBody = Markdown.Encode(content.Body);
            content.UserId   = user.Id;
            content.Type     = request.Type;
            var id = ContentApi.Insert(content);

            TagManager.SetTagsForContent(id, request.Tags);

            if (request.ParentId != null)
            {
                ContentApi.Relate(request.ParentId.Value, id);
            }

            return(ContentApi.Select(id).AsViewModel()
                   .WithChildren()
                   .WithChildrenCount()
                   .WithTags());
        }
Exemple #4
0
        public ContentViewModel Create(ContentRequest request)
        {
            Authorized();

            var content = new Content();

            content.Title    = request.Title;
            content.Body     = request.Body;
            content.HtmlBody = Markdown.Encode(content.Body);
            content.UserId   = user.Id;
            content.Type     = request.Type;
            var id = ContentApi.Insert(content);

            TagManager.SetTagsForContent(id, request.Tags);

            if (request.ParentId != null)
            {
                ContentApi.Relate(request.ParentId.Value, id);
            }

            var item = ContentApi.Select(id).AsViewModel()
                       .WithChildren()
                       .WithChildrenCount()
                       .WithUser()
                       .WithTags();

            Searcher.Instance.Index(new Searchable()
            {
                Id = id, Type = item.Type, Title = item.Title, Body = item.Body, Username = item.User.DisplayName
            });
            ContentApi.MarkAsIndexed(id);
            return(item);
        }
Exemple #5
0
        public ContentViewModel Update(ContentRequest request)
        {
            Authorized();

            if (request.ContentId == null)
            {
                throw new ArgumentNullException(nameof(request.ContentId));
            }

            var userId = user?.Id;

            if (userId == null)
            {
                throw new ArgumentNullException(nameof(request), "User must be set to update an answer");
            }

            TagManager.SetTagsForContent((int)request.ContentId, request.Tags);

            var htmlBody = Markdown.Encode(request.Body);

            ContentApi.Update(request.ContentId.Value, request.Title, request.Body, htmlBody, (int)userId);

            return(ContentApi.Select(request.ContentId.Value)
                   .AsViewModel()
                   .WithAll());
        }
Exemple #6
0
        public ContentViewModel Update(ContentRequest request)
        {
            Authorized();

            if (request.ContentId == null)
            {
                throw new ArgumentNullException(nameof(request.ContentId));
            }

            var userId = user?.Id;

            if (userId == null)
            {
                throw new ArgumentNullException(nameof(request), "User must be set to update an answer");
            }

            TagManager.SetTagsForContent((int)request.ContentId, request.Tags);

            var htmlBody = Markdown.Encode(request.Body);

            ContentApi.Update(request.ContentId.Value, request.Title, request.Body, htmlBody, (int)userId);

            var item = ContentApi.Select(request.ContentId.Value)
                       .AsViewModel()
                       .WithAll();

            Searcher.Instance.Index(new Searchable()
            {
                Id = item.Id, Type = item.Type, Title = item.Title, Body = item.Body, Username = item.User.DisplayName
            });
            ContentApi.MarkAsIndexed(item.Id);
            return(item);
        }
        public ActionResult Search(int?p, string o, string q, int?t)
        {
            if (p == null)
            {
                p = 1;
            }

            if (q == null)
            {
                return(SearchOld(p.Value, o, q, t));
            }

            var results = Searcher.Instance.Search(q, 100, 0)
                          .Select(r => r.Id)
                          .Select(i => ContentApi.Select(i).AsViewModel().WithTags().WithUser())
                          .ToList();

            var searchRequest = new SearchRequest();

            searchRequest.Text    = q;
            searchRequest.Page    = p;
            searchRequest.OrderBy = o;
            searchRequest.Type    = "question";

            var resultPage = new SearchResultViewModel()
            {
                Results = results, Request = searchRequest
            };

            resultPage.ResultsCount = results.Count;
            resultPage.MaxPages     = Math.Min(5, (int)Math.Floor((double)resultPage.ResultsCount / 10));
            resultPage.Results      = results.Skip((p.Value - 1) * 10).Take(10).ToList();
            return(View("Search", resultPage));
        }
        public IActionResult Similar(int id)
        {
            var content = ContentApi.Select(id);
            var ids     = Searcher.Instance.Search(content.Title, 100, 0);
            var results = ids.Where(i => i.Id != id).Select(i => ContentApi.Select(i.Id)).Where(c => c.Type == "question").Select(c => c.AsViewModel().WithTags().WithUser()).Take(3).ToList();

            return(View(results));
        }
        public static ContentViewModel WithChildren(this ContentViewModel i)
        {
            var children = ContentApi.SelectByParent(i.Id);

            i.ChildrenCount = ContentApi.GetChildrenCount(i.Id);
            i.Children      = children.Select(c => ContentViewModel.New(c).WithUser().WithTags()).ToList();
            return(i);
        }
Exemple #10
0
 public void Init()
 {
     _contentApi               = new ContentApi();
     _validArticleUrl          = "https://content.watchmedier.dk/api/finanswatch/content/article/9517468";
     _validFrontPageArticleUrl = "https://content.watchmedier.dk/api/finanswatch/content/frontpagearticles";
     _validLatestArticleUrl    = "https://content.watchmedier.dk/api/finanswatch/content/latest?hoursago=168&max=10";
     _validSectionUrl          = "https://content.watchmedier.dk/api/finanswatch/content/latest?hoursago=500&max=30&section=fw_finansnyt_penge";
     _invalidUrl               = "https://thiswebsitedoesnotexisteveeeeeeeeeeeeer.dk";
 }
        private void OnDestroy()
        {
            _bucketData  = null;
            _bucketsApi  = null;
            _entriesApi  = null;
            _contentApi  = null;
            _releasesApi = null;

            Debug.Log("Destroy Cloud Content Window");
        }
Exemple #12
0
        public IActionResult Show(int id, string response = null)
        {
            var content = ContentApi.Select(id);

            ViewData["Title"] = content.Title;
            return(View(new ShowContentModel()
            {
                Content = content, Response = response
            }));
        }
        public IActionResult Ask(int?id)
        {
            if (id == null)
            {
                return(View(new ContentRequest {
                    Type = "question", AvailableTags = TagApi.SelectSuggestions()
                }));
            }

            var c = ContentApi.Select(id.Value);

            ViewData["Title"] = c.Title;
            return(View(c.AsRequest()));
        }
Exemple #14
0
        public List <ContentViewModel> Search(SearchRequest request)
        {
            request.PageSize = request.PageSize ?? 10;
            request.Page     = request.Page ?? 1;

            var contents     = ContentApi.Search(request.PageSize.Value, request.Page, request.Type, request.Text, request.OrderBy, request.TagId);
            var searchResult = contents.Select(c => c.AsViewModel()
                                               .WithTags()
                                               .WithChildrenCount()
                                               .WithUser()
                                               .WithEditedBy()).ToList();

            return(searchResult);
        }
Exemple #15
0
        private void Update()
        {
            var content = ContentApi.NeedIndexing();
            var vms     = content.Select(c => c.AsViewModel().WithUser());

            Searcher.Instance.Index(vms.Select(c => new Searchable()
            {
                Id = c.Id, Title = c.Title, Body = c.Body, Type = c.Type, Username = c.User.DisplayName
            }));

            foreach (var c in content)
            {
                ContentApi.MarkAsIndexed(c.Id);
            }
        }
Exemple #16
0
        public IActionResult Ask(int?id)
        {
            if (id == null)
            {
                return(View(new Content()
                {
                    UserId = 60, Type = "question"
                }));
            }

            var c = ContentApi.Select(id.Value);

            ViewData["Title"] = c.Title;
            return(View(c));
        }
Exemple #17
0
        public IActionResult Ask(Content content)
        {
            int id = content.Id;

            if (id == 0)
            {
                content.HtmlBody = Markdown.Encode(content.Body);
                id = ContentApi.Insert(content);
            }
            else
            {
                ContentApi.Update(content.Id, content.Title, content.Body, Markdown.Encode(content.Body));
            }

            return(RedirectToAction("Show", new { Id = id, Response = "Your question was added" }));
        }
        public IActionResult SearchOld(int p, string o, string q, int t)
        {
            if (p == 0)
            {
                p = 1;
            }

            int?tagId = t;

            if (t == 0)
            {
                tagId = null;
            }

            if (o == null)
            {
                o = "created-desc";
            }

            var searchRequest = new SearchRequest();

            searchRequest.Text    = q;
            searchRequest.Page    = p;
            searchRequest.OrderBy = o;
            searchRequest.Type    = "question";
            searchRequest.TagId   = tagId;

            var manager    = new ContentManager();
            var results    = manager.Search(searchRequest);
            var resultPage = new SearchResultViewModel()
            {
                Results = results, Request = searchRequest
            };

            resultPage.ResultsCount = ContentApi.GetSearchResultCount("question", q, t);
            resultPage.MaxPages     = Math.Min(5, (int)Math.Floor((double)resultPage.ResultsCount / 10));

            if (resultPage.ResultsCount % 10 != 0)
            {
                resultPage.MaxPages++;
            }

            //move to Tag controller
            resultPage.Tags = TagApi.Select().OrderByDescending(tag => tag.Count).Take(8).ToList();

            return(View("Results", resultPage));
        }
        public IActionResult Similar(int id)
        {
            var        content = ContentApi.Select(id);
            WebRequest request = WebRequest.Create($"http://localhost:63683/api/Values?search={content.Title}&max=50&minScore=0");

            request.Method = "GET";
            var response = request.GetResponseAsync().Result;
            IEnumerable <int> ids;

            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                var s = reader.ReadToEnd().Replace("[", "").Replace("]", "").Split(',');
                ids = s.Where(str => !string.IsNullOrEmpty(str)).Select(i => int.Parse(i)).ToList();
            }
            var results = ids.Where(i => i != id).Select(i => ContentApi.Select(i)).Where(c => c.Type == "question").Select(c => c.AsViewModel().WithTags().WithUser()).Take(3).ToList();

            return(View(results));
        }
Exemple #20
0
 public string Answer([FromBody] AnswerRequest req)
 {
     if (req.AnswerId == 0)
     {
         var md       = Markdown.Encode(req.Answer);
         var htmlBody = Markdown.Encode(req.Answer);
         ContentApi.InsertAsChild(req.QuestionId, new Content()
         {
             Type = "answer", Body = req.Answer, UserId = 1, HtmlBody = htmlBody
         });
         return(md);
     }
     else
     {
         var md = Markdown.Encode(req.Answer);
         ContentApi.Update(req.AnswerId, null, req.Answer, md);
         return(md);
     }
 }
Exemple #21
0
        public IActionResult Search(int p, string o, string q, int t)
        {
            var db = new DbApi();

            if (p == 0)
            {
                p = 1;
            }

            int?tagId = t;

            if (t == 0)
            {
                tagId = null;
            }

            if (o == null)
            {
                o = "created-desc";
            }

            var content = ContentApi.Search(10, p, "question", q, o, tagId);

            var result = new ContentListModel();

            result.Content      = content;
            result.ResultsCount = ContentApi.GetSearchResultCount("question", q, t);

            result.MaxPages = Math.Min(5, (int)Math.Floor((double)result.ResultsCount / 10));

            if (result.ResultsCount % 10 != 0)
            {
                result.MaxPages++;
            }

            result.Page       = p;
            result.SearchText = q;
            result.OrderBy    = o;
            result.Tags       = TagApi.Select().OrderByDescending(tag => tag.Count).Take(8).ToList();

            return(View("Results", result));
        }
        public IActionResult Compare(int id)
        {
            var content    = ContentApi.Select(id);
            var oldContent = ContentApi.Select(id);
            var changeSets = ContentHistoryApi.SelectByContentId(id, DateTime.Now.AddDays(-1), DateTime.Now).OrderBy(c => c.Changed);

            foreach (var property in oldContent.GetType().GetProperties())
            {
                if (changeSets.Any(c => c.ChangedField == property.Name))
                {
                    var oldVal = changeSets.First(c => c.ChangedField == property.Name).OldValue;
                    property.SetValue(oldContent, oldVal);
                }
            }

            oldContent.HtmlBody = Site.Markdown.Encode(oldContent.Body);

            return(View(new Comparison {
                Old = oldContent, New = content, ChangeSets = changeSets.Select(cs => new ContentHistoryViewModel(cs)).ToList()
            }));
        }
        public IActionResult Search(int p, string o, string q, int t)
        {
            if (q == null)
            {
                return(SearchOld(p, o, q, t));
            }

            WebRequest request = WebRequest.Create($"http://localhost:63683/api/Values?search={q}&max=100");

            request.Method = "GET";
            var response = request.GetResponseAsync().Result;
            IEnumerable <int> ids;

            using (var reader = new StreamReader(response.GetResponseStream()))
            {
                ids = reader.ReadToEnd()
                      .Replace("[", "").Replace("]", "")
                      .Split(',')
                      .Where(s => !string.IsNullOrEmpty(s))
                      .Select(id => int.Parse(id));
            }
            var results = ids.Select(i => ContentApi.Select(i).AsViewModel().WithTags().WithUser()).ToList();

            var searchRequest = new SearchRequest();

            searchRequest.Text    = q;
            searchRequest.Page    = p;
            searchRequest.OrderBy = o;
            searchRequest.Type    = "question";

            var resultPage = new SearchResultViewModel()
            {
                Results = results, Request = searchRequest
            };

            resultPage.ResultsCount = results.Count;
            resultPage.MaxPages     = Math.Min(5, (int)Math.Floor((double)resultPage.ResultsCount / 10));
            resultPage.Results      = results.Skip((p - 1) * 10).Take(10).ToList();
            return(View("Search", resultPage));
        }
Exemple #24
0
        private void DownloadConfig()
        {
            string path = $"https://{Application.cloudProjectId}.client-api.unity3dusercontent.com/client_api/v1";

            _settings = Resources.Load <CloudContentSettings>("CloudContentSettings");

            var c = new ContentApi(path);
            var e = new EntriesApi(path);
            var r = new ReleasesApi(path);

            var entry = e.GetEntryByPathPublic(_settings.lastBucketData.bucketId, "config.json");

            if (entry != null)
            {
                string bid = _settings.lastBucketData.bucketId;
                var    rid = r.GetReleaseByBadgePublic(bid, "latest");

                Stream stream = c.GetReleaseContentPublic(bid, rid.Releaseid.ToString(), entry.Entryid.ToString());

                var ms = new MemoryStream();
                stream.CopyTo(ms);

                string rawString = Encoding.ASCII.GetString(ms.ToArray());

                var sb = new StringBuilder(rawString.Length);

                foreach (char i in rawString)
                {
                    if (i != '\n' && i != '\r' && i != '\t')
                    {
                        sb.Append(i);
                    }
                }

                rawString = sb.ToString();
                rawString = Regex.Replace(rawString, @"\s+", "");

                _data = deserializeToDictionary(rawString);
            }
        }
        public JSEngineInstance(IRequestApi requestApi,
                                IResponseApi responseApi,
                                IConsoleLogApi consoleLogApi)
        {
            _requestApi      = requestApi;
            _responseApi     = responseApi;
            _consoleLogApi   = consoleLogApi;
            _htmlDocumentApi = new HtmlDocumentApi();
            _formApi         = new FormApi();
            _javascriptApi   = new JavascriptApi();
            _jsonApi         = new JsonApi();
            _contentApi      = new ContentApi();

            AddHostObject("request", _requestApi);
            AddHostObject("response", _responseApi);
            AddHostObject("log", _consoleLogApi);
            AddHostObject("document", _htmlDocumentApi);
            AddHostObject("form", _formApi);
            AddHostObject("js", _javascriptApi);
            AddHostObject("json", _jsonApi);
            AddHostObject("content", _contentApi);
        }
        private void Connect()
        {
            _settings      = Resources.Load <CloudContentSettings>("CloudContentSettings");
            _decodedApiKey = CcdHelper.Base64Encode($":{_settings.apiKey}");

            _bucketsApi  = new BucketsApi();
            _entriesApi  = new EntriesApi();
            _contentApi  = new ContentApi();
            _releasesApi = new ReleasesApi();

            if (_settings.lastBucketData != null)
            {
                _bucketData = _settings.lastBucketData;
            }
            if (!_bucketsApi.Configuration.DefaultHeader.ContainsKey("Authorization"))
            {
                _bucketsApi.Configuration.DefaultHeader.Add("Authorization", $"Basic {_decodedApiKey}");
            }

            _windowElements = new CcdWindowElements(rootVisualElement, _settings, _bucketData);
            RegisterCallbacks();
        }
Exemple #27
0
 public void Init()
 {
     instance = new ContentApi();
 }
Exemple #28
0
 private void AddRelation(int parentId, int childId)
 {
     ContentApi.Relate(parentId, childId);
 }
 public static ContentViewModel WithParent(this ContentViewModel c)
 {
     c.Parent = ContentApi.GetParent(c.Id).First().AsViewModel();
     return(c);
 }
Exemple #30
0
        public IActionResult Edit(int id)
        {
            var content = ContentApi.Select(id);

            return(View(content));
        }