cleanup code
This commit is contained in:
parent
26452d0f53
commit
c06c7958e2
10 changed files with 64 additions and 14 deletions
|
@ -3,6 +3,7 @@ package core
|
|||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"sync"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
|
@ -16,7 +17,23 @@ import (
|
|||
"github.com/PaulSonOfLars/gotgbot/v2/ext"
|
||||
)
|
||||
|
||||
var InlineTasks = make(map[string]*models.DownloadContext)
|
||||
var InlineTasks sync.Map
|
||||
|
||||
func GetTask(id string) (*models.DownloadContext, bool) {
|
||||
value, ok := InlineTasks.Load(id)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
return value.(*models.DownloadContext), true
|
||||
}
|
||||
|
||||
func SetTask(id string, task *models.DownloadContext) {
|
||||
InlineTasks.Store(id, task)
|
||||
}
|
||||
|
||||
func DeleteTask(id string) {
|
||||
InlineTasks.Delete(id)
|
||||
}
|
||||
|
||||
func HandleInline(
|
||||
bot *gotgbot.Bot,
|
||||
|
@ -183,7 +200,7 @@ func StartInlineTask(
|
|||
log.Println("failed to answer inline query")
|
||||
return nil
|
||||
}
|
||||
InlineTasks[taskID] = dlCtx
|
||||
SetTask(taskID, dlCtx)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"slices"
|
||||
|
@ -21,6 +22,11 @@ func HandleDownloadRequest(
|
|||
ctx *ext.Context,
|
||||
dlCtx *models.DownloadContext,
|
||||
) error {
|
||||
taskCtx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
|
||||
defer cancel()
|
||||
|
||||
dlCtx.Context = taskCtx
|
||||
|
||||
chatID := ctx.EffectiveMessage.Chat.Id
|
||||
if dlCtx.Extractor.Type == enums.ExtractorTypeSingle {
|
||||
TypingEffect(bot, ctx, chatID)
|
||||
|
|
|
@ -198,6 +198,16 @@ func HandleErrorMessage(
|
|||
err error,
|
||||
) {
|
||||
currentError := err
|
||||
|
||||
if errors.As(currentError, context.Canceled) ||
|
||||
errors.As(currentError, context.DeadlineExceeded) {
|
||||
SendErrorMessage(
|
||||
bot, ctx,
|
||||
"download request canceled or timed out",
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
for currentError != nil {
|
||||
var botError *util.Error
|
||||
if errors.As(currentError, &botError) {
|
||||
|
|
|
@ -9,7 +9,9 @@ var helpMessage = "usage:\n" +
|
|||
"- you can add the bot to a group " +
|
||||
"to start catching sent links\n" +
|
||||
"- you can send a link to the bot privately " +
|
||||
"to download the media too\n\n" +
|
||||
"to download the media too\n" +
|
||||
"- you can use inline mode " +
|
||||
"to download media from any chat\n\n" +
|
||||
"group commands:\n" +
|
||||
"- /settings = show current settings\n" +
|
||||
"- /captions (true|false) = enable/disable descriptions\n" +
|
||||
|
|
|
@ -41,11 +41,12 @@ func InlineDownloadResultHandler(
|
|||
bot *gotgbot.Bot,
|
||||
ctx *ext.Context,
|
||||
) error {
|
||||
dlCtx, ok := core.InlineTasks[ctx.ChosenInlineResult.ResultId]
|
||||
taskID := ctx.ChosenInlineResult.ResultId
|
||||
dlCtx, ok := core.GetTask(taskID)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
defer delete(core.InlineTasks, ctx.ChosenInlineResult.ResultId)
|
||||
defer core.DeleteTask(taskID)
|
||||
|
||||
mediaChan := make(chan *models.Media, 1)
|
||||
errChan := make(chan error, 1)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue