forked from cory/tildefriends
ssb: Add a lookup table of max sequence and timestamp per account.f
This commit is contained in:
parent
c7a8ce7060
commit
c728e05032
@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"type": "tildefriends-app",
|
"type": "tildefriends-app",
|
||||||
"emoji": "🦀",
|
"emoji": "🦀",
|
||||||
"previous": "&cnc92v8+8JMxSIvs6vZ6j01rUzyVtKg6zkITqwQ02Lc=.sha256"
|
"previous": "&dQu+yQ4/be0vcgIZSMKtesQCbH8lVhHcZAEv9+dNknY=.sha256"
|
||||||
}
|
}
|
||||||
|
@ -402,10 +402,9 @@ class TfElement extends LitElement {
|
|||||||
async fetch_user_info(users) {
|
async fetch_user_info(users) {
|
||||||
let info = await tfrpc.rpc.query(
|
let info = await tfrpc.rpc.query(
|
||||||
`
|
`
|
||||||
SELECT messages.author, MAX(messages.sequence) AS max_seq, MAX(timestamp) AS max_ts FROM messages
|
SELECT messages_stats.author, messages_stats.max_sequence, messages_stats.max_timestamp AS max_ts FROM messages_stats
|
||||||
JOIN json_each(?) AS following
|
JOIN json_each(?) AS following
|
||||||
ON messages.author = following.value
|
ON messages_stats.author = following.value
|
||||||
GROUP BY messages.author
|
|
||||||
`,
|
`,
|
||||||
[JSON.stringify(Object.keys(users))]
|
[JSON.stringify(Object.keys(users))]
|
||||||
);
|
);
|
||||||
|
@ -190,11 +190,7 @@ class TfTabNewsFeedElement extends LitElement {
|
|||||||
UNION
|
UNION
|
||||||
SELECT TRUE AS is_primary, news.* FROM news
|
SELECT TRUE AS is_primary, news.* FROM news
|
||||||
`,
|
`,
|
||||||
[
|
[JSON.stringify(this.following), start_time, end_time]
|
||||||
JSON.stringify(this.following),
|
|
||||||
start_time,
|
|
||||||
end_time,
|
|
||||||
]
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
this.time_loading = undefined;
|
this.time_loading = undefined;
|
||||||
@ -223,9 +219,7 @@ class TfTabNewsFeedElement extends LitElement {
|
|||||||
let last_start_time = this.time_range[0];
|
let last_start_time = this.time_range[0];
|
||||||
more = await this.fetch_messages(null, last_start_time);
|
more = await this.fetch_messages(null, last_start_time);
|
||||||
this.update_time_range_from_messages(
|
this.update_time_range_from_messages(
|
||||||
more.filter(
|
more.filter((x) => x.timestamp < last_start_time)
|
||||||
(x) => x.timestamp < last_start_time
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
this.messages = await this.decrypt([...more, ...this.messages]);
|
this.messages = await this.decrypt([...more, ...this.messages]);
|
||||||
} finally {
|
} finally {
|
||||||
@ -315,14 +309,9 @@ class TfTabNewsFeedElement extends LitElement {
|
|||||||
let start_time = now - 24 * 60 * 60 * 1000;
|
let start_time = now - 24 * 60 * 60 * 1000;
|
||||||
this.start_time = start_time;
|
this.start_time = start_time;
|
||||||
this.time_range = [this.start_time, now + 24 * 60 * 60 * 1000];
|
this.time_range = [this.start_time, now + 24 * 60 * 60 * 1000];
|
||||||
messages = await this.fetch_messages(
|
messages = await this.fetch_messages(null, this.time_range[1]);
|
||||||
null,
|
|
||||||
this.time_range[1]
|
|
||||||
);
|
|
||||||
this.update_time_range_from_messages(
|
this.update_time_range_from_messages(
|
||||||
messages.filter(
|
messages.filter((x) => x.timestamp < this.time_range[1])
|
||||||
(x) => x.timestamp < this.time_range[1]
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
messages = await this.decrypt(messages);
|
messages = await this.decrypt(messages);
|
||||||
} finally {
|
} finally {
|
||||||
@ -330,7 +319,9 @@ class TfTabNewsFeedElement extends LitElement {
|
|||||||
}
|
}
|
||||||
this.messages = this.merge_messages(this.messages, messages);
|
this.messages = this.merge_messages(this.messages, messages);
|
||||||
this.time_loading = undefined;
|
this.time_loading = undefined;
|
||||||
console.log(`loading messages done for ${self.whoami} in ${(new Date() - start_time) / 1000}s`);
|
console.log(
|
||||||
|
`loading messages done for ${self.whoami} in ${(new Date() - start_time) / 1000}s`
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
mark_all_read() {
|
mark_all_read() {
|
||||||
|
19
src/ssb.db.c
19
src/ssb.db.c
@ -108,6 +108,20 @@ void tf_ssb_db_init(tf_ssb_t* ssb)
|
|||||||
" flags INTEGER,"
|
" flags INTEGER,"
|
||||||
" UNIQUE(author, sequence)"
|
" UNIQUE(author, sequence)"
|
||||||
")");
|
")");
|
||||||
|
_tf_ssb_db_exec(db,
|
||||||
|
"CREATE TABLE IF NOT EXISTS messages_stats ("
|
||||||
|
" author TEXT PRIMARY KEY,"
|
||||||
|
" max_sequence INTEGER,"
|
||||||
|
" max_timestamp READ"
|
||||||
|
")");
|
||||||
|
_tf_ssb_db_exec(db,
|
||||||
|
"CREATE TRIGGER IF NOT EXISTS messages_ai_stats AFTER INSERT ON messages BEGIN INSERT INTO messages_stats(author, max_sequence, max_timestamp) VALUES (new.author, "
|
||||||
|
"new.sequence, new.timestamp) ON CONFLICT DO UPDATE SET max_sequence = MAX(max_sequence, excluded.max_sequence), max_timestamp = MAX(max_timestamp, "
|
||||||
|
"excluded.max_timestamp); END");
|
||||||
|
_tf_ssb_db_exec(db,
|
||||||
|
"CREATE TRIGGER IF NOT EXISTS messages_ad_stats AFTER DELETE ON messages BEGIN UPDATE messages_stats SET max_sequence = (SELECT MAX(messages.sequence) FROM messages WHERE "
|
||||||
|
"messages.author = old.author), max_timestamp = (SELECT MAX(messages.timestamp) FROM messages WHERE messages.author = old.author); END");
|
||||||
|
_tf_ssb_db_exec(db, "INSERT OR REPLACE INTO messages_stats (author, max_sequence, max_timestamp) SELECT author, MAX(sequence), MAX(timestamp) FROM messages GROUP BY author");
|
||||||
|
|
||||||
if (_tf_ssb_db_has_rows(db, "SELECT name FROM pragma_table_info('messages') WHERE name = 'content' AND type == 'TEXT'"))
|
if (_tf_ssb_db_has_rows(db, "SELECT name FROM pragma_table_info('messages') WHERE name = 'content' AND type == 'TEXT'"))
|
||||||
{
|
{
|
||||||
@ -987,8 +1001,9 @@ int tf_ssb_sqlite_authorizer(void* user_data, int action_code, const char* arg0,
|
|||||||
break;
|
break;
|
||||||
case SQLITE_READ:
|
case SQLITE_READ:
|
||||||
result = (strcmp(arg0, "blob_wants_view") == 0 || strcmp(arg0, "json_each") == 0 || strcmp(arg0, "json_tree") == 0 || strcmp(arg0, "messages") == 0 ||
|
result = (strcmp(arg0, "blob_wants_view") == 0 || strcmp(arg0, "json_each") == 0 || strcmp(arg0, "json_tree") == 0 || strcmp(arg0, "messages") == 0 ||
|
||||||
strcmp(arg0, "messages_fts") == 0 || strcmp(arg0, "messages_fts_idx") == 0 || strcmp(arg0, "messages_fts_config") == 0 || strcmp(arg0, "messages_refs") == 0 ||
|
strcmp(arg0, "messages_stats") == 0 || strcmp(arg0, "messages_fts") == 0 || strcmp(arg0, "messages_fts_idx") == 0 ||
|
||||||
strcmp(arg0, "messages_refs_message_idx") == 0 || strcmp(arg0, "messages_refs_ref_idx") == 0 || strcmp(arg0, "sqlite_master") == 0 || false)
|
strcmp(arg0, "messages_fts_config") == 0 || strcmp(arg0, "messages_refs") == 0 || strcmp(arg0, "messages_refs_message_idx") == 0 ||
|
||||||
|
strcmp(arg0, "messages_refs_ref_idx") == 0 || strcmp(arg0, "sqlite_master") == 0 || false)
|
||||||
? SQLITE_OK
|
? SQLITE_OK
|
||||||
: SQLITE_DENY;
|
: SQLITE_DENY;
|
||||||
break;
|
break;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user