-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplayposts.js
177 lines (150 loc) · 6.6 KB
/
displayposts.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
import { getUserData, userData } from './userdata.js';
import { convertMarkdownToHTML } from './markdown.js';
var currentContext = 'home';
var currentChatID = '';
var posts = [];
var ws = null;
export function displayPosts(context, chatID) {
if (typeof context === 'undefined') context = currentContext;
if (typeof chatID === 'undefined') chatID = currentChatID;
posts = [];
currentContext = context;
currentChatID = chatID;
var data = getUserData();
if (ws) {
ws.close();
}
ws = new WebSocket('wss://server.meower.org/?v=1&token=' + data.token);
var url = context === 'groupchats' && chatID
? 'https://api.meower.org/posts/' + chatID + '?page=1'
: 'https://api.meower.org/home?page=1';
if (currentContext === 'livechat') {
posts = [];
updateTable();
} else {
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
if (data.token) {
xhr.setRequestHeader('Token', data.token);
}
xhr.onload = function() {
if (xhr.status >= 200 && xhr.status < 300) {
var responseData = JSON.parse(xhr.responseText);
posts.push.apply(posts, responseData.autoget);
updateTable();
} else {
console.error('Network response was not ok');
}
};
xhr.onerror = function() {
console.error('Error fetching posts:', xhr.statusText);
};
xhr.send();
}
function decodeHTML(html) {
var txt = document.createElement('textarea');
txt.innerHTML = html;
return txt.value;
}
ws.onmessage = function(event) {
var data = JSON.parse(event.data);
console.log('Received message:', data);
if ((currentContext === 'livechat' && data.val.post_origin === 'livechat') ||
(currentContext === 'groupchats' && data.val.post_origin === currentChatID) ||
(currentContext === 'home' && data.val.post_origin === 'home')) {
switch (data.cmd) {
case 'post':
posts.unshift(data.val);
break;
case 'update_post':
var updateIndex = findIndex(posts, function(post) { return post._id === data.val._id; });
if (updateIndex !== -1) posts[updateIndex] = data.val;
break;
case 'delete_post':
console.log('Attempting to delete post with ID:', data.val.post_id);
var deleteIndex = findIndex(posts, function(post) { return post._id === data.val.post_id; });
if (deleteIndex !== -1) {
posts.splice(deleteIndex, 1);
console.log('Post deleted successfully.');
} else {
console.warn('Post to delete not found.');
}
break;
default:
console.warn('Unknown command:', data.cmd);
}
updateTable();
}
};
function findIndex(array, predicate) {
for (var i = 0; i < array.length; i++) {
if (predicate(array[i])) return i;
}
return -1;
}
function updateTable() {
var table = document.getElementById('post-table');
if (!table) {
console.error('Table element not found');
return;
}
table.innerHTML = '';
posts.forEach(function(post) {
var row = table.insertRow();
var userImageCell = row.insertCell();
var contentCell = row.insertCell();
userImageCell.style.width = '50px';
userImageCell.style.padding = '5px';
contentCell.style.width = 'calc(100% - 50px)';
contentCell.style.wordWrap = 'break-word';
contentCell.style.wordBreak = 'break-all';
contentCell.style.whiteSpace = 'pre-wrap';
var avatarUrl = post.author.avatar ? 'https://uploads.meower.org/icons/' + post.author.avatar : '/public/img/defaultpfp.png';
var userColor = post.author.avatar_color || '#000';
userImageCell.innerHTML = '<img src="' + avatarUrl + '" style="width: 50px; height: 50px; object-fit: cover;" alt="Icon"><hr><b><span style="color: ' + userColor + ';">' + post.author._id + '</span></b>';
var icon = userImageCell.querySelector('img');
icon.addEventListener('mouseover', function() {
icon.style.cursor = 'help';
if (!icon.title) {
userData(post.author._id)
.then(function(userInfo) {
icon.title = 'User: ' + userInfo._id + '\nBio: ' + userInfo.quote + '\nDate Joined: ' + new Date(userInfo.created * 1000);
})
.catch(function(error) {
console.error('Error fetching user data:', error);
icon.title = 'User info not available';
});
}
});
icon.addEventListener('mouseout', function() { icon.style.cursor = 'pointer'; });
var reply_to_attachments = post.reply_to.map(function(reply) {
return reply.attachments.map(function(attachment) {
return '\n <img src="https://uploads.meower.org/attachments/' + attachment.id + '" alt="Attachment" style="max-width: 256px; max-height: auto; object-fit: scale-down; align-self: center;">';
}).join('\n');
}).join('\n');
var postReplies = post.reply_to.map(function(reply) {
var replyUserColor = reply && reply.author.avatar_color || '#000';
return '<blockquote id="reply"><table border="1" cellpadding="0" cellspacing="0" width="100%"><tr><td><b><font color="' + replyUserColor + '">' + (reply && reply.author._id || '') + ' said:</font></b></td></tr><tr><td><div style="word-wrap: break-word; word-break: break-all; max-width: 100%; white-space: pre-wrap;">' + (reply && convertMarkdownToHTML(decodeHTML(reply.p)) || '') + (reply_to_attachments ? reply_to_attachments + '\n' : '') + '</div></td></tr></table></blockquote>';
}).join('');
var attachments = post.attachments.map(function(attachment) {
return '\n <img src="https://uploads.meower.org/attachments/' + attachment.id + '" alt="Attachment" style="max-width: 256px; max-height: auto; object-fit: scale-down; align-self: center;"></img>';
}).join('\n');
postReplies += '<br>';
postReplies = DOMPurify.sanitize(postReplies);
var decodedContent = DOMPurify.sanitize(post.p, { ALLOWED_TAGS: [], ALLOWED_ATTR: [] });
decodedContent = decodeHTML(decodedContent);
contentCell.innerHTML = postReplies + convertMarkdownToHTML(decodeHTML(decodedContent)) + ' ' + attachments + '<hr>' + new Date(post.t.e * 1000) + '<div style="text-align: right;"><button>Reply</button></div>';
});
}
}
document.addEventListener('DOMContentLoaded', function() {
var channelSelect = document.getElementById('channel-select');
var currentChat = channelSelect.value;
if (currentChat === 'home') {
sendPost('home', '');
} else if (currentChat === 'livechat') {
sendPost('livechat', 'livechat');
} else {
sendPost('groupchats', currentChat);
}
});