Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attachments v2 #1813

Open
wants to merge 258 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
258 commits
Select commit Hold shift + click to select a range
6fabe1a
WIP attachments permissions
rafalp Jan 7, 2025
f4c7c72
WIP add noscript attachment upload to posting forms
rafalp Jan 7, 2025
3fb9b76
Add post attachments limit
rafalp Jan 7, 2025
a2d5d3f
Attachment upload validator
rafalp Jan 7, 2025
98c5689
Super basic start thread with attachments
rafalp Jan 7, 2025
2b80ecb
Group attachments in editor UI
rafalp Jan 7, 2025
2fafba5
Clear validation errors when user clicks on preview or upload buttons
rafalp Jan 7, 2025
2aeff38
WIP attachments lists
rafalp Jan 7, 2025
9fa9881
Small CSS simplification
rafalp Jan 7, 2025
134e9fa
WIP editor attachments list
rafalp Jan 7, 2025
646ba74
First CSS pass on uploaded files grid
rafalp Jan 7, 2025
8aaf0ad
Remove upload option from toolbar
rafalp Jan 8, 2025
f71cfa8
WIP attachments admin tests
rafalp Jan 8, 2025
ef09e10
Add tests for admin attachments list
rafalp Jan 8, 2025
b1c6501
Add delete attachment admin tests
rafalp Jan 8, 2025
de03158
Delete old attachments admin
rafalp Jan 8, 2025
a121004
Add attachment image settings
rafalp Jan 8, 2025
4b2f19b
Bump attachment limit per post to 50
rafalp Jan 8, 2025
df13c44
WIP store attachment image
rafalp Jan 8, 2025
09ac410
Fix build
rafalp Jan 8, 2025
d7e88c0
Fix unbound var error
rafalp Jan 8, 2025
452f779
Plugin data retrieval for attachments
rafalp Jan 9, 2025
000e24e
Add tests for attachment storage
rafalp Jan 9, 2025
5d0b414
Format with black
rafalp Jan 9, 2025
54f83d1
WIP attachment model tests
rafalp Jan 9, 2025
ee842b3
Add tests for attachment model
rafalp Jan 9, 2025
ec296f2
Add settings for limiting upload types and uploads in private threads
rafalp Jan 10, 2025
42f1880
Add option for disabling uploads in private threads only
rafalp Jan 10, 2025
cefef80
Tests for get_attachments_permissions utils
rafalp Jan 10, 2025
cb41f3b
WIP post form tests
rafalp Jan 10, 2025
ec03d94
More tests for PostForm
rafalp Jan 10, 2025
385261e
Test PostForm.show_attachments_upload
rafalp Jan 10, 2025
abb6469
Test PostForm attachments limits props
rafalp Jan 10, 2025
0b42a1d
Add tests for remaining post form attachments props and methods, minu…
rafalp Jan 11, 2025
5774038
Add attachments tests to PostForm
rafalp Jan 11, 2025
9c82660
Posting formsets tests: start thread formsets
rafalp Jan 11, 2025
5c0a41b
Fix tests
rafalp Jan 11, 2025
3ec0c6d
Test half of formsets for attachments setup
rafalp Jan 11, 2025
74765db
Test posting formsets setup for attachments
rafalp Jan 11, 2025
2d439ee
Edit formsets attachment loading tests
rafalp Jan 11, 2025
86a4d1e
Test in edit formsets skipping deleted attachments
rafalp Jan 11, 2025
446f05d
Test attachment handling in start thread state
rafalp Jan 11, 2025
329aff3
Add attachments to reply thread state
rafalp Jan 11, 2025
c3e02c4
Utils for deleting attachments
rafalp Jan 12, 2025
43363d6
Add delete attachments to posting states
rafalp Jan 12, 2025
a83e79f
Fix empty thumbnails and too long file storage paths
rafalp Jan 12, 2025
263ce0f
Redesign admin attachments list
rafalp Jan 12, 2025
6bea144
Redesign admin attachments list to a table
rafalp Jan 12, 2025
cd43d8a
New clearattachments command, change admin attachments filter
rafalp Jan 12, 2025
f6ce7c8
Rename location column to Thread on admin attachments list
rafalp Jan 12, 2025
c2fa694
Add testutil for clearing test attachment files
rafalp Jan 13, 2025
20ca26d
Commit missing file
rafalp Jan 13, 2025
8f98ff1
Rename clear_attachments fixture to teardown_attachments
rafalp Jan 13, 2025
64ee7ce
Add delete_category util, move category deletion to it, WIP tests
rafalp Jan 14, 2025
4993ac8
Commit delete categories hook and delete category tests
rafalp Jan 14, 2025
3bc4ccd
WIP tests for delete_category utils
rafalp Jan 14, 2025
1e78a31
Convert test cases for delete_category to dict
rafalp Jan 14, 2025
bd536cc
Simplify category tree healing algorithm, use bulk update in it for s…
rafalp Jan 15, 2025
f6ba3c6
Further simplify healing categories tree
rafalp Jan 15, 2025
5b6af08
Fix build
rafalp Jan 15, 2025
1748500
Add delete_category util
rafalp Jan 15, 2025
4e4f09a
Export attachments, delete them with user
rafalp Jan 15, 2025
63c865d
Quick pass on adding file upload support to all posting forms
rafalp Jan 15, 2025
a15cf0b
Fix: #1836
rafalp Jan 15, 2025
7666991
Tweak editor textarea height sizes
rafalp Jan 15, 2025
e883391
Fix editor responsive toolbar size calculation broken by upload button
rafalp Jan 15, 2025
d3d41b9
Clean attachment's filename on storage
rafalp Jan 16, 2025
4bc826a
WIP attachment download view
rafalp Jan 16, 2025
9d5ed26
WIP download attachment view
rafalp Jan 16, 2025
0820fd3
WIP download attachment permissions tests
rafalp Jan 16, 2025
c16125a
Test check_download_attachment_permission
rafalp Jan 16, 2025
7de21d8
WIP: attachments backend cleanup
rafalp Jan 17, 2025
afbfc32
Fix dev script help screen
rafalp Jan 17, 2025
b7ff607
Fix attachment filename handling
rafalp Jan 17, 2025
74e9407
Fix attachments admin and make it use attachment view
rafalp Jan 17, 2025
6b6cc9f
Fix tests after attachments cleanup
rafalp Jan 17, 2025
a3365ed
Attachment servers (without tests)
rafalp Jan 17, 2025
3568398
Fix posting tests
rafalp Jan 17, 2025
431b7cc
Write tests for django attachment servers
rafalp Jan 17, 2025
ee6fdad
Test serve attachment utils
rafalp Jan 17, 2025
a9aadc1
Use django redirect server for attachments in test runner
rafalp Jan 17, 2025
0027a23
Heal categories tree if child has greater parent id than parent category
rafalp Jan 17, 2025
22a4040
Re-use opened image's format in image save
rafalp Jan 17, 2025
85f65b0
Make attachment media paths harder to guess
rafalp Jan 17, 2025
646995a
WIP: migration updating attachments urls
rafalp Jan 18, 2025
ee0cc46
WIP posts migration, wip tests for attachments views
rafalp Jan 18, 2025
22f79ba
Attachments download views tests
rafalp Jan 18, 2025
19eff67
Tests tweaks
rafalp Jan 18, 2025
20d7cb5
WIP CategoryRelationsFactory util
rafalp Jan 18, 2025
23af5fe
Complete CategoryRelationsFactory util
rafalp Jan 18, 2025
1b22f6d
Update delete_category tests to use new CategoryRelationsFactory
rafalp Jan 18, 2025
396cc06
Implement attachment details view (without template and plugin outlets)
rafalp Jan 18, 2025
987ea62
WIP attachment details page
rafalp Jan 18, 2025
abb29c5
Attachment detail pages
rafalp Jan 19, 2025
96f1f73
Format code with black, update docs
rafalp Jan 19, 2025
4cb8a99
Attachment details css tweaks, add webm file support
rafalp Jan 19, 2025
db53ea5
Add thread to attachment's details
rafalp Jan 19, 2025
3b30dce
Don't order outlets list
rafalp Jan 20, 2025
660fc50
Fix build
rafalp Jan 20, 2025
641ff9e
Add new outlets to attachment page
rafalp Jan 20, 2025
a4d16be
Rename temporary attachments to unused attachments
rafalp Jan 20, 2025
93a21f5
Add more migrations for attachments
rafalp Jan 20, 2025
c8cb8ba
WIP attachments limits and attachments list in account settings
rafalp Jan 20, 2025
909939b
WIP attachments storage limits
rafalp Jan 20, 2025
c09df4c
Fix invalid condition
rafalp Jan 20, 2025
ea2b1ba
Simplify condition
rafalp Jan 20, 2025
293e36a
Test storage constraints
rafalp Jan 21, 2025
be906ad
Add sanity check to group perms form
rafalp Jan 21, 2025
8da3fa2
Write tests for storage left validation in attachment validator
rafalp Jan 21, 2025
fc213eb
Test storage validation in posting form
rafalp Jan 21, 2025
a932e87
Check delete attachment permission util with some tests
rafalp Jan 21, 2025
daaddc8
Write tests for deleting attachments permission
rafalp Jan 21, 2025
c5c3642
WIP: attachment delete feature
rafalp Jan 21, 2025
ae9ce15
WIP delete attachment view
rafalp Jan 21, 2025
03044d2
Format source with prettier
rafalp Jan 21, 2025
01295d7
Support noscript as toolbar item, fix delete button responsiveness on…
rafalp Jan 21, 2025
703ebc1
Split attachment delete to a separate template
rafalp Jan 21, 2025
bec6f0f
Cleanup referer urls on attachment details page
rafalp Jan 21, 2025
9c7ece4
Commit files
rafalp Jan 21, 2025
62ea00a
Fix editor modal not found error and add attachment delete confirmati…
rafalp Jan 21, 2025
69e30ec
Format code with black
rafalp Jan 21, 2025
0da9d41
Add tests to delete attachment view
rafalp Jan 21, 2025
430c97f
Add extra test for attachment delete permission failing anonymous users
rafalp Jan 21, 2025
870f9af
Add all supported attachment file types list to admin
rafalp Jan 22, 2025
056386c
Simplify search attachments by type link
rafalp Jan 22, 2025
0434706
Fix multiple dots in filename crashing upload
rafalp Jan 22, 2025
ed02e38
Add attachments available space check and update totals on admin dash…
rafalp Jan 22, 2025
9050ed0
Move some admin templates around
rafalp Jan 22, 2025
5d1dd5b
Add limitation for attachment types to accept attribute str
rafalp Jan 22, 2025
e37f8cb
WIP: validate uploaded files extensions against restrictions list
rafalp Jan 22, 2025
9cc3d2f
Test that post form respects custom attachment types restrictions
rafalp Jan 22, 2025
7d1e612
Test required/disallowed extension validators
rafalp Jan 22, 2025
ad440b7
Display attachments names on attachments list in account settings
rafalp Jan 22, 2025
ede6853
Add thread_ prefix to thread's posts permissions
rafalp Jan 22, 2025
da3fb38
Rename thread post permissions to use thread_post instead
rafalp Jan 22, 2025
4b72e45
Regen dev docs
rafalp Jan 22, 2025
bf5ca27
WIP tests for check_see_post_permission
rafalp Jan 22, 2025
341ed21
Write tests for check_see_post_permissions
rafalp Jan 23, 2025
3aeaf1b
Add check_permissions util
rafalp Jan 23, 2025
f9a16cc
Add tests checking if attachment post is visible on post details page
rafalp Jan 23, 2025
8e79b5f
Rename participants_ids property to private_thread_member_ids, add pr…
rafalp Jan 23, 2025
2ee966d
WIP attachments list in account settings
rafalp Jan 23, 2025
c277b2a
Attachments list in account settings design
rafalp Jan 23, 2025
09a5567
Tweak attachment title
rafalp Jan 23, 2025
74c52f8
Design storage usage section
rafalp Jan 24, 2025
fb861b2
Rename template
rafalp Jan 24, 2025
30a9f3d
Redesign help text for attachment storage usage
rafalp Jan 24, 2025
15f7da5
WIP for account settings attachments page tests
rafalp Jan 24, 2025
0c08dc7
Add tests for possible attachments and permissions combination
rafalp Jan 24, 2025
ab14ba7
Test another case on account attachments
rafalp Jan 24, 2025
0f0a574
Test account attachments
rafalp Jan 24, 2025
86184ff
Fix tests
rafalp Jan 24, 2025
5928cdd
Untie deleted attachments from uploaders, freeing attachment space
rafalp Jan 24, 2025
178853a
Add tests for broken attachments to admin
rafalp Jan 24, 2025
1cb1d7b
Fix posting states tests
rafalp Jan 24, 2025
2adef2f
Display broken attachments, let admins search for them
rafalp Jan 24, 2025
82c7cc7
Replace image field on new attachment model with standard file field
rafalp Jan 25, 2025
0693e14
Move shared attachment upload logic to handle_attachments_upload
rafalp Jan 25, 2025
309ba62
Test upload attachments view
rafalp Jan 25, 2025
95bfcf1
Finish attachments upload view
rafalp Jan 25, 2025
12c1767
Read attachment size limit from user permissions directly instead of …
rafalp Jan 25, 2025
6af1dda
Add parser markdown
rafalp Jan 25, 2025
996bb89
Gather parsed message attachments IDs in metadata
rafalp Jan 25, 2025
c774647
Add <attachment> markup to parser
rafalp Jan 25, 2025
b4b8726
Add insert attachment controls
rafalp Jan 26, 2025
784d0f2
Format HTML
rafalp Jan 26, 2025
1736741
Dataloader for feed first pass
rafalp Jan 26, 2025
3af8633
First pass on embedded attachments
rafalp Jan 27, 2025
1e71f28
WIP attachments post processor
rafalp Jan 27, 2025
95e6b40
Extract attachments from unsupported blocks up
rafalp Jan 27, 2025
5512192
WIP more work on embedded attachments
rafalp Jan 27, 2025
e220262
Embed attachments info buttons
rafalp Jan 27, 2025
b40bc45
WIP prefetch posts related objects util
rafalp Jan 27, 2025
f796d11
WIP prefetch posts related objects
rafalp Jan 27, 2025
7052bb0
Fill in post prefetch operations, fix threads list queries count
rafalp Jan 27, 2025
53c78df
Small cleanup
rafalp Jan 27, 2025
c4fd4d9
Add new hook for posts feed relation
rafalp Jan 28, 2025
6d47522
Format code with black, fix posts feed tests
rafalp Jan 28, 2025
98cad32
Unfuck parser tests
rafalp Jan 28, 2025
5d5e202
Misc parser tweaks
rafalp Jan 28, 2025
6cbfb9c
Small tweaks on attachments
rafalp Jan 28, 2025
d9cbd5d
Add limit for attachments prefetch
rafalp Jan 28, 2025
e1ac581
Don't allow attachments in lists items
rafalp Jan 28, 2025
ae4bea6
Commit missing template
rafalp Jan 28, 2025
ab80bee
Fix displaying embed link attachments
rafalp Jan 28, 2025
667d027
Lazy load attachment images
rafalp Jan 28, 2025
6d1b1c8
WIP prefetch posts related objects util tests
rafalp Jan 29, 2025
9756db8
Write tests for prefetch posts related objects
rafalp Jan 29, 2025
0e3c7d0
Add extra assert
rafalp Jan 29, 2025
8509670
Delete secret attr from Attachment model, use IDs instead
rafalp Jan 29, 2025
342f7a9
Insert attachments in editor
rafalp Jan 29, 2025
121855c
Test replace_rich_text_tokens
rafalp Jan 29, 2025
9e9ef8e
Add tests for posts feed prefetching rich text data
rafalp Jan 29, 2025
e7fc402
Fix editors textarea maxlength being none
rafalp Jan 29, 2025
469f96a
Temp fix for attachments form trying to display broken attachments
rafalp Jan 29, 2025
0bc1790
Embed attachments in posting previews
rafalp Jan 29, 2025
ce4e409
Add ratio property to attachment model
rafalp Jan 29, 2025
4307a1b
Add thumbnail dimensions to Attachment model
rafalp Jan 29, 2025
6abf551
Format code with black
rafalp Jan 29, 2025
da0c060
Fix failing test
rafalp Jan 29, 2025
261de05
Populate thumbnail dimensions on upload
rafalp Jan 30, 2025
1e0df5b
WIP generate attachments command
rafalp Jan 30, 2025
01507cc
Delete migrations replaced by management command
rafalp Jan 30, 2025
7460c86
Finish regenerate attachments thumbnails command
rafalp Jan 30, 2025
79a99cf
Delete old richtext css styles, set dimensions on attached images whe…
rafalp Jan 30, 2025
1677c48
Wrap attachments in attachment-group AST
rafalp Jan 30, 2025
8b884c7
Use attachment-group container for spacing attachments evenly
rafalp Jan 30, 2025
7a0a95a
Fix tests
rafalp Jan 30, 2025
6558e49
Placeholder post's remaining attachments list
rafalp Jan 30, 2025
0348b6c
Tweak remaining attachments lists styles
rafalp Jan 30, 2025
eab8bf7
Basic untested delete attachments from editor feature
rafalp Jan 31, 2025
bdae344
Add link to attachments settings to attachment upload form
rafalp Jan 31, 2025
6e24ffc
Show exceeded space in account attachments
rafalp Jan 31, 2025
e6bc1ae
Fix broken tests
rafalp Jan 31, 2025
02b8a52
Add tests for formset methods
rafalp Feb 1, 2025
03f79a2
Add tests for PostForm.is_request_upload
rafalp Feb 1, 2025
7f49209
Cleanup PostForm tests
rafalp Feb 1, 2025
f8d534a
Test PostForm.set_deleted_attachments
rafalp Feb 1, 2025
5f7c617
Remove unused field from AttachmentsPermissions, test form attachment…
rafalp Feb 1, 2025
4bd82cb
Add hard limit to posts attachments count
rafalp Feb 1, 2025
dd51416
Validate post's final attachments number in PostForm.clean
rafalp Feb 1, 2025
256d5fa
Remove AttachmentsPermissions util from PostForm
rafalp Feb 1, 2025
db1f53d
WIP remove AttachmentsPermissions util
rafalp Feb 1, 2025
f86dff3
max_attachments -> attachments_limit
rafalp Feb 1, 2025
752f43d
WIP change attachments permissions check
rafalp Feb 1, 2025
dc0d4b6
Add plugin hooks for new upload attachment permission checks
rafalp Feb 1, 2025
565b265
Add tests for can upload attachments checks
rafalp Feb 1, 2025
cbde44c
Display non-media attachments under media, support octet stream conte…
rafalp Feb 2, 2025
02387c9
Tweak copy
rafalp Feb 2, 2025
cfbad93
Add delete attachment feature to editor JS
rafalp Feb 3, 2025
078d92c
Display not embedded attachments under post
rafalp Feb 3, 2025
6b36111
WIP javascript attachments upload
rafalp Feb 3, 2025
5edb724
Replace uploads secrets with keys, use keys in upload attachments view
rafalp Feb 4, 2025
9ff410a
Replace 404 with 301 redirect for invalid attachment slug
rafalp Feb 4, 2025
4c0f465
Upload non-media attachment with JS, fix drag and drop not working on…
rafalp Feb 4, 2025
bd1ad8d
Add custom focus events to editor
rafalp Feb 4, 2025
7b6a0d1
WIP attachment upload progress UI
rafalp Feb 4, 2025
573e0d2
WIP tests for post accept methods
rafalp Feb 4, 2025
38df702
Attachment uploading UI and transition to final UI
rafalp Feb 5, 2025
9f75fb8
Add upload prompt to editor
rafalp Feb 5, 2025
6dbbd75
Replace upload input with message if JS is enabled
rafalp Feb 5, 2025
24cad3c
Fix broken attachment delete
rafalp Feb 5, 2025
cde76ba
Ellipsis too long attachment name in uploaded media list
rafalp Feb 5, 2025
512e177
Rebase on main, trans -> translate, new width tag syntax
rafalp Feb 5, 2025
54d04fc
Remove unused attachment settings
rafalp Feb 5, 2025
512fba2
WIP: upload attachments error handling
rafalp Feb 6, 2025
609b1e4
Handle upload errors in editor
rafalp Feb 6, 2025
d19cf3b
Upload attachments with JS
rafalp Feb 6, 2025
0de3234
Fix embedded images not keeping aspect ratio
rafalp Feb 6, 2025
4b0ed42
Reorder PostForm.accept methods tests
rafalp Feb 6, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion dev
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ intro() {
echo " ${BOLD}run${NORMAL} runs \"docker compose run --rm misago\"."
echo " ${BOLD}psql${NORMAL} runs psql connected to development database."
echo " ${BOLD}pyfmt${NORMAL} runs isort + black on python code."
echo " ${BOLD}loaddevfixture${NORMAL} populates database with pre-made data for dev."
echo " ${BOLD}loaddevfixture${NORMAL} populates database with pre-made data for dev."
echo " ${BOLD}fakedata${NORMAL} populates database with generated testing data."
echo " ${BOLD}fakebigdata${NORMAL} populates database with LARGE amount of geerated testing data."
echo " ${BOLD}pipcompile${NORMAL} run pip-compile to update requirements.txt"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# `can_upload_private_threads_attachments_hook`

This hook wraps the standard Misago function that checks whether a user has permission to upload attachments in private threads.


## Location

This hook can be imported from `misago.permissions.hooks`:

```python
from misago.permissions.hooks import can_upload_private_threads_attachments_hook
```


## Filter

```python
def custom_can_upload_private_threads_attachments_filter(
action: CanUploadPrivateThreadsAttachmentsHookAction,
permissions: 'UserPermissionsProxy',
) -> bool:
...
```

A function implemented by a plugin that can be registered in this hook.


### Arguments

#### `action: CanUploadPrivateThreadsAttachmentsHookAction`

A standard Misago function that checks whether a user has permission to upload attachments in private threads.

See the [action](#action) section for details.


#### `user_permissions: UserPermissionsProxy`

A proxy object with the current user's permissions.


### Return value

`True` if a user can upload attachments in a category, and `False` if they cannot.


## Action

```python
def can_upload_private_threads_attachments_action(permissions: 'UserPermissionsProxy') -> bool:
...
```

A standard Misago function that checks whether a user has permission to upload attachments in private threads.


### Arguments

#### `user_permissions: UserPermissionsProxy`

A proxy object with the current user's permissions.


### Return value

`True` if a user can upload attachments in a category, and `False` if they cannot.


## Example

The code below implements a custom filter function that prevents a user from uploading attachments in private threads if a custom flag is set on their account.

```python
from misago.permissions.hooks import can_upload_threads_attachments_hook
from misago.permissions.proxy import UserPermissionsProxy

@can_upload_private_threads_attachments_hook.append_filter
def user_can_upload_attachments_in_category(
action,
permissions: UserPermissionsProxy,
) -> bool:
if permissions.user.plugin_data.get("banned_private_threads_attachments"):
return False

result action(permissions)
```
101 changes: 101 additions & 0 deletions dev-docs/plugins/hooks/can-upload-threads-attachments-hook.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# `can_upload_threads_attachments_hook`

This hook wraps the standard Misago function that checks whether a user has permission to upload attachments in a category.


## Location

This hook can be imported from `misago.permissions.hooks`:

```python
from misago.permissions.hooks import can_upload_threads_attachments_hook
```


## Filter

```python
def custom_can_upload_threads_attachments_filter(
action: CanUploadThreadsAttachmentsHookAction,
permissions: 'UserPermissionsProxy',
category: Category,
) -> bool:
...
```

A function implemented by a plugin that can be registered in this hook.


### Arguments

#### `action: CanUploadThreadsAttachmentsHookAction`

A standard Misago function that checks if a user has permission to upload attachments in a category.

See the [action](#action) section for details.


#### `user_permissions: UserPermissionsProxy`

A proxy object with the current user's permissions.


#### `category: Category`

A category to check permissions for.


### Return value

`True` if a user can upload attachments in a category, and `False` if they cannot.


## Action

```python
def can_upload_threads_attachments_action(
permissions: 'UserPermissionsProxy', category: Category
) -> bool:
...
```

A standard Misago function that checks if a user has permission to upload attachments in a category.


### Arguments

#### `user_permissions: UserPermissionsProxy`

A proxy object with the current user's permissions.


#### `category: Category`

A category to check permissions for.


### Return value

`True` if a user can upload attachments in a category, and `False` if they cannot.


## Example

The code below implements a custom filter function that prevents a user from uploading attachments in a specific category if a custom flag is set on their account.

```python
from misago.categories.models import Category
from misago.permissions.hooks import can_upload_threads_attachments_hook
from misago.permissions.proxy import UserPermissionsProxy

@can_upload_threads_attachments_hook.append_filter
def user_can_upload_attachments_in_category(
action,
permissions: UserPermissionsProxy,
category: Category,
) -> bool:
if category.id in permissions.user.plugin_data.get("banned_attachments", []):
return False

result action(permissions, category)
```
138 changes: 138 additions & 0 deletions dev-docs/plugins/hooks/check-download-attachment-permission-hook.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# `check_download_attachment_permission_hook`

This hook wraps the standard function that Misago uses to check if the user has permission to download an attachment. It raises Django's `Http404` if the user cannot see the attachment or `PermissionDenied` if they are not allowed to download it.


## Location

This hook can be imported from `misago.permissions.hooks`:

```python
from misago.permissions.hooks import check_download_attachment_permission_hook
```


## Filter

```python
def custom_check_download_attachment_permission_filter(
action: CheckDownloadAttachmentPermissionHookAction,
permissions: 'UserPermissionsProxy',
category: Category | None,
thread: Thread | None,
post: Post | None,
attachment: Attachment,
) -> None:
...
```

A function implemented by a plugin that can be registered in this hook.


### Arguments

#### `action: CheckDownloadAttachmentPermissionHookAction`

A standard Misago function used to check if a user has permission to download an attachment. It raises Django's `Http404` if the user cannot see the attachment or `PermissionDenied` if they are not allowed to download it.

See the [action](#action) section for details.


#### `user_permissions: UserPermissionsProxy`

A proxy object with the current user's permissions.


#### `category: Category | None`

A category to check permissions for, or `None` if the attachment wasn't posted.


#### `thread: Thread | None`

A thread to check permissions for, or `None` if the attachment wasn't posted.


#### `post: Post | None`

A post to check permissions for, or `None` if the attachment wasn't posted.


#### `attachment: Attachment`

An attachment to check permissions for.


## Action

```python
def check_download_attachment_permission_action(
permissions: 'UserPermissionsProxy',
category: Category | None,
thread: Thread | None,
post: Post | None,
attachment: Attachment,
) -> None:
...
```

A standard Misago function used to check if a user has permission to download an attachment. It raises Django's `Http404` if the user cannot see the attachment or `PermissionDenied` if they are not allowed to download it.


### Arguments

#### `user_permissions: UserPermissionsProxy`

A proxy object with the current user's permissions.


#### `category: Category | None`

A category to check permissions for, or `None` if the attachment wasn't posted.


#### `thread: Thread | None`

A thread to check permissions for, or `None` if the attachment wasn't posted.


#### `post: Post | None`

A post to check permissions for, or `None` if the attachment wasn't posted.


#### `attachment: Attachment`

An attachment to check permissions for.


## Example

The code below implements a custom filter function that prevents a user from downloading an attachment if its flagged by plugin.

```python
from django.http import Http404
from misago.attachments.models import Attachment
from misago.categories.models import Category
from misago.permissions.hooks import check_download_attachment_permission_hook
from misago.permissions.proxy import UserPermissionsProxy
from misago.threads.models import Post, Thread

@check_download_attachment_permission_hook.append_filter
def check_user_can_download_attachment(
action,
permissions: UserPermissionsProxy,
category: Category | None,
thread: Thread | None,
post: Post | None,
attachment: Attachment,
) -> None:
action(permissions, category, thread, post, attachment)

if not (
attachment.plugin_data.get("hidden")
and permissions.user.is_authenticated
and permissions.user.is_misago_admin
):
raise Http404()
```
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def check_user_can_edit_thread(
thread: Thread,
post: Post,
) -> None:
action(permissions, category, post, thread)
action(permissions, thread, post)

if (
"[PROTECT]" in post.original
Expand Down
Loading