annotate src/chat.js @ 83:a47036fd0158

group chat
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 11 Mar 2025 13:56:59 -0600
parents a0d3477d5694
children 83fb90276334
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 'use strict';
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
18
0721dcf222e1 * alert
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
3 let title = document.title;
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 let currentChatId = null;
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
5 let eventSource;
73
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
6 let lastUpdate = Date.now();
35
27c41f22d2a9 improve fixPosts
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
7 let userId;
63
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
8 let filebinUrl;
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9
16
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
10 function evalEvent(event) {
17
7230c821c368 push fixes
Franklin Schmidt <fschmidt@gmail.com>
parents: 16
diff changeset
11 // console.log(event);
16
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
12 eval(event.data);
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
13 }
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
14
15
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
15 function setUserEventSource(userId) {
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
16 let userEventSource = new EventSource(`${location.origin}/user/${userId}`);
16
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
17 userEventSource.onmessage = evalEvent;
15
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
18 }
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
19
59
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 56
diff changeset
20 function selectChat(chatId) {
48
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
21 document.querySelector('div[chat_content]').setAttribute('show','posts');
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 if( chatId === currentChatId )
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 return;
59
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 56
diff changeset
24 let div = document.querySelector(`div[chat="${chatId}"]`);
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 let selected = div.parentNode.querySelector('[selected]');
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 if( selected ) selected.removeAttribute('selected');
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 div.setAttribute('selected','');
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 ajax(`get_chat.js?chat=${chatId}`);
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 currentChatId = chatId;
59
8270106644db add chat.luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 56
diff changeset
30 history.replaceState(null,null,`?chat=${chatId}`);
53
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
31 clearUnread();
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
32
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
33 if(eventSource) eventSource.close();
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
34 eventSource = new EventSource(`${location.origin}/chat/${chatId}`);
16
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
35 eventSource.onmessage = evalEvent;
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
36 }
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
37
27
809193524522 responsive
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
38 function back() {
48
Franklin Schmidt <fschmidt@gmail.com>
parents: 46
diff changeset
39 document.querySelector('div[chat_content]').setAttribute('show','chats');
27
809193524522 responsive
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
40 }
809193524522 responsive
Franklin Schmidt <fschmidt@gmail.com>
parents: 24
diff changeset
41
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
42 function gotChat(html) {
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
43 document.querySelector('div[posts]').innerHTML = html;
19
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
44 fixPosts();
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
45 document.querySelector('div[input] textarea').focus();
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
46 document.querySelector('div[input]').scrollIntoView({block: 'end'});
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 }
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 function fixTextarea(event) {
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 let textarea = event.target;
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 textarea.style.height = 'initial';
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 textarea.style.height = (textarea.scrollHeight+2) + 'px';
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 textarea.scrollIntoViewIfNeeded(false);
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 }
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 const isMobile = 'ontouchstart' in window || navigator.maxTouchPoints > 0 || navigator.msMaxTouchPoints > 0;
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 function addPost() {
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 let textarea = document.querySelector('div[input] textarea');
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 let text = textarea.value;
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 if( text.trim() === '' )
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 return;
79
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
63 let url = `add_post.js?chat=${currentChatId}`;
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
64 let reply = document.querySelector('div[reply]').getAttribute('reply');
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
65 if( reply )
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
66 url += `&reply=${reply}`;
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
67 ajax(url,`content=${encodeURIComponent(text)}`);
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68 textarea.value = '';
79
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
69 closeReply();
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 }
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 function textareaKey(event) {
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 if( event.keyCode===13 && !event.shiftKey && !event.ctrlKey && !isMobile ) {
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 event.preventDefault();
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 addPost();
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 }
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 }
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78
78
Franklin Schmidt <fschmidt@gmail.com>
parents: 77
diff changeset
79 function editTextareaKey(event) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 77
diff changeset
80 if( event.keyCode===13 && !event.shiftKey && !event.ctrlKey && !isMobile ) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 77
diff changeset
81 event.preventDefault();
Franklin Schmidt <fschmidt@gmail.com>
parents: 77
diff changeset
82 savePost(event.target);
Franklin Schmidt <fschmidt@gmail.com>
parents: 77
diff changeset
83 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 77
diff changeset
84 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 77
diff changeset
85
19
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
86 function fixPosts() {
35
27c41f22d2a9 improve fixPosts
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
87 let divs = document.querySelectorAll('div[post][fix]');
19
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
88 for( let div of divs ) {
79
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
89 for( let whenSpan of div.querySelectorAll('[when]') ) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
90 whenSpan.textContent = new Date(Number(whenSpan.textContent)).toLocaleString([],{dateStyle:'short',timeStyle:'short'});
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
91 }
35
27c41f22d2a9 improve fixPosts
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
92 let textDiv = div.querySelector('div[text]');
27c41f22d2a9 improve fixPosts
Franklin Schmidt <fschmidt@gmail.com>
parents: 34
diff changeset
93 textDiv.innerHTML = urlsToLinks(textDiv.innerHTML);
79
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
94 let reply = div.querySelector('blockquote');
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
95 if( reply )
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
96 reply.innerHTML = urlsToLinks(reply.innerHTML);
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
97 let html;
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
98 if( div.getAttribute('author') === userId ) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
99 html = document.querySelector('div[hidden] span[pulldown=author]').innerHTML;
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
100 } else {
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
101 html = document.querySelector('div[hidden] span[pulldown=other]').innerHTML;
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
102 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
103 div.querySelector('span[pulldown]').innerHTML = html;
19
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
104 div.removeAttribute('fix');
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
105 }
10
f9e6a4cc4f7d add Post
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 }
11
563a5358f2ee add delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
107
563a5358f2ee add delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
108 function deleteChat() {
20
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
109 let dialog = document.querySelector('dialog[delete_chat]');
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
110 openModal(dialog);
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
111 }
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
112
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
113 function doDeleteChat(el) {
dade6a560494 add dialogs
Franklin Schmidt <fschmidt@gmail.com>
parents: 19
diff changeset
114 closeModal(el);
11
563a5358f2ee add delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
115 ajax(`delete_chat.js?chat=${currentChatId}`);
563a5358f2ee add delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 10
diff changeset
116 }
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
117
23
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
118 let currentPostId;
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
119
38
436216d17a1b fix edit and delete
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
120 function getPostId(el) {
436216d17a1b fix edit and delete
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
121 while(true) {
436216d17a1b fix edit and delete
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
122 let postId = el.getAttribute('post');
436216d17a1b fix edit and delete
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
123 if( postId )
436216d17a1b fix edit and delete
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
124 return postId;
436216d17a1b fix edit and delete
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
125 el = el.parentNode;
436216d17a1b fix edit and delete
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
126 }
436216d17a1b fix edit and delete
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
127 }
436216d17a1b fix edit and delete
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
128
436216d17a1b fix edit and delete
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
129 function deletePost(el) {
436216d17a1b fix edit and delete
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
130 currentPostId = getPostId(el);
23
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
131 let dialog = document.querySelector('dialog[delete_post]');
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
132 openModal(dialog);
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
133 }
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
134
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
135 function doDeletePost(el) {
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
136 closeModal(el);
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
137 ajax(`delete_post.js?post=${currentPostId}`);
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
138 }
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
139
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
140 function deleted(postId) {
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
141 let div = document.querySelector(`div[post="${postId}"]`);
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
142 if( div )
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
143 div.outerHTML = '';
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
144 }
c54c806fcc6e add delete_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 22
diff changeset
145
38
436216d17a1b fix edit and delete
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
146 function editPost(el) {
436216d17a1b fix edit and delete
Franklin Schmidt <fschmidt@gmail.com>
parents: 35
diff changeset
147 ajax(`edit_post.js?post=${getPostId(el)}`);
24
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
148 }
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
149
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
150 function openEditPost(postId,text) {
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
151 currentPostId = postId;
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
152 let dialog = document.querySelector('dialog[edit_post]');
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
153 let textarea = dialog.querySelector('textarea');
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
154 textarea.value = text;
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
155 openModal(dialog);
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
156 }
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
157
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
158 function savePost(el) {
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
159 let text = document.querySelector('dialog[edit_post] textarea').value;
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
160 closeModal(el);
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
161 ajax(`save_post.js?post=${currentPostId}`,`content=${encodeURIComponent(text)}`);
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
162 }
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
163
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
164 function edited(postId,html) {
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
165 let div = document.querySelector(`div[post="${postId}"]`);
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
166 if( div ) {
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
167 div.outerHTML = html;
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
168 fixPosts();
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
169 }
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
170 }
af41be2dcdec add edit_post
Franklin Schmidt <fschmidt@gmail.com>
parents: 23
diff changeset
171
77
Franklin Schmidt <fschmidt@gmail.com>
parents: 76
diff changeset
172 function active() {
Franklin Schmidt <fschmidt@gmail.com>
parents: 76
diff changeset
173 let url = 'active.js';
Franklin Schmidt <fschmidt@gmail.com>
parents: 76
diff changeset
174 if( currentChatId )
Franklin Schmidt <fschmidt@gmail.com>
parents: 76
diff changeset
175 url += `?chat=${currentChatId}`;
Franklin Schmidt <fschmidt@gmail.com>
parents: 76
diff changeset
176 ajax(url);
Franklin Schmidt <fschmidt@gmail.com>
parents: 76
diff changeset
177 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 76
diff changeset
178
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
179 function added(html) {
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
180 let input = document.querySelector('div[input]');
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
181 input.insertAdjacentHTML('beforebegin',html);
19
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
182 fixPosts();
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
183 input.scrollIntoView({block: 'end'});
40
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 39
diff changeset
184 if( document.hasFocus() )
77
Franklin Schmidt <fschmidt@gmail.com>
parents: 76
diff changeset
185 active();
12
9f45d32670ae server push
Franklin Schmidt <fschmidt@gmail.com>
parents: 11
diff changeset
186 }
15
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
187
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
188 function getChats(chatId,updated) {
15
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
189 let first = document.querySelector('div[chat]');
53
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
190 if( !first || first.getAttribute('chat') !== chatId ) {
15
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
191 // console.log('getChats');
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
192 ajax('get_chats.js');
53
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
193 } else if( first && currentChatId !== chatId ) {
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
194 incUnread(first);
15
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
195 }
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
196 if( updated )
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
197 lastUpdate = updated;
40
7ea33179592a email notification
Franklin Schmidt <fschmidt@gmail.com>
parents: 39
diff changeset
198 if( !document.hasFocus() ) {
31
a0820965ba04 notify sound
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
199 document.title = title + ' *';
a0820965ba04 notify sound
Franklin Schmidt <fschmidt@gmail.com>
parents: 30
diff changeset
200 }
15
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
201 }
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
202
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
203 function gotChats(html) {
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
204 document.querySelector('div[chats]').innerHTML = html;
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
205 if( currentChatId ) {
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
206 let current = document.querySelector(`div[chat="${currentChatId}"]`);
16
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
207 if( current ) {
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
208 current.setAttribute('selected','');
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
209 current.scrollIntoViewIfNeeded(false);
61
Franklin Schmidt <fschmidt@gmail.com>
parents: 60
diff changeset
210 clearUnread();
16
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
211 } else {
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
212 currentChatId = null;
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
213 document.querySelector('div[posts]').innerHTML = '';
82b55186a4a0 fix delete_chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 15
diff changeset
214 }
15
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
215 }
8b8905f63d80 add get_chats
Franklin Schmidt <fschmidt@gmail.com>
parents: 14
diff changeset
216 }
18
0721dcf222e1 * alert
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
217
0721dcf222e1 * alert
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
218 window.onfocus = function() {
33
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
219 // console.log('onfocus');
18
0721dcf222e1 * alert
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
220 document.title = title;
77
Franklin Schmidt <fschmidt@gmail.com>
parents: 76
diff changeset
221 active();
18
0721dcf222e1 * alert
Franklin Schmidt <fschmidt@gmail.com>
parents: 17
diff changeset
222 };
19
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
223
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
224 let urlRegex = /(^|\s)(https?:\/\/\S+)/g;
66
Franklin Schmidt <fschmidt@gmail.com>
parents: 65
diff changeset
225 let filebinUrlRegex = /(^|\s)(https:\/\/filebin.net\/[0-9a-f]+\/(\S+))/g;
19
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
226
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
227 function urlsToLinks(text) {
66
Franklin Schmidt <fschmidt@gmail.com>
parents: 65
diff changeset
228 text = text.replace( filebinUrlRegex, '$1<a target="_blank" href="$2">$3</a>' );
Franklin Schmidt <fschmidt@gmail.com>
parents: 65
diff changeset
229 text = text.replace( urlRegex, '$1<a target="_blank" href="$2">$2</a>' );
Franklin Schmidt <fschmidt@gmail.com>
parents: 65
diff changeset
230 return text;
19
Franklin Schmidt <fschmidt@gmail.com>
parents: 18
diff changeset
231 }
22
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
232
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
233 let currentPulldown = null;
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
234 let newPulldown = null;
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
235
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
236 function clickMenu(clicked,display) {
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
237 //console.log("clickMenu");
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
238 let pulldown = clicked.parentNode.querySelector('div');
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
239 if( pulldown !== currentPulldown ) {
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
240 pulldown.style.display = display || "block";
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
241 newPulldown = pulldown;
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
242 window.onclick = function() {
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
243 //console.log("window.onclick");
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
244 if( currentPulldown ) {
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
245 currentPulldown.style.display = "none";
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
246 if( !newPulldown )
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
247 window.onclick = null;
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
248 }
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
249 currentPulldown = newPulldown;
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
250 newPulldown = null;
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
251 };
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
252 pulldown.scrollIntoViewIfNeeded(false);
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
253 }
949914955bfe add menu
Franklin Schmidt <fschmidt@gmail.com>
parents: 20
diff changeset
254 }
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
255
42
Franklin Schmidt <fschmidt@gmail.com>
parents: 40
diff changeset
256 function heartbeat() {
75
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
257 let url = `heartbeat.js?last_update=${lastUpdate}`;
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
258 if( currentChatId )
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
259 url += `&chat=${currentChatId}`;
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
260 ajax(url);
42
Franklin Schmidt <fschmidt@gmail.com>
parents: 40
diff changeset
261 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 40
diff changeset
262
Franklin Schmidt <fschmidt@gmail.com>
parents: 40
diff changeset
263 setInterval( heartbeat, 10000 );
73
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
264 heartbeat();
30
f4708943f29e add heartbeat
Franklin Schmidt <fschmidt@gmail.com>
parents: 27
diff changeset
265
33
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
266 let online = {};
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
267
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
268 function showOnline() {
42
Franklin Schmidt <fschmidt@gmail.com>
parents: 40
diff changeset
269 let old = Date.now() - 70000;
72
Franklin Schmidt <fschmidt@gmail.com>
parents: 66
diff changeset
270 let spans = document.querySelectorAll('span[online]');
Franklin Schmidt <fschmidt@gmail.com>
parents: 66
diff changeset
271 for( let span of spans ) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 66
diff changeset
272 let id = span.getAttribute('online');
73
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
273 let wasOnline = online[id];
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
274 let isOnline = !!wasOnline && wasOnline > old;
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
275 span.setAttribute('is_online',isOnline);
33
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
276 }
e2b7f6393dab add online
Franklin Schmidt <fschmidt@gmail.com>
parents: 32
diff changeset
277 }
53
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
278
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
279 function clearUnread() {
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
280 let span = document.querySelector(`div[chat="${currentChatId}"] span[unread]`);
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
281 span.setAttribute('unread','0');
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
282 span.textContent = '0';
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
283 }
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
284
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
285 function incUnread(div) {
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
286 let span = div.querySelector('span[unread]');
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
287 let n = parseInt(span.getAttribute('unread')) + 1;
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
288 span.setAttribute('unread',n);
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
289 span.textContent = n;
9298b04607ae add unread
Franklin Schmidt <fschmidt@gmail.com>
parents: 48
diff changeset
290 }
56
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
291
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
292 function invite() {
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
293 let email = document.querySelector('input[type=email]').value;
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
294 ajax(`invite.js?email=${encodeURIComponent(email)}`);
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
295 }
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
296
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
297 function openInvite(email) {
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
298 let dialog = document.querySelector('dialog[invite]');
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
299 let span = dialog.querySelector('span[email]');
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
300 span.textContent = email;
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
301 openModal(dialog);
323ddacc1593 start invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 54
diff changeset
302 }
60
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
303
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
304 function gotoInvite(el) {
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
305 let email = document.querySelector('dialog[invite] span[email]').textContent;
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
306 closeModal(el);
83
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 81
diff changeset
307 location = `chat?with=${encodeURIComponent(email)}`;
60
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
308 ajax(`save_post.js?post=${currentPostId}`,`content=${encodeURIComponent(text)}`);
3521166513b3 finish invite
Franklin Schmidt <fschmidt@gmail.com>
parents: 59
diff changeset
309 }
63
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
310
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
311 function uploadFile() {
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
312 document.querySelector('input[type="file"]').click();
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
313 }
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
314
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
315 function addFileUrl(url) {
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
316 let textarea = document.querySelector('div[input] textarea');
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
317 let text = textarea.value;
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
318 if( /\S$/.test(text) )
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
319 text += ' ';
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
320 text += url;
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
321 textarea.value = text;
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
322 }
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
323
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
324 function uploadToFilebin(fileName,fileContent) {
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
325 //console.log(fileContent.byteLength);
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
326 let request = new XMLHttpRequest();
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
327 let url = filebinUrl + fileName;
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
328 request.open( 'POST', url );
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
329 request.onload = function() {
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
330 if( request.status !== 201 ) {
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
331 let err = 'upload failed: ' + request.status;
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
332 if( request.responseText ) {
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
333 err += '\n' + request.responseText;
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
334 }
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
335 console.log(err);
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
336 ajax( '/error_log.js', 'err='+encodeURIComponent(err) );
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
337 return;
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
338 }
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
339 addFileUrl(url);
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
340 };
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
341 request.send(fileContent);
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
342 }
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
343
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
344 function loadedFile(input) {
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
345 let file = input.files[0];
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
346 input.value = null;
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
347 console.log(file);
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
348 let reader = new FileReader();
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
349 reader.onload = function() {
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
350 uploadToFilebin(file.name,reader.result);
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
351 };
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
352 reader.readAsArrayBuffer(file);
afd5ab5b02a2 file upload
Franklin Schmidt <fschmidt@gmail.com>
parents: 61
diff changeset
353 }
65
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
354
73
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
355 let times = [
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
356 {
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
357 time: 1000*60*60*24,
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
358 unit: 'day'
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
359 },
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
360 {
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
361 time: 1000*60*60,
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
362 unit: 'hour'
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
363 },
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
364 {
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
365 time: 1000*60,
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
366 unit: 'minute'
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
367 }
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
368 ];
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
369
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
370 function ago(time) {
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
371 for( let t of times ) {
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
372 let n = Math.floor(time / t.time);
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
373 if( n > 0 ) {
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
374 let s = `${n} ${t.unit}`;
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
375 if( n > 1 )
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
376 s = s + 's';
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
377 return s + ' ago';
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
378 }
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
379 }
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
380 return 'just now';
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
381 end
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
382 }
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
383
65
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
384 function openPeople() {
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
385 let dialog = document.querySelector('dialog[people]');
73
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
386 let spans = dialog.querySelectorAll('span[last_seen]');
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
387 let now = Date.now();
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
388 let old = now - 70000;
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
389 for( let span of spans ) {
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
390 let id = span.getAttribute('last_seen');
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
391 let s;
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
392 let lastOnline = online[id];
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
393 if( !lastOnline ) {
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
394 s = '';
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
395 } else if( lastOnline > old ) {
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
396 s = 'Active now';
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
397 } else {
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
398 s = 'Last seen ' + ago(now - lastOnline);
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
399 }
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
400 console.log(`${id} ${s}`);
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
401 span.textContent = s;
a63faf49e1d7 last seen
Franklin Schmidt <fschmidt@gmail.com>
parents: 72
diff changeset
402 }
65
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
403 openModal(dialog);
6cfef9850520 people popup
Franklin Schmidt <fschmidt@gmail.com>
parents: 63
diff changeset
404 }
75
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
405
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
406 function readUpTo(userId,userNameHtml,unread) {
76
Franklin Schmidt <fschmidt@gmail.com>
parents: 75
diff changeset
407 //console.log(`readUpTo ${unread}`);
75
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
408 let divs = document.querySelectorAll('div[post]');
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
409 if( unread >= divs.length )
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
410 return;
76
Franklin Schmidt <fschmidt@gmail.com>
parents: 75
diff changeset
411 let div = divs[divs.length - unread - 1];
Franklin Schmidt <fschmidt@gmail.com>
parents: 75
diff changeset
412 let old = document.querySelector(`div[unread][user="${userId}"]`);
Franklin Schmidt <fschmidt@gmail.com>
parents: 75
diff changeset
413 if( old ) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 75
diff changeset
414 //console.log(`was ${div.getAttribute('unread')}`);
Franklin Schmidt <fschmidt@gmail.com>
parents: 75
diff changeset
415 if( div == old.parentNode )
Franklin Schmidt <fschmidt@gmail.com>
parents: 75
diff changeset
416 return;
Franklin Schmidt <fschmidt@gmail.com>
parents: 75
diff changeset
417 old.outerHTML = '';
Franklin Schmidt <fschmidt@gmail.com>
parents: 75
diff changeset
418 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 75
diff changeset
419 //console.log('readUpTo');
75
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
420 let html = `<div user="${userId}" unread="${unread}">read by ${userNameHtml}</div>`;
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
421 div.insertAdjacentHTML('beforeend',html);
81
Franklin Schmidt <fschmidt@gmail.com>
parents: 79
diff changeset
422 if( !old ) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 79
diff changeset
423 let dy = document.querySelector('div[unread]').clientHeight;
Franklin Schmidt <fschmidt@gmail.com>
parents: 79
diff changeset
424 document.querySelector('div[main]').scrollBy(0,dy);
Franklin Schmidt <fschmidt@gmail.com>
parents: 79
diff changeset
425 }
75
377bdda60f0b read up to
Franklin Schmidt <fschmidt@gmail.com>
parents: 73
diff changeset
426 }
79
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
427
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
428 function replyPost(el) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
429 let postId = getPostId(el);
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
430 let div = document.querySelector('div[reply]');
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
431 div.removeAttribute('hidden');
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
432 div.setAttribute('reply',postId);
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
433 document.querySelector('div[reply] div[text]').innerHTML = document.querySelector(`div[post="${postId}"] div[text]`).innerHTML
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
434 let a = document.querySelector('div[reply] a');
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
435 a.href = `#p${postId}`;
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
436 a.textContent = document.querySelector(`div[post="${postId}"] span[when]`).textContent;
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
437 document.querySelector('div[input] textarea').focus();
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
438 document.querySelector('div[input]').scrollIntoView({block: 'end'});
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
439 }
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
440
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
441 function closeReply() {
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
442 let div = document.querySelector('div[reply]');
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
443 div.setAttribute('hidden','');
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
444 div.setAttribute('reply','');
Franklin Schmidt <fschmidt@gmail.com>
parents: 78
diff changeset
445 }
83
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 81
diff changeset
446
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 81
diff changeset
447 function openAddToChat() {
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 81
diff changeset
448 let dialog = document.querySelector('dialog[add]');
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 81
diff changeset
449 dialog.querySelector('input[name=email]').value = '';
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 81
diff changeset
450 openModal(dialog);
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 81
diff changeset
451 }
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 81
diff changeset
452
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 81
diff changeset
453 function addToChat() {
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 81
diff changeset
454 let dialog = document.querySelector('dialog[add]');
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 81
diff changeset
455 let email = dialog.querySelector('input[name=email]').value;
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 81
diff changeset
456 ajax(`add_to_chat.js?chat=${currentChatId}&email=${encodeURIComponent(email)}`);
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 81
diff changeset
457 }
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 81
diff changeset
458
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 81
diff changeset
459 function addToChatError(msg) {
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 81
diff changeset
460 let dialog = document.querySelector('dialog[add]');
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 81
diff changeset
461 dialog.querySelector('span[error]').textContent = msg;
a47036fd0158 group chat
Franklin Schmidt <fschmidt@gmail.com>
parents: 81
diff changeset
462 }