teldrive/utils/cron/cron.go

73 lines
1.7 KiB
Go
Raw Normal View History

2023-08-16 20:48:32 +08:00
package cron
import (
"context"
"fmt"
"github.com/divyam234/teldrive/cache"
"github.com/divyam234/teldrive/database"
"github.com/divyam234/teldrive/models"
"github.com/divyam234/teldrive/utils"
"github.com/gotd/td/tg"
)
type Result struct {
ID string
Parts models.Parts
TgSession string
2023-08-24 02:40:40 +08:00
UserId int64
2023-08-16 20:48:32 +08:00
ChannelId int64
}
func deleteTGMessage(ctx context.Context, client *tg.Client, result Result) error {
ids := make([]int, len(result.Parts))
for i, part := range result.Parts {
ids[i] = int(part.ID)
}
res, err := cache.CachedFunction(utils.GetChannelById, fmt.Sprintf("channels:%d", result.ChannelId))(ctx, client, result.ChannelId)
if err != nil {
return err
}
channel := res.(*tg.Channel)
messageDeleteRequest := tg.ChannelsDeleteMessagesRequest{Channel: &tg.InputChannel{ChannelID: result.ChannelId, AccessHash: channel.AccessHash},
ID: ids}
_, err = client.ChannelsDeleteMessages(ctx, &messageDeleteRequest)
if err != nil {
return err
}
return nil
}
func FilesDeleteJob() {
db := database.DB
ctx := context.Background()
var results []Result
if err := db.Model(&models.File{}).Select("files.id", "files.parts", "files.user_id", "files.channel_id", "u.tg_session").
Joins("left join teldrive.users as u on u.user_id = files.user_id").
Where("status = ?", "pending_deletion").Scan(&results).Error; err != nil {
return
}
for _, file := range results {
2023-08-24 02:40:40 +08:00
client, err := utils.GetAuthClient(ctx, file.TgSession, file.UserId)
2023-08-16 20:48:32 +08:00
if err != nil {
break
}
2023-08-24 02:40:40 +08:00
err = client.Run(ctx, func(ctx context.Context) error {
err = deleteTGMessage(ctx, client.API(), file)
return err
})
2023-08-16 20:48:32 +08:00
if err == nil {
db.Where("id = ?", file.ID).Delete(&models.File{})
}
}
}