A dynamic wrapper around an ISettingsRepository for retrieving, adding and updating settings.
Inheritance: System.Dynamic.DynamicObject
Ejemplo n.º 1
0
 public HomeModule(IIdeaRepository ideas, IUserRepository users, Settings settings)
 {
     _ideas = ideas;
     _users = users;
     _settings = settings;
     Get["/"] = _ => ListIdeas(_ideas.GetAll(), SelectedTab.Popular, "");
     Get["/top"] = _ => ListIdeas(_ideas.GetAll().OrderByDescending(i => i.Votes.Sum(s => s.Value)), SelectedTab.Hot, "");
     Get["/new"] = _ => ListIdeas(_ideas.GetAll().OrderByDescending(i => i.Time), SelectedTab.New, "");
     Get["/login"] = _ =>
                         {
                             return ListIdeas(_ideas.GetAll(), SelectedTab.Popular,
                                              Strings.HomeModule_LoginRequired);
                         };
 }
Ejemplo n.º 2
0
        public ApiSecuredModule(IIdeaRepository ideas, Settings settings)
            : base("/api")
        {
            _settings = settings;

            this.Before.AddItemToEndOfPipeline(ctx => {
                if (ctx.CurrentUser == null)
                    return HttpStatusCode.Unauthorized;
                return null;
            });

            Post["/ideas"] = _ => {
                var model = this.Bind<EditIdeaModel>();

                var idea = new Idea {
                    Title = model.title,
                    Description = model.description,
                    Time = DateTime.UtcNow,
                    Author = (User)Context.CurrentUser,
                    Status = _settings.IdeaStatusDefault
                };
                ideas.Add(idea);

                return HttpStatusCode.Created;  // TODO: Should return either the generated id or the json body
            };

            Put["/ideas/{id}"] = _ => {
                var model = this.Bind<EditIdeaModel>();
                int id = _.id;
                var idea = ideas.Get(id);
                if (idea == null)
                    return HttpStatusCode.NotFound;
                if (model.title != null)
                    idea.Title = model.title;
                if (model.description != null)
                    idea.Description = model.description;
                ideas.Edit(idea);

                return HttpStatusCode.OK;
            };
        }
Ejemplo n.º 3
0
        public AdminModule(IdeastrikeContext dbContext, Settings settings, IUserRepository users, IIdeaRepository ideas, IActivityRepository activities)
            : base("/admin")
        {
            this.RequiresAuthentication();
            this.RequiresValidatedClaims(c => c.Contains("admin"));

            _settings = settings;
            _users = users;
            _ideas = ideas;
            _activities = activities;

            Get["/"] = _ =>
            {
                var m = Context.Model(string.Format("Admin - {0}", (string)_settings.SiteTitle));
                m.Name = _settings.Name;
                m.WelcomeMessage = _settings.WelcomeMessage;
                m.HomePage = _settings.HomePage;
                m.GAnalyticsKey = _settings.GAnalyticsKey;
                return View["Admin/Index", m];
            };

            Get["/users"] = _ =>
            {
                var m = Context.Model(string.Format("Admin - {0}", (string)_settings.SiteTitle));
                m.Name = _settings.Name;
                m.WelcomeMessage = _settings.WelcomeMessage;
                m.HomePage = _settings.HomePage;
                m.GAnalyticsKey = _settings.GAnalyticsKey;
                m.Users = users.GetAll();
                return View["Admin/Users", m];
            };

            Get["/moderation"] = _ =>
            {
                var m = Context.Model(string.Format("Admin - {0}", (string)_settings.SiteTitle));
                m.Name = _settings.Name;
                m.WelcomeMessage = _settings.WelcomeMessage;
                m.HomePage = _settings.HomePage;
                m.GAnalyticsKey = _settings.GAnalyticsKey;
                return View["Admin/Moderation", m];
            };

            Get["/settings"] = _ =>
            {
                var m = Context.Model(string.Format("Admin - {0}", (string)_settings.SiteTitle));
                m.Name = _settings.Name;
                m.SiteTitle = _settings.SiteTitle;
                m.WelcomeMessage = _settings.WelcomeMessage;
                m.HomePage = _settings.HomePage;
                m.GAnalyticsKey = _settings.GAnalyticsKey;
                m.MaxThumbnailWidth = _settings.MaxThumbnailWidth;

                return View["Admin/Settings", m];
            };

            Post["/settings"] = _ =>
            {
                _settings.WelcomeMessage = Request.Form.welcomemessage;
                _settings.SiteTitle = Request.Form.sitetitle;
                _settings.Name = Request.Form.yourname;
                _settings.HomePage = Request.Form.homepage;
                _settings.GAnalyticsKey = Request.Form.analyticskey;
                _settings.MaxThumbnailWidth = Request.Form.maxthumbnailwidth;

                return Response.AsRedirect("/admin/settings");
            };

            Get["/search"] = _ => "";
            Get["/forums"] = _ => "";
            Get["/forum/{forumId}"] = _ => "";

            Get["/uservoice"] = _ => View["Admin/Uservoice", Context.Model("Admin")];
            Post["/uservoice"] = _ =>
            {
                var client = new WebClient();
                var suggestions = GetSuggestions(client, Request.Form.channel, Request.Form.forumid, Request.Form.apikey, Request.Form.trusted);

                foreach (var s in suggestions)
                {
                    string title = s.title;

                    //If the idea exists, skip it
                    if (ideas.FindBy(i => i.Title == title).Any())
                        continue;

                    string date = s.created_at;
                    var idea = new Idea
                    {
                        Title = title,
                        Description = s.text,
                        Time = DateTime.Parse(date.Substring(0, date.Length - 5)),
                    };

                    string status = string.Empty;
                    switch ((string)s.state)
                    {
                        case "approved":
                            status = "Active";
                            break;
                        case "closed" :
                            if (s.status.key == "completed")
                                status = "Completed";
                            else
                                status = "Declined";
                            break;
                        default:
                            status = "New";
                            break;
                    }
                    idea.Status = status;

                    //Get the author, or create
                    string name = s.creator.name;
                    var existing = users.FindBy(u => u.UserName == name).FirstOrDefault();
                    if (existing != null)
                        idea.Author = existing;
                    else
                    {
                        idea.Author = NewUser(s.creator);
                        users.Add(idea.Author);
                    }

                    ideas.Add(idea);

                    //Process all comments
                    var comments = GetComments(client, (string)s.id, Request.Form.channel, Request.Form.forumid, Request.Form.apikey, Request.Form.trusted);
                    List<Activity> ideaComments = new List<Activity>();
                    foreach (var c in comments)
                    {
                        string commentdate = c.created_at;
                        var comment = new Comment
                        {
                            Time = DateTime.Parse(commentdate),
                            Text = c.text
                        };

                        string commentname = c.creator.name;
                        existing = users.FindBy(u => u.UserName == commentname).FirstOrDefault();
                        if (existing != null)
                            comment.User = existing;
                        else
                        {
                            comment.User = NewUser(c.creator);
                            users.Add(comment.User);
                        }

                        activities.Add(idea.Id, comment);
                    }

                    //Process all votes
                    var votes = GetVotes(client, (string)s.id, Request.Form.channel, Request.Form.forumid, Request.Form.apikey, Request.Form.trusted);
                    foreach (var v in votes)
                    {
                        string votername = v.user.name;
                        string votesfor = v.votes_for;
                        int vote;
                        if (Int32.TryParse(votesfor, out vote))
                        {
                            existing = users.FindBy(u => u.UserName == votername).FirstOrDefault();
                            if (existing != null)
                                ideas.Vote(idea.Id, existing.Id, vote);
                            else
                            {
                                var author = NewUser(v.user);
                                users.Add(author);
                                ideas.Vote(idea.Id, author.Id, vote);
                            }
                        }
                    }
                }

                return Response.AsRedirect("/admin");
            };
        }
Ejemplo n.º 4
0
        public IdeaSecuredModule(IIdeaRepository ideas, IUserRepository users, Settings settings, IImageRepository imageRepository)
            : base("/idea")
        {
            _ideas = ideas;
            _settings = settings;
            _imageRepository = imageRepository;
            _users = users;

            this.RequiresAuthentication();

            Get["/new"] = _ =>
            {
                var m = Context.Model(string.Format("New Idea - {0}", (string)_settings.SiteTitle));
                m.Ideas = _ideas.GetAll();
                m.Errors = false;

                if (Request.Query["validation"] == "failed")
                {
                    m.Errors = true;
                }

                return View["Idea/New", m];
            };

            Get["/{id}/edit"] = parameters =>
            {
                int id = parameters.id;
                var idea = _ideas.Get(id);

                var m = Context.Model(string.Format(Strings.IdeaSecuredModule_EditIdea, idea.Title, (string)_settings.SiteTitle));
                m.PopularIdeas = _ideas.GetAll();
                m.Idea = idea;
                m.StatusChoices = _settings.IdeaStatusChoices.Split(',');
                m.Errors = false;

                if (Request.Query["validation"] == "failed")
                {
                    m.Errors = true;
                }

                return View["Idea/Edit", m];
            };

            // save result of edit to database
            Post["/{id}/edit"] = parameters =>
            {
                int id = parameters.id;

                if (string.IsNullOrEmpty(Request.Form.Title) || string.IsNullOrEmpty(Request.Form.Description))
                {
                    return Response.AsRedirect(string.Format("/idea/{0}/edit?validation=failed", id));
                }

                var idea = _ideas.Get(id);
                if (idea == null)
                    return View["404"];

                idea.Title = Request.Form.Title;
                idea.Description = Request.Form.Description;
                idea.Status = Request.Form.Status;

                //Add any images
                IEnumerable<string> keys = Context.Request.Form;
                var x = keys.Where(c => c.StartsWith("imageId"));
                var ids = x.Select(c => Context.Request.Form[c].ToString()).Cast<string>();
                var images = ids.Select(y => _imageRepository.Get(Convert.ToInt32(y)));
                foreach (var i in images)
                {
                    if (!idea.Images.Contains(i, i))
                    {
                        idea.Images.Add(i);
                    }
                }

                _ideas.Save();

                return Response.AsRedirect(string.Format("/idea/{0}", idea.Id));
            };

            // save result of create to database
            Post["/new"] = _ =>
            {
                if (string.IsNullOrEmpty(Request.Form.Title) || string.IsNullOrEmpty(Request.Form.Description))
                {
                    return Response.AsRedirect("/idea/new?validation=failed");
                }

                var user = _users.FindBy(u => u.UserName == Context.CurrentUser.UserName).FirstOrDefault();

                if (user == null)
                    return Response.AsRedirect("/login");

                var idea = new Idea
                            {
                                Author = user,
                                Time = DateTime.UtcNow,
                                Title = Request.Form.Title,
                                Description = Request.Form.Description,
                                Status = _settings.IdeaStatusDefault
                            };

                IEnumerable<string> keys = Context.Request.Form;

                var parameters = keys.Where(c => c.StartsWith("imageId"));
                var ids = parameters.Select(c => Context.Request.Form[c].ToString()).Cast<string>();
                var images = ids.Select(id => _imageRepository.Get(Convert.ToInt32(id)));
                idea.Images = images.ToList();

                //i.Images = form.Cast<string>()
                //    .Where(k => k.StartsWith("imageId"))
                //    .Select(k => _imageRepository.Get(Convert.ToInt32(form[k])))
                //    .ToList(); //is there a way to do this using Nancy?
                if (idea.Votes.Any(u => u.UserId == user.Id))
                    idea.UserHasVoted = true;

                ideas.Add(idea);

                return Response.AsRedirect("/idea/" + idea.Id);
            };

            // someone else votes for the idea
            Post["/{id}/vote"] = parameters =>
            {
                var user = Context.GetCurrentUser(_users);

                if (user == null)
                    return Response.AsRedirect("/login");

                int ideaId = parameters.id;
                int votes = ideas.Vote(ideaId, user.Id, 1);

                return Response.AsJson(new { Status = "OK", NewVotes = votes });
            };

            // the user decides to repeal his vote
            Post["/{id}/unvote"] = parameters =>
            {
                var user = Context.GetCurrentUser(_users);
                int votes = ideas.Unvote(parameters.id, user.Id);

                return Response.AsJson(new { Status = "OK", NewVotes = votes });
            };

            Post["/{id}/delete"] = parameters =>
            {
                int id = parameters.id;
                ideas.Delete(id);
                ideas.Save();

                // TODO: test
                return Response.AsJson(new { Status = "Error" });
            };

            // TODO: do we want unauthenticated users to be allowed to upload posts?
            Post["/uploadimage"] = parameters =>
            {
                var user = Context.GetCurrentUser(_users);
                if (user == null)
                    return Response.AsJson(new { status = "Error" });

                var imageFile = Request.Files.FirstOrDefault();
                if (imageFile == null)
                {
                    return null; //TODO: handle error case
                }

                var image = new Image { Name = imageFile.Name };
                var bytes = new byte[imageFile.Value.Length];
                imageFile.Value.Read(bytes, 0, bytes.Length);
                image.ImageBits = bytes;
                imageRepository.Add(image);
                var status = new ImageFileStatus(image.Id, bytes.Length, image.Name);
                return Response.AsJson(new[] { status }).WithHeader("Vary", "Accept");
            };

            Delete["/deleteimage/{id}"] = parameters =>
            {
                var user = Context.GetCurrentUser(_users);
                if (user == null)
                    return Response.AsJson(new { status = "Error" });

                imageRepository.Delete(parameters.id);
                return null;
            };
        }
Ejemplo n.º 5
0
        public IdeaModule(IIdeaRepository ideas, IUserRepository users, Settings settings, IImageRepository imageRepository)
            : base("/idea")
        {
            _ideas = ideas;
            _settings = settings;
            _users = users;

            Get["/{id}"] = parameters =>
            {
                int id = parameters.id;
                var idea = _ideas.Get(id);
                if (idea == null)
                    return View["404"];

                User user = Context.GetCurrentUser(_users);
                if (user != null)
                {
                    if (idea.Votes.Any(u => u.UserId == user.Id))
                        idea.UserHasVoted = true;

                }

                var viewModel = new IdeaViewModel(idea);
                var model = Context.Model(string.Format("{0} - {1}", idea.Title, (string)_settings.SiteTitle));
                model.Idea = viewModel;
                return View["Idea/Index", model];
            };

            Get["/{id}/activity"] = parameters =>
            {
                int id = parameters.id;
                var idea = _ideas.Get(id);
                if (idea == null)
                    return Response.AsJson(new { Status = "error" });

                var results = idea.Activities.OrderBy(a => a.Time).Select(MapToViewModel);

                return Response.AsJson(new
                {
                    Status = "success",
                    Items = results
                });
            };

            Get["/image/{id}"] = parameters =>
            {
                var id = (string)parameters.id;
                if (id.Contains("."))
                {
                    id = id.Substring(0, id.IndexOf(".")); //string .jpg in case it was send in
                }
                var image = imageRepository.Get(int.Parse(id));
                // TODO: format should be adaptive based on backing source?
                return Response.FromStream(new MemoryStream(image.ImageBits), "image/jpeg");
            };

            Get[@"/imagethumb/{id}/{width}"] = parameters =>
            {
                var image = (Image)imageRepository.Get(parameters.id);
                using (var memoryStream = new MemoryStream(image.ImageBits))
                {
                    var drawingImage = System.Drawing.Image.FromStream(memoryStream);
                    int thumbWidth = (int)parameters.width;
                    if (thumbWidth > int.Parse(_settings.MaxThumbnailWidth))
                    {
                        thumbWidth = int.Parse(_settings.MaxThumbnailWidth);
                    }
                    var thumb = drawingImage.ToThumbnail(thumbWidth);
                    using (var thumbnailStream = new MemoryStream())
                    {
                        // TODO: format should be adaptive based on backing source?
                        thumb.Save(thumbnailStream, ImageFormat.Jpeg);
                        return Response.FromStream(new MemoryStream(thumbnailStream.GetBuffer()), "image/jpeg"); //massive WTF? If I just use thumnailStream, it doesn't work...
                    }
                }
            };

            Get["/imagesforidea/{id}"] = parameters =>
                                             {
                                                 Idea idea = _ideas.Get((int)parameters.id);
                                                 return Response.AsJson(idea.Images.Select(i => new ImageFileStatus(i.Id, i.ImageBits.Length, i.Name))
                                                                                            .ToArray()
                                                                            ).WithHeader("Vary", "Accept");
                                             };
        }
Ejemplo n.º 6
0
        public ApiModule(IdeastrikeContext db, IIdeaRepository ideas, IUserRepository users, Settings settings)
            : base("/api")
        {
            _settings = settings;

            Get["/ideas"] = _ => {
                return Response.AsJson(db.Ideas.Select(idea =>
                    new {
                        id = idea.Id,
                        title = idea.Title,
                        description = idea.Description,
                        time = SqlFunctions.DateDiff("s", new DateTime(1970, 1, 1), idea.Time),
                        author = new { id = idea.Author.Id, username = idea.Author.UserName },
                        vote_count = idea.Votes.Sum(vote => (int?)vote.Value) ?? 0,
                        status = idea.Status
                    }));
            };

            Get["/ideas/{id}"] = _ => {
                int id = _.id;
                var o = db.Ideas.Where(idea => idea.Id == id).Select(idea =>
                    new {
                        id = idea.Id,
                        title = idea.Title,
                        description = idea.Description,
                        time = SqlFunctions.DateDiff("s", new DateTime(1970, 1, 1), idea.Time),
                        author = new { id = idea.Author.Id, username = idea.Author.UserName },
                        vote_count = idea.Votes.Sum(vote => (int?)vote.Value) ?? 0,
                        features = idea.Features.Select(feature => new { id = feature.Id, text = feature.Text, time = SqlFunctions.DateDiff("s", new DateTime(1970, 1, 1), feature.Time) }),
                        votes = idea.Votes.Select(vote => new { user = new { id = vote.UserId, username = vote.User.UserName }, value = vote.Value })
                    }).FirstOrDefault();
                if (o == null)
                    return HttpStatusCode.NotFound;
                return Response.AsJson(o);
            };

            Get["/ideas/{id}/features"] = _ => {
                int id = _.id;
                if (!db.Ideas.Any(idea => idea.Id == id))
                    return HttpStatusCode.NotFound;
                return Response.AsJson(db.Features.Where(d => d.Idea.Id == id).Select(feature =>
                    new {
                        id = feature.Id,
                        text = feature.Text,
                        time = SqlFunctions.DateDiff("s", new DateTime(1970, 1, 1), feature.Time),
                    }));
            };

            Get["/ideas/{id}/votes"] = _ => {
                int id = _.id;
                if (!db.Ideas.Any(idea => idea.Id == id))
                    return HttpStatusCode.NotFound;
                return Response.AsJson(db.Votes.Where(d => d.IdeaId == id).Select(vote =>
                    new {
                        value = vote.Value,
                        user = new { id = vote.UserId, username = vote.User.UserName }
                    }));
            };

            Post["/activity"] = _ =>
            {
                string content;
                using (var reader = new StreamReader(Context.Request.Body))
                {
                    content = reader.ReadToEnd();
                }
                var j = JsonConvert.DeserializeObject<dynamic>(content);
                string repourl = j.repository.url.ToString();
                string reponame = j.repository.name.ToString();
                var idea = ideas
                            .Include("Activities")
                            .Where(i => i.GithubUrl == repourl || i.GithubName == reponame)
                            .FirstOrDefault();

                if (idea == null)
                    return HttpStatusCode.NotFound;

                foreach (var c in j.commits)
                {
                    string date = c.timestamp;
                    var activity = new GitHubActivity
                    {
                        Time = DateTime.Parse(date),
                        Message = c.message,
                        CommitUrl = c.url,
                        AuthorName = c.author.name,
                        GravatarUrl = GravatarExtensions.ToGravatarUrl(c.author.email.ToString(), 40),
                        Sha = c.id
                    };

                    if (!idea.Activities.OfType<GitHubActivity>().Any(a => a.Sha == activity.Sha))
                        idea.Activities.Add(activity);
                }

                ideas.Save();
                return HttpStatusCode.Accepted;
            };
        }