42 lines
1.4 KiB
C#
42 lines
1.4 KiB
C#
using MongoDB.Entities;
|
|||
|
|
|
||
|
|
using PrivaPub.Models.Group;
|
||
|
|
using PrivaPub.Models.Post;
|
||
|
|
|
||
|
|
using System.Linq.Expressions;
|
||
|
|
|
||
|
|
using GroupEntity = PrivaPub.Models.Group.Group;
|
||
|
|
|
||
|
|
namespace PrivaPub.Domain.Privacy
|
||
|
|
{
|
||
|
|
public static class VisibilityPolicy
|
||
|
|
{
|
||
|
|
public static readonly Expression<Func<Post, bool>> IsPublic = p =>
|
||
|
|
!p.DeletedAt.HasValue && (p.Visibility == PostVisibility.Public || p.Visibility == PostVisibility.Unlisted);
|
||
|
|
|
||
|
|
static readonly Func<Post, bool> IsPublicCompiled = IsPublic.Compile();
|
||
|
|
|
||
|
|
public static async Task<bool> CanSee(Post post, string viewerAvatarId, CancellationToken token)
|
||
|
|
{
|
||
|
|
if (post == default || post.DeletedAt.HasValue)
|
||
|
|
return false;
|
||
|
|
if (IsPublicCompiled(post))
|
||
|
|
return true;
|
||
|
|
if (string.IsNullOrEmpty(viewerAvatarId))
|
||
|
|
return false;
|
||
|
|
if (post.GroupUserId == viewerAvatarId || post.Mentions.Any(m => m.IsLocal && m.AccountId == viewerAvatarId))
|
||
|
|
return true;
|
||
|
|
|
||
|
|
return post.Visibility switch
|
||
|
|
{
|
||
|
|
PostVisibility.Direct => !string.IsNullOrEmpty(post.ConversationId) && await DB.Default.Find<DmGroup>()
|
||
|
|
.Match(g => g.ID == post.ConversationId && g.Members.Any(m => !m.IsForeign && m.AvatarId == viewerAvatarId))
|
||
|
|
.ExecuteAnyAsync(token),
|
||
|
|
PostVisibility.Circle => !string.IsNullOrEmpty(post.GroupId) && await DB.Default.Find<GroupEntity>()
|
||
|
|
.Match(g => g.ID == post.GroupId && g.Members.Any(m => !m.IsForeign && m.AvatarId == viewerAvatarId))
|
||
|
|
.ExecuteAnyAsync(token),
|
||
|
|
_ => false
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|