add: post drafts
fix: allow question sender to view question
This commit is contained in:
parent
24162573ee
commit
f6cbeb9bd8
22 changed files with 642 additions and 100 deletions
8
Cargo.lock
generated
8
Cargo.lock
generated
|
@ -3282,7 +3282,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tetratto"
|
name = "tetratto"
|
||||||
version = "3.0.0"
|
version = "3.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ammonia",
|
"ammonia",
|
||||||
"async-stripe",
|
"async-stripe",
|
||||||
|
@ -3313,7 +3313,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tetratto-core"
|
name = "tetratto-core"
|
||||||
version = "3.0.0"
|
version = "3.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"async-recursion",
|
"async-recursion",
|
||||||
"base16ct",
|
"base16ct",
|
||||||
|
@ -3337,7 +3337,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tetratto-l10n"
|
name = "tetratto-l10n"
|
||||||
version = "3.0.0"
|
version = "3.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"pathbufd",
|
"pathbufd",
|
||||||
"serde",
|
"serde",
|
||||||
|
@ -3346,7 +3346,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tetratto-shared"
|
name = "tetratto-shared"
|
||||||
version = "3.0.0"
|
version = "3.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"ammonia",
|
"ammonia",
|
||||||
"chrono",
|
"chrono",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "tetratto"
|
name = "tetratto"
|
||||||
version = "3.0.0"
|
version = "3.1.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
|
|
|
@ -26,6 +26,7 @@ version = "1.0.0"
|
||||||
"general:action.open" = "Open"
|
"general:action.open" = "Open"
|
||||||
"general:action.view" = "View"
|
"general:action.view" = "View"
|
||||||
"general:action.copy_link" = "Copy link"
|
"general:action.copy_link" = "Copy link"
|
||||||
|
"general:action.post" = "Post"
|
||||||
"general:label.safety" = "Safety"
|
"general:label.safety" = "Safety"
|
||||||
"general:label.share" = "Share"
|
"general:label.share" = "Share"
|
||||||
"general:action.add_account" = "Add account"
|
"general:action.add_account" = "Add account"
|
||||||
|
@ -121,6 +122,8 @@ version = "1.0.0"
|
||||||
"communities:tab.emojis" = "Emojis"
|
"communities:tab.emojis" = "Emojis"
|
||||||
"communities:label.upload" = "Upload"
|
"communities:label.upload" = "Upload"
|
||||||
"communities:label.file" = "File"
|
"communities:label.file" = "File"
|
||||||
|
"communities:label.drafts" = "Drafts"
|
||||||
|
"communities:label.load" = "Load"
|
||||||
|
|
||||||
"notifs:action.mark_as_read" = "Mark as read"
|
"notifs:action.mark_as_read" = "Mark as read"
|
||||||
"notifs:action.mark_as_unread" = "Mark as unread"
|
"notifs:action.mark_as_unread" = "Mark as unread"
|
||||||
|
|
|
@ -511,6 +511,7 @@ table ol {
|
||||||
|
|
||||||
.card.secondary {
|
.card.secondary {
|
||||||
background: var(--color-surface);
|
background: var(--color-surface);
|
||||||
|
color: var(--color-text);
|
||||||
}
|
}
|
||||||
|
|
||||||
.card.tertiary {
|
.card.tertiary {
|
||||||
|
|
|
@ -187,6 +187,16 @@
|
||||||
<span>{{ text "communities:label.chats" }}</span>
|
<span>{{ text "communities:label.chats" }}</span>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
{% if user and can_post %}
|
||||||
|
<a
|
||||||
|
href="/communities/intents/post?community={{ community.id }}"
|
||||||
|
class="button quaternary"
|
||||||
|
>
|
||||||
|
{{ icon "plus" }}
|
||||||
|
<span>{{ text "general:action.post" }}</span>
|
||||||
|
</a>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
globalThis.leave_community = async () => {
|
globalThis.leave_community = async () => {
|
||||||
if (
|
if (
|
||||||
|
@ -224,6 +234,14 @@
|
||||||
{{ icon "message-circle" }}
|
{{ icon "message-circle" }}
|
||||||
<span>{{ text "communities:label.chats" }}</span>
|
<span>{{ text "communities:label.chats" }}</span>
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
|
<a
|
||||||
|
href="/communities/intents/post?community={{ community.id }}"
|
||||||
|
class="button quaternary"
|
||||||
|
>
|
||||||
|
{{ icon "plus" }}
|
||||||
|
<span>{{ text "general:action.post" }}</span>
|
||||||
|
</a>
|
||||||
{% endif %} {% if can_manage_community or is_manager %}
|
{% endif %} {% if can_manage_community or is_manager %}
|
||||||
<a
|
<a
|
||||||
href="/community/{{ community.id }}/manage"
|
href="/community/{{ community.id }}/manage"
|
||||||
|
|
|
@ -2,7 +2,21 @@
|
||||||
<title>Create post - {{ config.name }}</title>
|
<title>Create post - {{ config.name }}</title>
|
||||||
{% endblock %} {% block body %} {{ macros::nav() }}
|
{% endblock %} {% block body %} {{ macros::nav() }}
|
||||||
<main class="flex flex-col gap-2">
|
<main class="flex flex-col gap-2">
|
||||||
<div class="card-nest">
|
{% if drafts|length > 0 %}
|
||||||
|
<div class="pillmenu">
|
||||||
|
<a href="#/create" data-tab-button="create" class="active">
|
||||||
|
{{ icon "plus" }}
|
||||||
|
<span>{{ text "general:action.post" }}</span>
|
||||||
|
</a>
|
||||||
|
|
||||||
|
<a href="#/drafts" data-tab-button="drafts">
|
||||||
|
{{ icon "notepad-text-dashed" }}
|
||||||
|
<span>{{ text "communities:label.drafts" }}</span>
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
<div class="card-nest" data-tab="create">
|
||||||
<div class="card small flex items-center justify-between gap-2">
|
<div class="card small flex items-center justify-between gap-2">
|
||||||
<span class="flex items-center gap-2">
|
<span class="flex items-center gap-2">
|
||||||
{{ icon "pen" }}
|
{{ icon "pen" }}
|
||||||
|
@ -15,7 +29,33 @@
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="card tertiary">
|
<div class="card tertiary flex flex-col gap-2">
|
||||||
|
{% if draft %}
|
||||||
|
<div
|
||||||
|
class="card secondary w-full flex items-center justify-between gap-2 small"
|
||||||
|
>
|
||||||
|
<a class="flex items-center gap-2 flush" href="#/drafts">
|
||||||
|
{{ icon "notepad-text-dashed" }}
|
||||||
|
<span class="date">{{ draft.created }}</span>
|
||||||
|
</a>
|
||||||
|
|
||||||
|
<div class="flex gap-2">
|
||||||
|
<a href="?" class="button quaternary small">
|
||||||
|
{{ icon "x" }}
|
||||||
|
<span>{{ text "dialog:action.cancel" }}</span>
|
||||||
|
</a>
|
||||||
|
|
||||||
|
<button
|
||||||
|
class="button quaternary red small"
|
||||||
|
onclick="remove_draft('{{ draft.id }}')"
|
||||||
|
>
|
||||||
|
{{ icon "trash"}}
|
||||||
|
<span>{{ text "general:action.delete" }}</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
<div class="card-nest">
|
<div class="card-nest">
|
||||||
<div class="card small flex flex-row gap-2 items-center">
|
<div class="card small flex flex-row gap-2 items-center">
|
||||||
{{ components::avatar(username=user.id, size="32px",
|
{{ components::avatar(username=user.id, size="32px",
|
||||||
|
@ -25,12 +65,18 @@
|
||||||
id="community_to_post_to"
|
id="community_to_post_to"
|
||||||
onchange="update_community_avatar(event)"
|
onchange="update_community_avatar(event)"
|
||||||
>
|
>
|
||||||
<option value="{{ config.town_square }}" selected>
|
<option
|
||||||
|
value="{{ config.town_square }}"
|
||||||
|
selected="{% if not selected_community %}true{% else %}false{% endif %}"
|
||||||
|
>
|
||||||
{{ text "auth:link.my_profile" }}
|
{{ text "auth:link.my_profile" }}
|
||||||
</option>
|
</option>
|
||||||
|
|
||||||
{% for community in communities %}
|
{% for community in communities %}
|
||||||
<option value="{{ community.id }}">
|
<option
|
||||||
|
value="{{ community.id }}"
|
||||||
|
selected="{% if selected_community == community.id %}true{% else %}false{% endif %}"
|
||||||
|
>
|
||||||
<!-- prettier-ignore -->
|
<!-- prettier-ignore -->
|
||||||
{% if community.context.display_name %}
|
{% if community.context.display_name %}
|
||||||
{{ community.context.display_name }}
|
{{ community.context.display_name }}
|
||||||
|
@ -44,6 +90,7 @@
|
||||||
|
|
||||||
<form
|
<form
|
||||||
class="card flex flex-col gap-2"
|
class="card flex flex-col gap-2"
|
||||||
|
id="create_form"
|
||||||
onsubmit="create_post_from_form_town_square(event)"
|
onsubmit="create_post_from_form_town_square(event)"
|
||||||
>
|
>
|
||||||
<div class="flex flex-col gap-1">
|
<div class="flex flex-col gap-1">
|
||||||
|
@ -57,16 +104,19 @@
|
||||||
placeholder="content"
|
placeholder="content"
|
||||||
minlength="2"
|
minlength="2"
|
||||||
maxlength="4096"
|
maxlength="4096"
|
||||||
></textarea>
|
>
|
||||||
|
{% if draft %}{{ draft.content }}{% endif %}</textarea
|
||||||
|
>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="files_list" class="flex gap-2 flex-wrap"></div>
|
<div id="files_list" class="flex gap-2 flex-wrap"></div>
|
||||||
|
|
||||||
|
<div class="flex justify-between gap-2">
|
||||||
<div class="flex gap-2">
|
<div class="flex gap-2">
|
||||||
{{ components::emoji_picker(element_id="content",
|
{{ components::emoji_picker(element_id="content",
|
||||||
render_dialog=true) }} {% if is_supporter %} {{
|
render_dialog=true) }} {% if is_supporter %} {{
|
||||||
components::file_picker(files_list_id="files_list") }}
|
components::file_picker(files_list_id="files_list")
|
||||||
{% endif %}
|
}} {% endif %}
|
||||||
|
|
||||||
<button
|
<button
|
||||||
class="small square quaternary"
|
class="small square quaternary"
|
||||||
|
@ -76,11 +126,34 @@
|
||||||
>
|
>
|
||||||
{{ icon "ellipsis" }}
|
{{ icon "ellipsis" }}
|
||||||
</button>
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="flex gap-2">
|
||||||
|
{% if draft %}
|
||||||
|
<button
|
||||||
|
class="secondary small square"
|
||||||
|
title="Save as Draft"
|
||||||
|
onclick="update_draft('{{ draft.id }}')"
|
||||||
|
type="button"
|
||||||
|
>
|
||||||
|
{{ icon "notepad-text-dashed" }}
|
||||||
|
</button>
|
||||||
|
{% else %}
|
||||||
|
<button
|
||||||
|
class="secondary small square"
|
||||||
|
title="Save as Draft"
|
||||||
|
onclick="create_draft()"
|
||||||
|
type="button"
|
||||||
|
>
|
||||||
|
{{ icon "notepad-text-dashed" }}
|
||||||
|
</button>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
<button class="primary">
|
<button class="primary">
|
||||||
{{ text "communities:action.create" }}
|
{{ text "communities:action.create" }}
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
@ -147,6 +220,23 @@
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// remove draft
|
||||||
|
// {% if draft %}
|
||||||
|
if ("{{ draft.id }}") {
|
||||||
|
fetch("/api/v1/drafts/{{ draft.id }}", {
|
||||||
|
method: "DELETE",
|
||||||
|
})
|
||||||
|
.then((res) => res.json())
|
||||||
|
.then((res) => {
|
||||||
|
trigger("atto::toast", [
|
||||||
|
res.ok ? "success" : "error",
|
||||||
|
res.message,
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// {% endif %}
|
||||||
|
|
||||||
|
// ...
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
window.location.href = `/post/${res.payload}`;
|
window.location.href = `/post/${res.payload}`;
|
||||||
}, 100);
|
}, 100);
|
||||||
|
@ -157,9 +247,143 @@
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function create_draft() {
|
||||||
|
const e = {
|
||||||
|
target: document.getElementById("create_form"),
|
||||||
|
};
|
||||||
|
|
||||||
|
await trigger("atto::debounce", ["posts::create"]);
|
||||||
|
|
||||||
|
e.target
|
||||||
|
.querySelector("button.primary")
|
||||||
|
.classList.add("hidden");
|
||||||
|
|
||||||
|
fetch("/api/v1/drafts", {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
content: e.target.content.value,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
.then((res) => res.json())
|
||||||
|
.then((res) => {
|
||||||
|
trigger("atto::toast", [
|
||||||
|
res.ok ? "success" : "error",
|
||||||
|
res.message,
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (res.ok) {
|
||||||
|
setTimeout(() => {
|
||||||
|
window.location.href = `?from_draft=${res.payload}`;
|
||||||
|
}, 100);
|
||||||
|
} else {
|
||||||
|
e.target
|
||||||
|
.querySelector("button.primary")
|
||||||
|
.classList.remove("hidden");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function update_draft(id) {
|
||||||
|
const e = {
|
||||||
|
target: document.getElementById("create_form"),
|
||||||
|
};
|
||||||
|
|
||||||
|
await trigger("atto::debounce", ["posts::create"]);
|
||||||
|
|
||||||
|
e.target
|
||||||
|
.querySelector("button.primary")
|
||||||
|
.classList.add("hidden");
|
||||||
|
|
||||||
|
fetch(`/api/v1/drafts/${id}`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
content: e.target.content.value,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
.then((res) => res.json())
|
||||||
|
.then((res) => {
|
||||||
|
trigger("atto::toast", [
|
||||||
|
res.ok ? "success" : "error",
|
||||||
|
res.message,
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
e.target
|
||||||
|
.querySelector("button.primary")
|
||||||
|
.classList.remove("hidden");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{% if drafts|length > 0 %}
|
||||||
|
<div class="card-nest tertiary hidden" data-tab="drafts">
|
||||||
|
<div class="card small flex items-center gap-2">
|
||||||
|
{{ icon "notepad-text-dashed" }}
|
||||||
|
<span>{{ text "communities:label.drafts" }}</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="card flex flex-col gap-2">
|
||||||
|
{% for draft in drafts %}
|
||||||
|
<div class="card-nest">
|
||||||
|
<div class="card small flex flex-col gap-2">
|
||||||
|
<span class="no_p_margin"
|
||||||
|
>{{ draft.content|markdown|safe }}</span
|
||||||
|
>
|
||||||
|
<span class="fade date">{{ draft.created }}</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="card flex gap-2 secondary">
|
||||||
|
<a href="?from_draft={{ draft.id }}" class="button small">
|
||||||
|
{{ icon "pen-line"}}
|
||||||
|
<span>{{ text "communities:label.load" }}</span>
|
||||||
|
</a>
|
||||||
|
|
||||||
|
<button
|
||||||
|
class="button quaternary red small"
|
||||||
|
onclick="remove_draft('{{ draft.id }}')"
|
||||||
|
>
|
||||||
|
{{ icon "trash"}}
|
||||||
|
<span>{{ text "general:action.delete" }}</span>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
async function remove_draft(id) {
|
||||||
|
if (
|
||||||
|
!(await trigger("atto::confirm", [
|
||||||
|
"Are you sure you want to do this?",
|
||||||
|
]))
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fetch(`/api/v1/drafts/${id}`, {
|
||||||
|
method: "DELETE",
|
||||||
|
})
|
||||||
|
.then((res) => res.json())
|
||||||
|
.then((res) => {
|
||||||
|
trigger("atto::toast", [
|
||||||
|
res.ok ? "success" : "error",
|
||||||
|
res.message,
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
{% endif %}
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
|
|
@ -2,38 +2,7 @@
|
||||||
%} {% block content %}
|
%} {% block content %}
|
||||||
<div class="flex flex-col gap-4 w-full">
|
<div class="flex flex-col gap-4 w-full">
|
||||||
{{ macros::community_nav(community=community, selected="posts") }} {% if
|
{{ macros::community_nav(community=community, selected="posts") }} {% if
|
||||||
user and can_post %}
|
pinned|length != 0 %}
|
||||||
<div class="card-nest">
|
|
||||||
<div class="card small flex items-center gap-2">
|
|
||||||
{{ icon "pencil" }}
|
|
||||||
<span>{{ text "communities:label.create_post" }}</span>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<form
|
|
||||||
class="card flex flex-col gap-2"
|
|
||||||
onsubmit="create_post_from_form(event)"
|
|
||||||
>
|
|
||||||
<div class="flex flex-col gap-1">
|
|
||||||
<label for="content"
|
|
||||||
>{{ text "communities:label.content" }}</label
|
|
||||||
>
|
|
||||||
<textarea
|
|
||||||
type="text"
|
|
||||||
name="content"
|
|
||||||
id="content"
|
|
||||||
placeholder="content"
|
|
||||||
required
|
|
||||||
minlength="2"
|
|
||||||
maxlength="4096"
|
|
||||||
></textarea>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<button class="primary">
|
|
||||||
{{ text "communities:action.create" }}
|
|
||||||
</button>
|
|
||||||
</form>
|
|
||||||
</div>
|
|
||||||
{% endif %} {% if pinned|length != 0 %}
|
|
||||||
<div class="card-nest">
|
<div class="card-nest">
|
||||||
<div class="card small flex gap-2 items-center">
|
<div class="card small flex gap-2 items-center">
|
||||||
{{ icon "pin" }}
|
{{ icon "pin" }}
|
||||||
|
@ -73,40 +42,4 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
|
||||||
async function create_post_from_form(e) {
|
|
||||||
e.preventDefault();
|
|
||||||
await trigger("atto::debounce", ["posts::create"]);
|
|
||||||
|
|
||||||
// create body
|
|
||||||
const body = new FormData();
|
|
||||||
body.append(
|
|
||||||
"body",
|
|
||||||
JSON.stringify({
|
|
||||||
content: e.target.content.value,
|
|
||||||
community: "{{ community.id }}",
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
// ...
|
|
||||||
fetch("/api/v1/posts", {
|
|
||||||
method: "POST",
|
|
||||||
body,
|
|
||||||
})
|
|
||||||
.then((res) => res.json())
|
|
||||||
.then((res) => {
|
|
||||||
trigger("atto::toast", [
|
|
||||||
res.ok ? "success" : "error",
|
|
||||||
res.message,
|
|
||||||
]);
|
|
||||||
|
|
||||||
if (res.ok) {
|
|
||||||
setTimeout(() => {
|
|
||||||
window.location.href = `/post/${res.payload}`;
|
|
||||||
}, 100);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
75
crates/app/src/routes/api/v1/communities/drafts.rs
Normal file
75
crates/app/src/routes/api/v1/communities/drafts.rs
Normal file
|
@ -0,0 +1,75 @@
|
||||||
|
use axum::{extract::Path, response::IntoResponse, Extension, Json};
|
||||||
|
use axum_extra::extract::CookieJar;
|
||||||
|
use tetratto_core::model::{communities::PostDraft, ApiReturn, Error};
|
||||||
|
use crate::{
|
||||||
|
get_user_from_token,
|
||||||
|
routes::api::v1::{CreatePostDraft, UpdatePostContent},
|
||||||
|
State,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub async fn create_request(
|
||||||
|
jar: CookieJar,
|
||||||
|
Extension(data): Extension<State>,
|
||||||
|
Json(req): Json<CreatePostDraft>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
let data = &(data.read().await).0;
|
||||||
|
let user = match get_user_from_token!(jar, data) {
|
||||||
|
Some(ua) => ua,
|
||||||
|
None => return Json(Error::NotAllowed.into()),
|
||||||
|
};
|
||||||
|
|
||||||
|
match data
|
||||||
|
.create_draft(PostDraft::new(req.content, user.id))
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
Ok(id) => Json(ApiReturn {
|
||||||
|
ok: true,
|
||||||
|
message: "Draft created".to_string(),
|
||||||
|
payload: Some(id.to_string()),
|
||||||
|
}),
|
||||||
|
Err(e) => Json(e.into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn delete_request(
|
||||||
|
jar: CookieJar,
|
||||||
|
Extension(data): Extension<State>,
|
||||||
|
Path(id): Path<usize>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
let data = &(data.read().await).0;
|
||||||
|
let user = match get_user_from_token!(jar, data) {
|
||||||
|
Some(ua) => ua,
|
||||||
|
None => return Json(Error::NotAllowed.into()),
|
||||||
|
};
|
||||||
|
|
||||||
|
match data.delete_draft(id, user).await {
|
||||||
|
Ok(_) => Json(ApiReturn {
|
||||||
|
ok: true,
|
||||||
|
message: "Draft deleted".to_string(),
|
||||||
|
payload: (),
|
||||||
|
}),
|
||||||
|
Err(e) => Json(e.into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update_content_request(
|
||||||
|
jar: CookieJar,
|
||||||
|
Extension(data): Extension<State>,
|
||||||
|
Path(id): Path<usize>,
|
||||||
|
Json(req): Json<UpdatePostContent>,
|
||||||
|
) -> impl IntoResponse {
|
||||||
|
let data = &(data.read().await).0;
|
||||||
|
let user = match get_user_from_token!(jar, data) {
|
||||||
|
Some(ua) => ua,
|
||||||
|
None => return Json(Error::NotAllowed.into()),
|
||||||
|
};
|
||||||
|
|
||||||
|
match data.update_draft_content(id, user, req.content).await {
|
||||||
|
Ok(_) => Json(ApiReturn {
|
||||||
|
ok: true,
|
||||||
|
message: "Draft updated".to_string(),
|
||||||
|
payload: (),
|
||||||
|
}),
|
||||||
|
Err(e) => Json(e.into()),
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
pub mod communities;
|
pub mod communities;
|
||||||
|
pub mod drafts;
|
||||||
pub mod emojis;
|
pub mod emojis;
|
||||||
pub mod images;
|
pub mod images;
|
||||||
pub mod posts;
|
pub mod posts;
|
||||||
|
|
|
@ -113,6 +113,13 @@ pub fn routes() -> Router {
|
||||||
"/posts/{id}/context",
|
"/posts/{id}/context",
|
||||||
post(communities::posts::update_context_request),
|
post(communities::posts::update_context_request),
|
||||||
)
|
)
|
||||||
|
// drafts
|
||||||
|
.route("/drafts", post(communities::drafts::create_request))
|
||||||
|
.route("/drafts/{id}", delete(communities::drafts::delete_request))
|
||||||
|
.route(
|
||||||
|
"/drafts/{id}/content",
|
||||||
|
post(communities::drafts::update_content_request),
|
||||||
|
)
|
||||||
// questions
|
// questions
|
||||||
.route("/questions", post(communities::questions::create_request))
|
.route("/questions", post(communities::questions::create_request))
|
||||||
.route(
|
.route(
|
||||||
|
@ -581,3 +588,8 @@ pub struct AddOrRemoveStackUser {
|
||||||
pub struct UpdateEmojiName {
|
pub struct UpdateEmojiName {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct CreatePostDraft {
|
||||||
|
pub content: String,
|
||||||
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ use axum::{
|
||||||
response::{Html, IntoResponse},
|
response::{Html, IntoResponse},
|
||||||
};
|
};
|
||||||
use axum_extra::extract::CookieJar;
|
use axum_extra::extract::CookieJar;
|
||||||
|
use serde::Deserialize;
|
||||||
use tera::Context;
|
use tera::Context;
|
||||||
use tetratto_core::model::{
|
use tetratto_core::model::{
|
||||||
auth::User,
|
auth::User,
|
||||||
|
@ -236,10 +237,19 @@ pub async fn search_request(
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct CreatePostProps {
|
||||||
|
#[serde(default)]
|
||||||
|
pub community: usize,
|
||||||
|
#[serde(default)]
|
||||||
|
pub from_draft: usize,
|
||||||
|
}
|
||||||
|
|
||||||
/// `/communities/intents/post`
|
/// `/communities/intents/post`
|
||||||
pub async fn create_post_request(
|
pub async fn create_post_request(
|
||||||
jar: CookieJar,
|
jar: CookieJar,
|
||||||
Extension(data): Extension<State>,
|
Extension(data): Extension<State>,
|
||||||
|
Query(props): Query<CreatePostProps>,
|
||||||
) -> impl IntoResponse {
|
) -> impl IntoResponse {
|
||||||
let data = data.read().await;
|
let data = data.read().await;
|
||||||
let user = match get_user_from_token!(jar, data.0) {
|
let user = match get_user_from_token!(jar, data.0) {
|
||||||
|
@ -271,9 +281,32 @@ pub async fn create_post_request(
|
||||||
communities.push(community)
|
communities.push(community)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get draft
|
||||||
|
let draft = if props.from_draft != 0 {
|
||||||
|
match data.0.get_draft_by_id(props.from_draft).await {
|
||||||
|
Ok(d) => {
|
||||||
|
// drafts can only be used by their owner
|
||||||
|
if d.owner == user.id { Some(d) } else { None }
|
||||||
|
}
|
||||||
|
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
let drafts = match data.0.get_drafts_by_user_all(user.id).await {
|
||||||
|
Ok(l) => l,
|
||||||
|
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
|
||||||
|
};
|
||||||
|
|
||||||
|
// ...
|
||||||
let lang = get_lang!(jar, data.0);
|
let lang = get_lang!(jar, data.0);
|
||||||
let mut context = initial_context(&data.0.0, lang, &Some(user)).await;
|
let mut context = initial_context(&data.0.0, lang, &Some(user)).await;
|
||||||
|
|
||||||
|
context.insert("draft", &draft);
|
||||||
|
context.insert("drafts", &drafts);
|
||||||
context.insert("communities", &communities);
|
context.insert("communities", &communities);
|
||||||
|
context.insert("selected_community", &props.community);
|
||||||
|
|
||||||
// return
|
// return
|
||||||
Ok(Html(
|
Ok(Html(
|
||||||
|
@ -1118,10 +1151,16 @@ pub async fn question_request(
|
||||||
false
|
false
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let is_sender = if let Some(ref ua) = user {
|
||||||
|
ua.id == question.owner
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
};
|
||||||
|
|
||||||
// check permissions
|
// check permissions
|
||||||
let (can_read, _) = check_permissions!(community, jar, data, user);
|
let (can_read, _) = check_permissions!(community, jar, data, user);
|
||||||
|
|
||||||
if !can_read {
|
if !can_read && !is_sender {
|
||||||
return Err(Html(
|
return Err(Html(
|
||||||
render_error(Error::NotAllowed, &jar, &data, &user).await,
|
render_error(Error::NotAllowed, &jar, &data, &user).await,
|
||||||
));
|
));
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "tetratto-core"
|
name = "tetratto-core"
|
||||||
version = "3.0.0"
|
version = "3.1.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
|
|
|
@ -291,6 +291,28 @@ impl DataManager {
|
||||||
return Err(Error::DatabaseError(e.to_string()));
|
return Err(Error::DatabaseError(e.to_string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// delete stacks
|
||||||
|
let res = execute!(
|
||||||
|
&conn,
|
||||||
|
"DELETE FROM stacks WHERE owner = $1",
|
||||||
|
&[&(id as i64)]
|
||||||
|
);
|
||||||
|
|
||||||
|
if let Err(e) = res {
|
||||||
|
return Err(Error::DatabaseError(e.to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// delete drafts
|
||||||
|
let res = execute!(
|
||||||
|
&conn,
|
||||||
|
"DELETE FROM drafts WHERE owner = $1",
|
||||||
|
&[&(id as i64)]
|
||||||
|
);
|
||||||
|
|
||||||
|
if let Err(e) = res {
|
||||||
|
return Err(Error::DatabaseError(e.to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
// delete posts
|
// delete posts
|
||||||
let res = execute!(&conn, "DELETE FROM posts WHERE owner = $1", &[&(id as i64)]);
|
let res = execute!(&conn, "DELETE FROM posts WHERE owner = $1", &[&(id as i64)]);
|
||||||
|
|
||||||
|
|
|
@ -33,6 +33,7 @@ impl DataManager {
|
||||||
execute!(&conn, common::CREATE_TABLE_UPLOADS).unwrap();
|
execute!(&conn, common::CREATE_TABLE_UPLOADS).unwrap();
|
||||||
execute!(&conn, common::CREATE_TABLE_EMOJIS).unwrap();
|
execute!(&conn, common::CREATE_TABLE_EMOJIS).unwrap();
|
||||||
execute!(&conn, common::CREATE_TABLE_STACKS).unwrap();
|
execute!(&conn, common::CREATE_TABLE_STACKS).unwrap();
|
||||||
|
execute!(&conn, common::CREATE_TABLE_DRAFTS).unwrap();
|
||||||
|
|
||||||
self.2
|
self.2
|
||||||
.set("atto.active_connections:users".to_string(), "0".to_string())
|
.set("atto.active_connections:users".to_string(), "0".to_string())
|
||||||
|
|
185
crates/core/src/database/drafts.rs
Normal file
185
crates/core/src/database/drafts.rs
Normal file
|
@ -0,0 +1,185 @@
|
||||||
|
use super::*;
|
||||||
|
use crate::cache::Cache;
|
||||||
|
use crate::model::moderation::AuditLogEntry;
|
||||||
|
use crate::model::{Error, Result, auth::User, communities::PostDraft, permissions::FinePermission};
|
||||||
|
use crate::{auto_method, execute, get, query_row, query_rows, params};
|
||||||
|
|
||||||
|
#[cfg(feature = "sqlite")]
|
||||||
|
use rusqlite::Row;
|
||||||
|
|
||||||
|
#[cfg(feature = "postgres")]
|
||||||
|
use tokio_postgres::Row;
|
||||||
|
|
||||||
|
impl DataManager {
|
||||||
|
/// Get a [`PostDraft`] from an SQL row.
|
||||||
|
pub(crate) fn get_draft_from_row(
|
||||||
|
#[cfg(feature = "sqlite")] x: &Row<'_>,
|
||||||
|
#[cfg(feature = "postgres")] x: &Row,
|
||||||
|
) -> PostDraft {
|
||||||
|
PostDraft {
|
||||||
|
id: get!(x->0(i64)) as usize,
|
||||||
|
created: get!(x->1(i64)) as usize,
|
||||||
|
content: get!(x->2(String)),
|
||||||
|
owner: get!(x->3(i64)) as usize,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
auto_method!(get_draft_by_id()@get_draft_from_row -> "SELECT * FROM drafts WHERE id = $1" --name="draft" --returns=PostDraft --cache-key-tmpl="atto.draft:{}");
|
||||||
|
|
||||||
|
/// Get all drafts from the given user (from most recent, paginated).
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
/// * `id` - the ID of the user the requested drafts belong to
|
||||||
|
/// * `batch` - the limit of posts in each page
|
||||||
|
/// * `page` - the page number
|
||||||
|
pub async fn get_drafts_by_user(
|
||||||
|
&self,
|
||||||
|
id: usize,
|
||||||
|
batch: usize,
|
||||||
|
page: usize,
|
||||||
|
) -> Result<Vec<PostDraft>> {
|
||||||
|
let conn = match self.connect().await {
|
||||||
|
Ok(c) => c,
|
||||||
|
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||||
|
};
|
||||||
|
|
||||||
|
// ...
|
||||||
|
let res = query_rows!(
|
||||||
|
&conn,
|
||||||
|
"SELECT * FROM drafts WHERE owner = $1 ORDER BY created DESC LIMIT $2 OFFSET $3",
|
||||||
|
&[&(id as i64), &(batch as i64), &((page * batch) as i64)],
|
||||||
|
|x| { Self::get_draft_from_row(x) }
|
||||||
|
);
|
||||||
|
|
||||||
|
if res.is_err() {
|
||||||
|
return Err(Error::GeneralNotFound("draft".to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(res.unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get all drafts from the given user (from most recent).
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
/// * `id` - the ID of the user the requested drafts belong to
|
||||||
|
pub async fn get_drafts_by_user_all(&self, id: usize) -> Result<Vec<PostDraft>> {
|
||||||
|
let conn = match self.connect().await {
|
||||||
|
Ok(c) => c,
|
||||||
|
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||||
|
};
|
||||||
|
|
||||||
|
// ...
|
||||||
|
let res = query_rows!(
|
||||||
|
&conn,
|
||||||
|
"SELECT * FROM drafts WHERE owner = $1 ORDER BY created DESC",
|
||||||
|
&[&(id as i64)],
|
||||||
|
|x| { Self::get_draft_from_row(x) }
|
||||||
|
);
|
||||||
|
|
||||||
|
if res.is_err() {
|
||||||
|
return Err(Error::GeneralNotFound("draft".to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(res.unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a new post draft in the database.
|
||||||
|
///
|
||||||
|
/// # Arguments
|
||||||
|
/// * `data` - a mock [`PostDraft`] object to insert
|
||||||
|
pub async fn create_draft(&self, data: PostDraft) -> Result<usize> {
|
||||||
|
// check values
|
||||||
|
if data.content.len() < 2 {
|
||||||
|
return Err(Error::DataTooShort("content".to_string()));
|
||||||
|
} else if data.content.len() > 4096 {
|
||||||
|
return Err(Error::DataTooLong("content".to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// ...
|
||||||
|
let conn = match self.connect().await {
|
||||||
|
Ok(c) => c,
|
||||||
|
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||||
|
};
|
||||||
|
|
||||||
|
let res = execute!(
|
||||||
|
&conn,
|
||||||
|
"INSERT INTO drafts VALUES ($1, $2, $3, $4)",
|
||||||
|
params![
|
||||||
|
&(data.id as i64),
|
||||||
|
&(data.created as i64),
|
||||||
|
&data.content,
|
||||||
|
&(data.owner as i64),
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
if let Err(e) = res {
|
||||||
|
return Err(Error::DatabaseError(e.to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(data.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn delete_draft(&self, id: usize, user: User) -> Result<()> {
|
||||||
|
let y = self.get_draft_by_id(id).await?;
|
||||||
|
|
||||||
|
if user.id != y.owner {
|
||||||
|
if !user.permissions.check(FinePermission::MANAGE_POSTS) {
|
||||||
|
return Err(Error::NotAllowed);
|
||||||
|
} else {
|
||||||
|
self.create_audit_log_entry(AuditLogEntry::new(
|
||||||
|
user.id,
|
||||||
|
format!("invoked `delete_draft` with x value `{id}`"),
|
||||||
|
))
|
||||||
|
.await?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let conn = match self.connect().await {
|
||||||
|
Ok(c) => c,
|
||||||
|
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||||
|
};
|
||||||
|
|
||||||
|
let res = execute!(&conn, "DELETE FROM drafts WHERE id = $1", &[&(id as i64)]);
|
||||||
|
|
||||||
|
if let Err(e) = res {
|
||||||
|
return Err(Error::DatabaseError(e.to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
self.2.remove(format!("atto.draft:{}", id)).await;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn update_draft_content(&self, id: usize, user: User, x: String) -> Result<()> {
|
||||||
|
let y = self.get_draft_by_id(id).await?;
|
||||||
|
|
||||||
|
if user.id != y.owner {
|
||||||
|
if !user.permissions.check(FinePermission::MANAGE_POSTS) {
|
||||||
|
return Err(Error::NotAllowed);
|
||||||
|
} else {
|
||||||
|
self.create_audit_log_entry(AuditLogEntry::new(
|
||||||
|
user.id,
|
||||||
|
format!("invoked `update_draft_content` with x value `{id}`"),
|
||||||
|
))
|
||||||
|
.await?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ...
|
||||||
|
let conn = match self.connect().await {
|
||||||
|
Ok(c) => c,
|
||||||
|
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||||
|
};
|
||||||
|
|
||||||
|
let res = execute!(
|
||||||
|
&conn,
|
||||||
|
"UPDATE drafts SET content = $1 WHERE id = $2",
|
||||||
|
params![&x, &(id as i64)]
|
||||||
|
);
|
||||||
|
|
||||||
|
if let Err(e) = res {
|
||||||
|
return Err(Error::DatabaseError(e.to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
|
@ -18,3 +18,4 @@ pub const CREATE_TABLE_MESSAGES: &str = include_str!("./sql/create_messages.sql"
|
||||||
pub const CREATE_TABLE_UPLOADS: &str = include_str!("./sql/create_uploads.sql");
|
pub const CREATE_TABLE_UPLOADS: &str = include_str!("./sql/create_uploads.sql");
|
||||||
pub const CREATE_TABLE_EMOJIS: &str = include_str!("./sql/create_emojis.sql");
|
pub const CREATE_TABLE_EMOJIS: &str = include_str!("./sql/create_emojis.sql");
|
||||||
pub const CREATE_TABLE_STACKS: &str = include_str!("./sql/create_stacks.sql");
|
pub const CREATE_TABLE_STACKS: &str = include_str!("./sql/create_stacks.sql");
|
||||||
|
pub const CREATE_TABLE_DRAFTS: &str = include_str!("./sql/create_drafts.sql");
|
||||||
|
|
6
crates/core/src/database/drivers/sql/create_drafts.sql
Normal file
6
crates/core/src/database/drivers/sql/create_drafts.sql
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
CREATE TABLE IF NOT EXISTS drafts (
|
||||||
|
id BIGINT NOT NULL PRIMARY KEY,
|
||||||
|
created BIGINT NOT NULL,
|
||||||
|
content TEXT NOT NULL,
|
||||||
|
owner BIGINT NOT NULL
|
||||||
|
)
|
|
@ -3,6 +3,7 @@ mod auth;
|
||||||
mod common;
|
mod common;
|
||||||
mod communities;
|
mod communities;
|
||||||
pub mod connections;
|
pub mod connections;
|
||||||
|
mod drafts;
|
||||||
mod drivers;
|
mod drivers;
|
||||||
mod emojis;
|
mod emojis;
|
||||||
mod ipbans;
|
mod ipbans;
|
||||||
|
|
|
@ -918,10 +918,10 @@ impl DataManager {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new journal entry in the database.
|
/// Create a new post in the database.
|
||||||
///
|
///
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
/// * `data` - a mock [`JournalEntry`] object to insert
|
/// * `data` - a mock [`Post`] object to insert
|
||||||
pub async fn create_post(&self, mut data: Post) -> Result<usize> {
|
pub async fn create_post(&self, mut data: Post) -> Result<usize> {
|
||||||
// check values (if this isn't reposting something else)
|
// check values (if this isn't reposting something else)
|
||||||
let is_reposting = if let Some(ref repost) = data.context.repost {
|
let is_reposting = if let Some(ref repost) = data.context.repost {
|
||||||
|
|
|
@ -349,3 +349,23 @@ pub struct QuestionContext {
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub is_nsfw: bool,
|
pub is_nsfw: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
pub struct PostDraft {
|
||||||
|
pub id: usize,
|
||||||
|
pub created: usize,
|
||||||
|
pub content: String,
|
||||||
|
pub owner: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PostDraft {
|
||||||
|
/// Create a new [`PostDraft`].
|
||||||
|
pub fn new(content: String, owner: usize) -> Self {
|
||||||
|
Self {
|
||||||
|
id: Snowflake::new().to_string().parse::<usize>().unwrap(),
|
||||||
|
created: unix_epoch_timestamp() as usize,
|
||||||
|
content,
|
||||||
|
owner,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "tetratto-l10n"
|
name = "tetratto-l10n"
|
||||||
version = "3.0.0"
|
version = "3.1.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
authors.workspace = true
|
authors.workspace = true
|
||||||
repository.workspace = true
|
repository.workspace = true
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "tetratto-shared"
|
name = "tetratto-shared"
|
||||||
version = "3.0.0"
|
version = "3.1.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
authors.workspace = true
|
authors.workspace = true
|
||||||
repository.workspace = true
|
repository.workspace = true
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue