Files
Govor/Govor.Application/Medias/AccesserToDownloadMediaService.cs
Artemy 2e9d7e625e Add primitive groups and required channel
Adds server-side group and channel management, private invitations, member moderation, avatar handling, and required-channel configuration. This change also introduces SignalR group profile/member notifications, updates registration to atomically join required channels, and adds EF migration and test coverage for group membership, permissions, and invitation expiry rules.
2026-10-04 10:35:07 +07:00

54 lines
2.1 KiB
C#

using Govor.Domain.Models;
using Govor.Domain;
using Microsoft.EntityFrameworkCore;
using Govor.Domain.Models.Messages;
namespace Govor.Application.Medias;
public class AccesserToDownloadMediaService : IAccesserToDownloadMedia
{
private readonly GovorDbContext _dbContext;
public AccesserToDownloadMediaService(GovorDbContext dbContext)
{
_dbContext = dbContext;
}
public async Task<bool> HasAccessAsync(Guid mediaId, Guid userId)
{
var media = await _dbContext.MediaFiles
.AsNoTracking()
.Where(m => m.Id == mediaId)
.Select(m => new { m.OwnerType, m.OwnerId, m.UploaderId })
.FirstOrDefaultAsync();
if (media is null)
return false;
return media.OwnerType switch
{
MediaOwnerType.Avatar => true, // media.OwnerId == userId
MediaOwnerType.GroupAvatar => await _dbContext.ChatGroups.AnyAsync(g => g.Id == media.OwnerId &&
!_dbContext.GroupMemberships.Any(m => m.GroupId == g.Id && m.UserId == userId && m.IsBanned) &&
((!g.IsPrivate && g.ImageId == mediaId) || _dbContext.GroupMemberships
.Any(m => m.GroupId == g.Id && m.UserId == userId && !m.IsBanned))),
MediaOwnerType.Message => await _dbContext.MediaAttachments
.AnyAsync(ma =>
ma.MediaFileId == mediaId &&
(
(ma.Message.RecipientType == RecipientType.User &&
_dbContext.PrivateChats.Any(c => c.Id == ma.Message.RecipientId &&
(c.UserAId == userId || c.UserBId == userId))) ||
(ma.Message.RecipientType == RecipientType.Group &&
_dbContext.GroupMemberships.Any(gm =>
gm.GroupId == ma.Message.RecipientId && gm.UserId == userId && !gm.IsBanned))
)),
MediaOwnerType.System => true,
MediaOwnerType.Reaction => await _dbContext.ReactionItems.AnyAsync(r => r.MediaFileId == mediaId),
_ => false
};
}
}