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"
|
2023-11-06 02:19:58 +08:00
|
|
|
"github.com/divyam234/teldrive/utils"
|
2023-09-20 03:20:44 +08:00
|
|
|
"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-11-02 21:51:30 +08:00
|
|
|
Session 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
|
2023-11-02 21:51:30 +08:00
|
|
|
Session string
|
2023-11-01 01:46:52 +08:00
|
|
|
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-11-25 12:31:29 +08:00
|
|
|
client, err := tgc.UserLogin(ctx, result.Session)
|
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
|
|
|
|
|
2023-11-25 12:31:29 +08:00
|
|
|
client, err := tgc.UserLogin(ctx, result.Session)
|
2023-11-01 01:46:52 +08:00
|
|
|
|
|
|
|
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{}).
|
2023-11-02 21:51:30 +08:00
|
|
|
Select("JSONB_AGG(jsonb_build_object('id',files.id, 'parts',files.parts)) as files", "files.channel_id", "files.user_id", "s.session").
|
2023-08-16 20:48:32 +08:00
|
|
|
Joins("left join teldrive.users as u on u.user_id = files.user_id").
|
2023-11-02 21:51:30 +08:00
|
|
|
Joins("left join (select * from teldrive.sessions order by created_at desc limit 1) as s on u.user_id = s.user_id").
|
2023-09-20 03:20:44 +08:00
|
|
|
Where("type = ?", "file").
|
|
|
|
Where("status = ?", "pending_deletion").
|
2023-11-02 21:51:30 +08:00
|
|
|
Group("files.channel_id").Group("files.user_id").Group("s.session").
|
2023-09-20 03:20:44 +08:00
|
|
|
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())
|
2023-11-06 02:19:58 +08:00
|
|
|
config := utils.GetConfig()
|
2023-11-01 01:46:52 +08:00
|
|
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
var upResults []UploadResult
|
|
|
|
if err := db.Model(&models.Upload{}).
|
2023-11-02 21:51:30 +08:00
|
|
|
Select("JSONB_AGG(jsonb_build_object('id',uploads.id,'partId',uploads.part_id)) as files", "uploads.channel_id", "uploads.user_id", "s.session").
|
2023-11-01 01:46:52 +08:00
|
|
|
Joins("left join teldrive.users as u on u.user_id = uploads.user_id").
|
2023-11-02 21:51:30 +08:00
|
|
|
Joins("left join (select * from teldrive.sessions order by created_at desc limit 1) as s on s.user_id = uploads.user_id").
|
2023-11-06 02:19:58 +08:00
|
|
|
Where("uploads.created_at < ?", time.Now().UTC().AddDate(0, 0, -config.UploadRetention)).
|
2023-11-02 21:51:30 +08:00
|
|
|
Group("uploads.channel_id").Group("uploads.user_id").Group("s.session").
|
2023-11-01 01:46:52 +08:00
|
|
|
Scan(&upResults).Error; err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, row := range upResults {
|
|
|
|
cleanUploadsMessages(ctx, row)
|
|
|
|
}
|
|
|
|
}
|