2023-10-29 20:22:30 -04:00
|
|
|
import {LitElement, html} from './lit-all.min.js';
|
|
|
|
import * as tfrpc from '/static/tfrpc.js';
|
|
|
|
|
2023-11-01 18:21:42 -04:00
|
|
|
class TfCollectionElement extends LitElement {
|
2023-10-29 20:22:30 -04:00
|
|
|
static get properties() {
|
|
|
|
return {
|
|
|
|
whoami: {type: String},
|
|
|
|
collections: {type: Object},
|
|
|
|
collections_loading: {type: Number},
|
|
|
|
type: {type: String},
|
|
|
|
parent: {type: String},
|
2023-11-01 18:21:42 -04:00
|
|
|
selectname: {type: String},
|
2023-10-29 20:22:30 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.ids = [];
|
2023-11-01 18:21:42 -04:00
|
|
|
this.loaded = this.load();
|
2023-10-29 20:22:30 -04:00
|
|
|
this.type = 'collection';
|
|
|
|
this.collections_loading = 0;
|
|
|
|
}
|
|
|
|
|
2023-11-01 18:21:42 -04:00
|
|
|
process_message(message) {
|
|
|
|
let content = JSON.parse(message.content);
|
|
|
|
if (content?.key) {
|
|
|
|
if (content?.tombstone) {
|
|
|
|
delete this.by_id[content.key];
|
|
|
|
} else if (this.by_id[content.key]) {
|
|
|
|
this.by_id[content.key] = Object.assign(this.by_id[content.key], content);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.by_id[message.id] = Object.assign(content, {id: message.id});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-29 20:22:30 -04:00
|
|
|
async load() {
|
|
|
|
try {
|
|
|
|
this.collections_loading++;
|
|
|
|
if (this.whoami) {
|
|
|
|
let following = await tfrpc.rpc.following([this.whoami], 2);
|
2023-11-01 18:21:42 -04:00
|
|
|
this.following = following;
|
|
|
|
if (this.type) {
|
2023-10-29 20:22:30 -04:00
|
|
|
let collections = await tfrpc.rpc.query(`
|
|
|
|
SELECT messages.id, author, content, timestamp
|
|
|
|
FROM messages JOIN json_each(?1) AS id ON messages.author = id.value
|
|
|
|
WHERE
|
|
|
|
json_extract(content, '$.type') = ?2 AND
|
2023-11-01 18:21:42 -04:00
|
|
|
(?3 IS NULL OR json_extract(content, '$.parent') = ?3)
|
2023-10-29 20:22:30 -04:00
|
|
|
ORDER BY timestamp
|
2023-11-01 18:21:42 -04:00
|
|
|
`, [JSON.stringify(following), this.type, this.parent]);
|
|
|
|
this.by_id = {};
|
2023-10-29 20:22:30 -04:00
|
|
|
for (let collection of collections) {
|
2023-11-01 18:21:42 -04:00
|
|
|
this.process_message(collection);
|
2023-10-29 20:22:30 -04:00
|
|
|
}
|
2023-11-01 18:21:42 -04:00
|
|
|
this.collections = Object.values(this.by_id);
|
2023-10-29 20:22:30 -04:00
|
|
|
}
|
|
|
|
}
|
2023-11-01 18:21:42 -04:00
|
|
|
console.log('loaded', this.collections);
|
2023-10-29 20:22:30 -04:00
|
|
|
} finally {
|
|
|
|
this.collections_loading--;
|
|
|
|
}
|
2023-11-01 18:21:42 -04:00
|
|
|
if (this.selectname) {
|
|
|
|
this.set_selected_by_name(this.selectname);
|
|
|
|
}
|
2023-10-29 20:22:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async create(name, editors) {
|
|
|
|
let message = {
|
|
|
|
type: this.type,
|
|
|
|
name: name,
|
|
|
|
parent: this.parent,
|
|
|
|
editors: editors,
|
|
|
|
};
|
2023-11-01 18:21:42 -04:00
|
|
|
print('will append', message);
|
2023-10-29 20:22:30 -04:00
|
|
|
await tfrpc.rpc.appendMessage(this.whoami, message);
|
2023-11-01 18:21:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
notify_new_message(message) {
|
|
|
|
console.log('got notify new message', message);
|
|
|
|
if (this.following &&
|
|
|
|
this.following.indexOf(message.author) != -1 &&
|
|
|
|
JSON.parse(message.content).type == this.type) {
|
|
|
|
console.log('processing message', message);
|
|
|
|
this.process_message(message);
|
|
|
|
this.collections = [...Object.values(this.by_id)];
|
|
|
|
}
|
2023-10-29 20:22:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async on_create(event) {
|
|
|
|
let name = this.shadowRoot.getElementById('create_name').value;
|
|
|
|
if (name) {
|
|
|
|
await this.create(name, [this.whoami]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async on_tombstone(id) {
|
|
|
|
let message = {
|
|
|
|
type: this.type,
|
|
|
|
key: id,
|
2023-11-01 18:21:42 -04:00
|
|
|
parent: this.parent,
|
2023-10-29 20:22:30 -04:00
|
|
|
tombstone: {
|
|
|
|
date: new Date().valueOf(),
|
|
|
|
reason: 'archived',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
print(message);
|
|
|
|
await tfrpc.rpc.appendMessage(this.whoami, message);
|
2023-11-01 18:21:42 -04:00
|
|
|
//return this.load();
|
2023-10-29 20:22:30 -04:00
|
|
|
}
|
|
|
|
|
2023-11-01 18:21:42 -04:00
|
|
|
set_selected(id, value, by_user) {
|
2023-10-29 20:22:30 -04:00
|
|
|
this.dispatchEvent(new CustomEvent('selected', {
|
|
|
|
bubbles: true,
|
|
|
|
detail: {
|
|
|
|
id: id,
|
|
|
|
value: value,
|
2023-11-01 18:21:42 -04:00
|
|
|
by_user: by_user,
|
2023-10-29 20:22:30 -04:00
|
|
|
},
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2023-11-01 18:21:42 -04:00
|
|
|
set_selected_by_name(name) {
|
|
|
|
if (this.collections) {
|
|
|
|
for (let collection of this.collections) {
|
|
|
|
if (collection.name === name) {
|
|
|
|
this.set_selected(collection.id, collection);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this._select_by_name = name;
|
|
|
|
}
|
|
|
|
|
2023-10-29 20:22:30 -04:00
|
|
|
render_collection(collection) {
|
|
|
|
return html`
|
|
|
|
<div>
|
2023-11-01 18:21:42 -04:00
|
|
|
<button @click=${() => this.set_selected(collection.id, collection, true)}>${collection.name}</button>
|
2023-10-29 20:22:30 -04:00
|
|
|
<span>${collection.id}</span>
|
|
|
|
<button @click=${() => this.on_tombstone(collection.id)}>🪦</button>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
${collection.editors.map(id => html`<span>${id}</span>`)}
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2023-11-01 18:21:42 -04:00
|
|
|
let state = JSON.stringify([this.whoami, this.ids, this.parent]);
|
2023-10-29 20:22:30 -04:00
|
|
|
if (state !== this.loaded_for) {
|
|
|
|
this.loaded_for = state;
|
2023-11-01 18:21:42 -04:00
|
|
|
console.log('start load', state);
|
|
|
|
this.loaded = this.load();
|
2023-10-29 20:22:30 -04:00
|
|
|
}
|
|
|
|
return html`
|
2023-11-01 18:21:42 -04:00
|
|
|
<h2 style="color: #fff">${this.type}s</h2>
|
|
|
|
<div style="color: #fff">${this.whoami} ${this.selectname}</div>
|
|
|
|
<div>${this.parent}</div>
|
|
|
|
<input type="text" id="create_name"></input><button @click=${this.on_create}>Create ${this.type}</button>
|
2023-10-29 20:22:30 -04:00
|
|
|
${this.collections_loading ? html`<div>Loading...</div>` : html`
|
|
|
|
<ul>
|
|
|
|
${this.collections?.map(x => html`<li>${this.render_collection(x)}</li>`)}
|
|
|
|
</ul>
|
|
|
|
`}
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-01 18:21:42 -04:00
|
|
|
customElements.define('tf-collection', TfCollectionElement);
|