teldrive/utils/cron/cron.go

106 lines
2.2 KiB
Go
Raw Normal View History

2023-08-16 20:48:32 +08:00
package cron
import (
"context"
2023-09-20 03:20:44 +08:00
"database/sql/driver"
"encoding/json"
"strconv"
2023-08-16 20:48:32 +08:00
"github.com/divyam234/teldrive/database"
"github.com/divyam234/teldrive/models"
2023-09-20 03:20:44 +08:00
"github.com/divyam234/teldrive/services"
"github.com/divyam234/teldrive/utils/tgc"
2023-08-16 20:48:32 +08:00
"github.com/gotd/td/tg"
)
2023-09-20 03:20:44 +08:00
type Files []File
type File struct {
ID string `json:"id"`
Parts []models.Part `json:"parts"`
}
func (a Files) Value() (driver.Value, error) {
return json.Marshal(a)
}
func (a *Files) Scan(value interface{}) error {
if err := json.Unmarshal(value.([]byte), &a); err != nil {
return err
}
return nil
}
2023-08-16 20:48:32 +08:00
type Result struct {
2023-09-20 03:20:44 +08:00
Files Files
2023-08-16 20:48:32 +08:00
TgSession string
2023-08-24 02:40:40 +08:00
UserId int64
2023-08-16 20:48:32 +08:00
ChannelId int64
}
2023-09-20 03:20:44 +08:00
func deleteTGMessages(ctx context.Context, result Result) error {
2023-08-16 20:48:32 +08:00
2023-09-20 03:20:44 +08:00
db := database.DB
2023-08-16 20:48:32 +08:00
2023-09-20 03:20:44 +08:00
client, err := tgc.UserLogin(result.TgSession)
2023-08-16 20:48:32 +08:00
if err != nil {
return err
}
2023-09-20 03:20:44 +08:00
ids := []int{}
2023-08-16 20:48:32 +08:00
2023-09-20 03:20:44 +08:00
fileIds := []string{}
2023-08-16 20:48:32 +08:00
2023-09-20 03:20:44 +08:00
for _, file := range result.Files {
fileIds = append(fileIds, file.ID)
for _, part := range file.Parts {
ids = append(ids, int(part.ID))
}
}
err = tgc.RunWithAuth(ctx, client, "", func(ctx context.Context) error {
channel, err := services.GetChannelById(ctx, client, result.ChannelId, strconv.FormatInt(result.UserId, 10))
if err != nil {
return err
}
messageDeleteRequest := tg.ChannelsDeleteMessagesRequest{Channel: channel, ID: ids}
_, err = client.API().ChannelsDeleteMessages(ctx, &messageDeleteRequest)
if err != nil {
return err
}
return nil
})
if err == nil {
db.Where("id = any($1)", fileIds).Delete(&models.File{})
2023-08-16 20:48:32 +08:00
}
2023-09-20 03:20:44 +08:00
2023-08-16 20:48:32 +08:00
return nil
}
func FilesDeleteJob() {
db := database.DB
2023-09-20 03:20:44 +08:00
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
2023-08-16 20:48:32 +08:00
var results []Result
2023-09-20 03:20:44 +08:00
if err := db.Model(&models.File{}).
Select("JSONB_AGG(jsonb_build_object('id',files.id, 'parts',files.parts)) as files", "files.channel_id", "files.user_id", "u.tg_session").
2023-08-16 20:48:32 +08:00
Joins("left join teldrive.users as u on u.user_id = files.user_id").
2023-09-20 03:20:44 +08:00
Where("type = ?", "file").
Where("status = ?", "pending_deletion").
Group("files.channel_id").Group("files.user_id").Group("u.tg_session").
Scan(&results).Error; err != nil {
2023-08-16 20:48:32 +08:00
return
}
2023-09-20 03:20:44 +08:00
for _, row := range results {
deleteTGMessages(ctx, row)
2023-08-16 20:48:32 +08:00
}
}