From 3d58fff4a7ee1bd97f5849924f1693e3b3de0bf3 Mon Sep 17 00:00:00 2001 From: Sergey Gut Date: Sun, 11 Oct 2026 14:26:30 +0400 Subject: [PATCH] feat: invites --- miniapp/src/lib/api.js | 3 + miniapp/src/pages/CollectionsPage.svelte | 304 +++++++++++++++++- .../Controllers/BotController.cs | 20 ++ src/MemeVault.Api/Program.cs | 2 + src/MemeVault.Api/Services/BotInfoService.cs | 27 ++ .../GetUserCollectionsQuery.cs | 3 +- .../GetUserCollectionsQueryHandler.cs | 4 +- 7 files changed, 359 insertions(+), 4 deletions(-) create mode 100644 src/MemeVault.Api/Controllers/BotController.cs create mode 100644 src/MemeVault.Api/Services/BotInfoService.cs diff --git a/miniapp/src/lib/api.js b/miniapp/src/lib/api.js index fa4ec0e..74aab6d 100644 --- a/miniapp/src/lib/api.js +++ b/miniapp/src/lib/api.js @@ -66,6 +66,9 @@ export const api = { joinCollection: (id) => request('POST', `/collections/${id}/join`), regenerateShareLink: (id) => request('POST', `/collections/${id}/share-link/regenerate`), + // ── Bot ─────────────────────────────────────────────────────────────────── + getBotInfo: () => request('GET', '/bot/info'), + // ── Invites ─────────────────────────────────────────────────────────────── getInvites: (collectionId) => request('GET', `/collections/${collectionId}/invites`), createInvite: (collectionId, role, expiresAt) => diff --git a/miniapp/src/pages/CollectionsPage.svelte b/miniapp/src/pages/CollectionsPage.svelte index b5da363..f31909f 100644 --- a/miniapp/src/pages/CollectionsPage.svelte +++ b/miniapp/src/pages/CollectionsPage.svelte @@ -1,9 +1,11 @@
@@ -208,6 +317,16 @@ > + {#if col.myRole === ROLE_OWNER} + + {/if} {#if !col.isInbox}
+ + {#if invitesPanelId === col.id} +
  • +
    + Приглашения +
    + + {#if invitesLoading} +
    Загрузка…
    + {:else} + + {#if invitesByCol[col.id]?.length > 0} + + {:else} +
    Нет активных приглашений
    + {/if} + + +
    +
    +
    + + +
    + + +
    +
    + {/if} +
  • + {/if} + {#if confirmDelete === col.id && deleteTarget}
  • @@ -335,6 +539,9 @@ .icon-btn.nsfw { color: var(--tg-hint, #999); } .icon-btn.nsfw:hover { color: #e53935; background: var(--tg-secondary-bg, #e8e8e8); } .icon-btn.nsfw--on { color: #e53935; } + .icon-btn.invite { color: var(--tg-hint, #999); } + .icon-btn.invite:hover { color: var(--tg-button, #2196f3); } + .icon-btn.invite--on { color: var(--tg-button, #2196f3); } /* Подтверждение */ .confirm-panel { @@ -353,4 +560,97 @@ .btn-primary { background: var(--tg-button, #2196f3); color: var(--tg-btn-text, #fff); } .btn-ghost { background: none; color: var(--tg-hint, #888); } .btn-ghost:hover { background: var(--tg-secondary-bg, #e8e8e8); } + + /* ── Панель инвайтов ─────────────────────────────────────────────────────── */ + .invites-panel { + background: var(--tg-secondary-bg, #f5f5f5); + border-radius: 10px; padding: 10px 12px; + border-left: 3px solid var(--tg-button, #2196f3); + display: flex; flex-direction: column; gap: 8px; + } + + .invites-header { + display: flex; align-items: center; gap: 6px; + font-size: 12px; font-weight: 600; color: var(--tg-hint, #888); + text-transform: uppercase; letter-spacing: .05em; + } + + .invites-loading { font-size: 13px; color: var(--tg-hint, #888); } + .invites-empty { font-size: 13px; color: var(--tg-hint, #888); text-align: center; padding: 4px 0; } + + .invites-list { + list-style: none; margin: 0; padding: 0; + display: flex; flex-direction: column; gap: 4px; + } + + .invite-item { + display: flex; align-items: center; gap: 6px; + background: var(--tg-bg, #fff); + border-radius: 8px; padding: 6px 8px; + } + + .invite-role-badge { + font-size: 11px; font-weight: 600; + background: var(--tg-button, #2196f3); color: var(--tg-btn-text, #fff); + border-radius: 4px; padding: 1px 6px; + white-space: nowrap; flex-shrink: 0; + } + + .invite-link-btn { + display: flex; align-items: center; gap: 4px; + padding: 3px 8px; border-radius: 6px; border: none; + background: var(--tg-secondary-bg, #f0f0f0); + color: var(--tg-text, #000); font-size: 12px; cursor: pointer; + white-space: nowrap; flex-shrink: 0; + transition: background .12s; + } + .invite-link-btn:hover { background: var(--tg-button, #2196f3); color: var(--tg-btn-text, #fff); } + + .invite-expiry { + font-size: 11px; color: var(--tg-hint, #888); + white-space: nowrap; flex-shrink: 0; margin-left: auto; + } + + .invite-revoke { + width: 26px; height: 26px; flex-shrink: 0; + color: var(--tg-hint, #999); + } + .invite-revoke:hover { color: #e53935; } + + /* Форма создания инвайта */ + .invite-create { margin-top: 2px; } + .invite-create-row { + display: flex; gap: 6px; align-items: center; + } + + .role-toggle { + display: flex; border-radius: 8px; overflow: hidden; + border: 1px solid var(--tg-hint, #ccc); + flex-shrink: 0; + } + .role-btn { + padding: 5px 10px; border: none; font-size: 12px; font-weight: 500; + background: none; color: var(--tg-hint, #888); cursor: pointer; + transition: background .12s, color .12s; + } + .role-btn:first-child { border-right: 1px solid var(--tg-hint, #ccc); } + .role-btn--active { + background: var(--tg-button, #2196f3); color: var(--tg-btn-text, #fff); + } + + .expiry-input { + flex: 1; min-width: 0; padding: 5px 8px; border-radius: 8px; + border: 1px solid var(--tg-hint, #ccc); + background: var(--tg-bg, #fff); color: var(--tg-text, #000); + font-size: 12px; + } + + .invite-add-btn { + display: flex; align-items: center; justify-content: center; + width: 32px; height: 32px; border-radius: 8px; border: none; + background: var(--tg-button, #2196f3); color: var(--tg-btn-text, #fff); + cursor: pointer; flex-shrink: 0; + transition: opacity .12s; + } + .invite-add-btn:disabled { opacity: .4; cursor: default; } diff --git a/src/MemeVault.Api/Controllers/BotController.cs b/src/MemeVault.Api/Controllers/BotController.cs new file mode 100644 index 0000000..210a7a4 --- /dev/null +++ b/src/MemeVault.Api/Controllers/BotController.cs @@ -0,0 +1,20 @@ +using MemeVault.Api.Services; +using Microsoft.AspNetCore.Mvc; + +namespace MemeVault.Api.Controllers; + +[ApiController] +[Route("api/bot")] +public sealed class BotController(BotInfoService botInfo) : ControllerBase +{ + /// + /// Возвращает публичный username бота. + /// Не требует авторизации — username бота публичен. + /// + [HttpGet("info")] + public async Task GetInfo(CancellationToken ct) + { + var username = await botInfo.GetBotUsernameAsync(); + return Ok(new { username }); + } +} diff --git a/src/MemeVault.Api/Program.cs b/src/MemeVault.Api/Program.cs index 3bfeba7..e5fbd03 100644 --- a/src/MemeVault.Api/Program.cs +++ b/src/MemeVault.Api/Program.cs @@ -1,5 +1,6 @@ using MemeVault.Api.Middleware; using MemeVault.Api.Auth; +using MemeVault.Api.Services; using System.Text; using MemeVault.Api.Options; using MemeVault.Infrastructure; @@ -29,6 +30,7 @@ builder.Services.Configure( builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); +builder.Services.AddSingleton(); builder.Services.AddHttpContextAccessor(); builder.Services.AddScoped(); diff --git a/src/MemeVault.Api/Services/BotInfoService.cs b/src/MemeVault.Api/Services/BotInfoService.cs new file mode 100644 index 0000000..1d54e34 --- /dev/null +++ b/src/MemeVault.Api/Services/BotInfoService.cs @@ -0,0 +1,27 @@ +using Telegram.Bot; + +namespace MemeVault.Api.Services; + +/// +/// Кэширует username бота на весь lifetime процесса. +/// GetMeAsync вызывается ровно один раз при первом обращении. +/// +public sealed class BotInfoService +{ + private readonly Lazy> _username; + + public BotInfoService(ITelegramBotClient botClient) + { + _username = new Lazy>( + () => FetchAsync(botClient), + LazyThreadSafetyMode.ExecutionAndPublication); + } + + private static async Task FetchAsync(ITelegramBotClient botClient) + { + var me = await botClient.GetMe(); + return me.Username ?? string.Empty; + } + + public Task GetBotUsernameAsync() => _username.Value; +} diff --git a/src/MemeVault.Application/Queries/GetUserCollections/GetUserCollectionsQuery.cs b/src/MemeVault.Application/Queries/GetUserCollections/GetUserCollectionsQuery.cs index d3274f9..cf98537 100644 --- a/src/MemeVault.Application/Queries/GetUserCollections/GetUserCollectionsQuery.cs +++ b/src/MemeVault.Application/Queries/GetUserCollections/GetUserCollectionsQuery.cs @@ -15,5 +15,6 @@ public sealed record CollectionDto( bool IsNsfw, string? ShareToken, int ItemCount, - DateTimeOffset CreatedAt + DateTimeOffset CreatedAt, + CollectionRole MyRole ); diff --git a/src/MemeVault.Application/Queries/GetUserCollections/GetUserCollectionsQueryHandler.cs b/src/MemeVault.Application/Queries/GetUserCollections/GetUserCollectionsQueryHandler.cs index 898f7f1..67bc5ef 100644 --- a/src/MemeVault.Application/Queries/GetUserCollections/GetUserCollectionsQueryHandler.cs +++ b/src/MemeVault.Application/Queries/GetUserCollections/GetUserCollectionsQueryHandler.cs @@ -1,4 +1,5 @@ using MediatR; +using MemeVault.Domain.Enums; using MemeVault.Domain.Repositories; namespace MemeVault.Application.Queries.GetUserCollections; @@ -35,7 +36,8 @@ public sealed class GetUserCollectionsQueryHandler( c.IsNsfw, c.ShareToken, countByCollection.GetValueOrDefault(c.Id, 0), - c.CreatedAt + c.CreatedAt, + c.GetRole(request.OwnerTelegramId) ?? CollectionRole.Viewer )).ToList(); } }