Skip to content

Commit

Permalink
feat: show user edit actions in timeline
Browse files Browse the repository at this point in the history
  • Loading branch information
trim21 committed Oct 24, 2024
1 parent 6c22482 commit 2213452
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 12 deletions.
7 changes: 6 additions & 1 deletion server/contrib.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
subject_infobox_queue,
)
from server.config import TURNSTILE_SECRET_KEY, UTC
from server.db import new_edit_suggestion
from server.errors import BadRequestException
from server.model import PatchAction, PatchState, SubjectPatch, SubjectType
from server.model import PatchAction, PatchState, PatchType, SubjectPatch, SubjectType
from server.router import Router
from server.strings import check_invalid_input_str, contains_invalid_input_str

Expand Down Expand Up @@ -409,6 +410,10 @@ async def _(
patch_id,
)

await new_edit_suggestion(
conn, patch_id, PatchType.Subject, text="提交者进行了修改", from_user=0
)

return Redirect(f"/subject/{patch_id}")


Expand Down
26 changes: 26 additions & 0 deletions server/db.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
from __future__ import annotations

from typing import Any
from uuid import UUID

import asyncpg
from asyncpg.pool import PoolConnectionProxy

from server.base import pg
from server.model import PatchType


async def fetch_users(rows: list[asyncpg.Record]) -> dict[int, asyncpg.Record]:
Expand All @@ -14,3 +21,22 @@ async def fetch_users(rows: list[asyncpg.Record]) -> dict[int, asyncpg.Record]:
}

return users


async def new_edit_suggestion(
conn: asyncpg.Connection[Any] | PoolConnectionProxy[Any] | asyncpg.Pool[Any],
patch_id: UUID,
type: PatchType,
text: str,
from_user: int,
) -> None:
await conn.execute(
"""
insert into edit_suggestion (id, patch_id, patch_type, text, from_user)
VALUES (uuid_generate_v7(), $1, $2, $3, $4)
""",
patch_id,
type,
text,
from_user,
)
2 changes: 1 addition & 1 deletion server/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async def get_patch(patch_id: UUID, request: Request) -> Template:
suggestions = await pg.fetch(
"""
select * from edit_suggestion
inner join patch_users on patch_users.user_id = edit_suggestion.from_user
left join patch_users on patch_users.user_id = edit_suggestion.from_user
where deleted_at IS NULL AND
patch_id = $1 AND
patch_type = $2
Expand Down
29 changes: 19 additions & 10 deletions server/templates/patch.html.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,27 @@

{% if suggestions %}
<div class="row mb-2">
<h2>修改建议</h2>
{# <h2>修改建议</h2>#}
<ul class="list-group">
{% for s in suggestions %}
<li class="list-group-item border-dark-subtle list-group-item-warning">
<div class="d-flex w-100 justify-content-between">
<h5> [{{ s.nickname }}]: </h5>
<small>{{ s.created_at | to_user_local_time }}</small>
</div>
<p class="mb-1" style="white-space: pre-wrap">
{{- s.text | auto_url -}}
</p>
</li>
{% if s.from_user != 0 %}
<li class="list-group-item border-dark-subtle list-group-item-warning">
<div class="d-flex w-100 justify-content-between">
<h5> [{{ s.nickname }}]: </h5>
<small>{{ s.created_at | to_user_local_time }}</small>
</div>
<p class="mb-1" style="white-space: pre-wrap">
{{- s.text | auto_url -}}
</p>
</li>
{% else %}
<li class="list-group-item border-dark-subtle list-group-item-info">
<div class="d-flex w-100 justify-content-between">
<h5 class="mb-0"> {{ s.text }} </h5>
<small>{{ s.created_at | to_user_local_time }}</small>
</div>
</li>
{% endif %}
{% endfor %}
</ul>
</div>
Expand Down

0 comments on commit 2213452

Please sign in to comment.