forked from 3r1s-s/meo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile.js
210 lines (183 loc) · 9.77 KB
/
profile.js
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
function fetchprofile() {
const urlParams = new URLSearchParams(window.location.search);
const username = urlParams.get('u');
fetch(`https://api.meower.org/users/${username}`)
.then(response => response.json())
.then(data => {
const profilecont = document.createElement('div');
profilecont.classList.add('mdl-sec');
if (data.avatar) {
profilecont.innerHTML = `
<img class="avatar-big" style="border: 6px solid #${data.avatar_color}"; src="https://uploads.meower.org/icons/${data.avatar}"></img>
`
} else if (data.pfp_data) {
profilecont.innerHTML = `
<img class="avatar-big" style="border: 6px solid #${data.avatar_color}"; src="images/avatars/icon_${data.pfp_data - 1}.svg"></img>
`
}
if (data._id === localStorage.getItem('uname')) {
profilecont.innerHTML += `
<div class="usr-header">
<h2 class="uname">${data._id}</h2>
</div>
<hr>
`
profilecont.innerHTML += `
<span class="subheader">Quote</span>
<div class="sec">
<textarea class="quote-edit" id="quote">${data.quote}</textarea>
</div>
<span class="subheader">Personalization</span>
<div class="sec"><span>Profile Colour:</span><input id="avtr-clr" type="color" value="#${data.avatar_color}"></input>
</div>
<dic class="sec">
<span>Profile Picture:</span><input type="file" id="profile-photo" accept="image/png,image/jpeg,image/webp,image/gif">
</div>
`;
} else {
if (data.permissions === 1) {
profilecont.innerHTML += `
<div class="usr-header">
<h2 class="uname">${data._id}</h2>
<button>
<svg width="20" height="20" viewBox="0 0 24 24"><path fill="currentColor" d="M19 6.00001C15.56 6.00001 12.826 2.43501 12.799 2.39801C12.421 1.89801 11.579 1.89801 11.201 2.39801C11.174 2.43501 8.44 6.00001 5 6.00001C4.447 6.00001 4 6.44801 4 7.00001V14C4 17.807 10.764 21.478 11.534 21.884C11.68 21.961 11.84 21.998 12 21.998C12.16 21.998 12.32 21.96 12.466 21.884C13.236 21.478 20 17.807 20 14V7.00001C20 6.44801 19.553 6.00001 19 6.00001ZM15 16L12 14L9 16L10 13L8 11H11L12 8.00001L13 11H16L14 13L15 16Z"></path></svg>
</button>
<button class="button dm-btn" onclick="opendm('${data._id}');">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="none" viewBox="0 0 24 24"><path fill="currentColor" d="M12 22a10 10 0 1 0-8.45-4.64c.13.19.11.44-.04.61l-2.06 2.37A1 1 0 0 0 2.2 22H12Z" class=""></path></svg>
</button>
</div>
<hr>
`;
} else {
profilecont.innerHTML += `
<div class="usr-header">
<h2 class="uname">${data._id}</h2>
<button class="button dm-btn" onclick="opendm('${data._id}');">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="none" viewBox="0 0 24 24"><path fill="currentColor" d="M12 22a10 10 0 1 0-8.45-4.64c.13.19.11.44-.04.61l-2.06 2.37A1 1 0 0 0 2.2 22H12Z" class=""></path></svg>
</button>
</div>
<hr>
`;
}
profilecont.innerHTML += `
<span class="subheader">Quote</span>
<div class="sec">
<span>${data.quote}</span>
</div>
`;
}
profilecont.innerHTML += `
<i>Created: ${new Date(data.created * 1000).toLocaleDateString()} | Last Seen: ${timeago(data.last_seen)}</i>
`;
if (data._id === localStorage.getItem('uname')) {
profilecont.innerHTML += `
<button class="modal-button updt-prfl" onclick="updateprofile()"><div>Update</div></button>
`;
}
document.getElementById('page').appendChild(profilecont);
})
.catch(error => console.error('Error fetching user profile:', error));
var t = localStorage.getItem('theme');
if (t) {
document.documentElement.classList.add(t + "-theme");
}
}
fetchprofile();
function timeago(tstamp) {
const currentTime = Date.now();
const lastSeenTime = tstamp * 1000;
const timeDifference = currentTime - lastSeenTime;
const seconds = Math.floor(timeDifference / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
if (days > 0) {
return `${days} day${days > 1 ? 's' : ''} ago`;
} else if (hours > 0) {
return `${hours} hour${hours > 1 ? 's' : ''} ago`;
} else if (minutes > 0) {
return `${minutes} minute${minutes > 1 ? 's' : ''} ago`;
} else {
return `${seconds} second${seconds > 1 ? 's' : ''} ago`;
}
}
function updateprofile() {
const quote = document.getElementById("quote").value;
const avatarColor = document.getElementById("avtr-clr").value.substring(1);
const fileInput = document.getElementById("profile-photo");
const file = fileInput.files[0];
const token = localStorage.getItem("token");
const xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 200) {
console.log('Profile updated successfully.');
parent.closemodal("Profile Updated!");
} else {
console.error('Failed to update profile. HTTP ' + this.status.toString());
}
}
};
xhttp.open("PATCH", "https://api.meower.org/me/config");
xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhttp.setRequestHeader("token", token);
const data = {
quote: quote,
avatar_color: avatarColor
};
if (file) {
const formData = new FormData();
formData.append("file", file);
fetch("https://api.meower.org/uploads/token/icon", {
method: "GET",
headers: {
"token": token
}
})
.then(response => response.json())
.then(tokenData => {
fetch("https://uploads.meower.org/icons", {
method: "POST",
headers: {
"Authorization": tokenData.token
},
body: formData
})
.then(uploadResponse => uploadResponse.json())
.then(uploadData => {
const avatarId = uploadData.id;
data.avatar = avatarId;
xhttp.send(JSON.stringify(data));
})
.catch(error => console.error('Error uploading file:', error));
})
.catch(error => console.error('Error fetching uploads token:', error));
} else {
// If no file is selected, just send the data object
xhttp.send(JSON.stringify(data));
}
}
function opendm(user) {
const token = localStorage.getItem("token");
const url = `https://api.meower.org/users/${user}/dm`;
fetch(url, {
method: 'GET',
headers: {
'token': `${token}`
}
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
console.log(data);
parent.loadchat(data._id);
parent.closemodal();
})
.catch(error => {
console.error('There was a problem with the fetch operation:', error);
});
}