From ade944a9d666253969637717b9cd8a41d8e5692b Mon Sep 17 00:00:00 2001 From: Bohdan Horbeshko Date: Sat, 6 Dec 2025 08:11:59 -0500 Subject: [PATCH] Ping XEP-0363 link back for files uploaded in MUCs --- telegram/client.go | 4 ++++ telegram/handlers.go | 18 ++++++++++++++++++ telegram/utils.go | 24 ++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/telegram/client.go b/telegram/client.go index e9e1954..fa98135 100644 --- a/telegram/client.go +++ b/telegram/client.go @@ -148,6 +148,8 @@ type Client struct { mucCache map[int64]*MUCState + uploadingFiles map[int32]string + LastBotCmdString string XmppClientFeatures map[string]*[]string @@ -174,6 +176,7 @@ type clientLocks struct { lastMsgHashesLock sync.Mutex lastMsgIdsLock sync.RWMutex loginFinish barrier + uploadingFilesLock sync.Mutex authorizerReadLock sync.Mutex authorizerWriteLock sync.Mutex @@ -243,6 +246,7 @@ func NewClient(conf config.TelegramConfig, jid string, component *xmpp.Component editOutbox: make(map[string]string), pinOutbox: make(map[IntPair]chan int64), mucCache: make(map[int64]*MUCState), + uploadingFiles: make(map[int32]string), options: options, DelayedStatuses: make(map[int64]*DelayedStatus), lastMsgHashes: make(map[int64]uint64), diff --git a/telegram/handlers.go b/telegram/handlers.go index f63ef35..70fa8cf 100644 --- a/telegram/handlers.go +++ b/telegram/handlers.go @@ -136,6 +136,9 @@ func (c *Client) updateHandler() { case client.TypeUpdateChatPermissions: typedUpdate, _ := update.(*client.UpdateChatPermissions) c.updateChatPermissions(typedUpdate) + case client.TypeUpdateFile: + typedUpdate, _ := update.(*client.UpdateFile) + c.updateFile(typedUpdate) default: // log only handled types continue @@ -613,6 +616,21 @@ func (c *Client) updateChatPermissions(update *client.UpdateChatPermissions) { } } +func (c *Client) updateFile(update *client.UpdateFile) { + if update.File != nil && update.File.Local != nil { + // not really needed, why did I even write this then lol (TODO: maybe clean by some heur anyway) + /* c.locks.uploadingFilesLock.Lock() + if _, ok := c.uploadingFiles[update.File.Id]; ok && update.File.Local.CanBeDeleted && update.File.Local.Path != "" { + err := os.Remove(update.File.Local.Path) + if err != nil { + log.Warningf("Couldn't delete uploaded file: %v", err.Error()) + } + delete(c.uploadingFiles, update.File.Id) + } + c.locks.uploadingFilesLock.Unlock() */ + } +} + func (c *Client) tryUnlockMessageId(chatId, messageId int64) { c.MessageIdChangesLock.Lock() idsMap, ok := c.MessageIdChanges[chatId] diff --git a/telegram/utils.go b/telegram/utils.go index 04821fd..608b84a 100644 --- a/telegram/utils.go +++ b/telegram/utils.go @@ -1280,6 +1280,7 @@ func (c *Client) formatFile(file *client.File, compact bool) (string, string) { if file == nil { return "", "" } + log.Debugf("formatFile: %v %#v %#v", c.jid, file.Local, file.Remote) src, link := c.PermastoreFile(file, false) if compact { @@ -1313,6 +1314,16 @@ func (c *Client) PermastoreFile(file *client.File, clone bool) (string, string) size64 := uint64(file.Size) c.prepareDiskSpace(size64) + // detect uploading files, there's no remote id for them yet + c.locks.uploadingFilesLock.Lock() + var ok bool + link, ok = c.uploadingFiles[file.Id] + if ok && !file.Local.CanBeDeleted { + defer c.locks.uploadingFilesLock.Unlock() + return src, link + } + c.locks.uploadingFilesLock.Unlock() + basename := file.Remote.UniqueId + filepath.Ext(src) dest := c.content.Path + "/" + basename // destination path link = c.content.Link + "/" + basename // download link @@ -1792,6 +1803,8 @@ func (c *Client) ensureDownloadFile(file *client.File) *client.File { newFile, err := c.DownloadFile(file.Id, 1, true) if err == nil { return newFile + } else { + log.Errorf("Couldn't force-download file: %v", err.Error()) } } @@ -2113,6 +2126,7 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str // attach a file var file *client.InputFileLocal + link := text if c.content.Upload != "" && strings.HasPrefix(text, c.content.Upload) { response, err := http.Get(text) if err != nil { @@ -2183,6 +2197,16 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str c.returnError(returnJid, chatID, "Not sent", err, 400, isGroupchat) return nil, false } + + if file != nil { + document, ok := tgMessage.Content.(*client.MessageDocument) + if ok && document.Document != nil && document.Document.Document != nil { + c.locks.uploadingFilesLock.Lock() + c.uploadingFiles[document.Document.Document.Id] = link + c.locks.uploadingFilesLock.Unlock() + } + } + return tgMessage, false }