Ping XEP-0363 link back for files uploaded in MUCs

This commit is contained in:
Bohdan Horbeshko 2025-12-06 08:11:59 -05:00
parent cf2183e9ca
commit ade944a9d6
3 changed files with 46 additions and 0 deletions

View file

@ -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),

View file

@ -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]

View file

@ -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
}