-
Notifications
You must be signed in to change notification settings - Fork 39
/
ModerateChat.svelte
126 lines (115 loc) · 2.5 KB
/
ModerateChat.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<script>
import Modal from "../../Modal.svelte";
import FormattedDate from "../../FormattedDate.svelte";
import AdminNotes from "../../AdminNotes.svelte";
import * as modals from "../../modals.js";
import {authHeader, chat} from "../../stores.js";
import {apiUrl} from "../../urls.js";
import {adminPermissions, hasPermission} from "../../bitField.js";
let loading, error;
async function deleteChat() {
error = "";
loading = true;
try {
const resp = await fetch(`${apiUrl}admin/chats/${$chat._id}`, {
method: "DELETE",
headers: $authHeader,
});
if (!resp.ok) {
throw new Error(
"Response code is not OK; code is " + resp.status
);
}
$chat = await resp.json();
loading = false;
} catch (e) {
loading = false;
error = e;
}
}
async function restoreChat() {
error = "";
loading = true;
try {
const resp = await fetch(
`${apiUrl}admin/chats/${$chat._id}/restore`,
{
method: "POST",
headers: $authHeader,
}
);
if (!resp.ok) {
throw new Error(
"Response code is not OK; code is " + resp.status
);
}
$chat = await resp.json();
loading = false;
} catch (e) {
loading = false;
error = e;
}
}
$: {
if (!$chat._id) modals.closeLastModal();
}
</script>
<Modal showClose={true} on:close={modals.closeLastModal}>
<h2 slot="header">
Moderate {$chat.type === 0 ? $chat.nickname : $chat._id}
</h2>
<div slot="default">
<h2>Chat Info</h2>
<b>UUID:</b>
{$chat._id}<br />
<b>Nickname:</b>
{$chat.nickname}<br />
<b>Owner:</b>
{$chat.owner}<br />
<b>Members:</b>
{$chat.members.length}<br />
<b>Created:</b>
<FormattedDate date={$chat.created} /><br />
<b>Last active:</b>
<FormattedDate date={$chat.last_active} /><br />
<b>Deleted?</b>
{$chat.deleted ? "yes" : "no"}
<br /><br />
{#if hasPermission(adminPermissions.VIEW_NOTES)}
<h2>Notes</h2>
<AdminNotes identifier={$chat._id} />
<br /><br />
{/if}
{#if hasPermission(adminPermissions.EDIT_CHATS)}
<h2>Danger Zone</h2>
{#if $chat.deleted}
<button
class="action-button"
disabled={loading}
on:click={restoreChat}
>
Restore chat
</button>
{:else}
<button
class="action-button"
disabled={loading}
on:click={deleteChat}
>
Delete chat
</button>
{/if}
{#if error}
<p style="color: crimson;">{error}</p>
{:else}
<br /><br />
{/if}
{/if}
</div></Modal
>
<style>
.action-button {
margin-right: 0.25em;
margin-bottom: 0.25em;
}
</style>