xssbook/public/js/profile.js

160 lines
4.6 KiB
JavaScript
Raw Normal View History

function swap(value) {
let postsb = document.getElementById("profilepostbutton");
let aboutb = document.getElementById("profileaboutbutton");
let posts = document.getElementById("posts");
let about = document.getElementById("about");
if (value) {
postsb.classList.add("selected")
aboutb.classList.remove("selected")
about.classList.add("hidden")
posts.classList.remove("hidden")
} else {
postsb.classList.remove("selected")
aboutb.classList.add("selected")
about.classList.remove("hidden")
posts.classList.add("hidden")
}
}
2023-02-01 03:21:19 +00:00
function changeimage(fn) {
var input = document.createElement('input')
input.type = 'file'
input.accept= 'image/png'
input.onchange = async (e) => {
var file = e.target.files[0];
if (file.type !== 'image/png') {
return
}
let response = await fn(file);
alert(response.msg)
}
input.click();
}
2023-01-26 22:29:16 +00:00
function render() {
const html = `
<div id="top">
<div id="banner">
2023-02-01 03:21:19 +00:00
<div class="bg">
${banner(data.user.user_id)}
2023-01-26 22:29:16 +00:00
</div>
2023-02-01 03:21:19 +00:00
${ isself ? `<div class="changebanner" onclick="changeimage(updatebanner)"></div>` : '' }
2023-01-26 22:29:16 +00:00
</div>
<div id="info">
<div class="face">
2023-01-31 03:55:14 +00:00
${pfp(data.user.user_id)}
2023-02-01 03:21:19 +00:00
${ isself ? `<div class="changeavatar" onclick="changeimage(updateavatar)"></div>` : '' }
2023-01-26 22:29:16 +00:00
</div>
<div class="infodata">
<span class="bold ltext">${data.user.firstname + ' ' + data.user.lastname}</span>
<span class="gtext">Joined ${parseDate(new Date(data.user.date))}</span>
</div>
</div>
2023-02-01 03:21:19 +00:00
<div class="fullline" style="width: 80em; margin-bottom: 0; z-index: 0;"></div>
2023-01-26 22:29:16 +00:00
<div class="profilebuttons">
<button id="profilepostbutton" class="${posts ? 'selected' : ''}" onclick="swap(true)">
2023-01-26 22:29:16 +00:00
Posts
</button>
<button id="profileaboutbutton" class="${posts ? '' : 'selected'}" onclick="swap(false)">
2023-01-26 22:29:16 +00:00
About
</button>
<div style="flex: 20"></div>
${ isself ? `<button class="logout" onclick="logout_button()">Logout</button>` : ''}
2023-01-26 22:29:16 +00:00
</div>
</div>
`
append(html)
2023-01-26 22:29:16 +00:00
const postsh = `
<div id="posts" class="${posts ? '' : 'hidden'}">
${data.posts.map(p => parsePost(p)).join('')}
</div>
`
append(postsh)
2023-01-26 22:29:16 +00:00
const about = `
<div id="about" class="post ${posts ? 'hidden' : ''}">
<span class="bold ltext">About</span>
<div class="data">
<span class="gtext bold">Name: ${data.user.firstname + ' ' + data.user.lastname}</span>
<span class="gtext bold">Email: ${data.user.email}</span>
<span class="gtext bold">Gender: ${data.user.gender}</span>
<span class="gtext bold">Birthday: ${parseMonth(data.user.month) + ' ' + data.user.day + ', ' + data.user.year}</span>
2023-01-26 22:29:16 +00:00
<span class="gtext bold">User ID: ${data.user.user_id}</span>
</div>
</div>
`
append(about)
2023-02-01 03:21:19 +00:00
2023-01-26 22:29:16 +00:00
}
async function logout_button() {
const response = await logout()
if (response.status != 200) return;
location.href = '/login'
}
2023-01-26 22:29:16 +00:00
var posts = true
var isself = false
2023-01-26 22:29:16 +00:00
async function load() {
var params = {};
for (const [key, value] of new URLSearchParams(location.search)) {
params[key] = value
}
2023-01-31 05:00:13 +00:00
let request = await loadself()
if (request.status === 429) {
header(false, false)
throw new Error("Rate limited");
}
data.self = request.json;
2023-01-30 23:55:36 +00:00
data.users[data.self.user_id] = data.self
let id;
2023-01-31 03:55:14 +00:00
header(false, false, data.self.user_id)
if (params.id !== undefined && !isNaN(params.id)) {
id = parseInt(params.id);
} else {
2023-01-30 23:55:36 +00:00
id = data.self.user_id
}
2023-01-30 23:55:36 +00:00
isself = id === data.self.user_id
2023-01-26 22:29:16 +00:00
const posts = (await loadusersposts(id)).json
data.posts.push(... posts)
const batch = []
2023-01-30 23:55:36 +00:00
if (!isself) {
batch.push(id)
}
2023-01-26 22:29:16 +00:00
for (const post of posts) {
for(const comment of post.comments) {
if (data.users[comment[0]] !== undefined) continue
if (batch.includes(comment[0])) continue
batch.push(comment[0])
}
if (data.users[post.user_id] !== undefined) continue
if (batch.includes(post.user_id)) continue
batch.push(post.user_id)
}
const users = batch.length == 0 ? [] : (await loadusers(batch)).json
2023-01-26 22:29:16 +00:00
for (const user of users) {
data.users[user.user_id] = user
}
2023-01-30 23:55:36 +00:00
data.user = data.users[id]
2023-01-26 22:29:16 +00:00
render()
}
load()