Пример #1
0
        public IEnumerable<ApiMessage> SubverseFrontpage(string subverse)
        {
            if (String.IsNullOrEmpty(subverse)) {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            //IAmAGate: Perf mods for caching
            string cacheKey = String.Format("LegacyApi.SubverseFrontpage.{0}", subverse).ToLower();
            object cacheData = CacheHandler.Retrieve(cacheKey);

            if (cacheData == null)
            {

                cacheData = CacheHandler.Register(cacheKey, new Func<object>(() => {
                    // get only submissions from given subverses, order by rank - ignoring messages in any given banned subverse
                    var frontpageSubmissions = (from message in _db.Messages.Include("subverses")
                                                where message.Name != "deleted" && message.Subverse == subverse && message.Subverses.admin_disabled != true
                                                select message)
                                                .OrderByDescending(s => s.Rank)
                                                .Take(100)
                                                .ToList();

                    if (frontpageSubmissions == null || frontpageSubmissions.Count == 0)
                    {
                        return null; // throw new HttpResponseException(HttpStatusCode.NotFound);
                    }

                    var resultList = new List<ApiMessage>();

                    foreach (var item in frontpageSubmissions)
                    {
                        var resultModel = new ApiMessage
                        {
                            CommentCount = item.Comments.Count,
                            Date = item.Date,
                            Dislikes = item.Dislikes,
                            Id = item.Id,
                            LastEditDate = item.LastEditDate,
                            Likes = item.Likes,
                            Linkdescription = item.Linkdescription,
                            MessageContent = item.MessageContent
                        };

                        if (item.Anonymized || item.Subverses.anonymized_mode)
                        {
                            resultModel.Name = item.Id.ToString();
                        }
                        else
                        {
                            resultModel.Name = item.Name;
                        }
                        resultModel.Rank = item.Rank;
                        resultModel.Subverse = item.Subverse;
                        resultModel.Thumbnail = item.Thumbnail;
                        resultModel.Title = item.Title;
                        resultModel.Type = item.Type;

                        resultList.Add(resultModel);
                    }
                    return resultList;
                }), TimeSpan.FromMinutes(5), 2);
            }
            //means we didn't find one.
            if (cacheData == null) {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            return (IEnumerable<ApiMessage>)cacheData;
        }
Пример #2
0
        public ApiMessage SingleSubmission(int id)
        {
            ApiMessage singleSubmission = CacheHandler.Register<ApiMessage>(String.Format("LegacyApi.SingleSubmission.{0}", id),
              new Func<ApiMessage>(() =>
              {
                  using (voatEntities db = new voatEntities(CONSTANTS.CONNECTION_READONLY))
                  {
                      var submission = db.Messages.Find(id);

                      if (submission == null || submission.Subverses.admin_disabled == true)
                      {
                          return null; // throw new HttpResponseException(HttpStatusCode.NotFound);
                      }

                      var resultModel = new ApiMessage
                      {
                          CommentCount = submission.Comments.Count,
                          Id = submission.Id,
                          Date = submission.Date,
                          LastEditDate = submission.LastEditDate,
                          Likes = submission.Likes,
                          Dislikes = submission.Dislikes
                      };

                      if (submission.Anonymized || submission.Subverses.anonymized_mode)
                      {
                          resultModel.Name = submission.Id.ToString();
                      }
                      else
                      {
                          resultModel.Name = submission.Name;
                      }
                      resultModel.Rank = submission.Rank;
                      resultModel.Thumbnail = submission.Thumbnail;
                      resultModel.Subverse = submission.Subverse;
                      resultModel.Type = submission.Type;
                      resultModel.Title = submission.Title;
                      resultModel.Linkdescription = submission.Linkdescription;
                      resultModel.MessageContent = submission.MessageContent;

                      return resultModel;
                  }
              }), TimeSpan.FromMinutes(5), 2);

            if (singleSubmission == null) {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            return singleSubmission;
        }
Пример #3
0
        public IEnumerable<ApiMessage> Frontpage()
        {
            //IAmAGate: Perf mods for caching
            string cacheKey = String.Format("LegacyApi.Frontpage").ToLower();
            List<ApiMessage> cacheData = CacheHandler.Retrieve<List<ApiMessage>>(cacheKey);
            if (cacheData == null)
            {
                cacheData = CacheHandler.Register(cacheKey, new Func<List<ApiMessage>>(() =>
                {

                        // get only submissions from default subverses, order by rank
                        var frontpageSubmissions = (from message in _db.Messages.Include("subverses")
                                                    where !message.IsArchived && message.Name != "deleted" && message.Subverses.admin_disabled != true
                                                    join defaultsubverse in _db.Defaultsubverses on message.Subverse equals defaultsubverse.name
                                                    select message)
                                                    .OrderByDescending(s => s.Rank)
                                                    .Take(100)
                                                    .ToList();

                        var resultList = new List<ApiMessage>();

                        foreach (var item in frontpageSubmissions)
                        {
                            var resultModel = new ApiMessage
                            {
                                CommentCount = item.Comments.Count,
                                Date = item.Date,
                                Dislikes = item.Dislikes,
                                Id = item.Id,
                                LastEditDate = item.LastEditDate,
                                Likes = item.Likes,
                                Linkdescription = item.Linkdescription,
                                MessageContent = item.MessageContent
                            };

                            if (item.Anonymized || item.Subverses.anonymized_mode)
                            {
                                resultModel.Name = item.Id.ToString();
                            }
                            else
                            {
                                resultModel.Name = item.Name;
                            }
                            resultModel.Rank = item.Rank;
                            resultModel.Subverse = item.Subverse;
                            resultModel.Thumbnail = item.Thumbnail;
                            resultModel.Title = item.Title;
                            resultModel.Type = item.Type;

                            resultList.Add(resultModel);
                        }

                        return resultList;
                }), TimeSpan.FromMinutes(5), 4);
            }
            return (IEnumerable<ApiMessage>)cacheData;
        }
Пример #4
0
        public IEnumerable<ApiMessage> Frontpage()
        {
            // get only submissions from default subverses, order by rank
            var frontpageSubmissions = (from message in _db.Messages.Include("subverses")
                                        where message.Name != "deleted" && message.Subverses.admin_disabled != true
                                        join defaultsubverse in _db.Defaultsubverses on message.Subverse equals defaultsubverse.name
                                        select message)
                                        .OrderByDescending(s => s.Rank)
                                        .Take(100)
                                        .ToList();

            var resultList = new List<ApiMessage>();

            foreach (var item in frontpageSubmissions)
            {
                var resultModel = new ApiMessage
                {
                    CommentCount = item.Comments.Count,
                    Date = item.Date,
                    Dislikes = item.Dislikes,
                    Id = item.Id,
                    LastEditDate = item.LastEditDate,
                    Likes = item.Likes,
                    Linkdescription = item.Linkdescription,
                    MessageContent = item.MessageContent
                };

                if (item.Anonymized || item.Subverses.anonymized_mode)
                {
                    resultModel.Name = item.Id.ToString();
                }
                else
                {
                    resultModel.Name = item.Name;
                }
                resultModel.Rank = item.Rank;
                resultModel.Subverse = item.Subverse;
                resultModel.Thumbnail = item.Thumbnail;
                resultModel.Title = item.Title;
                resultModel.Type = item.Type;

                resultList.Add(resultModel);
            }

            return resultList;
        }
Пример #5
0
        public IEnumerable<ApiMessage> SubverseFrontpage(string subverse)
        {
            // get only submissions from given subverses, order by rank - ignoring messages in any given banned subverse
            var frontpageSubmissions = (from message in _db.Messages.Include("subverses")
                                        where message.Name != "deleted" && message.Subverse == subverse && message.Subverses.admin_disabled != true
                                        select message)
                                        .OrderByDescending(s => s.Rank)
                                        .Take(100)
                                        .ToList();

            if (frontpageSubmissions == null || frontpageSubmissions.Count == 0)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            var resultList = new List<ApiMessage>();

            foreach (var item in frontpageSubmissions)
            {
                var resultModel = new ApiMessage
                {
                    CommentCount = item.Comments.Count,
                    Date = item.Date,
                    Dislikes = item.Dislikes,
                    Id = item.Id,
                    LastEditDate = item.LastEditDate,
                    Likes = item.Likes,
                    Linkdescription = item.Linkdescription,
                    MessageContent = item.MessageContent
                };

                if (item.Anonymized || item.Subverses.anonymized_mode)
                {
                    resultModel.Name = item.Id.ToString();
                }
                else
                {
                    resultModel.Name = item.Name;
                }
                resultModel.Rank = item.Rank;
                resultModel.Subverse = item.Subverse;
                resultModel.Thumbnail = item.Thumbnail;
                resultModel.Title = item.Title;
                resultModel.Type = item.Type;

                resultList.Add(resultModel);
            }

            return resultList;
        }
Пример #6
0
        public ApiMessage SingleSubmission(int id)
        {
            var submission = _db.Messages.Find(id);

            if (submission == null || submission.Subverses.admin_disabled == true)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            var resultModel = new ApiMessage
            {
                CommentCount = submission.Comments.Count,
                Id = submission.Id,
                Date = submission.Date,
                LastEditDate = submission.LastEditDate,
                Likes = submission.Likes,
                Dislikes = submission.Dislikes
            };

            if (submission.Anonymized || submission.Subverses.anonymized_mode)
            {
                resultModel.Name = submission.Id.ToString();
            }
            else
            {
                resultModel.Name = submission.Name;
            }
            resultModel.Rank = submission.Rank;
            resultModel.Thumbnail = submission.Thumbnail;
            resultModel.Subverse = submission.Subverse;
            resultModel.Type = submission.Type;
            resultModel.Title = submission.Title;
            resultModel.Linkdescription = submission.Linkdescription;
            resultModel.MessageContent = submission.MessageContent;

            return resultModel;
        }