mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 19:54:55 +00:00
new app server and database + added hub and controller of user profile
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
using Govor.Application.Interfaces.Medias;
|
||||
using Govor.Core.Models.Messages;
|
||||
using Govor.Core.Models;
|
||||
using Govor.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
@@ -14,20 +14,35 @@ public class AccesserToDownloadMediaService : IAccesserToDownloadMedia
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public async Task<bool> HasAccessAsync(Guid mediaFileId, Guid userId)
|
||||
public async Task<bool> HasAccessAsync(Guid mediaId, Guid userId)
|
||||
{
|
||||
return await _dbContext.MediaAttachments
|
||||
.Include(ma => ma.Message)
|
||||
.AnyAsync(ma =>
|
||||
ma.MediaFileId == mediaFileId &&
|
||||
(
|
||||
(ma.Message.RecipientType == RecipientType.User &&
|
||||
(ma.Message.SenderId == userId || ma.Message.RecipientId == userId))
|
||||
||
|
||||
(ma.Message.RecipientType == RecipientType.Group &&
|
||||
_dbContext.GroupMemberships.Any(gm =>
|
||||
gm.GroupId == ma.Message.RecipientId &&
|
||||
gm.UserId == 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.GroupMemberships
|
||||
.AnyAsync(gm => gm.GroupId == media.OwnerId && gm.UserId == userId),
|
||||
|
||||
MediaOwnerType.Message => await _dbContext.MediaAttachments
|
||||
.AnyAsync(ma =>
|
||||
ma.MediaFileId == mediaId &&
|
||||
(
|
||||
ma.Message.SenderId == userId ||
|
||||
ma.Message.RecipientId == userId ||
|
||||
_dbContext.GroupMemberships.Any(gm =>
|
||||
gm.GroupId == ma.Message.RecipientId && gm.UserId == userId)
|
||||
)),
|
||||
|
||||
MediaOwnerType.System => true,
|
||||
_ => false
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -35,7 +35,9 @@ public class MediaService : IMediaService
|
||||
DateCreated = file.UploadedOn,
|
||||
MediaType = file.Type,
|
||||
MineType = file.MimeType,
|
||||
Url = url
|
||||
Url = url,
|
||||
OwnerType = file.OwnerType,
|
||||
OwnerId = file.OwnerId,
|
||||
});
|
||||
|
||||
await _dbContext.SaveChangesAsync();
|
||||
@@ -87,7 +89,9 @@ public class MediaService : IMediaService
|
||||
contentBytes,
|
||||
mediaFile.MediaType,
|
||||
mediaFile.MineType,
|
||||
string.Empty
|
||||
string.Empty,
|
||||
mediaFile.OwnerType,
|
||||
mediaFile.OwnerId
|
||||
);
|
||||
}
|
||||
catch (FileNotFoundException ex)
|
||||
@@ -96,4 +100,24 @@ public class MediaService : IMediaService
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public async Task AttachToMessageAsync(Guid mediaId, Guid messageId)
|
||||
{
|
||||
var mediaFile = await _dbContext.MediaFiles
|
||||
.FirstOrDefaultAsync(x => x.Id == mediaId)
|
||||
?? throw new KeyNotFoundException($"No media found by given id {mediaId}");
|
||||
|
||||
if (mediaFile.OwnerType != MediaOwnerType.Message)
|
||||
{
|
||||
_logger.LogWarning("Attempt to attach already owned media {MediaId}", mediaId);
|
||||
throw new InvalidOperationException($"Media {mediaId} is already attached to {mediaFile.OwnerType}");
|
||||
}
|
||||
|
||||
mediaFile.OwnerType = MediaOwnerType.Message;
|
||||
mediaFile.OwnerId = messageId;
|
||||
|
||||
_dbContext.MediaFiles.Update(mediaFile);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
_logger.LogInformation("Media {MediaId} successfully attached to message {MessageId}", mediaId, messageId);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user