an experimental irc client

irc: request history when needed

+33 -21
+33 -21
src/irc.zig
··· 900 900 } 901 901 } 902 902 903 + // Request more history when we are within 5 messages of the top of the screen 904 + if (iter.index < 5 and !self.at_oldest) { 905 + try self.client.requestHistory(.before, self); 906 + } 907 + 903 908 return .{ 904 909 .size = max, 905 910 .widget = self.messageViewWidget(), ··· 1786 1791 // from the batch start. We also never notify from a 1787 1792 // batched message. Batched messages also require 1788 1793 // sorting 1789 - var tag_iter = msg2.tagIterator(); 1790 - while (tag_iter.next()) |tag| { 1791 - if (mem.eql(u8, tag.key, "batch")) { 1792 - const entry = client.batches.getEntry(tag.value) orelse @panic("TODO"); 1793 - var channel = entry.value_ptr.*; 1794 - try channel.messages.append(msg2); 1795 - std.sort.insertion(Message, channel.messages.items, {}, Message.compareTime); 1796 - channel.at_oldest = false; 1797 - const time = msg2.time() orelse continue; 1798 - if (time.unixTimestamp() > channel.last_read) { 1799 - channel.has_unread = true; 1800 - const content = iter.next() orelse continue; 1801 - if (std.mem.indexOf(u8, content, client.nickname())) |_| { 1802 - channel.has_unread_highlight = true; 1803 - } 1794 + if (msg2.getTag("batch")) |tag| { 1795 + const entry = client.batches.getEntry(tag) orelse @panic("TODO"); 1796 + var channel = entry.value_ptr.*; 1797 + try channel.messages.append(msg2); 1798 + std.sort.insertion(Message, channel.messages.items, {}, Message.compareTime); 1799 + if (channel.scroll.msg_offset) |offset| { 1800 + channel.scroll.msg_offset = offset + 1; 1801 + } 1802 + channel.at_oldest = false; 1803 + const time = msg2.time() orelse return; 1804 + if (time.unixTimestamp() > channel.last_read) { 1805 + channel.has_unread = true; 1806 + const content = iter.next() orelse return; 1807 + if (std.mem.indexOf(u8, content, client.nickname())) |_| { 1808 + channel.has_unread_highlight = true; 1804 1809 } 1805 - break; 1806 1810 } 1807 1811 } else { 1808 1812 // standard handling ··· 2099 2103 } 2100 2104 2101 2105 /// fetch the history for the provided channel. 2102 - pub fn requestHistory(self: *Client, cmd: ChatHistoryCommand, channel: *Channel) !void { 2106 + pub fn requestHistory( 2107 + self: *Client, 2108 + cmd: ChatHistoryCommand, 2109 + channel: *Channel, 2110 + ) Allocator.Error!void { 2103 2111 if (!self.caps.@"draft/chathistory") return; 2104 2112 if (channel.history_requested) return; 2105 2113 ··· 2118 2126 .before => { 2119 2127 assert(channel.messages.items.len > 0); 2120 2128 const first = channel.messages.items[0]; 2121 - const time = first.getTag("time") orelse 2122 - return error.NoTimeTag; 2129 + const time = first.getTag("time") orelse { 2130 + log.warn("can't request history: no time tag", .{}); 2131 + return; 2132 + }; 2123 2133 try self.print( 2124 2134 "CHATHISTORY BEFORE {s} timestamp={s} 50\r\n", 2125 2135 .{ channel.name, time }, ··· 2129 2139 .after => { 2130 2140 assert(channel.messages.items.len > 0); 2131 2141 const last = channel.messages.getLast(); 2132 - const time = last.getTag("time") orelse 2133 - return error.NoTimeTag; 2142 + const time = last.getTag("time") orelse { 2143 + log.warn("can't request history: no time tag", .{}); 2144 + return; 2145 + }; 2134 2146 try self.print( 2135 2147 // we request 500 because we have no 2136 2148 // idea how long we've been offline