public FavouriteStatus Set(int id) { FavouriteStatus status = new FavouriteStatus { Status = false }; if (User.Identity.IsAuthenticated) { string uid = User.Identity.GetUserId(); using (PixurfDBContext db = new PixurfDBContext()) { Favourite favourite = db.Favourites.FirstOrDefault(f => f.User_ID == uid && f.Content_ID == id); Content content = db.Contents.Find(id); if (content != null) { UserRelationship userRelationship = new UserRelationship(); if ((content.User_ID == uid) || (userRelationship.Following(content.User_ID, uid) && content.Access != "Private") || (!userRelationship.Blocked(content.User_ID, uid) && content.Access == "Public")) { if (favourite != null) { db.Favourites.Remove(favourite); try { db.SaveChanges(); status.Status = false; } catch (Exception e) { Console.WriteLine(e); status.Status = true; } } else { db.Favourites.Add(new Favourite { Content = content, User_ID = uid, Creation_Date = DateTime.Now }); try { db.SaveChanges(); status.Status = true; } catch (Exception e) { Console.WriteLine(e); } } } } } } return(status); }
private List <Content> GetViewableContents(string ownerId) { List <Content> contents = new List <Content>(); string viewerId = ""; if (User.Identity.IsAuthenticated) { viewerId = User.Identity.GetUserId(); } using (PixurfDBContext db = new PixurfDBContext()) { if (viewerId.IsNullOrWhiteSpace()) { //Only the public contents contents = db.Contents.Where(a => a.User_ID == ownerId && a.Access == "Public" && a.Status == 1).Take(4).ToList(); } else { User viewer = db.Users.Find(viewerId); if (viewer != null) { if (viewer.Admin) { contents = db.Contents.Where(a => a.User_ID == ownerId).Take(4).ToList(); } else if (viewer.User_ID == ownerId) { contents = db.Contents.Where(a => a.User_ID == ownerId && a.Status == 1).Take(4).ToList(); } else { //Handle followers UserRelationship relationship = new UserRelationship(); if (relationship.Following(ownerId, viewerId)) { contents = db.Contents .Where(a => a.User_ID == ownerId && a.Status == 1 && (a.Access == "Public" || a.Access == "Follower")).Take(4).ToList(); } else { contents = db.Contents .Where(a => a.User_ID == ownerId && a.Status == 1 && a.Access == "Public").Take(4).ToList(); } } } } } return(contents); }
//[Route("api/{controller}/follow/{action}/{id}")] public RelationStatus Get(string id) { RelationStatus status = new RelationStatus { Type = RelationStatus.Follow }; if (User.Identity.IsAuthenticated && !id.IsNullOrWhiteSpace()) { string uid = User.Identity.GetUserId(); UserRelationship userRelationship = new UserRelationship(); status.Status = userRelationship.Following(id, uid); } return(status); }
public ActionResult View(int id) { List <StatusReport> reports = new List <StatusReport>(); using (PixurfDBContext db = new PixurfDBContext()) { Album album = db.Albums.Find(id); if (album == null) { StatusReport report = new StatusReport { Title = "Error", Description = "Album not found", Status = StatusReport.Info }; reports.Add(report); Session["Reports"] = reports; return(RedirectToAction("Index", "Home")); } ViewAlbumModel albumModel = new ViewAlbumModel { Album_ID = album.Album_ID, Name = album.Name, Description = album.Description, User = db.Users.Find(album.User_ID), Access = album.Access, Status = album.Status, Creation_Date = album.Creation_Date, UserAuthenticated = false }; if (album.Status == 1) { if (album.User_ID == User.Identity.GetUserId()) { if (reports.Count > 0) { Session["Reports"] = reports; } albumModel.Contents = album.Contents.ToList(); albumModel.UserAuthenticated = true; return(View(albumModel)); } UserRelationship relationship = new UserRelationship(); //Private if (album.Access == "Private") { StatusReport report = new StatusReport { Title = "Access Denied", Description = "This Album is not accessible", Status = StatusReport.Warning }; reports.Add(report); Session["Reports"] = reports; return(RedirectToAction("Index", "Home")); } //Follower else if (album.Access == "Follower") { if (relationship.Following(album.User_ID, User.Identity.GetUserId())) { if (reports.Count > 0) { Session["Reports"] = reports; } var temp = album.Contents.Where(c => c.Access != "Private").ToList(); albumModel.Contents = temp; return(View(albumModel)); } else { StatusReport report = new StatusReport { Title = "Access Denied", Description = "Only Followers can view this Album", Status = StatusReport.Warning }; reports.Add(report); Session["Reports"] = reports; return(RedirectToAction("Index", "Home")); } } //Not Blocked else { if (relationship.Following(album.User_ID, User.Identity.GetUserId())) { if (reports.Count > 0) { Session["Reports"] = reports; } var temp = album.Contents.Where(c => c.Access != "Private").ToList(); albumModel.Contents = temp; return(View(albumModel)); } else if (!relationship.Blocked(album.User_ID, User.Identity.GetUserId())) { if (reports.Count > 0) { Session["Reports"] = reports; } var temp = album.Contents.Where(c => c.Access == "Public").ToList(); albumModel.Contents = temp; return(View(albumModel)); } StatusReport report = new StatusReport { Title = "Error", Description = "Album not found", Status = StatusReport.Warning }; reports.Add(report); Session["Reports"] = reports; return(RedirectToAction("Index", "Home")); } } else { User user = db.Users.Find(User.Identity.GetUserId()); if (user != null && user.Admin) { if (reports.Count > 0) { Session["Reports"] = reports; } albumModel.UserAuthenticated = true; return(View(albumModel)); } else { StatusReport report = new StatusReport { Title = "Error", Description = "Album not found", Status = StatusReport.Warning }; reports.Add(report); Session["Reports"] = reports; return(RedirectToAction("Index", "Home")); } } } }
public RelationStatus Set(string id) { RelationStatus status = new RelationStatus { Type = RelationStatus.Follow }; if (User.Identity.IsAuthenticated && !id.IsNullOrWhiteSpace()) { string uid = User.Identity.GetUserId(); using (PixurfDBContext db = new PixurfDBContext()) { User followingUser = db.Users.Find(id); if (followingUser != null) { UserRelationship userRelationship = new UserRelationship(); bool following = userRelationship.Following(id, uid); if (following) { db.User_Relations.Remove(db.User_Relations.FirstOrDefault(r => r.User_ID == uid && r.Related_User_ID == id && r.Status == "Follow")); try { db.SaveChanges(); status.Status = false; } catch (Exception e) { Console.WriteLine(e); status.Status = true; } } else { bool isBlocked = userRelationship.Blocked(id, uid); if (!isBlocked) { User_Relation relation = new User_Relation { User_ID = uid, Related_User_ID = id, Status = "Follow" }; db.User_Relations.Add(relation); try { db.SaveChanges(); status.Status = true; } catch (Exception e) { Console.WriteLine(e); status.Status = false; } } } } } } else { status.Status = false; } return(status); }
// GET: Search public ActionResult Index(string query, string category) { ViewSearchModel model = new ViewSearchModel { Query = "" + query, Target = "" + category }; using (PixurfDBContext db = new PixurfDBContext()) { //Retrieve Users if (category == null || category.Equals("People") || category.Equals("All")) { var queryable = db.Users.Where(user => user.Name.Contains(query) || (query.Contains("@") && user.Email.Contains(query))); UserRelationship relationship = new UserRelationship(); foreach (User user in queryable) { model.Users.Add(new ViewPeopleSearch { Id = user.User_ID, Name = user.Name, Email = user.Email, NoofFollowers = relationship.NoOfFollowers(user.User_ID) }); if (model.Users.Count >= 5) { break; } } } //Retrieve Contents if (category == null || category.Equals("Content") || category.Equals("All")) { var queryable = db.Contents.Where(c => c.Title.Contains(query) || c.Description.Contains(query)); foreach (Content content in queryable) { bool add = false; if (content.Access == "Public") { add = true; } else if (User.Identity.IsAuthenticated) { string uid = User.Identity.GetUserId(); if (content.User_ID.Equals(uid)) { add = true; } else { UserRelationship relationship = new UserRelationship(); if (content.Access == "Follower" && relationship.Following(content.User_ID, uid)) { add = true; } // if not blocked // lol..... what about non logged in users :P } } if (add) { model.Contents.Add(new ViewContentSearch { Id = content.Content_ID, Title = content.Title, Description = content.Description, Path = content.Path, OwnerId = content.User_ID, OwnerName = content.User.Name, CreationDate = content.Creation_Date }); } if (model.Contents.Count >= 4) { break; } } } //Retrieve Albums if (category == null || category.Equals("Album") || category.Equals("All")) { var queryable = db.Albums.Where(a => a.Name.Contains(query) || a.Description.Contains(query)); foreach (Album album in queryable) { bool add = false; if (album.Access == "Public") { add = true; } else if (User.Identity.IsAuthenticated) { string uid = User.Identity.GetUserId(); if (album.User_ID.Equals(uid)) { add = true; } else { UserRelationship relationship = new UserRelationship(); if (album.Access == "Follower" && relationship.Following(album.User_ID, uid)) { add = true; } // if not blocked // lol..... what about non logged in users :P } } if (add) { model.Albums.Add(new ViewAlbumSearch { Id = album.Album_ID, Title = album.Name, OwnerId = album.User_ID, OwnerName = album.User.Name, CreationDate = album.Creation_Date }); } if (model.Albums.Count >= 5) { break; } } } } ViewBag.Title = "" + query; return(View(model)); }
public ActionResult Tag(string tag) { ViewSearchModel model = new ViewSearchModel { Query = "" + tag, }; using (PixurfDBContext db = new PixurfDBContext()) { IQueryable <Content> queryableContents = db.Contents.Where(c => c.Title.Contains("#" + tag) || c.Description.Contains("#" + tag)); foreach (Content content in queryableContents) { bool add = false; if (content.Access == "Public") { add = true; } else if (User.Identity.IsAuthenticated) { string uid = User.Identity.GetUserId(); if (content.User_ID.Equals(uid)) { add = true; } else { UserRelationship relationship = new UserRelationship(); if (content.Access == "Follower" && relationship.Following(content.User_ID, uid)) { add = true; } // if not blocked // lol..... what about non logged in users :P } } if (add) { model.Contents.Add(new ViewContentSearch { Id = content.Content_ID, Title = content.Title, Description = content.Description, Path = content.Path, OwnerId = content.User_ID, OwnerName = content.User.Name, CreationDate = content.Creation_Date }); } if (model.Contents.Count >= 4) { break; } } //Retrieve Albums IQueryable <Album> queryableAlbums = db.Albums.Where(a => a.Name.Contains("#" + tag) || a.Description.Contains("#" + tag)); foreach (Album album in queryableAlbums) { bool add = false; if (album.Access == "Public") { add = true; } else if (User.Identity.IsAuthenticated) { string uid = User.Identity.GetUserId(); if (album.User_ID.Equals(uid)) { add = true; } else { UserRelationship relationship = new UserRelationship(); if (album.Access == "Follower" && relationship.Following(album.User_ID, uid)) { add = true; } // if not blocked // lol..... what about non logged in users :P } } if (add) { model.Albums.Add(new ViewAlbumSearch { Id = album.Album_ID, Title = album.Name, OwnerId = album.User_ID, OwnerName = album.User.Name, CreationDate = album.Creation_Date }); } if (model.Albums.Count >= 5) { break; } } } ViewBag.Title = "#" + tag; return(View(model)); }
public ActionResult View(int id) { List <StatusReport> reports = new List <StatusReport>(); using (PixurfDBContext db = new PixurfDBContext()) { Content content = db.Contents.Find(id); if (content == null) { reports.Add(new StatusReport { Title = "None", Description = "Content not found", Status = StatusReport.Warning }); Session["Reports"] = reports; return(RedirectToAction("Index", "Home")); } ViewContentModel vcm = new ViewContentModel { Content_ID = content.Content_ID, Title = content.Title, Description = content.Description, Album = content.Album, Path = content.Path, User = content.User, Access = content.Access, Creation_Date = content.Creation_Date, Status = content.Status, Type = content.Type }; if (content.Status == 1) { if (content.User_ID == User.Identity.GetUserId()) { if (reports.Count > 0) { Session["Reports"] = reports; } vcm.UserAuthenticated = true; return(View(vcm)); } UserRelationship relationship = new UserRelationship(); //Private if (content.Access == "Private") { StatusReport report = new StatusReport { Title = "Access Denied", Description = "This content is not accessible", Status = StatusReport.Warning }; reports.Add(report); Session["Reports"] = reports; return(RedirectToAction("Index", "Home")); } //Follower else if (content.Access == "Follower") { if (relationship.Following(content.User_ID, User.Identity.GetUserId())) { if (reports.Count > 0) { Session["Reports"] = reports; } return(View(vcm)); } else { StatusReport report = new StatusReport { Title = "Access Denied", Description = "Only Followers can view this content", Status = StatusReport.Warning }; reports.Add(report); Session["Reports"] = reports; return(RedirectToAction("Index", "Home")); } } //Not Blocked else { if (!relationship.Blocked(content.User_ID, User.Identity.GetUserId())) { if (reports.Count > 0) { Session["Reports"] = reports; } return(View(vcm)); } StatusReport report = new StatusReport { Title = "Error", Description = "Content not found", Status = StatusReport.Warning }; reports.Add(report); Session["Reports"] = reports; return(RedirectToAction("Index", "Home")); } } else { User user = db.Users.Find(User.Identity.GetUserId()); if (user != null && user.Admin) { if (reports.Count > 0) { Session["Reports"] = reports; } return(View(vcm)); } else { StatusReport report = new StatusReport { Title = "Error", Description = "Content not found", Status = StatusReport.Warning }; reports.Add(report); Session["Reports"] = reports; return(RedirectToAction("Index", "Home")); } } } }