Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions bot/button/handlers/gdprconfirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,3 +356,133 @@ func (h *GDPRConfirmMessagesHandler) Execute(ctx *cmdcontext.ButtonContext) {
container := utils.BuildContainerWithComponents(ctx, customisation.Orange, title, components)
ctx.Edit(command.NewMessageResponseWithComponents([]component.Component{container}))
}

type GDPRConfirmExportGuildHandler struct{}

func (h *GDPRConfirmExportGuildHandler) Matcher() matcher.Matcher {
return matcher.NewFuncMatcher(func(customId string) bool {
return strings.HasPrefix(customId, "gdpr_confirm_export_guild_")
})
}

func (h *GDPRConfirmExportGuildHandler) Properties() registry.Properties {
return registry.Properties{
Flags: registry.SumFlags(registry.DMsAllowed),
Timeout: constants.TimeoutGDPR,
}
}

func (h *GDPRConfirmExportGuildHandler) Execute(ctx *cmdcontext.ButtonContext) {
locale := utils.ExtractLanguageFromCustomId(ctx.InteractionData.CustomId)

if !gdprrelay.IsWorkerAlive(redis.Client) {
container := utils.BuildGDPRWorkerOfflineView(ctx, locale)
ctx.Edit(command.NewMessageResponseWithComponents([]component.Component{container}))
return
}

guildIds := utils.ParseGuildIds(ctx.InteractionData.CustomId)
if len(guildIds) == 0 {
ctx.ReplyRaw(customisation.Red, "Error", i18n.GetMessage(locale, i18n.GdprErrorInvalidServerId))
return
}

guildNames := utils.FetchGuildNames(ctx, guildIds)

request := gdprrelay.GDPRRequest{
Type: gdprrelay.RequestTypeExportGuild,
UserId: ctx.UserId(),
GuildIds: guildIds,
GuildNames: guildNames,
Language: locale.IsoLongCode,
InteractionToken: ctx.Interaction.Token,
InteractionGuildId: ctx.GuildId(),
ApplicationId: ctx.Worker().BotId,
}

scrambledId := sha256.New()
fmt.Fprintf(scrambledId, "%d", ctx.UserId())
id, err := dbclient.Client.GdprLogs.InsertLog(fmt.Sprintf("%x", scrambledId.Sum(nil)), "ExportGuild", "Queued")
if err != nil {
ctx.ReplyRaw(customisation.Red, "Error", i18n.GetMessage(locale, i18n.GdprErrorQueueFailed))
return
}

if err := gdprrelay.Publish(redis.Client, request, id); err != nil {
ctx.ReplyRaw(customisation.Red, "Error", i18n.GetMessage(locale, i18n.GdprErrorQueueFailed))
return
}

guildDisplays := make([]string, len(guildIds))
for i, guildId := range guildIds {
guildDisplays[i] = utils.FormatGuildDisplay(guildId, guildNames)
}

var queuedContent string
if len(guildIds) == 1 {
queuedContent = i18n.GetMessage(locale, i18n.GdprQueuedExportGuild, guildDisplays[0])
} else {
queuedContent = i18n.GetMessage(locale, i18n.GdprQueuedExportGuildMulti, strings.Join(guildDisplays, "\n* "))
}
queuedContent += i18n.GetMessage(locale, i18n.GdprQueuedFooter)

queuedComponents := []component.Component{component.BuildTextDisplay(component.TextDisplay{Content: queuedContent})}
queuedTitle := i18n.GetMessage(locale, i18n.GdprQueuedTitle)
queuedContainer := utils.BuildContainerWithComponents(ctx, customisation.Green, queuedTitle, queuedComponents)
ctx.Edit(command.NewMessageResponseWithComponents([]component.Component{queuedContainer}))
}

type GDPRConfirmExportUserHandler struct{}

func (h *GDPRConfirmExportUserHandler) Matcher() matcher.Matcher {
return matcher.NewFuncMatcher(func(customId string) bool {
return strings.HasPrefix(customId, "gdpr_confirm_export_user_")
})
}

func (h *GDPRConfirmExportUserHandler) Properties() registry.Properties {
return registry.Properties{
Flags: registry.SumFlags(registry.DMsAllowed),
Timeout: constants.TimeoutGDPR,
}
}

func (h *GDPRConfirmExportUserHandler) Execute(ctx *cmdcontext.ButtonContext) {
locale := utils.ExtractLanguageFromCustomId(ctx.InteractionData.CustomId)

if !gdprrelay.IsWorkerAlive(redis.Client) {
container := utils.BuildGDPRWorkerOfflineView(ctx, locale)
ctx.Edit(command.NewMessageResponseWithComponents([]component.Component{container}))
return
}

request := gdprrelay.GDPRRequest{
Type: gdprrelay.RequestTypeExportUser,
UserId: ctx.UserId(),
Language: locale.IsoLongCode,
InteractionToken: ctx.Interaction.Token,
InteractionGuildId: ctx.GuildId(),
ApplicationId: ctx.Worker().BotId,
}

scrambledId := sha256.New()
fmt.Fprintf(scrambledId, "%d", ctx.UserId())
id, err := dbclient.Client.GdprLogs.InsertLog(fmt.Sprintf("%x", scrambledId.Sum(nil)), "ExportUser", "Queued")
if err != nil {
ctx.ReplyRaw(customisation.Red, "Error", i18n.GetMessage(locale, i18n.GdprErrorQueueFailed))
return
}

if err := gdprrelay.Publish(redis.Client, request, id); err != nil {
ctx.ReplyRaw(customisation.Red, "Error", i18n.GetMessage(locale, i18n.GdprErrorQueueFailed))
return
}

exportContent := i18n.GetMessage(locale, i18n.GdprQueuedExportUser)
exportContent += i18n.GetMessage(locale, i18n.GdprQueuedFooter)

exportComponents := []component.Component{component.BuildTextDisplay(component.TextDisplay{Content: exportContent})}
exportTitle := i18n.GetMessage(locale, i18n.GdprQueuedTitle)
exportContainer := utils.BuildContainerWithComponents(ctx, customisation.Green, exportTitle, exportComponents)
ctx.Edit(command.NewMessageResponseWithComponents([]component.Component{exportContainer}))
}
34 changes: 30 additions & 4 deletions bot/button/handlers/gdprconfirmview.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const (
GDPRSpecificTranscripts
GDPRAllMessages
GDPRSpecificMessages
GDPRExportGuild
GDPRExportUser
)

type GDPRConfirmationData struct {
Expand Down Expand Up @@ -57,9 +59,33 @@ func buildGDPRConfirmationView(ctx interface{}, locale *i18n.Locale, data GDPRCo

case GDPRSpecificMessages:
content = i18n.GetMessage(locale, i18n.GdprConfirmSpecificMessages, data.GuildNames[0], data.TicketIdsStr)

case GDPRExportGuild:
if len(data.GuildIds) == 1 {
content = i18n.GetMessage(locale, i18n.GdprConfirmExportGuild, data.GuildNames[0])
} else {
serversList := strings.Join(data.GuildNames, "\n* ")
content = i18n.GetMessage(locale, i18n.GdprConfirmExportGuildMulti, serversList)
}

case GDPRExportUser:
content = i18n.GetMessage(locale, i18n.GdprConfirmExportUser)
}

content += i18n.GetMessage(locale, i18n.GdprConfirmWarning)
isExport := data.RequestType == GDPRExportGuild || data.RequestType == GDPRExportUser

if !isExport {
content += i18n.GetMessage(locale, i18n.GdprConfirmWarning)
}

buttonStyle := component.ButtonStyleDanger
buttonLabel := i18n.GetMessage(locale, i18n.GdprConfirmButton)
buttonEmoji := utils.BuildEmoji("⚠️")
if isExport {
buttonStyle = component.ButtonStylePrimary
buttonLabel = i18n.GetMessage(locale, i18n.GdprConfirmExportButton)
buttonEmoji = utils.BuildEmoji("📦")
}

innerComponents := []component.Component{
component.BuildTextDisplay(component.TextDisplay{
Expand All @@ -68,10 +94,10 @@ func buildGDPRConfirmationView(ctx interface{}, locale *i18n.Locale, data GDPRCo
component.BuildSeparator(component.Separator{}),
component.BuildActionRow(
component.BuildButton(component.Button{
Label: i18n.GetMessage(locale, i18n.GdprConfirmButton),
Label: buttonLabel,
CustomId: data.ConfirmButtonId,
Style: component.ButtonStyleDanger,
Emoji: utils.BuildEmoji("⚠️"),
Style: buttonStyle,
Emoji: buttonEmoji,
}),
),
}
Expand Down
141 changes: 141 additions & 0 deletions bot/button/handlers/gdprexportbutton.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package handlers

import (
"fmt"
"strings"

"github.com/TicketsBot-cloud/gdl/objects/interaction"
"github.com/TicketsBot-cloud/gdl/objects/interaction/component"
"github.com/TicketsBot-cloud/worker/bot/button"
"github.com/TicketsBot-cloud/worker/bot/button/registry"
"github.com/TicketsBot-cloud/worker/bot/button/registry/matcher"
"github.com/TicketsBot-cloud/worker/bot/command"
cmdcontext "github.com/TicketsBot-cloud/worker/bot/command/context"
"github.com/TicketsBot-cloud/worker/bot/customisation"
"github.com/TicketsBot-cloud/worker/bot/gdprrelay"
"github.com/TicketsBot-cloud/worker/bot/redis"
"github.com/TicketsBot-cloud/worker/bot/utils"
"github.com/TicketsBot-cloud/worker/i18n"
)

// GDPRExportGuildHandler handles the "Export Guild Data" button click.
// Shows a guild selection modal (same pattern as transcript deletion).
type GDPRExportGuildHandler struct{}

func (h *GDPRExportGuildHandler) Matcher() matcher.Matcher {
return matcher.NewFuncMatcher(func(customId string) bool {
return strings.HasPrefix(customId, "gdpr_export_guild_")
})
}

func (h *GDPRExportGuildHandler) Properties() registry.Properties {
return gdprProperties()
}

func (h *GDPRExportGuildHandler) Execute(ctx *cmdcontext.ButtonContext) {
locale := utils.ExtractLanguageFromCustomId(ctx.InteractionData.CustomId)

if !gdprrelay.IsWorkerAlive(redis.Client) {
container := utils.BuildGDPRWorkerOfflineView(ctx, locale)
ctx.Edit(command.NewMessageResponseWithComponents([]component.Component{container}))
return
}

guilds, err := getOwnedGuildsWithTranscripts(ctx, ctx.UserId())
if err != nil {
ctx.HandleError(err)
return
}

if len(guilds) == 0 {
ctx.ReplyRaw(customisation.Red, "Error", i18n.GetMessage(locale, i18n.GdprErrorNoServers))
return
}

var modal interaction.ModalResponseData
if len(guilds) > 25 {
modal = buildExportGuildTextModal(locale)
} else {
modal = buildExportGuildModal(locale, guilds)
}

ctx.Modal(button.ResponseModal{Data: modal})
}

func buildExportGuildModal(locale *i18n.Locale, guilds []guildInfo) interaction.ModalResponseData {
options := buildGuildSelectOptions(guilds)
minVal, maxVal := 1, len(options)
if maxVal > 25 {
maxVal = 25
}

return interaction.ModalResponseData{
CustomId: fmt.Sprintf("gdpr_modal_export_guild_%s", locale.IsoShortCode),
Title: i18n.GetMessage(locale, i18n.GdprModalExportGuildTitle),
Components: []component.Component{
component.BuildLabel(component.Label{
Label: i18n.GetMessage(locale, i18n.GdprModalSelectServers),
Component: component.BuildSelectMenu(component.SelectMenu{
CustomId: "server_ids",
MinValues: &minVal,
MaxValues: &maxVal,
Options: options,
}),
}),
},
}
}

func buildExportGuildTextModal(locale *i18n.Locale) interaction.ModalResponseData {
return interaction.ModalResponseData{
CustomId: fmt.Sprintf("gdpr_modal_export_guild_%s", locale.IsoShortCode),
Title: i18n.GetMessage(locale, i18n.GdprModalExportGuildTitle),
Components: []component.Component{
component.BuildLabel(component.Label{
Label: i18n.GetMessage(locale, i18n.GdprModalServerIdsLabel),
Component: component.BuildInputText(component.InputText{
CustomId: "server_ids",
Style: component.TextStyleParagraph,
Placeholder: utils.Ptr(i18n.GetMessage(locale, i18n.GdprModalServerIdsPlaceholder)),
Required: utils.Ptr(true),
MinLength: utils.Ptr(uint32(17)),
MaxLength: utils.Ptr(uint32(2000)),
}),
}),
},
}
}

// GDPRExportUserHandler handles the "Export My Data" button click.
// Skips guild selection and goes directly to confirmation.
type GDPRExportUserHandler struct{}

func (h *GDPRExportUserHandler) Matcher() matcher.Matcher {
return matcher.NewFuncMatcher(func(customId string) bool {
return strings.HasPrefix(customId, "gdpr_export_user_")
})
}

func (h *GDPRExportUserHandler) Properties() registry.Properties {
return gdprProperties()
}

func (h *GDPRExportUserHandler) Execute(ctx *cmdcontext.ButtonContext) {
locale := utils.ExtractLanguageFromCustomId(ctx.InteractionData.CustomId)

if !gdprrelay.IsWorkerAlive(redis.Client) {
container := utils.BuildGDPRWorkerOfflineView(ctx, locale)
ctx.Edit(command.NewMessageResponseWithComponents([]component.Component{container}))
return
}

data := GDPRConfirmationData{
RequestType: GDPRExportUser,
UserId: ctx.UserId(),
Locale: locale,
ConfirmButtonId: fmt.Sprintf("gdpr_confirm_export_user_%s", locale.IsoShortCode),
}

components := buildGDPRConfirmationView(ctx, locale, data)
ctx.Edit(command.NewMessageResponseWithComponents(components))
}
Loading