Ejemplo n.º 1
0
        public void SetMark(EditathonCode code, [FromBody] MarkPostData body)
        {
            var e    = code.Get(q => q.FetchMany(_ => _.Articles).ThenFetch(a => a.Marks));
            var user = _identity.GetUserInfo();

            var article = e.Articles.SingleOrDefault(a => a.Name == body.Title);

            if (article == null)
            {
                throw NotFound();
            }

            var mark = article.Marks.SingleOrDefault(m => m.User == user.Username);

            if (mark == null)
            {
                mark = new Mark
                {
                    Article = article,
                    User    = user.Username,
                };
                article.Marks.Add(mark);
            }

            mark.Marks   = JObject.Parse(body.Marks);
            mark.Comment = body.Comment;
        }
Ejemplo n.º 2
0
 public IEnumerable <Editathon.ResultRow> Results(EditathonCode code, int limit)
 {
     return(code
            .Get(q => q.FetchMany(_ => _.Articles).ThenFetch(_ => _.Marks))
            .GetResults()
            .Where(r => r.Rank <= limit));
 }
Ejemplo n.º 3
0
        public async Task SetConfig(EditathonCode code, [FromBody] EditathonConfig cfg)
        {
            var e = code.Get(q => q
                             .Fetch(_ => _.Jury)
                             .Fetch(_ => _.Rules), false);

            if (_session.Query <Editathon>().Any(_ => (_.Code == cfg.Code || _.Name == cfg.Name) && _ != e))
            {
                throw Forbidden();
            }

            var user   = _identity.GetUserInfo();
            var rights = _identity.GetUserRights();
            await rights.Actualize();

            if (!e.IsPublished)
            {
                // before publishing only creator and admins of the source wiki can edit
                if (e.Creator != user.Username && !rights.IsAdminIn(e.Wiki))
                {
                    throw Forbidden();
                }
            }
            else
            {
                if (e.Code != cfg.Code)
                {
                    throw Forbidden();
                }

                // after publishing only admins of the source wiki can edit
                if (!rights.IsAdminIn(e.Wiki))
                {
                    throw Forbidden();
                }

                if (e.Wiki != cfg.Wiki)
                {
                    // changing wikis is prohibited if editathon has any articles
                    if (_session.Query <Article>().Any(a => a.Editathon == e))
                    {
                        throw Forbidden();
                    }

                    // don't publish to target wiki if user is not admin there
                    if (!rights.IsAdminIn(cfg.Wiki))
                    {
                        e.IsPublished = false;
                    }
                }
            }

            ApplyConfig(e, cfg);
        }
Ejemplo n.º 4
0
        public async Task Publish(EditathonCode code)
        {
            var e = code.Get(false);

            var rights = _identity.GetUserRights();
            await rights.Actualize();

            if (!rights.IsAdminIn(e.Wiki))
            {
                throw Forbidden();
            }

            e.IsPublished = true;
        }
Ejemplo n.º 5
0
        public async Task Award(EditathonCode code, [FromBody] IDictionary <string, string> awards)
        {
            if (awards == null || awards.Count == 0)
            {
                throw BadRequest();
            }

            var e    = code.Get();
            var wiki = MediaWikis.Create(e.Wiki, _identity);

            foreach (var userTalk in await UserTalk.GetAsync(wiki, awards.Select(r => r.Key)))
            {
                await userTalk.WriteAsync(awards[userTalk.UserName], "Automated awarding");
            }
        }
Ejemplo n.º 6
0
        public void RemoveArticles(EditathonCode code, [FromBody] long[] ids)
        {
            var e = code.Get(q => q.Fetch(_ => _.Articles));

            foreach (var id in ids)
            {
                var article = e.Articles.SingleOrDefault(a => a.Id == id);
                if (article == null)
                {
                    throw NotFound();
                }

                e.Articles.Remove(article);
            }
        }
Ejemplo n.º 7
0
        public object Get(EditathonCode code)
        {
            var e = code.Get(q => q
                             .FetchMany(_ => _.Articles).ThenFetch(a => a.Marks)
                             .Fetch(_ => _.Jury)
                             .Fetch(_ => _.Rules));

            var user = _identity.GetUserInfo();

            return(new
            {
                e.Code,
                e.Name,
                e.Description,
                e.Start,
                e.Finish,
                e.Wiki,
                e.Flags,
                e.Jury,
                e.Marks,
                e.MinMarks,
                Rules = e.Rules.Select(r => new
                {
                    r.Type,
                    r.Flags,
                    r.Params,
                }),
                Articles = e.Articles.OrderByDescending(a => a.DateAdded).Select(a => new
                {
                    a.Id,
                    a.DateAdded,
                    a.Name,
                    a.User,
                    Marks = e.GetArticleMarks(a, user).Select(m => new
                    {
                        m.User,
                        m.Marks,
                        m.Comment,
                    }),
                }),
            });
        }
Ejemplo n.º 8
0
        public async Task Delete(EditathonCode code)
        {
            var user = _identity.GetUserInfo();
            var e    = code.Get(false);

            if (e.IsPublished)
            {
                throw Forbidden();
            }

            if (e.Creator != user.Username)
            {
                var rights = _identity.GetUserRights();
                await rights.Actualize();

                if (!rights.IsAdminIn(e.Wiki))
                {
                    throw Forbidden();
                }
            }

            _session.Delete(e);
        }
Ejemplo n.º 9
0
        public EditathonConfig GetConfig(EditathonCode code)
        {
            var e = code.Get(q => q
                             .Fetch(_ => _.Jury)
                             .Fetch(_ => _.Rules), false);
            var user = _identity.GetUserInfo();

            return(new EditathonConfig
            {
                Code = e.Code,
                Name = e.Name,
                Description = e.Description,
                Start = e.Start,
                Finish = e.Finish,
                Wiki = e.Wiki,
                Flags = e.Flags,

                Rules = e.Rules.ToArray(),
                Marks = e.Marks,
                Template = e.Template,
                Jury = e.Jury.ToArray(),
            });
        }
Ejemplo n.º 10
0
        public async Task AddArticle(EditathonCode code, [FromBody] ArticlePostData body)
        {
            if (body == null || string.IsNullOrWhiteSpace(body.Title))
            {
                throw BadRequest();
            }

            var user = _identity.GetUserInfo();

            var e = code.Get(q => q
                             .Fetch(_ => _.Jury)
                             .Fetch(_ => _.Rules)
                             .Fetch(_ => _.Articles));

            var now = DateTime.UtcNow;

            if (now < e.Start || now.Date > e.Finish)
            {
                throw Forbidden();
            }

            if (e.Articles.Any(a => a.Name == body.Title))
            {
                throw Forbidden();
            }

            var wiki = MediaWikis.Create(e.Wiki, _identity);

            if (user.Username != body.User)
            {
                if (!e.Jury.Contains(user.Username))
                {
                    throw Forbidden();
                }
                else
                {
                    user = await wiki.GetUser(body.User);

                    if (user == null)
                    {
                        throw Forbidden();
                    }
                }
            }

            var page = await wiki.GetPage(body.Title);

            if (page == null)
            {
                throw Forbidden();
            }

            var rules = e.Rules
                        .Where(r => !r.Flags.HasFlag(RuleFlags.Optional))
                        .Select(r => r.Get())
                        .ToArray();

            if (rules.Any())
            {
                var loader = new ArticleDataLoader(rules.SelectMany(r => r.GetReqs()));
                var data   = await loader.LoadAsync(wiki, body.Title);

                var ctx = new RuleContext {
                    User = user
                };
                foreach (var rule in rules)
                {
                    if (!rule.Check(data, ctx))
                    {
                        throw Forbidden();
                    }
                }
            }

            if (e.Template != null)
            {
                await UpdateTemplate(wiki, user, body.Title, page, e.Template, ControllerContext.ResolveUrl($"~/editathons/{e.Code}"));
            }

            e.Articles.Add(new Article
            {
                Name      = body.Title,
                User      = user.Username,
                DateAdded = now,
            });
        }