2023-05-02 12:47:27 -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';
|
|
|
|
|
|
|
|
class TfTabNewsFeedElement extends LitElement {
|
|
|
|
static get properties() {
|
|
|
|
return {
|
|
|
|
whoami: {type: String},
|
|
|
|
users: {type: Object},
|
|
|
|
hash: {type: String},
|
|
|
|
following: {type: Array},
|
|
|
|
messages: {type: Array},
|
|
|
|
drafts: {type: Object},
|
|
|
|
expanded: {type: Object},
|
2024-11-30 15:05:14 -05:00
|
|
|
channels_unread: {type: Object},
|
|
|
|
loading: {type: Number},
|
|
|
|
time_range: {type: Array},
|
2023-05-02 12:47:27 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static styles = styles;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
let self = this;
|
|
|
|
this.whoami = null;
|
|
|
|
this.users = {};
|
|
|
|
this.hash = '#';
|
|
|
|
this.following = [];
|
|
|
|
this.drafts = {};
|
|
|
|
this.expanded = {};
|
2024-11-30 15:05:14 -05:00
|
|
|
this.channels_unread = {};
|
|
|
|
this.start_time = (new Date()).valueOf();
|
|
|
|
this.time_range = [0, 0];
|
2023-05-02 12:47:27 -04:00
|
|
|
}
|
|
|
|
|
2024-11-30 15:05:14 -05:00
|
|
|
channel() {
|
|
|
|
return this.hash.startsWith('##') ? this.hash.substring(2) : '';
|
|
|
|
}
|
|
|
|
|
|
|
|
async fetch_messages(start_time, end_time) {
|
2023-05-02 12:47:27 -04:00
|
|
|
if (this.hash.startsWith('#@')) {
|
|
|
|
let r = await tfrpc.rpc.query(
|
|
|
|
`
|
2024-11-30 15:05:14 -05:00
|
|
|
WITH mine AS (SELECT rowid, id, previous, author, sequence, timestamp, hash, json(content) AS content, signature
|
2023-05-02 12:47:27 -04:00
|
|
|
FROM messages
|
|
|
|
WHERE messages.author = ?
|
|
|
|
ORDER BY sequence DESC
|
|
|
|
LIMIT 20)
|
2024-11-30 15:05:14 -05:00
|
|
|
SELECT messages.rowid, messages.id, messages.previous, messages.author, messages.sequence, messages.timestamp, messages.hash, json(messages.content) AS content, messages.signature
|
2023-05-02 12:47:27 -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
|
|
|
|
`,
|
2024-02-24 11:09:34 -05:00
|
|
|
[this.hash.substring(1)]
|
|
|
|
);
|
2023-05-02 12:47:27 -04:00
|
|
|
return r;
|
|
|
|
} else if (this.hash.startsWith('#%')) {
|
|
|
|
return await tfrpc.rpc.query(
|
|
|
|
`
|
2024-02-28 20:01:52 -05:00
|
|
|
SELECT id, previous, author, sequence, timestamp, hash, json(content) AS content, signature
|
2023-05-02 12:47:27 -04:00
|
|
|
FROM messages
|
|
|
|
WHERE id = ?1
|
|
|
|
UNION
|
2024-02-28 20:01:52 -05:00
|
|
|
SELECT id, previous, author, sequence, timestamp, hash, json(content) AS content, signature
|
2023-05-02 12:47:27 -04:00
|
|
|
FROM messages JOIN messages_refs
|
|
|
|
ON messages.id = messages_refs.message
|
|
|
|
WHERE messages_refs.ref = ?1
|
|
|
|
`,
|
2024-02-24 11:09:34 -05:00
|
|
|
[this.hash.substring(1)]
|
|
|
|
);
|
2024-11-30 15:05:14 -05:00
|
|
|
} else if (this.hash.startsWith('##')) {
|
|
|
|
let promises = [];
|
|
|
|
const k_following_limit = 256;
|
|
|
|
for (let i = 0; i < this.following.length; i += k_following_limit) {
|
|
|
|
promises.push(
|
|
|
|
tfrpc.rpc.query(
|
|
|
|
`
|
|
|
|
WITH news AS (SELECT messages.rowid, messages.id, messages.previous, messages.author, messages.sequence, messages.timestamp, messages.hash, json(messages.content) AS content, messages.signature
|
|
|
|
FROM messages
|
|
|
|
JOIN json_each(?) AS following ON messages.author = following.value
|
|
|
|
WHERE
|
|
|
|
messages.timestamp > ? AND
|
|
|
|
messages.timestamp < ? AND
|
|
|
|
messages.content ->> 'channel' = ?
|
|
|
|
ORDER BY messages.timestamp DESC)
|
|
|
|
SELECT messages.rowid, messages.id, messages.previous, messages.author, messages.sequence, messages.timestamp, messages.hash, json(messages.content) AS content, messages.signature
|
|
|
|
FROM news
|
|
|
|
JOIN messages_refs ON news.id = messages_refs.ref
|
|
|
|
JOIN messages ON messages_refs.message = messages.id
|
|
|
|
UNION
|
|
|
|
SELECT messages.rowid, messages.id, messages.previous, messages.author, messages.sequence, messages.timestamp, messages.hash, json(messages.content) AS content, messages.signature
|
|
|
|
FROM news
|
|
|
|
JOIN messages_refs ON news.id = messages_refs.message
|
|
|
|
JOIN messages ON messages_refs.ref = messages.id
|
|
|
|
UNION
|
|
|
|
SELECT news.* FROM news
|
|
|
|
`,
|
|
|
|
[
|
|
|
|
JSON.stringify(this.following.slice(i, i + k_following_limit)),
|
|
|
|
start_time,
|
|
|
|
end_time,
|
|
|
|
this.hash.substring(2),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return [].concat(...(await Promise.all(promises)));
|
2023-05-02 12:47:27 -04:00
|
|
|
} else {
|
2023-11-05 18:05:26 -05:00
|
|
|
let promises = [];
|
|
|
|
const k_following_limit = 256;
|
2023-11-05 08:07:07 -05:00
|
|
|
for (let i = 0; i < this.following.length; i += k_following_limit) {
|
2024-02-24 11:09:34 -05:00
|
|
|
promises.push(
|
|
|
|
tfrpc.rpc.query(
|
|
|
|
`
|
2024-11-30 15:05:14 -05:00
|
|
|
WITH news AS (SELECT messages.rowid, messages.id, messages.previous, messages.author, messages.sequence, messages.timestamp, messages.hash, json(messages.content) AS content, messages.signature
|
2023-11-05 08:07:07 -05:00
|
|
|
FROM messages
|
|
|
|
JOIN json_each(?) AS following ON messages.author = following.value
|
|
|
|
WHERE messages.timestamp > ? AND messages.timestamp < ?
|
|
|
|
ORDER BY messages.timestamp DESC)
|
2024-11-30 15:05:14 -05:00
|
|
|
SELECT messages.rowid, messages.id, messages.previous, messages.author, messages.sequence, messages.timestamp, messages.hash, json(messages.content) AS content, messages.signature
|
2023-11-05 08:07:07 -05:00
|
|
|
FROM news
|
|
|
|
JOIN messages_refs ON news.id = messages_refs.ref
|
|
|
|
JOIN messages ON messages_refs.message = messages.id
|
|
|
|
UNION
|
2024-11-30 15:05:14 -05:00
|
|
|
SELECT messages.rowid, messages.id, messages.previous, messages.author, messages.sequence, messages.timestamp, messages.hash, json(messages.content) AS content, messages.signature
|
2023-11-05 08:07:07 -05:00
|
|
|
FROM news
|
|
|
|
JOIN messages_refs ON news.id = messages_refs.message
|
|
|
|
JOIN messages ON messages_refs.ref = messages.id
|
|
|
|
UNION
|
|
|
|
SELECT news.* FROM news
|
|
|
|
`,
|
2024-02-24 11:09:34 -05:00
|
|
|
[
|
|
|
|
JSON.stringify(this.following.slice(i, i + k_following_limit)),
|
2024-11-30 15:05:14 -05:00
|
|
|
start_time,
|
|
|
|
end_time,
|
2024-02-24 11:09:34 -05:00
|
|
|
]
|
|
|
|
)
|
|
|
|
);
|
2023-11-05 08:07:07 -05:00
|
|
|
}
|
2023-11-05 18:05:26 -05:00
|
|
|
return [].concat(...(await Promise.all(promises)));
|
2023-05-02 12:47:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-03 18:32:21 -04:00
|
|
|
async load_more() {
|
2024-11-30 15:05:14 -05:00
|
|
|
this.loading++;
|
|
|
|
try {
|
|
|
|
let more = [];
|
|
|
|
while (!more.length) {
|
|
|
|
let last_start_time = this.start_time;
|
|
|
|
this.start_time = last_start_time - 7 * 24 * 60 * 60 * 1000;
|
|
|
|
more = await this.fetch_messages(this.start_time, last_start_time);
|
|
|
|
this.time_range = [this.start_time, this.time_range[1]];
|
|
|
|
}
|
|
|
|
this.messages = await this.decrypt([...more, ...this.messages]);
|
|
|
|
} finally {
|
|
|
|
this.loading--;
|
|
|
|
}
|
2023-09-01 22:06:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async decrypt(messages) {
|
2023-11-05 08:07:07 -05:00
|
|
|
console.log('decrypt');
|
2023-09-01 22:06:29 -04:00
|
|
|
let result = [];
|
|
|
|
for (let message of messages) {
|
|
|
|
let content;
|
|
|
|
try {
|
|
|
|
content = JSON.parse(message?.content);
|
2024-02-24 11:09:34 -05:00
|
|
|
} catch {}
|
|
|
|
if (typeof content === 'string') {
|
2023-09-01 22:06:29 -04:00
|
|
|
let decrypted;
|
|
|
|
try {
|
|
|
|
decrypted = await tfrpc.rpc.try_decrypt(this.whoami, content);
|
2024-02-24 11:09:34 -05:00
|
|
|
} catch {}
|
2023-09-01 22:06:29 -04:00
|
|
|
if (decrypted) {
|
|
|
|
try {
|
|
|
|
message.decrypted = JSON.parse(decrypted);
|
|
|
|
} catch {
|
|
|
|
message.decrypted = decrypted;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.push(message);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
async add_messages(messages) {
|
|
|
|
this.messages = await this.decrypt([...messages, ...this.messages]);
|
2023-05-03 18:32:21 -04:00
|
|
|
}
|
|
|
|
|
2024-11-30 15:05:14 -05:00
|
|
|
async load_messages() {
|
|
|
|
let self = this;
|
|
|
|
this.loading = true;
|
|
|
|
let messages = [];
|
|
|
|
try {
|
|
|
|
this.messages = [];
|
|
|
|
this._messages_hash = this.hash;
|
|
|
|
this._messages_following = this.following;
|
|
|
|
let now = new Date().valueOf();
|
|
|
|
let start_time = now - 24 * 60 * 60 * 1000;
|
|
|
|
this.start_time = start_time;
|
|
|
|
this.time_range = [this.start_time, now + 24 * 60 * 60 * 1000];
|
|
|
|
messages = await this.fetch_messages(this.time_range[0], this.time_range[1]);
|
|
|
|
messages = await this.decrypt(messages);
|
|
|
|
if (!messages.length) {
|
|
|
|
let more = [];
|
|
|
|
while (!more.length && start_time >= 0) {
|
|
|
|
let last_start_time = start_time;
|
|
|
|
start_time = last_start_time - 7 * 24 * 60 * 60 * 1000;
|
|
|
|
more = await this.fetch_messages(start_time, last_start_time);
|
|
|
|
}
|
|
|
|
this.time_range = [start_time, this.time_range[1]];
|
|
|
|
messages = await this.decrypt([...more, ...this.messages]);
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
this.loading = false;
|
|
|
|
}
|
|
|
|
this.messages = messages;
|
|
|
|
console.log(`loading messages done for ${self.whoami}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
mark_all_read() {
|
|
|
|
let newest = this.messages.reduce((accumulator, current) => Math.max(accumulator, current.rowid), -1);
|
|
|
|
if (newest >= 0) {
|
|
|
|
this.dispatchEvent(new CustomEvent('channelsetunread', {
|
|
|
|
bubbles: true,
|
|
|
|
composed: true,
|
|
|
|
detail: {
|
|
|
|
channel: this.channel(),
|
|
|
|
unread: newest + 1,
|
|
|
|
},
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-02 12:47:27 -04:00
|
|
|
render() {
|
2024-02-24 11:09:34 -05:00
|
|
|
if (
|
|
|
|
!this.messages ||
|
2023-05-02 12:47:27 -04:00
|
|
|
this._messages_hash !== this.hash ||
|
2024-02-24 11:09:34 -05:00
|
|
|
this._messages_following !== this.following
|
|
|
|
) {
|
|
|
|
console.log(
|
|
|
|
`loading messages for ${this.whoami} (following ${this.following.length})`
|
|
|
|
);
|
2024-11-30 15:05:14 -05:00
|
|
|
this.load_messages();
|
2023-05-02 12:47:27 -04:00
|
|
|
}
|
2023-05-03 18:32:21 -04:00
|
|
|
let more;
|
|
|
|
if (!this.hash.startsWith('#@') && !this.hash.startsWith('#%')) {
|
|
|
|
more = html`
|
2024-01-11 23:23:31 -05:00
|
|
|
<p>
|
2024-11-30 15:05:14 -05:00
|
|
|
<button class="w3-button w3-theme-d1" @click=${this.mark_all_read}>Mark All Read</button>
|
|
|
|
<button ?disabled=${this.loading} class="w3-button w3-theme-d1" @click=${this.load_more}>
|
2024-02-24 11:09:34 -05:00
|
|
|
Load More
|
|
|
|
</button>
|
2024-11-30 15:05:14 -05:00
|
|
|
<span>Showing ${new Date(this.time_range[0]).toLocaleDateString()} - ${new Date(this.time_range[1]).toLocaleDateString()}.</span>
|
2024-01-11 23:23:31 -05:00
|
|
|
</p>
|
2023-05-03 18:32:21 -04:00
|
|
|
`;
|
|
|
|
}
|
|
|
|
return html`
|
2024-02-24 11:09:34 -05:00
|
|
|
<tf-news
|
|
|
|
id="news"
|
|
|
|
whoami=${this.whoami}
|
|
|
|
.users=${this.users}
|
|
|
|
.messages=${this.messages}
|
|
|
|
.following=${this.following}
|
|
|
|
.drafts=${this.drafts}
|
|
|
|
.expanded=${this.expanded}
|
2024-11-30 15:05:14 -05:00
|
|
|
channel=${this.channel()}
|
|
|
|
channel_unread=${this.channels_unread?.[this.channel()]}
|
2024-02-24 11:09:34 -05:00
|
|
|
></tf-news>
|
2023-05-03 18:32:21 -04:00
|
|
|
${more}
|
|
|
|
`;
|
2023-05-02 12:47:27 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-24 11:09:34 -05:00
|
|
|
customElements.define('tf-tab-news-feed', TfTabNewsFeedElement);
|