Collapse nick changes

This commit is contained in:
Ken-Håvard Lieng 2020-07-05 08:32:29 +02:00
parent 1fe4c4d17e
commit ea4f321fe7
4 changed files with 87 additions and 80 deletions

File diff suppressed because one or more lines are too long

View File

@ -52,7 +52,7 @@ function initNetworks(state, networks = []) {
}); });
} }
const collapsedEvents = ['join', 'part', 'quit']; const collapsedEvents = ['join', 'part', 'quit', 'nick'];
function shouldCollapse(msg1, msg2) { function shouldCollapse(msg1, msg2) {
return ( return (
@ -75,38 +75,33 @@ const eventVerbs = {
quit: 'quit' quit: 'quit'
}; };
function renderEvent(event, type, nicks) { function renderEvent(result, type, events) {
const ending = eventVerbs[type]; const ending = eventVerbs[type];
if (nicks.length === 1) { if (result.length > 1) {
event.push(blocks.nick(nicks[0])); result[result.length - 1].text += ', ';
event.push(blocks.text(` ${ending}`)); }
} else if (nicks.length === 2) {
event.push(blocks.nick(nicks[0])); if (events.length === 1) {
event.push(blocks.text(' and ')); result.push(blocks.nick(events[0][0]));
event.push(blocks.nick(nicks[1])); result.push(blocks.text(` ${ending}`));
event.push(blocks.text(` ${ending}`)); } else if (events.length === 2) {
} else if (nicks.length > 2) { result.push(blocks.nick(events[0][0]));
event.push(blocks.nick(nicks[0])); result.push(blocks.text(' and '));
event.push(blocks.text(', ')); result.push(blocks.nick(events[1][0]));
event.push(blocks.nick(nicks[1])); result.push(blocks.text(` ${ending}`));
event.push(blocks.text(' and ')); } else if (events.length > 2) {
event.push(blocks.events(nicks.length - 2)); result.push(blocks.nick(events[0][0]));
event.push(blocks.text(` ${ending}`)); result.push(blocks.text(', '));
result.push(blocks.nick(events[1][0]));
result.push(blocks.text(' and '));
result.push(blocks.events(events.length - 2));
result.push(blocks.text(` ${ending}`));
} }
} }
function renderEvents(events) { function renderEvents(events) {
const first = events[0]; const first = events[0];
if (first.type === 'nick') {
const [oldNick, newNick] = first.params;
return [
blocks.nick(oldNick),
blocks.text(' changed nick to '),
blocks.nick(newNick)
];
}
if (first.type === 'kick') { if (first.type === 'kick') {
const [kicked, by] = first.params; const [kicked, by] = first.params;
@ -134,9 +129,9 @@ function renderEvents(events) {
const [nick] = event.params; const [nick] = event.params;
if (!byType[event.type]) { if (!byType[event.type]) {
byType[event.type] = [nick]; byType[event.type] = [event.params];
} else if (byType[event.type].indexOf(nick) === -1) { } else if (byType[event.type].indexOf(nick) === -1) {
byType[event.type].push(nick); byType[event.type].push(event.params);
} }
} }
@ -147,17 +142,29 @@ function renderEvents(events) {
} }
if (byType.part) { if (byType.part) {
if (result.length > 1) {
result[result.length - 1].text += ', ';
}
renderEvent(result, 'part', byType.part); renderEvent(result, 'part', byType.part);
} }
if (byType.quit) { if (byType.quit) {
renderEvent(result, 'quit', byType.quit);
}
if (byType.nick) {
if (result.length > 1) { if (result.length > 1) {
result[result.length - 1].text += ', '; result[result.length - 1].text += ', ';
} }
renderEvent(result, 'quit', byType.quit);
const [oldNick, newNick] = byType.nick[0];
result.push(blocks.nick(oldNick));
result.push(blocks.text(' changed nick to '));
result.push(blocks.nick(newNick));
if (byType.nick.length > 1) {
result.push(blocks.text(' and '));
result.push(blocks.events(byType.nick.length - 1));
result.push(blocks.text(' changed nick'));
}
} }
return result; return result;

View File

@ -322,7 +322,7 @@ func (u *User) LogEvent(network, name string, params []string, channels ...strin
return nil return nil
} }
var collapsed = []string{"join", "part", "quit"} var collapsed = []string{"join", "part", "quit", "nick"}
func shouldCollapse(msg *Message, event Event) bool { func shouldCollapse(msg *Message, event Event) bool {
matches := 0 matches := 0

View File

@ -226,26 +226,26 @@ func TestMessages(t *testing.T) {
assert.True(t, hasMore) assert.True(t, hasMore)
assert.Len(t, messages[0].Events, 2) assert.Len(t, messages[0].Events, 2)
assert.Equal(t, "part", messages[0].Events[1].Type) assert.Equal(t, "part", messages[0].Events[1].Type)
assert.NotZero(t, messages[0].Events[0].Time) assert.NotZero(t, messages[0].Events[1].Time)
user.LogEvent("irc.freenode.net", "nick", []string{"bob", "rob"}, "#go-nuts") user.LogEvent("irc.freenode.net", "nick", []string{"bob", "rob"}, "#go-nuts")
messages, hasMore, err = user.LastMessages("irc.freenode.net", "#go-nuts", 1) messages, hasMore, err = user.LastMessages("irc.freenode.net", "#go-nuts", 1)
assert.Zero(t, messages[0].Content) assert.Zero(t, messages[0].Content)
assert.Nil(t, err) assert.Nil(t, err)
assert.True(t, hasMore) assert.True(t, hasMore)
assert.Len(t, messages[0].Events, 1) assert.Len(t, messages[0].Events, 3)
assert.Equal(t, "nick", messages[0].Events[0].Type) assert.Equal(t, "nick", messages[0].Events[2].Type)
assert.NotZero(t, messages[0].Events[0].Time) assert.NotZero(t, messages[0].Events[2].Time)
user.LogEvent("irc.freenode.net", "quit", []string{"rob", "bored"}, "#go-nuts") user.LogEvent("irc.freenode.net", "quit", []string{"rob", "bored"}, "#go-nuts")
messages, hasMore, err = user.LastMessages("irc.freenode.net", "#go-nuts", 1) messages, hasMore, err = user.LastMessages("irc.freenode.net", "#go-nuts", 1)
assert.Zero(t, messages[0].Content) assert.Zero(t, messages[0].Content)
assert.Nil(t, err) assert.Nil(t, err)
assert.True(t, hasMore) assert.True(t, hasMore)
assert.Len(t, messages[0].Events, 1) assert.Len(t, messages[0].Events, 4)
assert.Equal(t, "quit", messages[0].Events[0].Type) assert.Equal(t, "quit", messages[0].Events[3].Type)
assert.Equal(t, []string{"rob", "bored"}, messages[0].Events[0].Params) assert.Equal(t, []string{"rob", "bored"}, messages[0].Events[3].Params)
assert.NotZero(t, messages[0].Events[0].Time) assert.NotZero(t, messages[0].Events[3].Time)
db.Close() db.Close()
} }