// ToDo: More additional parameters which can be passed to the constructor of Thread/Post class public async Task <VBThread> CreateThreadAsync(VBUser author, string authorIpAddress, int forumId, string title, string text) { // We check the forum first so that no thread is created when Forum id doesn't exist var forum = db.Forums.Find(forumId); if (forum == null) { throw new Exception($"No forum with id #{forumId.ToString()} exists!"); } var post = new VBPost(author, title, text, authorIpAddress); db.Posts.Add(post); await db.SaveChangesAsync(); var thread = new VBThread(post.AuthorId.Value, post.AuthorName, post.Id, post.Id, forumId, post.AuthorId.Value, post.AuthorName, DateTime.Now, DateTime.Now, title); db.Threads.Add(thread); await db.SaveChangesAsync(); post.ThreadId = thread.Id; await db.SaveChangesAsync(); forum.LastPostId = post.Id; forum.LastThreadId = thread.Id; forum.LastThreadTitle = thread.Title; forum.PostCount++; forum.ThreadsCount++; forum.LastPostDate = DateTime.UtcNow; await db.SaveChangesAsync(); await userManager.IncrementPostCounterAsync(author.Id, post.Id, post.CreatedTime); return(thread); }
public async Task <VBSession> CreateAsync(VBUser user, int inForumId = 0, int inThreadId = 0, string location = "", int styleId = 0, int languageId = 0, bool saveChanges = true) { var context = contextAccessor.HttpContext; var session = new VBSession() { SessionHash = GenerateHash(), IdHash = GenerateIdHash(), IsBot = false, UserId = user.Id, UserAgent = GetUserAgent(), LoggedInRaw = user.Id > 0 ? 1 : 0, InForumId = inForumId, InThreadId = inThreadId, Location = location, StyleId = styleId, LanguageId = languageId, Host = GetClientIpAddress().ToString(), LastActivity = DateTime.UtcNow, ApiAccessToken = "" }; await CreateAsync(session); return(session); }
public CreateReplyModel(VBUser author, int threadId, string text, string ipAddress, string title = "", bool updatePostCounter = true) { Author = author; ThreadId = threadId; Text = text; IpAddress = ipAddress; Title = title; UpdatePostCounter = updatePostCounter; }
public async Task <bool> IsGlobalModeratorAsync(VBUser user) { if (user.UserGroup.AdminPermissions.HasFlag(VBAdminFlags.IsModerator)) { return(true); } var allGroups = await GetAllMemberGroupsAsync(user); return(allGroups.Any(group => group.AdminPermissions.HasFlag(VBAdminFlags.IsModerator))); }
// ToDo: IP shouldn't be optional public VBPost(VBUser author, string title, string text, string ipAddress, int threadId = 0, bool allowSmilies = true, VBPostVisibleState visibility = VBPostVisibleState.Visible) { ThreadId = threadId; AuthorName = author.UserName; AuthorId = author.Id; Title = title; Text = text; IpAddress = ipAddress; CreatedTime = DateTime.Now; AllowSmilies = allowSmilies; Visibility = visibility; }
async Task <VBSession> CreateGuestSessionAsync() { var user = new VBUser() { UserGroupId = guestUserGroupId }; user.UserGroup = await db.UserGroups.FindAsync(guestUserGroupId); // ToDo: Add session attributes like location var session = await CreateAsync(user); // Tracking for guest sessions is tricky and dangerous: We change the user attribute to a non existing one, which cause exceptions on save - so EF shouldn track them db.Entry(session).State = EntityState.Detached; // CreateAsync only assign the UserId so that we don't insert dummy guest users here by those realtion session.User = user; return(session); }
public async Task <List <VBUserGroup> > GetAllMemberGroupsAsync(VBUser user) { if (user.UserGroup == null) { throw new Exception($"UserGroup is null for {user.Id}"); } var groups = new List <VBUserGroup>() { user.UserGroup }; if (user.MemberGroupIds.Any()) { // ToDo: Load all groups in one query for better performance var memberGroups = await GetUserGroupsAsync(user.MemberGroupIds); groups.AddRange(memberGroups); } return(groups); }
static void WarmUpManagers(VBThreadManager threadManager, VBSessionManager sessionManager, VBSettingsManager settingsManager, VBForumManager forumManager, VBUserManager userManager, VBThread thread, VBUser user, VBSession session) { var managerThread = threadManager.GetThreadAsync(thread.Id, writeable: true).Result; var replys = threadManager.GetReplysAsync(thread.Id, start: 0, count: 1).Result; var settings = settingsManager.GetCommonSettings(); if (session != null) { // Do not update last activity since we're not in a http context => No request path avaliable var managerSession = sessionManager.GetAsync(session.SessionHash, updateLastActivity: false).Result; } if (user != null) { var forum = forumManager.GetCategoriesWhereUserCanAsync(user.UserGroup).Result; var randomUserFromManager = userManager.GetUserAsync(user.Id).Result; } }