forked from cory/tildefriends
		
	ssb: Use most recent post timestamps to feature more relevant people to follow.
This commit is contained in:
		@@ -1,5 +1,5 @@
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
	"type": "tildefriends-app",
 | 
						"type": "tildefriends-app",
 | 
				
			||||||
	"emoji": "🦀",
 | 
						"emoji": "🦀",
 | 
				
			||||||
	"previous": "&ywgXrIlVu5chLPseAwTXdpzaxGep+rjs1ZPeV4tj3wM=.sha256"
 | 
						"previous": "&IbDnKRKxIZzdIhs2wVVdNt560YlLuM/8coMuTuJhVNs=.sha256"
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -163,8 +163,8 @@ class TfElement extends LitElement {
 | 
				
			|||||||
		let max_row_id = (
 | 
							let max_row_id = (
 | 
				
			||||||
			await tfrpc.rpc.query(
 | 
								await tfrpc.rpc.query(
 | 
				
			||||||
				`
 | 
									`
 | 
				
			||||||
			SELECT MAX(rowid) AS max_row_id FROM messages
 | 
										SELECT MAX(rowid) AS max_row_id FROM messages
 | 
				
			||||||
		`,
 | 
									`,
 | 
				
			||||||
				[]
 | 
									[]
 | 
				
			||||||
			)
 | 
								)
 | 
				
			||||||
		)[0].max_row_id;
 | 
							)[0].max_row_id;
 | 
				
			||||||
@@ -177,7 +177,7 @@ class TfElement extends LitElement {
 | 
				
			|||||||
		let abouts = await tfrpc.rpc.query(
 | 
							let abouts = await tfrpc.rpc.query(
 | 
				
			||||||
			`
 | 
								`
 | 
				
			||||||
				SELECT
 | 
									SELECT
 | 
				
			||||||
					messages.id, messages.previous, messages.author, messages.sequence, messages.timestamp, messages.hash, json(messages.content) AS content, messages.signature
 | 
										messages.author, json(messages.content) AS content, messages.sequence
 | 
				
			||||||
				FROM
 | 
									FROM
 | 
				
			||||||
					messages,
 | 
										messages,
 | 
				
			||||||
					json_each(?1) AS following
 | 
										json_each(?1) AS following
 | 
				
			||||||
@@ -188,7 +188,7 @@ class TfElement extends LitElement {
 | 
				
			|||||||
					json_extract(messages.content, '$.type') = 'about'
 | 
										json_extract(messages.content, '$.type') = 'about'
 | 
				
			||||||
				UNION
 | 
									UNION
 | 
				
			||||||
				SELECT
 | 
									SELECT
 | 
				
			||||||
					messages.id, messages.previous, messages.author, messages.sequence, messages.timestamp, messages.hash, json(messages.content) AS content, messages.signature
 | 
										messages.author, json(messages.content) AS content, messages.sequence
 | 
				
			||||||
				FROM
 | 
									FROM
 | 
				
			||||||
					messages,
 | 
										messages,
 | 
				
			||||||
					json_each(?2) AS following
 | 
										json_each(?2) AS following
 | 
				
			||||||
@@ -383,10 +383,9 @@ class TfElement extends LitElement {
 | 
				
			|||||||
		this.channels_latest = Object.fromEntries(
 | 
							this.channels_latest = Object.fromEntries(
 | 
				
			||||||
			channels.map((x) => [x.channel, x.rowid])
 | 
								channels.map((x) => [x.channel, x.rowid])
 | 
				
			||||||
		);
 | 
							);
 | 
				
			||||||
		console.log('latest', this.channels_latest);
 | 
					 | 
				
			||||||
		console.log('unread', this.channels_unread);
 | 
					 | 
				
			||||||
		console.log('channels took', (new Date() - start_time) / 1000.0);
 | 
							console.log('channels took', (new Date() - start_time) / 1000.0);
 | 
				
			||||||
		let self = this;
 | 
							let self = this;
 | 
				
			||||||
 | 
							start_time = new Date();
 | 
				
			||||||
		latest_private.then(function (latest) {
 | 
							latest_private.then(function (latest) {
 | 
				
			||||||
			self.channels_latest = Object.assign({}, self.channels_latest, {
 | 
								self.channels_latest = Object.assign({}, self.channels_latest, {
 | 
				
			||||||
				'🔐': latest,
 | 
									'🔐': latest,
 | 
				
			||||||
@@ -410,6 +409,24 @@ class TfElement extends LitElement {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						async fetch_user_info(users) {
 | 
				
			||||||
 | 
							let info = await tfrpc.rpc.query(
 | 
				
			||||||
 | 
								`
 | 
				
			||||||
 | 
									SELECT messages.author, MAX(messages.sequence) AS max_seq, MAX(timestamp) AS max_ts FROM messages
 | 
				
			||||||
 | 
									JOIN json_each(?) AS following
 | 
				
			||||||
 | 
									ON messages.author = following.value
 | 
				
			||||||
 | 
									GROUP BY messages.author
 | 
				
			||||||
 | 
								`,
 | 
				
			||||||
 | 
								[
 | 
				
			||||||
 | 
									JSON.stringify(Object.keys(users)),
 | 
				
			||||||
 | 
								]);
 | 
				
			||||||
 | 
							for (let row of info) {
 | 
				
			||||||
 | 
								users[row.author].seq = row.max_seq;
 | 
				
			||||||
 | 
								users[row.author].ts = row.max_ts;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return users;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	async load() {
 | 
						async load() {
 | 
				
			||||||
		this.loading_latest = true;
 | 
							this.loading_latest = true;
 | 
				
			||||||
		try {
 | 
							try {
 | 
				
			||||||
@@ -440,6 +457,9 @@ class TfElement extends LitElement {
 | 
				
			|||||||
				Object.keys(users).length,
 | 
									Object.keys(users).length,
 | 
				
			||||||
				'users'
 | 
									'users'
 | 
				
			||||||
			);
 | 
								);
 | 
				
			||||||
 | 
								start_time = new Date();
 | 
				
			||||||
 | 
								users = await this.fetch_user_info(users);
 | 
				
			||||||
 | 
								console.log('user info took', (new Date() - start_time) / 1000.0, 'seconds');
 | 
				
			||||||
			this.users = users;
 | 
								this.users = users;
 | 
				
			||||||
			console.log(
 | 
								console.log(
 | 
				
			||||||
				`load finished ${whoami} => ${this.whoami} in ${(new Date() - start_time) / 1000}`
 | 
									`load finished ${whoami} => ${this.whoami} in ${(new Date() - start_time) / 1000}`
 | 
				
			||||||
@@ -573,8 +593,7 @@ class TfElement extends LitElement {
 | 
				
			|||||||
				style="position: static; top: 0; z-index: 10"
 | 
									style="position: static; top: 0; z-index: 10"
 | 
				
			||||||
			>
 | 
								>
 | 
				
			||||||
				<button
 | 
									<button
 | 
				
			||||||
					class=${'w3-bar-item w3-button w3-circle w3-ripple' +
 | 
										class=${'w3-bar-item w3-button w3-circle w3-ripple' + (this.connections?.some(x => x.flags.one_shot) ? ' w3-spin' : '')}
 | 
				
			||||||
					(this.connections?.some((x) => x.flags.one_shot) ? ' w3-spin' : '')}
 | 
					 | 
				
			||||||
					@click=${this.refresh}
 | 
										@click=${this.refresh}
 | 
				
			||||||
				>
 | 
									>
 | 
				
			||||||
					↻
 | 
										↻
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -275,8 +275,8 @@ class TfTabNewsFeedElement extends LitElement {
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	merge_messages(old_messages, new_messages) {
 | 
						merge_messages(old_messages, new_messages) {
 | 
				
			||||||
		let old_by_id = Object.fromEntries(old_messages.map((x) => [x.id, x]));
 | 
							let old_by_id = Object.fromEntries(old_messages.map(x => [x.id, x]));
 | 
				
			||||||
		return new_messages.map((x) => (old_by_id[x.id] ? old_by_id[x.id] : x));
 | 
							return new_messages.map(x => old_by_id[x.id] ? old_by_id[x.id] : x);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	async load_latest() {
 | 
						async load_latest() {
 | 
				
			||||||
@@ -298,17 +298,14 @@ class TfTabNewsFeedElement extends LitElement {
 | 
				
			|||||||
		} finally {
 | 
							} finally {
 | 
				
			||||||
			this.loading--;
 | 
								this.loading--;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		this.messages = this.merge_messages(
 | 
							this.messages = this.merge_messages(this.messages, Object.values(
 | 
				
			||||||
			this.messages,
 | 
								Object.fromEntries(
 | 
				
			||||||
			Object.values(
 | 
									[...this.messages, ...messages]
 | 
				
			||||||
				Object.fromEntries(
 | 
										.sort((x, y) => x.timestamp - y.timestamp)
 | 
				
			||||||
					[...this.messages, ...messages]
 | 
										.slice(-1024)
 | 
				
			||||||
						.sort((x, y) => x.timestamp - y.timestamp)
 | 
										.map((x) => [x.id, x])
 | 
				
			||||||
						.slice(-1024)
 | 
					 | 
				
			||||||
						.map((x) => [x.id, x])
 | 
					 | 
				
			||||||
				)
 | 
					 | 
				
			||||||
			)
 | 
								)
 | 
				
			||||||
		);
 | 
							));
 | 
				
			||||||
		console.log('done loading latest messages.');
 | 
							console.log('done loading latest messages.');
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -128,21 +128,24 @@ class TfTabNewsElement extends LitElement {
 | 
				
			|||||||
		return this.hash.startsWith('##') ? this.hash.substring(2) : undefined;
 | 
							return this.hash.startsWith('##') ? this.hash.substring(2) : undefined;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	compare_follows(a, b) {
 | 
						compare_follows() {
 | 
				
			||||||
		return a[1].followed - b[1].followed;
 | 
							const now = new Date().valueOf();
 | 
				
			||||||
 | 
							return function (a, b) {
 | 
				
			||||||
 | 
								return (b[1].ts > now ? -1 : b[1].ts) - (a[1].ts > now ? -1 : a[1].ts);
 | 
				
			||||||
 | 
							};
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	suggested_follows() {
 | 
						suggested_follows() {
 | 
				
			||||||
 | 
							/*
 | 
				
			||||||
 | 
							** Filter out people who have used future timestamps so that they aren't
 | 
				
			||||||
 | 
							** pinned at the top.
 | 
				
			||||||
 | 
							*/
 | 
				
			||||||
		let self = this;
 | 
							let self = this;
 | 
				
			||||||
		return Object.entries(this.users).filter(
 | 
							return Object.entries(this.users)
 | 
				
			||||||
			(x) => (x[1].follow_depth > 1)
 | 
								.filter((x) => x[1].follow_depth > 1)
 | 
				
			||||||
		).sort(
 | 
								.sort(self.compare_follows())
 | 
				
			||||||
			self.compare_follows
 | 
								.slice(0, 8)
 | 
				
			||||||
		).slice(
 | 
								.map((x) => x[0]);
 | 
				
			||||||
			0, 8
 | 
					 | 
				
			||||||
		).map(
 | 
					 | 
				
			||||||
			(x) => (x[0])
 | 
					 | 
				
			||||||
		);
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	render_sidebar() {
 | 
						render_sidebar() {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user