private static TrackFollowTableEntity TrackFollowToTableEntity(TrackFollow trackFollow) { return(new TrackFollowTableEntity() { PartitionKey = trackFollow.track_id, RowKey = trackFollow.user_id, feed_follow_type = trackFollow.feed_follow_type.ToString(), notifications_follow_type = trackFollow.notifications_follow_type.ToString(), criteria = JsonConvert.SerializeObject(trackFollow.criteria) }); }
public static async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "track/{trackId}/follow")] HttpRequest req, string trackId, TraceWriter log) { try { // check postId and trackId provided if (!Tools.IsValidGuid(trackId)) { return(new UnauthorizedResult()); } string authToken = req.Headers["Authorization"]; if (authToken == null) { return(new UnauthorizedResult()); } // validate authKey AuthClaim authClaim = AuthRepository.ValidateAuthClaim(authToken); if (authClaim == null) { return(new UnauthorizedResult()); } // get the track TrackAuth track = await TrackRepository.GetTrack(trackId); if (track == null || (track.is_private && track.PartitionKey != authClaim.user_id)) { return(new UnauthorizedResult()); } // trackFollow body string requestBody = new StreamReader(req.Body).ReadToEnd(); TrackFollowDTO dto = JsonConvert.DeserializeObject <TrackFollowDTO>(requestBody); // insert or update the follow TrackFollow trackFollow = new TrackFollow(); trackFollow.feed_follow_type = dto.feed?.ToLower() == "all" || dto.feed?.ToLower() == "none" ? dto.feed.ToLower() : null; trackFollow.notifications_follow_type = dto.notifications?.ToLower() == "all" || dto.notifications?.ToLower() == "none" ? dto.notifications.ToLower() : null; trackFollow.criteria = dto.criteria; trackFollow.user_id = authClaim.user_id; trackFollow.track_id = track.RowKey; FollowRepository.InsertOrReplaceTrackFollow(trackFollow); return(new OkResult()); } catch (Exception e) { log.Error(e.Message); return(new UnauthorizedResult()); } }
public static void InsertOrReplaceTrackFollow(TrackFollow trackFollow) { // check user and track id if (trackFollow?.user_id == null || trackFollow?.track_id == null) { return; } // validate criteria List <TagCriteria> validatedTagCriteria = new List <TagCriteria>(); // ensure the follow is in the user follow table toowoo woo InsertOrReplaceUserFollow(trackFollow.user_id, trackFollow.track_id); if (trackFollow.criteria != null && trackFollow.criteria.Count > 0) { foreach (var criterion in trackFollow.criteria.Take(12)) { criterion.feed = trackFollow.feed_follow_type != null ? false : criterion.feed; criterion.notifications = trackFollow.notifications_follow_type != null ? false : criterion.notifications; TagCriteria validatedCriterion = ValidateTagCriteria(criterion); if (validatedCriterion != null) { validatedTagCriteria.Add(validatedCriterion); } } } trackFollow.criteria = validatedTagCriteria; // get basic follow modes trackFollow.feed_follow_type = trackFollow.feed_follow_type == "all" || trackFollow.feed_follow_type == "none" ? trackFollow.feed_follow_type : GetFollowType(trackFollow.criteria, Enums.FollowMode.Feed); trackFollow.notifications_follow_type = trackFollow.notifications_follow_type == "all" || trackFollow.notifications_follow_type == "none" ? trackFollow.notifications_follow_type : GetFollowType(trackFollow.criteria, Enums.FollowMode.Notification); TableStorageRepository.InsertOrReplaceTrackFollow(TrackFollowToTableEntity(trackFollow)); }