-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-community.html
301 lines (271 loc) · 13.5 KB
/
create-community.html
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>oldemmi</title>
<link rel="apple-touch-icon" sizes="180x180" href="https://oldemmi.vercel.app/resources/favicon/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="https://oldemmi.vercel.app/resources/favicon/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="https://oldemmi.vercel.app/resources/favicon/favicon-16x16.png">
<link rel="manifest" href="https://oldemmi.vercel.app/resources/favicon/site.webmanifest">
<meta name="mobile-web-app-capable" content="yes">
<meta name="theme-color" content="#303030">
<script>
let bebra = window.innerWidth < 550 ? 550 : window.innerWidth;
let conebebra = bebra / 1300;
document.querySelector('meta[name="viewport"]').setAttribute('content', `width=${bebra}, initial-scale=${conebebra}`);
const colorned = localStorage.getItem("theme") || "green";
const link = document.createElement("link");
link.rel = "stylesheet";
link.href = `https://oldemmi.vercel.app/css/${colorned}/dark.css?nocache=` + new Date().getTime();
document.head.appendChild(link);
</script>
</head>
<body>
<div class="navbar">
<div onclick="goToHome()" class="oldemmi">
<img src="https://oldemmi.vercel.app/resources/logo.png" alt="oldemmi logo">
<div class="text">
oldemmi
<div class="instance">
<!-- тут будет сервер -->
</div>
</div>
</div>
<div class="normal">
<button id="logout" style="display: none;" onclick="logout('userlogout')">logout</button>
<button style="display: inline;" id="login" class="login" onclick="login()">login</button>
<div style="display: inline;" class="neP"><div style="display: none;" class="profileInBar"></div></div>
</div>
</div>
<input accept="image/*" onchange="pastePicture('body', 'imageButtonBody')" type="file" id="fileInput" hidden>
<div class="caca">
<div class="creating-form">
<div class="accountsettings" style="width: 600px;">
<div style="font-weight: bold;" class="header">create community</div>
<img style="width: 150px; margin-botton: 2px;" onclick="document.getElementById('fileInputIcon').click();" src='https://oldemmi.vercel.app/resources/community.png' id="icon">
<input accept="image/*" onchange="pastePictureInImage('icon', 'fileInputIcon')" type="file" id="fileInputIcon">
banner
<input accept="image/*" onchange="pastePictureInImage('banner', 'fileInputBanner')" type="file" id="fileInputBanner">
<img id="banner" hidden>
community name*
<input id="name" placeholder="community name" required>
community title
<input id="title" placeholder="community title">
community description
<div style="display: block;">
<textarea style="margin-bottom: 2px; width: 100%; display: flex;" id="body" placeholder="community description"></textarea>
<button style="margin-top: 0; margin-bottom: 5px;" onclick="document.getElementById('fileInput').click();" id="imageButtonBody">🖼️</button>
</div>
this community is nsfw (18+)
<input type="checkbox" id="nsfw"></input>
only admins can post in community
<input type="checkbox" id="modPost"></input>
<button style="margin-top: 15px;" onclick="publish()" id="publish">create</button>
</div>
</div>
</div>
<script src="https://oldemmi.vercel.app/js/actions.js"></script>
<script>
const urlParams = new URLSearchParams(window.location.search);
const server = localStorage.getItem("accountServer") || urlParams.get('server') || "lemmy.world";
const api = `https://${server}/api/v3`
const lemmyToken = localStorage.getItem("lemmyToken");
function letmakendody(body) {
body = body.replace(/\\n/g, "\\\n");
return body;
}
async function pastePicture(where, id) {
document.getElementById(id).disabled = true;
const link = await uploadFileToLemmy();
document.getElementById(where).value = document.getElementById(where).value + `![image](${link})`;
document.getElementById(id).disabled = false;
}
async function pastePictureInImage(where, id) {
document.getElementById(id).disabled = true;
const link = await uploadFileToLemmy(id);
document.getElementById(where).src = `${link}`;
document.getElementById(id).disabled = false;
}
function instanceapply() {
const instdiv = document.querySelector(".navbar .oldemmi .text .instance");
instdiv.innerHTML = `
${server}
`;
}
function getInstanceFromActorId(actorId) {
const url = new URL(actorId);
return url.hostname; // возвращает домен, например, "lemmy.world"
}
async function publish() {
document.getElementById("publish").disabled = true;
const icon = document.getElementById("icon").src !== "https://oldemmi.vercel.app/resources/community.png" ? document.getElementById("icon").src : "";
const banner = document.getElementById("banner").src;
const nameInput = document.getElementById("name");
const titleInput = document.getElementById("title");
const nsfwInput = document.getElementById("nsfw");
const modInput = document.getElementById("modPost");
const proxyUrl = "https://oldemmi.vercel.app/api/server.js";
const title = titleInput.value ? titleInput.value : nameInput.value;
const body = letmakendody(document.getElementById("body").value);
if (nameInput.value.length < 3) {
nameInput.setCustomValidity("minimum 3 letters");
nameInput.reportValidity();
return;
}
try {
const response = await fetch("https://oldemmi.vercel.app/api/server.js", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
url: `https://${server}/api/v3/community`,
method: "POST",
headers: {
"content-type": "application/json",
"authorization": `Bearer ${localStorage.getItem("lemmyToken")}`
},
body: {
"name": `${nameInput.value}`,
"title": `${title}`,
"description": body,
"nsfw": nsfwInput.checked,
"icon": icon,
"banner": banner,
"posting_restricted_to_mods": modInput.checked
}
})
});
const data = await response.json();
if (data.error) {
document.getElementById("publish").disabled = false;
if (data.error === "invalid_name") { alert('sorry, but community name does not fit :('); }
if (data.error === "community_already_exists") { alert('sorry, but community with that name are already exist :('); } else { alert(JSON.stringify(data.error, null, 2)); }
return;
}
goToCreated(`${nameInput.value}`);
} catch (error) {
alert(`error while creating community: ${error}`);
return;
}
document.getElementById("publish").disabled = false;
}
async function getLoggined() {
try {
const response = await fetch(`${api}/site`, {
method: "GET",
headers: {
"content-type": "application/json",
"authorization": `Bearer ${lemmyToken}`
}
});
const data = await response.json();
return data.my_user.local_user_view.person;
} catch (error) {
alert(`error while loading profile: ${error}`);
}
}
async function validateToken() {
const serverUrl = `https://${server}/api/v3/user/validate_auth`;
try {
const response = await fetch(serverUrl, {
method: "GET",
headers: {
"content-type": "application/json",
"authorization": `Bearer ${lemmyToken}`
}
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
console.log("token is valid:", data);
return true; // токен валидный
} catch (error) {
console.error("token validation failed:", error);
return false; // токен не валидный
}
}
async function loginedornot() {
try {
const profileInBar = document.querySelector(".navbar .normal .neP .profileInBar");
const login = document.getElementById("login");
const logout = document.getElementById("logout");
if (lemmyToken) {
const lemmyTokenValid = await validateToken();
if (lemmyTokenValid) {
profileInBar.style.display = "flex";
logout.style.display = "flex";
login.style.display = "none";
const user = await getLoggined();
profileInBar.innerHTML = `
<img onclick="goToProfile('${user.name}', '${server}')" src="${user.avatar || 'https://lgor360.github.io/oldemmi/user.png'}">
<div class="text">
${user.display_name || user.name}
</div>
`;
} else {
logout("notvalid");
}
}
return;
} catch (error) {
alert(`error while account login check: ${error}`);
}
}
// функция для входа
function login() {
window.location.href = `https://oldemmi.vercel.app/login`;
}
async function logout(option) {
const confirmation = confirm("are you sure want to logout?");
if (!confirmation) {
return; // если пользователь нажал "нет", ничего не делаем
}
const proxyUrl = "https://oldemmi.vercel.app/api/server.js";
const logoutData = {
url: `https://${server}/api/v3/user/logout`,
method: "POST",
headers: {
"content-type": "application/json",
"authorization": `Bearer ${lemmyToken}`
},
body: {}
};
try {
if (option === "userlogout") {
// выполняем запрос на выход через прокси
const response = await fetch(proxyUrl, {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify(logoutData)
});
if (!response.ok) {
throw new Error(`logout failed: ${response.status} ${response.statusText}`);
}
}
// удаляем токен и сервер из LocalStorage
localStorage.removeItem("lemmyToken");
localStorage.removeItem("accountServer");
// перезапускаем текущую страницу
window.location.href = "https://oldemmi.vercel.app";
} catch (error) {
alert(`error during logout: ${error}`);
}
}
function goToCreated(postoid) {
window.location.href = `https://oldemmi.vercel.app/community?server=${server}&community=${postoid}`
}
function goToHome() {
window.location.href = `https://oldemmi.vercel.app/home?server=${server}`
}
function goToProfile(id, servero) {
window.location.href = `https://oldemmi.vercel.app/profile?server=${servero}&user=${id}`
}
if (!lemmyToken) { login(); }
// загружаем пост и комментарии при загрузке страницы
loginedornot();
// вставляем название сервера в заголовок
instanceapply();
</script>
</body>
</html>