• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Genfic / Ogma / 36388606018

28 Sep 2026 06:51AM UTC coverage: 0.555% (-0.02%) from 0.573%
36388606018

push

github

Atulin
Fix security and correctness issues
- Fixed club deletion deleting all clubs the user had
- Fixed voting potentially changing the vote count of the story even if the vote itself failed
- Fixed comment deletion potentially deleting all revisions and updating comment count in the thread, even if the deletion itself failed
- Rendered Markdown in comments is now escaped
- Removed unused `/comments/md` endpoint
- Better handling of invite codes consumption
- Fixed invite codes not being required when registering with a 3rd party service
- Fixed bans applying to the moderator who issued the ban, not to the banned user lmao
- Fixed ban cache reconciliation potentially reinstating bans that were manually revoked
- Comment creation now takes the source from the thread, not from user input
- Folders can now only be fetched by the users who are members of the given club
- Fix club moderator action foreign keys
- Revoked subscriptions are now marked for deletion first, then deleted in another pass after some time, to prevent short periods of time when a potentially entitled user would lose their entitlements
- Yeeted deleted compiled models (again) since they don't like query filters
- Minor code cleanup

3 of 11455 new or added lines in 31 files covered. (0.03%)

307 existing lines in 16 files now uncovered.

887 of 159773 relevant lines covered (0.56%)

0.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/Ogma3/Services/InviteCodeService/InviteCodeService.cs
1
using Immediate.Injections.Shared;
2
using JetBrains.Annotations;
3
using Microsoft.EntityFrameworkCore;
4
using Ogma3.Data;
5

6
namespace Ogma3.Services.InviteCodeService;
7

8
/// <summary>
9
/// Single point of truth for redeeming an invite code. Registration is invite-only, so every
10
/// path that can create a user must reserve a code through here.
11
/// </summary>
12
/// <remarks>
13
/// A code moves through two states:
14
/// <list type="bullet">
15
/// <item><b>Reserved</b> — <c>UsedDate</c> is set but <c>UsedById</c> is null. The code is taken and
16
/// no longer redeemable by anyone else, but no user is attached to it yet.</item>
17
/// <item><b>Claimed</b> — <c>UsedById</c> is set. The code is redeemed by a specific user.</item>
18
/// </list>
19
/// Because a code can only ever have one reservation, a row with <c>UsedById == null</c> is
20
/// unambiguously <i>this</i> caller's reservation. That is what makes <see cref="ReleaseAsync" />
21
/// safe: it can only ever undo the caller's own in-flight reservation, never a completed claim
22
/// belonging to another registration.
23
/// <para>
24
/// The intended order is reserve → create the user → attach, so no user is ever created only to be
25
/// thrown away, and the window in which a crash can leave a user with no code consumed is a code
26
/// being burned rather than a free account being created.
27
/// </para>
28
/// </remarks>
29
[RegisterTransient]
30
[UsedImplicitly]
NEW
31
public sealed class InviteCodeService(AppDbContext context, ILogger<InviteCodeService> logger)
×
32
{
33
        /// <summary>
34
        /// Atomically reserves an unused invite code. Call this <b>before</b> creating a user.
35
        /// </summary>
36
        /// <remarks>
37
        /// The reservation is a single conditional <c>UPDATE ... WHERE Code = @code AND UsedDate IS NULL</c>,
38
        /// so the database — not a prior read — decides the winner when two requests race with the same
39
        /// code. Under PostgreSQL <c>READ COMMITTED</c> the loser's <c>UPDATE</c> blocks on the row lock
40
        /// and then re-evaluates the predicate, so exactly one caller can ever see
41
        /// <see cref="InviteCodeReservationResult.Reserved" />.
42
        /// <para>
43
        /// If the user then cannot be created, call <see cref="ReleaseAsync" /> to hand the code back.
44
        /// </para>
45
        /// </remarks>
46
        public async Task<InviteCodeReservationResult> ReserveAsync(string? code, CancellationToken cancellationToken)
47
        {
NEW
48
                if (string.IsNullOrWhiteSpace(code)) return InviteCodeReservationResult.NotFound;
×
49

NEW
50
                var reserved = await context.InviteCodes
×
NEW
51
                        .Where(ic => ic.Code == code)
×
NEW
52
                        .Where(ic => ic.UsedDate == null)
×
NEW
53
                        .ExecuteUpdateAsync(s => s
×
NEW
54
                                        .SetProperty(ic => ic.UsedDate, DateTimeOffset.UtcNow),
×
NEW
55
                                cancellationToken);
×
56

NEW
57
                if (reserved > 0) return InviteCodeReservationResult.Reserved;
×
58

59
                // The conditional update matched nothing. Distinguish "no such code" from "already taken"
60
                // so the caller can show the right message. This only runs on the failure path.
NEW
61
                var usedDate = await context.InviteCodes
×
NEW
62
                        .Where(ic => ic.Code == code)
×
NEW
63
                        .Select(ic => ic.UsedDate)
×
NEW
64
                        .FirstOrDefaultAsync(cancellationToken);
×
65

NEW
66
                if (usedDate is null)
×
67
                {
NEW
68
                        logger.LogWarning("An unknown invite code was submitted for redemption");
×
NEW
69
                        return InviteCodeReservationResult.NotFound;
×
70
                }
71

NEW
72
                return InviteCodeReservationResult.AlreadyClaimed;
×
73
        }
74

75
        /// <summary>
76
        /// Attaches a successfully created user to a reservation made by <see cref="ReserveAsync" />.
77
        /// </summary>
78
        /// <remarks>
79
        /// The predicate is <c>UsedById == null</c>, which is only ever true for the caller's own
80
        /// reservation, so this cannot be steered onto another registration's code. Returns
81
        /// <see langword="false" /> if the reservation was lost or already attached, in which case the
82
        /// caller must treat the user it created as unregistered.
83
        /// </remarks>
84
        public async Task<bool> AttachAsync(string? code, long userId, CancellationToken cancellationToken)
85
        {
NEW
86
                if (string.IsNullOrWhiteSpace(code)) return false;
×
87

NEW
88
                var attached = await context.InviteCodes
×
NEW
89
                        .Where(ic => ic.Code == code)
×
NEW
90
                        .Where(ic => ic.UsedById == null)
×
NEW
91
                        .ExecuteUpdateAsync(s => s
×
NEW
92
                                        .SetProperty(ic => ic.UsedById, userId),
×
NEW
93
                                cancellationToken);
×
94

NEW
95
                if (attached > 0) return true;
×
96

NEW
97
                logger.LogError("Could not attach user {UserId} to their reserved invite code", userId);
×
NEW
98
                return false;
×
99
        }
100

101
        /// <summary>
102
        /// Hands a reservation made by <see cref="ReserveAsync" /> back to the unused pool.
103
        /// </summary>
104
        /// <remarks>
105
        /// Only for when the user could not be created, so a transient failure does not permanently
106
        /// burn a legitimate code. Does nothing once the code has been attached to a user, so it is
107
        /// safe to call defensively.
108
        /// </remarks>
109
        /// <returns><see langword="true" /> if a reservation was released.</returns>
110
        public async Task<bool> ReleaseAsync(string? code, CancellationToken cancellationToken)
111
        {
NEW
112
                if (string.IsNullOrWhiteSpace(code)) return false;
×
113

NEW
114
                var released = await context.InviteCodes
×
NEW
115
                        .Where(ic => ic.Code == code)
×
NEW
116
                        .Where(ic => ic.UsedById == null)
×
NEW
117
                        .ExecuteUpdateAsync(s => s
×
NEW
118
                                        .SetProperty(ic => ic.UsedDate, (DateTimeOffset?)null),
×
NEW
119
                                cancellationToken);
×
120

NEW
121
                return released > 0;
×
122
        }
123
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc