2022-09-11 13:42:41 -04:00
|
|
|
import {LitElement, html, css, guard, until} from './lit-all.min.js';
|
2022-09-06 19:26:43 -04:00
|
|
|
import * as tfrpc from '/static/tfrpc.js';
|
2022-09-09 22:56:15 -04:00
|
|
|
import {styles} from './tf-styles.js';
|
2022-09-06 19:26:43 -04:00
|
|
|
|
|
|
|
class TfElement extends LitElement {
|
|
|
|
static get properties() {
|
|
|
|
return {
|
|
|
|
whoami: {type: String},
|
|
|
|
hash: {type: String},
|
|
|
|
unread: {type: Array},
|
2022-09-09 22:56:15 -04:00
|
|
|
tab: {type: String},
|
|
|
|
broadcasts: {type: Array},
|
|
|
|
connections: {type: Array},
|
2022-09-14 19:33:57 -04:00
|
|
|
loading: {type: Boolean},
|
|
|
|
loaded: {type: Boolean},
|
|
|
|
following: {type: Array},
|
|
|
|
users: {type: Object},
|
2022-11-09 19:03:39 -05:00
|
|
|
ids: {type: Array},
|
2023-06-21 20:27:27 -04:00
|
|
|
tags: {type: Array},
|
2022-09-06 19:26:43 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-09-09 22:56:15 -04:00
|
|
|
static styles = styles;
|
|
|
|
|
2022-09-06 19:26:43 -04:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
let self = this;
|
|
|
|
this.hash = '#';
|
|
|
|
this.unread = [];
|
2022-09-09 22:56:15 -04:00
|
|
|
this.tab = 'news';
|
|
|
|
this.broadcasts = [];
|
|
|
|
this.connections = [];
|
2022-09-14 19:33:57 -04:00
|
|
|
this.following = [];
|
|
|
|
this.users = {};
|
|
|
|
this.loaded = false;
|
2023-06-21 20:27:27 -04:00
|
|
|
this.tags = [];
|
2024-02-24 11:09:34 -05:00
|
|
|
tfrpc.rpc.getBroadcasts().then((b) => {
|
|
|
|
self.broadcasts = b || [];
|
|
|
|
});
|
|
|
|
tfrpc.rpc.getConnections().then((c) => {
|
|
|
|
self.connections = c || [];
|
|
|
|
});
|
|
|
|
tfrpc.rpc.getHash().then((hash) => self.set_hash(hash));
|
2022-09-06 19:26:43 -04:00
|
|
|
tfrpc.register(function hashChanged(hash) {
|
2022-09-14 20:16:37 -04:00
|
|
|
self.set_hash(hash);
|
2022-09-06 19:26:43 -04:00
|
|
|
});
|
|
|
|
tfrpc.register(async function notifyNewMessage(id) {
|
|
|
|
await self.fetch_new_message(id);
|
|
|
|
});
|
2022-09-09 22:56:15 -04:00
|
|
|
tfrpc.register(function set(name, value) {
|
|
|
|
if (name === 'broadcasts') {
|
|
|
|
self.broadcasts = value;
|
|
|
|
} else if (name === 'connections') {
|
|
|
|
self.connections = value;
|
2024-04-13 19:52:40 -04:00
|
|
|
} else if (name === 'identity') {
|
|
|
|
self.whoami = value;
|
2022-09-09 22:56:15 -04:00
|
|
|
}
|
|
|
|
});
|
2022-11-16 19:30:58 -05:00
|
|
|
this.initial_load();
|
|
|
|
}
|
|
|
|
|
|
|
|
async initial_load() {
|
2024-04-13 19:52:40 -04:00
|
|
|
let whoami = await tfrpc.rpc.getActiveIdentity();
|
2022-11-16 19:30:58 -05:00
|
|
|
let ids = (await tfrpc.rpc.getIdentities()) || [];
|
|
|
|
this.whoami = whoami ?? (ids.length ? ids[0] : undefined);
|
|
|
|
this.ids = ids;
|
2022-09-06 19:26:43 -04:00
|
|
|
}
|
|
|
|
|
2022-09-14 20:16:37 -04:00
|
|
|
set_hash(hash) {
|
2024-11-09 18:04:58 -05:00
|
|
|
this.hash = decodeURIComponent(hash || '#');
|
2022-09-14 20:16:37 -04:00
|
|
|
if (this.hash.startsWith('#q=')) {
|
|
|
|
this.tab = 'search';
|
2022-09-25 08:33:54 -04:00
|
|
|
} else if (this.hash === '#connections') {
|
|
|
|
this.tab = 'connections';
|
2023-06-14 18:51:58 -04:00
|
|
|
} else if (this.hash === '#mentions') {
|
|
|
|
this.tab = 'mentions';
|
2023-08-16 18:48:59 -04:00
|
|
|
} else if (this.hash.startsWith('#sql=')) {
|
|
|
|
this.tab = 'query';
|
2022-09-14 20:16:37 -04:00
|
|
|
} else {
|
|
|
|
this.tab = 'news';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-11 13:42:41 -04:00
|
|
|
async fetch_about(ids, users) {
|
2022-09-06 19:26:43 -04:00
|
|
|
const k_cache_version = 1;
|
|
|
|
let cache = await tfrpc.rpc.databaseGet('about');
|
|
|
|
cache = cache ? JSON.parse(cache) : {};
|
|
|
|
if (cache.version !== k_cache_version) {
|
|
|
|
cache = {
|
|
|
|
version: k_cache_version,
|
|
|
|
about: {},
|
|
|
|
last_row_id: 0,
|
|
|
|
};
|
|
|
|
}
|
2024-02-24 11:09:34 -05:00
|
|
|
let max_row_id = (
|
|
|
|
await tfrpc.rpc.query(
|
|
|
|
`
|
2022-09-06 19:26:43 -04:00
|
|
|
SELECT MAX(rowid) AS max_row_id FROM messages
|
2024-02-24 11:09:34 -05:00
|
|
|
`,
|
|
|
|
[]
|
|
|
|
)
|
|
|
|
)[0].max_row_id;
|
2022-09-06 19:26:43 -04:00
|
|
|
for (let id of Object.keys(cache.about)) {
|
|
|
|
if (ids.indexOf(id) == -1) {
|
|
|
|
delete cache.about[id];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let abouts = await tfrpc.rpc.query(
|
|
|
|
`
|
|
|
|
SELECT
|
2024-02-28 20:31:25 -05:00
|
|
|
messages.id, messages.previous, messages.author, messages.sequence, messages.timestamp, messages.hash, json(messages.content) AS content, messages.signature
|
2022-09-06 19:26:43 -04:00
|
|
|
FROM
|
|
|
|
messages,
|
|
|
|
json_each(?1) AS following
|
|
|
|
WHERE
|
|
|
|
messages.author = following.value AND
|
|
|
|
messages.rowid > ?3 AND
|
|
|
|
messages.rowid <= ?4 AND
|
|
|
|
json_extract(messages.content, '$.type') = 'about'
|
|
|
|
UNION
|
|
|
|
SELECT
|
2024-02-28 20:31:25 -05:00
|
|
|
messages.id, messages.previous, messages.author, messages.sequence, messages.timestamp, messages.hash, json(messages.content) AS content, messages.signature
|
2022-09-06 19:26:43 -04:00
|
|
|
FROM
|
|
|
|
messages,
|
|
|
|
json_each(?2) AS following
|
|
|
|
WHERE
|
|
|
|
messages.author = following.value AND
|
|
|
|
messages.rowid <= ?4 AND
|
|
|
|
json_extract(messages.content, '$.type') = 'about'
|
|
|
|
ORDER BY messages.author, messages.sequence
|
|
|
|
`,
|
|
|
|
[
|
2024-02-24 11:09:34 -05:00
|
|
|
JSON.stringify(ids.filter((id) => cache.about[id])),
|
|
|
|
JSON.stringify(ids.filter((id) => !cache.about[id])),
|
2022-09-06 19:26:43 -04:00
|
|
|
cache.last_row_id,
|
|
|
|
max_row_id,
|
2024-02-24 11:09:34 -05:00
|
|
|
]
|
|
|
|
);
|
2022-09-06 19:26:43 -04:00
|
|
|
for (let about of abouts) {
|
|
|
|
let content = JSON.parse(about.content);
|
|
|
|
if (content.about === about.author) {
|
|
|
|
delete content.type;
|
|
|
|
delete content.about;
|
2024-02-24 11:09:34 -05:00
|
|
|
cache.about[about.author] = Object.assign(
|
|
|
|
cache.about[about.author] || {},
|
|
|
|
content
|
|
|
|
);
|
2022-09-06 19:26:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
cache.last_row_id = max_row_id;
|
|
|
|
await tfrpc.rpc.databaseSet('about', JSON.stringify(cache));
|
2022-09-11 13:42:41 -04:00
|
|
|
users = users || {};
|
2022-09-06 19:26:43 -04:00
|
|
|
for (let id of Object.keys(cache.about)) {
|
|
|
|
users[id] = Object.assign(users[id] || {}, cache.about[id]);
|
|
|
|
}
|
2022-09-11 13:42:41 -04:00
|
|
|
return Object.assign({}, users);
|
2022-09-06 19:26:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async fetch_new_message(id) {
|
|
|
|
let messages = await tfrpc.rpc.query(
|
|
|
|
`
|
2024-02-28 20:41:27 -05:00
|
|
|
SELECT messages.id, previous, author, sequence, timestamp, hash, json(content) AS content, signature
|
2022-09-06 19:26:43 -04:00
|
|
|
FROM messages
|
|
|
|
JOIN json_each(?) AS following ON messages.author = following.value
|
|
|
|
WHERE messages.id = ?
|
|
|
|
`,
|
2024-02-24 11:09:34 -05:00
|
|
|
[JSON.stringify(this.following), id]
|
|
|
|
);
|
2022-09-14 19:33:57 -04:00
|
|
|
if (messages && messages.length) {
|
|
|
|
this.unread = [...this.unread, ...messages];
|
2023-11-18 10:58:16 -05:00
|
|
|
this.unread = this.unread.slice(this.unread.length - 1024);
|
2022-09-06 19:26:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-16 19:30:58 -05:00
|
|
|
async _handle_whoami_changed(event) {
|
|
|
|
let old_id = this.whoami;
|
|
|
|
let new_id = event.srcElement.selected;
|
|
|
|
console.log('received', new_id);
|
|
|
|
if (this.whoami !== new_id) {
|
|
|
|
console.log(event);
|
|
|
|
this.whoami = new_id;
|
|
|
|
console.log(`whoami ${old_id} => ${new_id}`);
|
|
|
|
await tfrpc.rpc.localStorageSet('whoami', new_id);
|
2022-09-06 19:26:43 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-11 13:42:41 -04:00
|
|
|
async create_identity() {
|
2024-02-24 11:09:34 -05:00
|
|
|
if (confirm('Are you sure you want to create a new identity?')) {
|
2022-09-11 13:42:41 -04:00
|
|
|
await tfrpc.rpc.createIdentity();
|
2022-11-09 19:03:39 -05:00
|
|
|
this.ids = (await tfrpc.rpc.getIdentities()) || [];
|
2023-08-02 20:30:48 -04:00
|
|
|
if (this.ids && !this.whoami) {
|
|
|
|
this.whoami = this.ids[0];
|
|
|
|
}
|
2022-09-09 22:56:15 -04:00
|
|
|
}
|
2022-09-06 19:26:43 -04:00
|
|
|
}
|
|
|
|
|
2023-06-21 20:27:27 -04:00
|
|
|
async load_recent_tags() {
|
2023-07-05 20:37:16 -04:00
|
|
|
let start = new Date();
|
2024-02-24 11:09:34 -05:00
|
|
|
this.tags = await tfrpc.rpc.query(
|
|
|
|
`
|
2023-07-05 20:37:16 -04:00
|
|
|
WITH
|
2024-02-28 20:01:52 -05:00
|
|
|
recent AS (SELECT id, json(content) AS content FROM messages
|
2023-07-05 20:37:16 -04:00
|
|
|
WHERE messages.timestamp > ? AND json_extract(content, '$.type') = 'post'
|
|
|
|
ORDER BY timestamp DESC LIMIT 1024),
|
2023-07-16 19:41:41 -04:00
|
|
|
recent_channels AS (SELECT recent.id, '#' || json_extract(content, '$.channel') AS tag
|
2023-07-05 20:37:16 -04:00
|
|
|
FROM recent
|
|
|
|
WHERE json_extract(content, '$.channel') IS NOT NULL),
|
2023-07-16 19:41:41 -04:00
|
|
|
recent_mentions AS (SELECT recent.id, json_extract(mention.value, '$.link') AS tag
|
2023-07-05 20:37:16 -04:00
|
|
|
FROM recent, json_each(recent.content, '$.mentions') AS mention
|
|
|
|
WHERE json_valid(mention.value) AND tag LIKE '#%'),
|
2023-07-26 19:56:40 -04:00
|
|
|
combined AS (SELECT id, tag FROM recent_channels UNION ALL SELECT id, tag FROM recent_mentions),
|
|
|
|
by_message AS (SELECT DISTINCT id, tag FROM combined)
|
|
|
|
SELECT tag, COUNT(*) AS count FROM by_message GROUP BY tag ORDER BY count DESC LIMIT 10
|
2024-02-24 11:09:34 -05:00
|
|
|
`,
|
|
|
|
[new Date() - 7 * 24 * 60 * 60 * 1000]
|
|
|
|
);
|
2023-07-05 20:37:16 -04:00
|
|
|
console.log('tags took', (new Date() - start) / 1000.0, 'seconds');
|
2023-06-21 20:27:27 -04:00
|
|
|
}
|
|
|
|
|
2022-09-14 19:33:57 -04:00
|
|
|
async load() {
|
|
|
|
let whoami = this.whoami;
|
2023-06-21 20:27:27 -04:00
|
|
|
let tags = this.load_recent_tags();
|
2024-11-25 11:20:01 -05:00
|
|
|
let following = await tfrpc.rpc.following([whoami], 3);
|
2023-10-30 12:18:07 -04:00
|
|
|
let users = {};
|
2023-11-05 08:07:07 -05:00
|
|
|
let by_count = [];
|
2023-11-02 20:45:30 -04:00
|
|
|
for (let [id, v] of Object.entries(following)) {
|
|
|
|
users[id] = {
|
|
|
|
following: v.of,
|
|
|
|
blocking: v.ob,
|
|
|
|
followed: v.if,
|
|
|
|
blocked: v.ib,
|
|
|
|
};
|
2023-11-05 08:07:07 -05:00
|
|
|
by_count.push({count: v.of, id: id});
|
2023-11-02 20:45:30 -04:00
|
|
|
}
|
2023-11-05 08:07:07 -05:00
|
|
|
console.log(by_count.sort((x, y) => y.count - x.count).slice(0, 20));
|
2024-04-21 14:18:06 -04:00
|
|
|
let start_time = new Date();
|
2023-11-02 20:45:30 -04:00
|
|
|
users = await this.fetch_about(Object.keys(following).sort(), users);
|
2024-04-24 19:23:13 -04:00
|
|
|
console.log(
|
|
|
|
'about took',
|
|
|
|
(new Date() - start_time) / 1000.0,
|
|
|
|
'seconds for',
|
|
|
|
Object.keys(users).length,
|
|
|
|
'users'
|
|
|
|
);
|
2023-11-02 20:45:30 -04:00
|
|
|
this.following = Object.keys(following);
|
2022-09-14 19:33:57 -04:00
|
|
|
this.users = users;
|
2023-06-21 20:27:27 -04:00
|
|
|
await tags;
|
2022-11-16 19:30:58 -05:00
|
|
|
console.log(`load finished ${whoami} => ${this.whoami}`);
|
2022-09-14 19:33:57 -04:00
|
|
|
this.whoami = whoami;
|
|
|
|
this.loaded = whoami;
|
|
|
|
}
|
|
|
|
|
|
|
|
render_tab() {
|
|
|
|
let following = this.following;
|
|
|
|
let users = this.users;
|
2022-09-11 13:42:41 -04:00
|
|
|
if (this.tab === 'news') {
|
|
|
|
return html`
|
2024-02-24 11:09:34 -05:00
|
|
|
<tf-tab-news
|
|
|
|
id="tf-tab-news"
|
|
|
|
.following=${this.following}
|
|
|
|
whoami=${this.whoami}
|
|
|
|
.users=${this.users}
|
|
|
|
hash=${this.hash}
|
|
|
|
.unread=${this.unread}
|
|
|
|
@refresh=${() => (this.unread = [])}
|
2024-05-11 10:40:34 -04:00
|
|
|
?loading=${this.loading}
|
2024-02-24 11:09:34 -05:00
|
|
|
></tf-tab-news>
|
2022-09-11 13:42:41 -04:00
|
|
|
`;
|
|
|
|
} else if (this.tab === 'connections') {
|
|
|
|
return html`
|
2024-02-24 11:09:34 -05:00
|
|
|
<tf-tab-connections
|
|
|
|
.users=${this.users}
|
|
|
|
.connections=${this.connections}
|
|
|
|
.broadcasts=${this.broadcasts}
|
|
|
|
></tf-tab-connections>
|
2022-09-11 13:42:41 -04:00
|
|
|
`;
|
2023-06-14 18:51:58 -04:00
|
|
|
} else if (this.tab === 'mentions') {
|
|
|
|
return html`
|
2024-02-24 11:09:34 -05:00
|
|
|
<tf-tab-mentions
|
|
|
|
.following=${this.following}
|
|
|
|
whoami=${this.whoami}
|
|
|
|
.users="${this.users}}"
|
|
|
|
></tf-tab-mentions>
|
2023-06-14 18:51:58 -04:00
|
|
|
`;
|
2022-09-11 13:42:41 -04:00
|
|
|
} else if (this.tab === 'search') {
|
|
|
|
return html`
|
2024-02-24 11:09:34 -05:00
|
|
|
<tf-tab-search
|
|
|
|
.following=${this.following}
|
|
|
|
whoami=${this.whoami}
|
|
|
|
.users=${this.users}
|
|
|
|
query=${this.hash?.startsWith('#q=')
|
|
|
|
? decodeURIComponent(this.hash.substring(3))
|
|
|
|
: null}
|
|
|
|
></tf-tab-search>
|
2022-09-11 13:42:41 -04:00
|
|
|
`;
|
2023-08-16 18:48:59 -04:00
|
|
|
} else if (this.tab === 'query') {
|
|
|
|
return html`
|
2024-02-24 11:09:34 -05:00
|
|
|
<tf-tab-query
|
|
|
|
.following=${this.following}
|
|
|
|
whoami=${this.whoami}
|
|
|
|
.users=${this.users}
|
|
|
|
query=${this.hash?.startsWith('#sql=')
|
|
|
|
? decodeURIComponent(this.hash.substring(5))
|
|
|
|
: null}
|
|
|
|
></tf-tab-query>
|
2023-08-16 18:48:59 -04:00
|
|
|
`;
|
2022-09-10 14:23:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-25 08:33:54 -04:00
|
|
|
async set_tab(tab) {
|
|
|
|
this.tab = tab;
|
|
|
|
if (tab === 'news') {
|
|
|
|
await tfrpc.rpc.setHash('#');
|
|
|
|
} else if (tab === 'connections') {
|
|
|
|
await tfrpc.rpc.setHash('#connections');
|
2023-06-14 18:51:58 -04:00
|
|
|
} else if (tab === 'mentions') {
|
|
|
|
await tfrpc.rpc.setHash('#mentions');
|
2023-08-16 18:48:59 -04:00
|
|
|
} else if (tab === 'query') {
|
|
|
|
await tfrpc.rpc.setHash('#sql=');
|
2022-09-25 08:33:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-23 16:49:33 -05:00
|
|
|
refresh() {
|
|
|
|
tfrpc.rpc.sync();
|
|
|
|
}
|
|
|
|
|
2022-09-06 19:26:43 -04:00
|
|
|
render() {
|
2022-09-09 22:56:15 -04:00
|
|
|
let self = this;
|
2022-09-14 19:33:57 -04:00
|
|
|
|
|
|
|
if (!this.loading && this.whoami && this.loaded !== this.whoami) {
|
|
|
|
this.loading = true;
|
2024-02-24 11:09:34 -05:00
|
|
|
this.load().finally(function () {
|
2022-09-14 19:33:57 -04:00
|
|
|
self.loading = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-01-11 23:23:31 -05:00
|
|
|
const k_tabs = {
|
|
|
|
'📰': 'news',
|
|
|
|
'📡': 'connections',
|
2024-01-28 16:11:24 -05:00
|
|
|
'@': 'mentions',
|
2024-01-11 23:23:31 -05:00
|
|
|
'🔍': 'search',
|
|
|
|
'👩💻': 'query',
|
|
|
|
};
|
|
|
|
|
2022-09-09 22:56:15 -04:00
|
|
|
let tabs = html`
|
2024-04-10 18:43:48 -04:00
|
|
|
<div class="w3-bar w3-theme-l1">
|
2024-11-23 16:49:33 -05:00
|
|
|
<button
|
|
|
|
class="w3-bar-item w3-button w3-circle w3-ripple"
|
|
|
|
@click=${this.refresh}
|
|
|
|
>
|
|
|
|
↻
|
|
|
|
</button>
|
2024-02-24 11:09:34 -05:00
|
|
|
${Object.entries(k_tabs).map(
|
|
|
|
([k, v]) => html`
|
|
|
|
<button
|
|
|
|
title=${v}
|
2024-05-12 07:48:34 -04:00
|
|
|
class="w3-bar-item w3-padding w3-hover-theme tab ${self.tab == v
|
2024-04-04 20:35:09 -04:00
|
|
|
? 'w3-theme-l2'
|
|
|
|
: 'w3-theme-l1'}"
|
2024-02-24 11:09:34 -05:00
|
|
|
@click=${() => self.set_tab(v)}
|
|
|
|
>
|
|
|
|
${k}
|
2024-05-12 07:48:34 -04:00
|
|
|
<span class=${self.tab == v ? '' : 'w3-hide-small'}
|
|
|
|
>${v.charAt(0).toUpperCase() + v.substring(1)}</span
|
|
|
|
>
|
2024-02-24 11:09:34 -05:00
|
|
|
</button>
|
|
|
|
`
|
|
|
|
)}
|
2022-09-09 22:56:15 -04:00
|
|
|
</div>
|
|
|
|
`;
|
2024-02-24 11:09:34 -05:00
|
|
|
let contents = !this.loaded
|
|
|
|
? this.loading
|
2024-05-12 07:48:34 -04:00
|
|
|
? html`<div
|
|
|
|
class="w3-panel w3-theme-l5 w3-card-4 w3-padding-large w3-round-xlarge"
|
|
|
|
>
|
|
|
|
Loading...
|
|
|
|
</div>
|
|
|
|
${this.render_tab()}`
|
2024-02-24 11:09:34 -05:00
|
|
|
: html`<div>Select or create an identity.</div>`
|
|
|
|
: this.render_tab();
|
2022-09-11 13:42:41 -04:00
|
|
|
return html`
|
2024-04-11 18:36:31 -04:00
|
|
|
<div
|
|
|
|
style="width: 100vw; min-height: 100vh; height: 100%"
|
|
|
|
class="w3-theme-dark"
|
|
|
|
>
|
2024-05-02 20:35:00 -04:00
|
|
|
${tabs}
|
2024-04-10 18:43:48 -04:00
|
|
|
<div style="padding: 8px">
|
|
|
|
${this.tags.map(
|
|
|
|
(x) => html`<tf-tag tag=${x.tag} count=${x.count}></tf-tag>`
|
|
|
|
)}
|
|
|
|
${contents}
|
|
|
|
</div>
|
2024-04-04 20:35:09 -04:00
|
|
|
</div>
|
2022-09-06 19:26:43 -04:00
|
|
|
`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-02 20:30:48 -04:00
|
|
|
customElements.define('tf-app', TfElement);
|