protected async Task<List<Place>> ProcessPlacesInNewContent(ICollection<Place> places, ApplicationUser currentUser, List<Place> finalPlaces = null) { // Go through the creation's Places, adding them to the database if necessary if (finalPlaces == null) { finalPlaces = new List<Place>(); } else { // avoiding creating new, which seems to cause conflicts finalPlaces.Clear(); } if (places == null) return finalPlaces; for (int i = 0; i < places.Count; i++) { Place thisPlace = places.ElementAt(i); Place existing = await db.Places.Where(p => p.GooglePlaceId == thisPlace.GooglePlaceId).FirstOrDefaultAsync(); if (existing != null) { finalPlaces.Add(existing); } else { Common.Models.GMapsResult result; using (HttpClient client = new HttpClient()) { string reqUrl = string.Format("https://maps.googleapis.com/maps/api/place/details/json?placeid={0}&key={1}", thisPlace.GooglePlaceId, Common.ConfidentialData.mapsk); var response = await client.GetStringAsync(reqUrl); result = JsonConvert.DeserializeObject<Common.Models.GMapsResult>(response); } if (result.status == "OK") { double lat = result.result.geometry.location.lat; double lon = result.result.geometry.location.lng; Place finalPlace = new Place { GooglePlaceId = result.result.place_id, Latitude = new decimal(lat), Longitude = new decimal(lon), Location = ServerUtils.CreatePoint(lat, lon), Name = result.result.name, CreatedAt = DateTime.UtcNow, AddedBy = currentUser }; // Check for parent locality PlaceLocality locality = await LocationLogic.GetLocality(lat, lon); if (locality != null) { PlaceLocality existingLocality = await db.PlaceLocalities .Where(p => p.GooglePlaceId == locality.GooglePlaceId).FirstOrDefaultAsync(); if (existingLocality == null) { finalPlace.Locality = db.PlaceLocalities.Add(locality); await db.SaveChangesAsync(); } else { finalPlace.Locality = existingLocality; } } finalPlaces.Add(db.Places.Add(finalPlace)); } } } return finalPlaces; }
// GET: api/LearningActivities public async Task <HttpResponseMessage> GetFeed(double lat = 0, double lon = 0) { ApplicationUser thisUser = await GetUser(); if (thisUser == null) { return(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Please log in")); } // Researcher accounts privvy to all uploaded activities bool isResearcher = Common.ConfidentialData.TestEmails.Contains(thisUser.Email); List <Common.Models.FeedSection> feed = new List <Common.Models.FeedSection>(); if (!lat.AlmostEquals(0, 0.0001) || !lon.AlmostEquals(0, 0.0001)) { DbGeography thisLoc = ServerUtils.CreatePoint(lat, lon); IEnumerable <Place> places = db.Places.Where(pl => pl.Location.Distance(thisLoc) <= 2500).OrderBy(pl => pl.Location.Distance(thisLoc)); foreach (Place pl in places) { // Get collections near the user's position, which have been approved and still // contain at least one activity List <Common.Models.ActivityCollection> collectionsHere = GetAllCollectionsWhere(c => !c.SoftDeleted && c.Places.Any(l => l.Id == pl.Id) && (c.Approved || thisUser.Trusted) && c.Activities.Any(a => a.SoftDeleted == false) && (isResearcher || c.IsPublic)).ToList(); // Get activities which are near the user's position, public and approved List <Common.Models.LearningActivity> actsHere = GetAllActivitiesWhere(a => !a.SoftDeleted && a.Places.Any(l => l.Id == pl.Id) && (a.Approved || thisUser.Trusted) && (isResearcher || a.IsPublic)).ToList(); if (actsHere.Count > 0 || collectionsHere.Count > 0) { feed.Add(new Common.Models.FeedSection { Title = $"Creations at {pl.Name}", Description = $"Here are some things to do near {pl.Name}", Activities = actsHere, Collections = collectionsHere }); } } } feed.Add(new Common.Models.FeedSection { Title = "Recently Uploaded", Description = "Here are some of the latest creations that have been uploaded", // Get the most recent activities which are public and approved, or made by the current user Activities = GetAllActivitiesWhere(a => !a.SoftDeleted && (((isResearcher || a.IsPublic) && (a.Approved || thisUser.Trusted)) || a.Author.Id == thisUser.Id)) .OrderByDescending(a => a.CreatedAt).Take(16).ToList(), Collections = GetAllCollectionsWhere(a => !a.SoftDeleted && (((isResearcher || a.IsPublic) && (a.Approved || thisUser.Trusted)) || a.Author.Id == thisUser.Id)) .OrderByDescending(a => a.CreatedAt).Take(16).ToList() }); var resp = Request.CreateResponse(HttpStatusCode.OK); resp.Content = new StringContent( JsonConvert.SerializeObject( feed, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Serialize, MaxDepth = 12 }), Encoding.UTF8, "application/json"); await MakeLog(new Dictionary <string, string>(){ { "lat", lat.ToString() }, { "lon", lon.ToString() } }); return(resp); }