2022-09-11 13:42:41 -04:00
|
|
|
import {LitElement, html, unsafeHTML, until} from './lit-all.min.js';
|
|
|
|
import * as tfrpc from '/static/tfrpc.js';
|
|
|
|
import {styles} from './tf-styles.js';
|
|
|
|
|
2022-09-14 19:33:57 -04:00
|
|
|
class TfTabNewsFeedElement extends LitElement {
|
2022-09-11 13:42:41 -04:00
|
|
|
static get properties() {
|
|
|
|
return {
|
|
|
|
whoami: {type: String},
|
|
|
|
users: {type: Object},
|
|
|
|
hash: {type: String},
|
|
|
|
following: {type: Array},
|
2022-09-14 19:33:57 -04:00
|
|
|
messages: {type: Array},
|
2022-09-11 13:42:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static styles = styles;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
let self = this;
|
|
|
|
this.whoami = null;
|
|
|
|
this.users = {};
|
|
|
|
this.hash = '#';
|
|
|
|
this.following = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
async fetch_messages() {
|
|
|
|
if (this.hash.startsWith('#@')) {
|
2022-10-21 19:31:32 -04:00
|
|
|
let r = await tfrpc.rpc.query(
|
2022-09-11 13:42:41 -04:00
|
|
|
`
|
2022-10-21 19:31:32 -04:00
|
|
|
WITH mine AS (SELECT messages.*
|
|
|
|
FROM messages
|
|
|
|
WHERE messages.author = ?
|
|
|
|
ORDER BY sequence DESC
|
|
|
|
LIMIT 20)
|
2022-09-11 13:42:41 -04:00
|
|
|
SELECT messages.*
|
2022-10-21 19:31:32 -04:00
|
|
|
FROM mine
|
|
|
|
JOIN messages_refs ON mine.id = messages_refs.ref
|
|
|
|
JOIN messages ON messages_refs.message = messages.id
|
|
|
|
UNION
|
|
|
|
SELECT * FROM mine
|
2022-09-11 13:42:41 -04:00
|
|
|
`,
|
|
|
|
[
|
|
|
|
this.hash.substring(1),
|
|
|
|
]);
|
2022-10-21 19:31:32 -04:00
|
|
|
return r;
|
2022-09-11 13:42:41 -04:00
|
|
|
} else if (this.hash.startsWith('#%')) {
|
|
|
|
return await tfrpc.rpc.query(
|
|
|
|
`
|
|
|
|
SELECT messages.*
|
|
|
|
FROM messages
|
2022-10-15 09:39:45 -04:00
|
|
|
WHERE id = ?1
|
|
|
|
UNION
|
|
|
|
SELECT messages.*
|
|
|
|
FROM messages JOIN messages_refs
|
|
|
|
ON messages.id = messages_refs.message
|
|
|
|
WHERE messages_refs.ref = ?1
|
2022-09-11 13:42:41 -04:00
|
|
|
`,
|
|
|
|
[
|
|
|
|
this.hash.substring(1),
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
return await tfrpc.rpc.query(
|
|
|
|
`
|
2022-10-21 19:31:32 -04:00
|
|
|
WITH news AS (SELECT messages.*
|
2022-09-11 13:42:41 -04:00
|
|
|
FROM messages
|
|
|
|
JOIN json_each(?) AS following ON messages.author = following.value
|
|
|
|
WHERE messages.timestamp > ?
|
2022-10-21 19:31:32 -04:00
|
|
|
ORDER BY messages.timestamp DESC)
|
|
|
|
SELECT messages.*
|
|
|
|
FROM news
|
|
|
|
JOIN messages_refs ON news.id = messages_refs.ref
|
|
|
|
JOIN messages ON messages_refs.message = messages.id
|
|
|
|
UNION
|
|
|
|
SELECT * FROM news
|
2022-09-11 13:42:41 -04:00
|
|
|
`,
|
|
|
|
[
|
|
|
|
JSON.stringify(this.following),
|
|
|
|
new Date().valueOf() - 24 * 60 * 60 * 1000,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-14 19:33:57 -04:00
|
|
|
render() {
|
|
|
|
if (!this.messages ||
|
|
|
|
this._messages_hash !== this.hash ||
|
|
|
|
this._messages_following !== this.following) {
|
|
|
|
console.log('loading messages');
|
|
|
|
let self = this;
|
|
|
|
this.messages = [];
|
|
|
|
this._messages_hash = this.hash;
|
|
|
|
this._messages_following = this.following;
|
|
|
|
this.fetch_messages().then(function(messages) {
|
|
|
|
self.messages = messages;
|
2022-10-21 19:31:32 -04:00
|
|
|
}).catch(function(error) {
|
|
|
|
alert(JSON.stringify(error, null, 2));
|
2022-09-14 19:33:57 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return html`<tf-news id="news" whoami=${this.whoami} .users=${this.users} .messages=${this.messages} .following=${this.following}></tf-news>`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class TfTabNewsElement extends LitElement {
|
|
|
|
static get properties() {
|
|
|
|
return {
|
|
|
|
whoami: {type: String},
|
|
|
|
users: {type: Object},
|
|
|
|
hash: {type: String},
|
|
|
|
unread: {type: Array},
|
|
|
|
following: {type: Array},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static styles = styles;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
let self = this;
|
|
|
|
this.whoami = null;
|
|
|
|
this.users = {};
|
|
|
|
this.hash = '#';
|
2022-09-11 13:42:41 -04:00
|
|
|
this.unread = [];
|
2022-09-14 19:33:57 -04:00
|
|
|
this.following = [];
|
|
|
|
this.cache = {};
|
2022-09-11 13:42:41 -04:00
|
|
|
}
|
|
|
|
|
2022-09-14 19:33:57 -04:00
|
|
|
show_more() {
|
|
|
|
let unread = this.unread;
|
|
|
|
let news = this.renderRoot?.getElementById('news');
|
|
|
|
if (news) {
|
|
|
|
console.log('injecting messages', news.messages);
|
2022-11-08 20:47:47 -05:00
|
|
|
news.messages = Object.values(Object.fromEntries([...this.unread, ...news.messages].map(x => [x.id, x])));
|
2022-09-14 19:33:57 -04:00
|
|
|
this.dispatchEvent(new CustomEvent('refresh'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
new_messages_text() {
|
|
|
|
if (!this.unread?.length) {
|
|
|
|
return 'No new messages.';
|
|
|
|
}
|
|
|
|
let counts = {};
|
|
|
|
for (let message of this.unread) {
|
2022-09-25 08:33:54 -04:00
|
|
|
let type = 'private';
|
2022-09-14 19:33:57 -04:00
|
|
|
try {
|
|
|
|
type = JSON.parse(message.content).type || type;
|
|
|
|
} catch {
|
|
|
|
}
|
|
|
|
counts[type] = (counts[type] || 0) + 1;
|
2022-09-11 13:42:41 -04:00
|
|
|
}
|
2022-09-14 19:33:57 -04:00
|
|
|
return 'Show New: ' + Object.keys(counts).sort().map(x => (counts[x].toString() + ' ' + x + 's')).join(', ');
|
2022-09-11 13:42:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
let profile = this.hash.startsWith('#@') ?
|
|
|
|
html`<tf-profile id=${this.hash.substring(1)} whoami=${this.whoami} .users=${this.users}></tf-profile>` : undefined;
|
|
|
|
return html`
|
2022-09-14 19:33:57 -04:00
|
|
|
<div><input type="button" value=${this.new_messages_text()} @click=${this.show_more}></input></div>
|
2022-09-11 13:42:41 -04:00
|
|
|
<a target="_top" href="#" ?hidden=${this.hash.length <= 1}>🏠Home</a>
|
|
|
|
<div>Welcome, <tf-user id=${this.whoami} .users=${this.users}></tf-user>!</div>
|
|
|
|
<div><tf-compose whoami=${this.whoami} .users=${this.users}></tf-compose></div>
|
|
|
|
${profile}
|
2022-09-14 19:33:57 -04:00
|
|
|
<tf-tab-news-feed id="news" whoami=${this.whoami} .users=${this.users} .following=${this.following} hash=${this.hash}></tf-tab-news-feed>
|
2022-09-11 13:42:41 -04:00
|
|
|
`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-14 19:33:57 -04:00
|
|
|
customElements.define('tf-tab-news-feed', TfTabNewsFeedElement);
|
2022-09-11 13:42:41 -04:00
|
|
|
customElements.define('tf-tab-news', TfTabNewsElement);
|