teldrive/utils/cron/cron.go

194 lines
4.1 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-11-01 01:46:52 +08:00
"time"
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-11-01 01:46:52 +08:00
type UpFiles []UpFile
type UpFile struct {
ID string `json:"id"`
PartID int `json:"partId"`
}
func (a UpFiles) Value() (driver.Value, error) {
return json.Marshal(a)
}
func (a *UpFiles) 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-11-01 01:46:52 +08:00
type UploadResult struct {
Files UpFiles
TgSession string
UserId int64
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
}
2023-11-01 01:46:52 +08:00
func cleanUploadsMessages(ctx context.Context, result UploadResult) error {
db := database.DB
client, err := tgc.UserLogin(result.TgSession)
if err != nil {
return err
}
ids := []int{}
fileIds := []string{}
for _, file := range result.Files {
fileIds = append(fileIds, file.ID)
ids = append(ids, file.PartID)
}
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.Upload{})
}
return nil
}
2023-08-16 20:48:32 +08:00
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
}
}
2023-11-01 01:46:52 +08:00
func UploadCleanJob() {
db := database.DB
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var upResults []UploadResult
if err := db.Model(&models.Upload{}).
Select("JSONB_AGG(jsonb_build_object('id',uploads.id,'partId',uploads.part_id)) as files", "uploads.channel_id", "uploads.user_id", "u.tg_session").
Joins("left join teldrive.users as u on u.user_id = uploads.user_id").
Where("uploads.created_at < ?", time.Now().UTC().AddDate(0, 0, -15)).
Group("uploads.channel_id").Group("uploads.user_id").Group("u.tg_session").
Scan(&upResults).Error; err != nil {
return
}
for _, row := range upResults {
cleanUploadsMessages(ctx, row)
}
}