// GET api/Category public async Task<HttpResponseMessage> Get() { using (CrowdContext db = new CrowdContext()) { User user = await AuthenticateUser(GetAuthentication(), db); if (user == null) { return new HttpResponseMessage(HttpStatusCode.Unauthorized); } var defaultCats = await (from category in db.ParticipantActivityCategories where category.DefaultSubscription && (category.App == Crowd.Model.Data.User.AppType.None || category.App == user.App) select category).ToArrayAsync(); bool changed = false; foreach (ParticipantActivityCategory category in defaultCats) { if (user.SubscribedCategories.Contains(category)) continue; user.SubscribedCategories.Add(category); changed = true; } if (changed) { await db.SaveChangesAsync(); } for (int i = 0; i < user.SubscribedCategories.Count; i++) { user.SubscribedCategories.ElementAt(i).Activities = (from act in user.SubscribedCategories.ElementAt(i).Activities where act.AppType == user.App || act.AppType == Crowd.Model.Data.User.AppType.None select act).ToList(); } return new HttpResponseMessage() { Content = new JsonContent(user.SubscribedCategories) }; } }
/// <summary> /// Checks if the given details validate /// </summary> /// <param name="auth"></param> /// <param name="db"></param> /// <returns>User obj if allowed, otherwise null</returns> protected async Task<User> AuthenticateUser(AuthenticationModel auth, CrowdContext db) { if (auth == null) return null; User found = await db.Users.FindAsync(auth.Email); if (found != null && found.Key == auth.Key) { User.AppType app; if (Enum.TryParse(auth.App, true, out app)) { if (app != found.App) { found.App = app; await db.SaveChangesAsync(); } } return found; } return null; }
public async Task<HttpResponseMessage> Post(ParticipantResult result) { using (CrowdContext db = new CrowdContext()) { User user = await AuthenticateUser(GetAuthentication(), db); if (user == null) { return new HttpResponseMessage(HttpStatusCode.Unauthorized); } if (!ModelState.IsValid) return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); if (result == null) return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Activity result cannot be null"); result.User = user; result.UploadedAt = DateTime.Now; result = db.ParticipantResults.Add(result); user.Submissions.Add(result); if (result.IsAssessment) { user.LastAssessment = result; } try { db.SaveChanges(); } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.ExpectationFailed, ex.Message); } SvcStatus status = await CrowdFlowerApi.CreateJob(result, await db.ParticipantActivities.FindAsync(result.ParticipantActivityId)); if (status.Level == 0) { string json = await status.Response.Content.ReadAsStringAsync(); CFJobResponse jobRes = JsonConvert.DeserializeObject<CFJobResponse>(json); result.CrowdJobId = jobRes.id; if (status.CreatedRows != null) { foreach (CrowdRowResponse row in status.CreatedRows) { db.CrowdRowResponses.Add(row); } } try { db.SaveChanges(); } catch (Exception e) { throw new HttpResponseException(Request.CreateResponse( HttpStatusCode.ExpectationFailed, e.Message)); } status = CrowdFlowerApi.LaunchJob(jobRes.id, status.Count); return status.Response; } db.DebugMessages.Add(new DebugMessage { Message = status.Description, Filename = "ActivityResultController", FunctionName = "Post" }); await db.SaveChangesAsync(); return Request.CreateErrorResponse(HttpStatusCode.BadRequest, status.Description + " \n\n " + status.Response.ToString()); } }
public async Task<HttpResponseMessage> Post() { using (CrowdContext db = new CrowdContext()) { string jsonData = HttpUtility.UrlDecode(await Request.Content.ReadAsStringAsync()); bool isDebug; try { isDebug = Request.Headers.GetValues("DebugSecret").First() == ConfidentialData.DebugSecret; } catch (Exception) { // Debug header not found isDebug = false; } User thisUser = JsonConvert.DeserializeObject<User>(jsonData); string encodedToken = thisUser.IdToken; if (string.IsNullOrEmpty(encodedToken) || string.IsNullOrWhiteSpace(thisUser.Email)) { return new HttpResponseMessage(HttpStatusCode.Unauthorized); } try { // bypass validation if debug bool validated = (isDebug) || ValidateToken(encodedToken, thisUser.Email, thisUser.App); if (!validated) return new HttpResponseMessage(HttpStatusCode.Unauthorized); } catch (Exception ex) { return new HttpResponseMessage(HttpStatusCode.InternalServerError); } User existingUser = await db.Users.FindAsync(thisUser.Email); if (existingUser == null) { // User not found - add new thisUser.IsAdmin = false; thisUser.Key = RandomString(32); db.Users.Add(thisUser); existingUser = thisUser; } else { // Update the found user's details if they've been given existingUser.Name = thisUser.Name ?? existingUser.Email; existingUser.Nickname = thisUser.Nickname ?? existingUser.Nickname; existingUser.Avatar = thisUser.Avatar ?? existingUser.Avatar; existingUser.App = thisUser.App; } try { await db.SaveChangesAsync(); } catch (Exception) { return new HttpResponseMessage(HttpStatusCode.InternalServerError); } return new HttpResponseMessage { Content = new JsonContent(existingUser), StatusCode = HttpStatusCode.OK }; } }
public async Task<HttpResponseMessage> Post() { using (CrowdContext db = new CrowdContext()) { string jsonData = HttpUtility.UrlDecode(await Request.Content.ReadAsStringAsync()); ParticipantFeedItem newItem = JsonConvert.DeserializeObject<ParticipantFeedItem>(jsonData); if (string.IsNullOrEmpty(newItem.Title) || string.IsNullOrEmpty(newItem.Description)) { return new HttpResponseMessage(HttpStatusCode.ExpectationFailed); } newItem.Date = DateTime.Now; newItem.Global = true; db.ParticipantFeedItems.Add(newItem); try { await db.SaveChangesAsync(); } catch (Exception) { return new HttpResponseMessage(HttpStatusCode.InternalServerError); } return new HttpResponseMessage(HttpStatusCode.Accepted); } }
public async Task<HttpResponseMessage> Dismiss(int id) { using (CrowdContext db = new CrowdContext()) { User user = await AuthenticateUser(GetAuthentication(), db); if (user == null) { return new HttpResponseMessage(HttpStatusCode.Unauthorized); } ParticipantFeedItem feedItem = await db.ParticipantFeedItems.FindAsync(id); if (!user.DismissedPublicFeedItems.Contains(feedItem)) { user.DismissedPublicFeedItems.Add(feedItem); } if (user.FeedItems.Contains(feedItem)) { user.FeedItems.Remove(feedItem); } try { await db.SaveChangesAsync(); } catch (Exception) { return new HttpResponseMessage(HttpStatusCode.InternalServerError); } return new HttpResponseMessage(HttpStatusCode.OK); } }