# Chat Upload Bug

**URL:** https://meta.discourse.org/t/chat-upload-bug/379253
**Category:** Bug
**Tags:** chat
**Created:** [August 19, 2025, 8:31am UTC](https://meta.discourse.org/t/chat-upload-bug/379253 "2025-08-19T08:31:15Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![wings](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/wings/32/331381_2.png) [@wings](https://meta.discourse.org/u/wings)
#### Post date: [August 19, 2025, 8:31am UTC](https://meta.discourse.org/t/chat-upload-bug/379253/1 "2025-08-19T08:31:15Z")

</div>

Hi, I want to report a problem I found when uploading files or images in **chat DMs**.

- When **User A** uploads **File A** in a post or chat, it works fine and the file is registered correctly.

- But when **User B** downloads **File A** and then tries to upload it again in another chat:

However, if **User A** re-uploads the same file again, it works fine and the file is included in the message.

My question:  
Is this the intended behavior, where only the original uploader can re-use an uploaded file? Or should other users also be able to upload and send the same file in chat?

 ![image](https://global.discourse-cdn.com/meta/original/4X/b/4/5/b4541f403d47ab4406dad5fa1687bb96c67eff92.png)

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [August 20, 2025, 1:12am UTC](https://meta.discourse.org/t/chat-upload-bug/379253/3 "2025-08-20T01:12:03Z")

</div>

Yeah this sounds like a bit of a glitch, someone will have a look over the next few weeks, happy to put a PR welcome on this in the interim.

---

<div class="post-metadata">

### Author: ![clechasseur](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/clechasseur/32/517802_2.png) [@clechasseur](https://meta.discourse.org/u/clechasseur)
#### Post date: [August 26, 2025, 5:30am UTC](https://meta.discourse.org/t/chat-upload-bug/379253/5 "2025-08-26T05:30:26Z")

</div>

It took me a while to dig through this, but I think I find the cause. I think it has to do with [this code](https://github.com/discourse/discourse/blob/d2516cbdcdebe799d710fcb93a53f3298365dc5a/plugins/chat/app/services/chat/create_message.rb#L151-L154):

```ruby
def fetch_uploads(params:, guardian:)
  return [] if !SiteSetting.chat_allow_uploads
  guardian.user.uploads.where(id: params.upload_ids) # Specifically, here
end

```

When a new chat message is created, this is called to fetch the uploads to attach to the message. Possibly to ensure ownership, the method goes through the Guardian’s user to fetch the uploads so as to only allow uploads belonging to that user.

The problem is that uploads are deduplicated, as seen [here](https://github.com/discourse/discourse/blob/d2516cbdcdebe799d710fcb93a53f3298365dc5a/lib/upload_creator.rb#L131-L145):

```ruby
# do we already have that upload?
@upload = Upload.find_by(sha1: sha1)

# ...

# return the previous upload if any
if @upload
  add_metadata!
  UserUpload.find_or_create_by!(user_id: user_id, upload_id: @upload.id) if user_id
  return @upload
end

```

I _think_ that a possible fix would be to go through the `UserUpload`s instead of just considering the uploads belonging to the user. `UserUpload` links uploads to multiple users, which seems to be what we need. I’m not yet 100% sure how to do it properly; it’s getting late, so I’ll tuck in, but if no one else fixes it, I’ll try to come back later and work on a PR. 🙂

---

<div class="post-metadata">

### Author: ![sam](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/sam/32/102149_2.png) [@sam](https://meta.discourse.org/u/sam)
#### Post date: [August 26, 2025, 6:27am UTC](https://meta.discourse.org/t/chat-upload-bug/379253/6 "2025-08-26T06:27:20Z")

</div>

This is a fantastic catch and spot on. 🤗

Uploads have a user\_id, but that is just the **original** user that created the upload.

Converting that code to:

```ruby
Upload.where(id: params.upload_ids).joins(:user_uploads).where(user_uploads: { user_id: guardian.user.id })

```

will do the trick

Can you send a PR through!

---

<div class="post-metadata">

### Author: ![clechasseur](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/clechasseur/32/517802_2.png) [@clechasseur](https://meta.discourse.org/u/clechasseur)
#### Post date: [August 26, 2025, 9:51pm UTC](https://meta.discourse.org/t/chat-upload-bug/379253/7 "2025-08-26T21:51:44Z")

</div>

Thanks, I wasn’t sure how to best build that query (I’m fairly new here 🙂 ). I’ll work on a PR.

---

<div class="post-metadata">

### Author: ![clechasseur](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/clechasseur/32/517802_2.png) [@clechasseur](https://meta.discourse.org/u/clechasseur)
#### Post date: [August 28, 2025, 2:55am UTC](https://meta.discourse.org/t/chat-upload-bug/379253/8 "2025-08-28T02:55:23Z")

</div>

Ok, I finally finished the PR. Sorry for the delay, life issues intruded.

[FIX: in chat messages, filter uploads by `UserUpload`, not by `Upload.user` by clechasseur · Pull Request #34596 · discourse/discourse](https://github.com/discourse/discourse/pull/34596)

---

<div class="post-metadata">

### Author: ![clechasseur](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/clechasseur/32/517802_2.png) [@clechasseur](https://meta.discourse.org/u/clechasseur)
#### Post date: [August 28, 2025, 2:58am UTC](https://meta.discourse.org/t/chat-upload-bug/379253/9 "2025-08-28T02:58:51Z")

</div>

I see that [some of the tests have failed](https://github.com/discourse/discourse/actions/runs/17284354117/job/49058548104?pr=34596), but they’re not tests I’ve touched. I’ve been having issues running the tests in these files locally as well - I got the same _duplicate key_ errors I see here… I’m not sure if it’s me that broke something or if the tests are sometimes flaky…

---

<div class="post-metadata">

### Author: ![chapoi](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/chapoi/32/537252_2.png) [@chapoi](https://meta.discourse.org/u/chapoi)
#### Post date: [August 28, 2025, 8:46am UTC](https://meta.discourse.org/t/chat-upload-bug/379253/10 "2025-08-28T08:46:42Z")

</div>

> [@clechasseur](#):
>
> I’m not sure if it’s me that broke something or if the tests are sometimes flaky…

Good rule of thumb is to re-run the failed tests at least thrice and only start panicking if they fail the third time 😜

That being said, @j.jaffeux can maybe help?

---

<div class="post-metadata">

### Author: ![martin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/martin/32/491371_2.png) [@martin](https://meta.discourse.org/u/martin)
#### Post date: [August 28, 2025, 9:29am UTC](https://meta.discourse.org/t/chat-upload-bug/379253/11 "2025-08-28T09:29:29Z")

</div>

Yeah these are just flaky and other random errors, your changes are fine. Re-running the PR tests, if it passes or just fails with the same random sequence error will approve + merge.

---

<div class="post-metadata">

### Author: ![clechasseur](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/clechasseur/32/517802_2.png) [@clechasseur](https://meta.discourse.org/u/clechasseur)
#### Post date: [August 28, 2025, 12:13pm UTC](https://meta.discourse.org/t/chat-upload-bug/379253/12 "2025-08-28T12:13:51Z")

</div>

> [@chapoi](#):
>
> Good rule of thumb is to re-run the failed tests at least thrice and only start panicking if they fail the third time 😜

I’m too much of a horror fan not to know what happens when you do something thrice 😅

---

<div class="post-metadata">

### Author: ![martin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/martin/32/491371_2.png) [@martin](https://meta.discourse.org/u/martin)
#### Post date: [September 1, 2025, 2:34pm UTC](https://meta.discourse.org/t/chat-upload-bug/379253/13 "2025-09-01T14:34:02Z")

</div>

This PR has been merged, thanks again for the contrib @clechasseur 🚀

---

<div class="post-metadata">

### Author: ![martin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/martin/32/491371_2.png) [@martin](https://meta.discourse.org/u/martin)
#### Post date: [September 1, 2025, 2:34pm UTC](https://meta.discourse.org/t/chat-upload-bug/379253/14 "2025-09-01T14:34:04Z")

</div>


