public async Task <HttpResponseMessage> Parse([FromBody] ParseData data) { var user = _identity.GetUserInfo(); if (user == null) { return(Unauthorized()); } return(Ok(await MediaWikis.Create(data.Wiki, _identity).Parse(data.Text, data.Title))); }
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"); } }
public async Task <HttpResponseMessage> Award(string code, [FromBody] IDictionary <string, string> awards) { if (awards == null || awards.Count == 0) { return(Request.CreateResponse(System.Net.HttpStatusCode.BadRequest)); } var user = _identity.GetUserInfo(); if (user == null) { return(Unauthorized()); } var e = Session.Query <Editathon>() .Fetch(_ => _.Jury) .SingleOrDefault(i => i.Code == code); if (e == null) { return(NotFound()); } if (!e.Jury.Contains(user.Username)) { return(Forbidden()); } 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"); } return(Ok()); }
public async Task <HttpResponseMessage> AddArticle(string code, [FromBody] ArticlePostData body) { _auditContext.Operation = OperationType.AddArticle; if (body == null || string.IsNullOrWhiteSpace(body.Title)) { return(Request.CreateResponse(System.Net.HttpStatusCode.BadRequest)); } var user = _identity.GetUserInfo(); if (user == null) { return(Unauthorized()); } var e = Session.Query <Editathon>() .Fetch(_ => _.Rules) .Fetch(_ => _.Articles) .SingleOrDefault(i => i.Code == code); if (e == null) { return(NotFound()); } var now = DateTime.UtcNow; if (now < e.Start || now.Date > e.Finish) { return(Forbidden()); } if (e.Articles.Any(a => a.Name == body.Title)) { return(Forbidden()); } var wiki = MediaWikis.Create(e.Wiki, _identity); if (user.Username != body.User) { if (!e.Jury.Contains(user.Username)) { return(Forbidden()); } else { user = await wiki.GetUser(body.User); if (user == null) { return(Forbidden()); } } } var page = await wiki.GetPage(body.Title); if (page == null) { return(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)) { return(Forbidden()); } } } if (e.Template != null) { await UpdateTemplate(wiki, user, body.Title, page, (JObject)e.Template.DeepClone()); } e.Articles.Add(new Article { Name = body.Title, User = user.Username, DateAdded = now, }); return(Ok()); }
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, }); }
public Task <string> Parse([FromBody] ParseData data) { return(MediaWikis.Create(data.Wiki, _identity).Parse(data.Text, data.Title)); }