Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def check_user_can_edit_thread(
action(permissions, thread, post)

if (
"[PROTECT]" in post.original
"[PROTECT]" in post.content
and not permissions.is_private_threads_moderator
):
raise PermissionError("Only a moderator can edit this post.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def check_user_can_edit_thread_post(
action(permissions, category, thread, post)

if (
"[PROTECT]" in post.original
"[PROTECT]" in post.content
and not (
permissions.is_global_moderator
or permissions.is_category_moderator(thread.category_id)
Expand Down
19 changes: 12 additions & 7 deletions dev-docs/plugins/hooks/create-post-edit-hook.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,6 @@ The user who performed the edit, a `User` instance or a `str` with the user's na
A `str` with a short description of the changes, or `None`.


#### `old_content: str`

A `str` with a snapshot of `Post.original` before the edit.


#### `old_title: str | None = None`

A `str` with the previous thread title, or `None`.
Expand All @@ -76,6 +71,16 @@ A `str` with the previous thread title, or `None`.
A `str` with the new thread title, or `None`.


#### `old_content: str | None`

A `str` with a snapshot of `Post.content` before the edit, or `None`.


#### `new_content: str | None`

A `str` with a snapshot of new `Post.content`, or `None`.


#### `attachments: list[Attachment]`

A `list` of new or current `Attachment` instances.
Expand Down Expand Up @@ -163,12 +168,12 @@ A `str` with the new thread title, or `None`.

#### `old_content: str | None`

A `str` with a snapshot of `Post.original` before the edit, or `None`.
A `str` with a snapshot of `Post.content` before the edit, or `None`.


#### `new_content: str | None`

A `str` with a snapshot of new `Post.original`, or `None`.
A `str` with a snapshot of new `Post.content`, or `None`.


#### `attachments: list[Attachment]`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# `upgrade_post_code_blocks_hook`
# `highlight_post_code_blocks_hook`

This hook wraps a standard Misago function used to upgrade a post's code blocks after it has been posted.
This hook wraps a standard Misago function used to highlight a post's code blocks after it has been saved.

The standard code highlighting feature runs in a Celery task because Pygments can get stuck in an infinite loop due to unknown bugs or malicious input.

Expand All @@ -10,15 +10,15 @@ The standard code highlighting feature runs in a Celery task because Pygments ca
This hook can be imported from `misago.posting.hooks`:

```python
from misago.posting.hooks import upgrade_post_code_blocks_hook
from misago.posting.hooks import highlight_post_code_blocks_hook
```


## Filter

```python
def custom_upgrade_post_code_blocks_filter(
action: UpgradePostCodeBlocksHookAction, post: Post
def custom_highlight_post_code_blocks_filter(
action: HighlightPostCodeBlocksHookAction, post: Post
):
...
```
Expand All @@ -41,11 +41,11 @@ The `Post` instance to update.
## Action

```python
def upgrade_post_code_blocks_action(post: Post):
def highlight_post_code_blocks_action(post: Post):
...
```

Misago function used to upgrade a post's code blocks or the next filter function from another plugin.
Misago function used to highlight a post's code blocks after it has been saved.


### Arguments
Expand All @@ -57,17 +57,17 @@ The `Post` instance to update.

## Example

The code below implements a custom filter function that highlights code using custom implementation:
The code below implements a custom filter function that highlights code using a custom implementation:

```python
from misago.posting.hooks import upgrade_post_code_blocks_hook
from misago.posting.hooks import highlight_post_code_blocks_hook
from misago.threads.models import Post


@upgrade_post_code_blocks_hook.append_filter
def plugin_upgrade_post_code_blocks(action, post: Post):
@highlight_post_code_blocks_hook.append_filter
def plugin_highlight_post_code_blocks(action, post: Post):
if post.metadata.get("highlight_code"):
post.parsed = custom_highlight_code_util(post.parsed)
post.content_parsed = custom_highlight_code_util(post.content_parsed)
post.metadata.pop("highlight_code")
post.save(update_fields=["parsed", "metadata"])
```
83 changes: 0 additions & 83 deletions dev-docs/plugins/hooks/post-needs-content-upgrade-hook.md

This file was deleted.

72 changes: 72 additions & 0 deletions dev-docs/plugins/hooks/process-post-content-hook.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# `process_post_content_hook`

This hook wraps a standard Misago function used to process post content after saving.

The process runs in a Celery task scheduled after the post is created or updated, allowing slow and costly operations, such as embedding previews of linked sites, to be performed without slowing down the posting process.


## Location

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

```python
from misago.posting.hooks import process_post_content_hook
```


## Filter

```python
def custom_process_post_content_filter(action: ProcessPostContentHookAction, post: Post):
...
```

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


### Arguments

The next function registered in this hook, either a custom function or Misago's default.

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


#### `post: Post`

The `Post` instance to update.


## Action

```python
def process_post_content_action(post: Post):
...
```

Misago function used to process post content or the next filter function from another plugin.


### Arguments

#### `post: Post`

The `Post` instance to update.


## Example

The code below implements a custom filter function that replaces a custom plugin's HTML with a new version:

```python
from misago.posting.hooks import process_post_content_hook
from misago.threads.models import Post


@process_post_content_hook.append_filter
def enrich_post_plugin_html(action, post: Post):
if "<plugin-html" in post.content_parsed:
post.content_parsed = very_costful_html_change_operation(post.content_parsed)
post.save(update_fields=["parsed"])

action(post)
```
10 changes: 5 additions & 5 deletions dev-docs/plugins/hooks/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ Hooks instances are importable from the following Python modules:
- [`get_thread_reply_state_hook`](./get-thread-reply-state-hook.md)
- [`get_thread_start_formset_hook`](./get-thread-start-formset-hook.md)
- [`get_thread_start_state_hook`](./get-thread-start-state-hook.md)
- [`post_needs_content_upgrade_hook`](./post-needs-content-upgrade-hook.md)
- [`highlight_post_code_blocks_hook`](./highlight-post-code-blocks-hook.md)
- [`process_post_content_hook`](./process-post-content-hook.md)
- [`require_private_thread_approval_hook`](./require-private-thread-approval-hook.md)
- [`require_private_thread_reply_approval_hook`](./require-private-thread-reply-approval-hook.md)
- [`require_thread_approval_hook`](./require-thread-approval-hook.md)
Expand All @@ -241,10 +242,9 @@ Hooks instances are importable from the following Python modules:
- [`save_thread_post_edit_state_hook`](./save-thread-post-edit-state-hook.md)
- [`save_thread_reply_state_hook`](./save-thread-reply-state-hook.md)
- [`save_thread_start_state_hook`](./save-thread-start-state-hook.md)
- [`upgrade_post_code_blocks_hook`](./upgrade-post-code-blocks-hook.md)
- [`upgrade_post_content_hook`](./upgrade-post-content-hook.md)
- [`validate_post_hook`](./validate-post-hook.md)
- [`validate_posted_contents_hook`](./validate-posted-contents-hook.md)
- [`should_process_post_content_hook`](./should-process-post-content-hook.md)
- [`validate_post_content_hook`](./validate-post-content-hook.md)
- [`validate_posting_hook`](./validate-posting-hook.md)
- [`validate_thread_title_hook`](./validate-thread-title-hook.md)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,6 @@ def require_private_thread_reply_approval(

return bool(
(timezone.now() - state.user.joined_on).total_seconds() < 72 * 3600
and "<a" in state.post.parsed
and "<a" in state.post.content_parsed
)
```
2 changes: 1 addition & 1 deletion dev-docs/plugins/hooks/require-thread-approval-hook.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ def require_thread_approval(

return bool(
(timezone.now() - state.user.joined_on).total_seconds() < 72 * 3600
and "<a" in state.post.parsed
and "<a" in state.post.content_parsed
)
```
Loading
Loading