Compare commits

...

12 Commits

Author SHA1 Message Date
c3784de9cd Merge branch 'master' into fix-prune 2026-01-02 22:22:36 +00:00
cake
5aaa6ff35c update submodule 2026-01-02 23:21:33 +01:00
cake
2ef0725f4e fix submodule 2026-01-02 23:18:04 +01:00
cake
6e540ad0c6 Removal of zero days. 2025-12-30 02:05:06 +01:00
cake
863042d1fe Merge branch 'Location' into fix-prune 2025-12-30 01:52:42 +01:00
cake
b29475ea41 Check on redis if user is online as well. 2025-12-30 01:40:41 +01:00
Tsubasahane
53e089a65b ToggleLocationSharing returns bool 2025-12-29 15:35:54 +08:00
Tsubasahane
c3e87eb7df ShareLocation : Migration 2025-12-29 09:55:53 +08:00
Tsubasahane
a533fca195 transfer sharing status 2025-12-28 19:54:26 +08:00
Tsubasahane
717d8e46f1 Location Share but with expireAt 2025-12-28 13:07:36 +08:00
Tsubasahane
d3790013c6 Revert "Share Location"
This reverts commit 9971b14177.
2025-12-28 11:00:50 +08:00
Tsubasahane
9971b14177 Share Location 2025-12-27 19:52:28 +08:00
2 changed files with 59 additions and 19 deletions

View File

@@ -2,15 +2,19 @@
using LightlessSyncShared.Data;
using LightlessSyncShared.Models;
using Microsoft.EntityFrameworkCore;
using StackExchange.Redis.Extensions.Core.Abstractions;
namespace LightlessSyncServer.Services
{
public class PruneService(LightlessDbContext dbContext) : IPruneService
public class PruneService(LightlessDbContext dbContext, IRedisDatabase redis) : IPruneService
{
private readonly LightlessDbContext _dbContext = dbContext;
private readonly IRedisDatabase _redis = redis;
public async Task<int> CountPrunableUsersAsync(string groupGid, int days, CancellationToken ct)
{
var onlineUids = await GetOnlineUidsAsync().ConfigureAwait(false);
var allGroupUsers = await _dbContext.GroupPairs
.Include(p => p.GroupUser)
.Include(p => p.Group)
@@ -20,17 +24,14 @@ namespace LightlessSyncServer.Services
var inactivitySpan = GetInactivitySpan(days);
var now = DateTime.UtcNow;
var usersToPrune = allGroupUsers.Where(p =>
!p.IsPinned &&
!p.IsModerator &&
!string.Equals(p.Group.OwnerUID, p.GroupUserUID, StringComparison.Ordinal) &&
p.GroupUser.LastLoggedIn < now - inactivitySpan);
return usersToPrune.Count();
var usersToPrune = GetPruneUserList(allGroupUsers, onlineUids, inactivitySpan, now);
return usersToPrune.Count;
}
public async Task<IReadOnlyList<GroupPair>> ExecutePruneAsync(string groupGid, int days, CancellationToken ct)
{
var onlineUids = await GetOnlineUidsAsync().ConfigureAwait(false);
var allGroupUsers = await _dbContext.GroupPairs
.Include(p => p.GroupUser)
.Include(p => p.Group)
@@ -40,12 +41,7 @@ namespace LightlessSyncServer.Services
var inactivitySpan = GetInactivitySpan(days);
var now = DateTime.UtcNow;
var usersToPrune = allGroupUsers.Where(p =>
!p.IsPinned &&
!p.IsModerator &&
!string.Equals(p.Group.OwnerUID, p.GroupUserUID, StringComparison.Ordinal) &&
p.GroupUser.LastLoggedIn < now - inactivitySpan)
.ToList();
var usersToPrune = GetPruneUserList(allGroupUsers, onlineUids, inactivitySpan, now);
_dbContext.GroupPairs.RemoveRange(usersToPrune);
await _dbContext.SaveChangesAsync(ct).ConfigureAwait(false);
@@ -53,8 +49,52 @@ namespace LightlessSyncServer.Services
return usersToPrune;
}
private static TimeSpan GetInactivitySpan(int days) => days == 0
? TimeSpan.FromMinutes(15)
: TimeSpan.FromDays(days);
private static List<GroupPair> GetPruneUserList(
List<GroupPair> allGroupUsers,
HashSet<string> onlineUids,
TimeSpan inactivitySpan,
DateTime now)
{
return
[
.. allGroupUsers.Where(p =>
!p.IsPinned &&
!p.IsModerator &&
!string.Equals(p.Group.OwnerUID, p.GroupUserUID, StringComparison.Ordinal) &&
!onlineUids.Contains(p.GroupUserUID) &&
p.GroupUser.LastLoggedIn < now - inactivitySpan),
];
}
private async Task<HashSet<string>> GetOnlineUidsAsync()
{
var keys = await _redis.SearchKeysAsync("UID:*").ConfigureAwait(false);
var set = new HashSet<string>(StringComparer.Ordinal);
foreach (var k in keys)
{
if (string.IsNullOrEmpty(k)) continue;
const string prefix = "UID:";
if (k.StartsWith(prefix, StringComparison.Ordinal))
{
var uid = k.Substring(prefix.Length);
if (!string.IsNullOrEmpty(uid))
set.Add(uid);
}
else
{
var idx = k.IndexOf(':', StringComparison.Ordinal);
if (idx >= 0 && idx < k.Length - 1)
set.Add(k[(idx + 1)..]);
}
}
return set;
}
private static TimeSpan GetInactivitySpan(int days) =>
days == 0 ? TimeSpan.FromHours(2) : TimeSpan.FromDays(days);
}
}

View File

@@ -24,7 +24,7 @@ namespace LightlessSyncServer.Worker
var hubContext = scope.ServiceProvider.GetRequiredService<IHubContext<LightlessHub>>();
var groups = await db.Groups
.Where(g => g.AutoPruneEnabled && g.AutoPruneDays > 0)
.Where(g => g.AutoPruneEnabled)
.ToListAsync(stoppingToken).ConfigureAwait(false);
foreach (var group in groups)