~emersion/goguma-dev

Store channel topic & user realnames locally v1 APPLIED

delthas: 1
 Store channel topic & user realnames locally

 4 files changed, 68 insertions(+), 37 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~emersion/goguma-dev/patches/30998/mbox | git am -3
Learn more about email & git

[PATCH] Store channel topic & user realnames locally Export this patch

This info is what is used for buffer_list item subtitles.
---
 lib/client_controller.dart | 70 ++++++++++++++++++++++++--------------
 lib/database.dart          | 21 ++++++++++--
 lib/models.dart            | 12 +++----
 lib/page/buffer.dart       |  2 +-
 4 files changed, 68 insertions(+), 37 deletions(-)

diff --git a/lib/client_controller.dart b/lib/client_controller.dart
index ad18292..76752bd 100644
--- a/lib/client_controller.dart
+++ b/lib/client_controller.dart
@@ -163,6 +163,28 @@ class ClientProvider {
			print('Failed to enable sync background service');
		}
	}

	void fetchBufferUser(BufferModel buffer) async {
		var client = get(buffer.network);
		List<WhoReply> replies;
		try {
			replies = await client.who(buffer.name);
		} on Exception catch (err) {
			print('Failed to fetch WHO ${buffer.name}: $err');
			return;
		}

		if (replies.length == 0) {
			return; // User is offline
		} else if (replies.length != 1) {
			throw FormatException('Expected a single WHO reply, got ${replies.length}');
		}

		var reply = replies[0];
		buffer.realname = reply.realname;
		buffer.away = reply.away;
		_db.storeBuffer(buffer.entry);
	}
}

/// A lock which enables automatic reconnection when enabled.
@@ -287,7 +309,7 @@ class ClientController {
					continue;
				}
				if (buffer.realname == null) {
					fetchBufferUser(client, buffer);
					_provider.fetchBufferUser(buffer);
				}
				l.add(buffer.name);
			}
@@ -395,16 +417,28 @@ class ClientController {
				network.realname = realname;
			}

			_bufferList.get(msg.source.name, network)?.realname = realname;
			var buffer = _bufferList.get(msg.source.name, network);
			if(buffer != null) {
				buffer.realname = realname;
				_db.storeBuffer(buffer.entry);
			}
			break;
		case RPL_TOPIC:
			var channel = msg.params[1];
			var topic = msg.params[2];
			_bufferList.get(channel, network)?.topic = topic;
			var buffer = _bufferList.get(channel, network);
			if (buffer != null) {
				buffer.topic = topic;
				_db.storeBuffer(buffer.entry);
			}
			break;
		case RPL_NOTOPIC:
			var channel = msg.params[1];
			_bufferList.get(channel, network)?.topic = null;
			var buffer = _bufferList.get(channel, network);
			if (buffer != null) {
				buffer.topic = null;
				_db.storeBuffer(buffer.entry);
			}
			break;
		case 'TOPIC':
			var channel = msg.params[0];
@@ -412,7 +446,11 @@ class ClientController {
			if (msg.params.length > 1) {
				topic = msg.params[1];
			}
			_bufferList.get(channel, network)?.topic = topic;
			var buffer = _bufferList.get(channel, network);
			if (buffer != null) {
				buffer.topic = topic;
				_db.storeBuffer(buffer.entry);
			}
			break;
		case RPL_ENDOFNAMES:
			var channel = msg.params[1];
@@ -593,7 +631,7 @@ class ClientController {
		_bufferList.bumpLastDeliveredTime(buf, t);

		if (isNewBuffer && client.isNick(buf.name)) {
			fetchBufferUser(client, buf);
			_provider.fetchBufferUser(buf);
		}
	}

@@ -684,23 +722,3 @@ class ClientController {
		}));
	}
}

void fetchBufferUser(Client client, BufferModel buffer) async {
	List<WhoReply> replies;
	try {
		replies = await client.who(buffer.name);
	} on Exception catch (err) {
		print('Failed to fetch WHO ${buffer.name}: $err');
		return;
	}

	if (replies.length == 0) {
		return; // User is offline
	} else if (replies.length != 1) {
		throw FormatException('Expected a single WHO reply, got ${replies.length}');
	}

	var reply = replies[0];
	buffer.realname = reply.realname;
	buffer.away = reply.away;
}
diff --git a/lib/database.dart b/lib/database.dart
index 799f3a4..ebe81ff 100644
--- a/lib/database.dart
+++ b/lib/database.dart
@@ -70,6 +70,9 @@ class BufferEntry {
	bool pinned;
	bool muted;

	String? topic;
	String? realname;

	Map<String, Object?> toMap() {
		return <String, Object?>{
			'id': id,
@@ -78,6 +81,8 @@ class BufferEntry {
			'last_read_time': lastReadTime,
			'pinned': pinned ? 1 : 0,
			'muted': muted ? 1 : 0,
			'topic': topic,
			'realname': realname,
		};
	}

@@ -89,7 +94,9 @@ class BufferEntry {
		network = m['network'] as int,
		lastReadTime = m['last_read_time'] as String?,
		pinned = m['pinned'] == 1,
		muted = m['muted'] == 1;
		muted = m['muted'] == 1,
		topic = m['topic'] as String?,
		realname = m['realname'] as String?;
}

class MessageEntry {
@@ -184,6 +191,8 @@ class DB {
						last_read_time TEXT,
						pinned INTEGER NOT NULL DEFAULT 0,
						muted INTEGER NOT NULL DEFAULT 0,
						topic TEXT,
						realname TEXT,
						FOREIGN KEY (network) REFERENCES Network(id) ON DELETE CASCADE,
						UNIQUE(name, network)
					)
@@ -223,12 +232,18 @@ class DB {
						ALTER TABLE Buffer ADD COLUMN muted INTEGER NOT NULL DEFAULT 0;
					''');
				}
				if (prevVersion < 5) {
					batch.execute('''
						ALTER TABLE Buffer ADD COLUMN topic TEXT;
						ALTER TABLE Buffer ADD COLUMN realname TEXT;
					''');
				}
				await batch.commit();
			},
			onDowngrade: (_, prevVersion, newVersion) async {
				throw Exception('Attempted to downgrade database from version $prevVersion to version $newVersion');
			},
			version: 4,
			version: 5,
		);
		return DB._(db);
	}
@@ -325,7 +340,7 @@ class DB {

	Future<List<BufferEntry>> listBuffers() async {
		var entries = await _db.rawQuery('''
			SELECT id, name, network, last_read_time, pinned, muted FROM Buffer ORDER BY id
			SELECT id, name, network, last_read_time, pinned, muted, topic, realname FROM Buffer ORDER BY id
		''');
		return entries.map((m) => BufferEntry.fromMap(m)).toList();
	}
diff --git a/lib/models.dart b/lib/models.dart
index e40f3c4..63bc3d5 100644
--- a/lib/models.dart
+++ b/lib/models.dart
@@ -315,13 +315,11 @@ class BufferModel extends ChangeNotifier {
	bool focused = false;

	// For channels only
	String? _topic;
	bool _joining = false;
	bool _joined = false;
	MemberListModel? _members;

	// For users only
	String? _realname;
	bool? _online;
	bool? _away;

@@ -339,7 +337,7 @@ class BufferModel extends ChangeNotifier {
	bool get pinned => entry.pinned;
	bool get muted => entry.muted;

	String? get topic => _topic;
	String? get topic => entry.topic;
	bool get joining => _joining;
	bool get joined => _joined;
	MemberListModel? get members => _members;
@@ -348,14 +346,14 @@ class BufferModel extends ChangeNotifier {
	bool? get away => _away;

	String? get realname {
		if (_realname == null || isStubRealname(_realname!, name)) {
		if (entry.realname == null || isStubRealname(entry.realname!, name)) {
			return null;
		}
		return _realname!;
		return entry.realname!;
	}

	set topic(String? topic) {
		_topic = topic;
		entry.topic = topic;
		notifyListeners();
	}

@@ -390,7 +388,7 @@ class BufferModel extends ChangeNotifier {
	}

	set realname(String? realname) {
		_realname = realname;
		entry.realname = realname;
		notifyListeners();
	}

diff --git a/lib/page/buffer.dart b/lib/page/buffer.dart
index bb6206f..bd927c9 100644
--- a/lib/page/buffer.dart
+++ b/lib/page/buffer.dart
@@ -46,7 +46,7 @@ class BufferPage extends StatefulWidget {
		if (client.isChannel(name)) {
			_join(client, buffer);
		} else {
			fetchBufferUser(client, buffer);
			clientProvider.fetchBufferUser(buffer);
			client.monitor([name]);
		}
	}

base-commit: cc4faadf0f8b96de5248cc056c7457cd6c5ab4c2
-- 
2.17.1
Pushed, thanks!