Ejemplo n.º 1
0
        public async Task <VBSession> GetAsync(string sessionHash, bool updateLastActivity = false)
        {
            if (!updateLastActivity && cache.TryGet(VBCacheKey.UserSession, sessionHash, out VBSession session))
            {
                return(session);
            }

            int cookieTimeoutSpan = GetCookieTimeoutSpam();

            session = await db.Sessions.Include(s => s.User)
                      .ThenInclude(u => u.UserGroup)
                      .Include(s => s.User)
                      .ThenInclude(u => u.CustomAvatar)
                      .SingleOrDefaultAsync(s => s.SessionHash == sessionHash && s.LastActivityRaw > cookieTimeoutSpan);

            if (session != null && updateLastActivity)
            {
                session.LastActivity = DateTime.UtcNow;
                session.Location     = contextAccessor.HttpContext.Request.Path;
                await db.SaveChangesAsync();
            }

            cache.Set(VBCacheKey.UserSession, sessionHash, session, settingsManager.GetCommonSettings().CookieTimeout);
            return(session);
        }
        public async Task MarkThreadAsReadAsync(int userId, int threadId)
        {
            if (userId <= 0)
            {
                return;
            }

            var threadRead = await GetThreadReadAsync(userId, threadId);

            if (threadRead == null)
            {
                threadRead = new VBThreadRead(userId, threadId);
                await db.ThreadReads.AddAsync(threadRead);
            }

            threadRead.ReadTime = DateTime.Now;
            await db.SaveChangesAsync();
        }
Ejemplo n.º 3
0
        public async Task <PhysicalFileResult> GetAttachmentAsync(VBAttachment attachment, string attachmentsPath)
        {
            string fullPath            = Path.Combine(attachmentsPath, attachment.FilePath);
            var    fileContentProvider = new FileExtensionContentTypeProvider();
            string mimeType;

            if (!fileContentProvider.TryGetContentType(attachment.FileName, out mimeType))
            {
                mimeType = "application/octet-stream";
            }

            var file = new PhysicalFileResult(fullPath, mimeType);

            attachment.DownloadsCount++;
            await db.SaveChangesAsync();

            return(file);
        }
Ejemplo n.º 4
0
        public async Task IncrementPostCounterAsync(int userId, int lastPostId, DateTime lastPostDateTime, bool saveChanges = true)
        {
            var user = await db.Users.FindAsync(userId);

            if (user == null)
            {
                throw new Exception($"No user exists with id #{userId}!");
            }

            user.LastActivityTime = user.LastVisitTime = DateTime.UtcNow;
            user.LastPostTime     = lastPostDateTime;
            user.LastPostId       = lastPostId;
            user.PostsCount++;

            if (saveChanges)
            {
                await db.SaveChangesAsync();
            }
        }