2023-09-20 03:20:44 +08:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-07-26 23:50:26 +08:00
|
|
|
"time"
|
2023-09-20 03:20:44 +08:00
|
|
|
|
2024-08-10 02:37:56 +08:00
|
|
|
"github.com/gotd/td/telegram"
|
2023-09-20 03:20:44 +08:00
|
|
|
"github.com/gotd/td/tg"
|
|
|
|
"github.com/pkg/errors"
|
2024-08-31 23:50:17 +08:00
|
|
|
"github.com/tgdrive/teldrive/internal/cache"
|
|
|
|
"github.com/tgdrive/teldrive/internal/crypt"
|
|
|
|
"github.com/tgdrive/teldrive/internal/tgc"
|
|
|
|
"github.com/tgdrive/teldrive/pkg/models"
|
|
|
|
"github.com/tgdrive/teldrive/pkg/schemas"
|
|
|
|
"github.com/tgdrive/teldrive/pkg/types"
|
2024-02-12 04:52:41 +08:00
|
|
|
"gorm.io/gorm"
|
2023-09-20 03:20:44 +08:00
|
|
|
)
|
|
|
|
|
2024-08-10 02:37:56 +08:00
|
|
|
func getParts(ctx context.Context, client *telegram.Client, cache cache.Cacher, file *schemas.FileOutFull) ([]types.Part, error) {
|
2024-07-26 23:50:26 +08:00
|
|
|
|
2023-11-09 19:10:37 +08:00
|
|
|
parts := []types.Part{}
|
|
|
|
|
2024-08-10 02:37:56 +08:00
|
|
|
key := fmt.Sprintf("files:messages:%s", file.Id)
|
2023-11-09 19:10:37 +08:00
|
|
|
|
2024-02-12 04:52:41 +08:00
|
|
|
err := cache.Get(key, &parts)
|
2023-11-09 19:10:37 +08:00
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return parts, nil
|
|
|
|
}
|
|
|
|
|
2024-06-22 20:29:59 +08:00
|
|
|
ids := []int{}
|
|
|
|
for _, part := range file.Parts {
|
|
|
|
ids = append(ids, int(part.ID))
|
|
|
|
}
|
2024-09-20 16:21:47 +08:00
|
|
|
messages, err := tgc.GetMessages(ctx, client.API(), ids, *file.ChannelID)
|
2023-11-09 19:10:37 +08:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-05-20 04:13:26 +08:00
|
|
|
for i, message := range messages {
|
2023-09-20 03:20:44 +08:00
|
|
|
item := message.(*tg.Message)
|
|
|
|
media := item.Media.(*tg.MessageMediaDocument)
|
|
|
|
document := media.Document.(*tg.Document)
|
2024-03-20 01:01:56 +08:00
|
|
|
|
|
|
|
part := types.Part{
|
2024-06-22 20:29:59 +08:00
|
|
|
ID: file.Parts[i].ID,
|
|
|
|
Size: document.Size,
|
|
|
|
Salt: file.Parts[i].Salt,
|
2024-03-20 01:01:56 +08:00
|
|
|
}
|
|
|
|
if file.Encrypted {
|
|
|
|
part.DecryptedSize, _ = crypt.DecryptedSize(document.Size)
|
|
|
|
}
|
|
|
|
parts = append(parts, part)
|
2023-09-20 03:20:44 +08:00
|
|
|
}
|
2024-07-26 23:50:26 +08:00
|
|
|
cache.Set(key, &parts, 60*time.Minute)
|
2023-09-20 03:20:44 +08:00
|
|
|
return parts, nil
|
|
|
|
}
|
|
|
|
|
2024-07-26 23:50:26 +08:00
|
|
|
func getDefaultChannel(db *gorm.DB, cache cache.Cacher, userID int64) (int64, error) {
|
|
|
|
|
2023-11-16 23:21:35 +08:00
|
|
|
var channelId int64
|
2023-11-02 21:51:30 +08:00
|
|
|
key := fmt.Sprintf("users:channel:%d", userID)
|
2023-09-20 03:20:44 +08:00
|
|
|
|
2024-02-12 04:52:41 +08:00
|
|
|
err := cache.Get(key, &channelId)
|
2023-09-20 03:20:44 +08:00
|
|
|
|
2023-11-02 21:51:30 +08:00
|
|
|
if err == nil {
|
2023-11-16 23:21:35 +08:00
|
|
|
return channelId, nil
|
2023-11-02 21:51:30 +08:00
|
|
|
}
|
2023-09-20 03:20:44 +08:00
|
|
|
|
2023-11-02 21:51:30 +08:00
|
|
|
var channelIds []int64
|
2024-02-12 04:52:41 +08:00
|
|
|
db.Model(&models.Channel{}).Where("user_id = ?", userID).Where("selected = ?", true).
|
2023-11-02 21:51:30 +08:00
|
|
|
Pluck("channel_id", &channelIds)
|
|
|
|
|
|
|
|
if len(channelIds) == 1 {
|
2023-11-16 23:21:35 +08:00
|
|
|
channelId = channelIds[0]
|
2024-02-12 04:52:41 +08:00
|
|
|
cache.Set(key, channelId, 0)
|
2023-09-20 03:20:44 +08:00
|
|
|
}
|
|
|
|
|
2023-11-16 23:21:35 +08:00
|
|
|
if channelId == 0 {
|
|
|
|
return channelId, errors.New("default channel not set")
|
2023-09-20 03:20:44 +08:00
|
|
|
}
|
|
|
|
|
2023-11-16 23:21:35 +08:00
|
|
|
return channelId, nil
|
2023-09-20 03:20:44 +08:00
|
|
|
}
|
|
|
|
|
2024-07-26 23:50:26 +08:00
|
|
|
func getBotsToken(db *gorm.DB, cache cache.Cacher, userID, channelId int64) ([]string, error) {
|
2023-09-20 03:20:44 +08:00
|
|
|
var bots []string
|
|
|
|
|
2023-11-02 21:51:30 +08:00
|
|
|
key := fmt.Sprintf("users:bots:%d:%d", userID, channelId)
|
2023-09-27 19:38:33 +08:00
|
|
|
|
2024-02-12 04:52:41 +08:00
|
|
|
err := cache.Get(key, &bots)
|
2023-09-20 03:20:44 +08:00
|
|
|
|
2023-11-02 21:51:30 +08:00
|
|
|
if err == nil {
|
|
|
|
return bots, nil
|
2023-09-20 03:20:44 +08:00
|
|
|
}
|
|
|
|
|
2024-02-12 04:52:41 +08:00
|
|
|
if err := db.Model(&models.Bot{}).Where("user_id = ?", userID).
|
2023-11-02 21:51:30 +08:00
|
|
|
Where("channel_id = ?", channelId).Pluck("token", &bots).Error; err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-12 04:52:41 +08:00
|
|
|
cache.Set(key, &bots, 0)
|
2023-09-20 03:20:44 +08:00
|
|
|
return bots, nil
|
|
|
|
|
|
|
|
}
|