Add query to list conversations with unread messages in them

This commit is contained in:
Alessio 2024-04-27 23:01:56 -07:00
parent 28267a802a
commit 5c546bf308
3 changed files with 38 additions and 6 deletions

View File

@ -626,3 +626,21 @@ func (p Profile) NextDMPage(c DMCursor) DMChatView {
p.fill_dm_contents(&ret.DMTrove) p.fill_dm_contents(&ret.DMTrove)
return ret return ret
} }
func (p Profile) GetUnreadConversations(user_id UserID) (ret []DMChatRoomID) {
err := p.DB.Select(&ret, `
with latest_messages as (select chat_room_id, max(id) as latest_message_id from chat_messages group by chat_room_id),
unread_room_ids as (
select chat_room_participants.chat_room_id
from chat_room_participants
join latest_messages on latest_messages.chat_room_id = chat_room_participants.chat_room_id
where chat_room_participants.user_id = ?
and latest_messages.latest_message_id > chat_room_participants.last_read_event_id
)
select id from chat_rooms where id in unread_room_ids
`, user_id)
if err != nil {
panic(err)
}
return
}

View File

@ -266,7 +266,7 @@ func TestGetChatRoomContentsAfterTimestamp(t *testing.T) {
chat_view := profile.GetChatRoomContents(room_id, 1686025129141) chat_view := profile.GetChatRoomContents(room_id, 1686025129141)
// MessageIDs should just be the ones in the thread // MessageIDs should just be the ones in the thread
require.Equal(chat_view.MessageIDs, []DMMessageID{1665936253483614215, 1665936253483614216, 1665936253483614217}) require.Equal(chat_view.MessageIDs, []DMMessageID{1665936253483614215, 1665936253483614216, 1665937253483614217})
// Replied messages should be available, but not in the list of MessageIDs // Replied messages should be available, but not in the list of MessageIDs
require.Len(chat_view.Messages, 4) require.Len(chat_view.Messages, 4)
@ -279,3 +279,17 @@ func TestGetChatRoomContentsAfterTimestamp(t *testing.T) {
assert.Equal(msg.ID, msg_id) assert.Equal(msg.ID, msg_id)
} }
} }
func TestGetUnreadConversations(t *testing.T) {
require := require.New(t)
assert := assert.New(t)
profile, err := persistence.LoadProfile("../../sample_data/profile")
require.NoError(err)
offline_twatter_unreads := profile.GetUnreadConversations(UserID(1488963321701171204))
require.Len(offline_twatter_unreads, 1)
assert.Equal(offline_twatter_unreads[0], DMChatRoomID("1488963321701171204-1178839081222115328"))
mystery_unreads := profile.GetUnreadConversations(UserID(1178839081222115328))
assert.Len(mystery_unreads, 0)
}

View File

@ -388,10 +388,10 @@ create table chat_room_participants(rowid integer primary key,
unique(chat_room_id, user_id) unique(chat_room_id, user_id)
); );
INSERT INTO chat_room_participants VALUES INSERT INTO chat_room_participants VALUES
(1,'1458284524761075714-1488963321701171204',1458284524761075714,1665936253483614212,0,0,0,0,0,0,''), (1,'1458284524761075714-1488963321701171204',1458284524761075714,1766595519000760325,0,0,0,0,0,0,''),
(2,'1458284524761075714-1488963321701171204',1488963321701171204,1665936253483614212,1,0,0,0,1,0,'AT_END'), (2,'1458284524761075714-1488963321701171204',1488963321701171204,1766595519000760325,1,0,0,0,1,0,'AT_END'),
(3,'1488963321701171204-1178839081222115328',1488963321701171204,1686075343331,1,0,0,0,1,0,'AT_END'), (3,'1488963321701171204-1178839081222115328',1488963321701171204,1665936253834578774,1,0,0,0,1,0,'TODO (what is it if there''s unreads?'),
(4,'1488963321701171204-1178839081222115328',1178839081222115328,1686075343331,0,0,0,0,0,0,''); (4,'1488963321701171204-1178839081222115328',1178839081222115328,1665937253483614217,0,0,0,0,0,0,'');
create table chat_messages (rowid integer primary key, create table chat_messages (rowid integer primary key,
id integer unique not null check(typeof(id) = 'integer'), id integer unique not null check(typeof(id) = 'integer'),
@ -414,7 +414,7 @@ INSERT INTO chat_messages VALUES
(6,1665936253483614214,'1488963321701171204-1178839081222115328',1178839081222115328,1686025129141,'',0,'bruh2',0), (6,1665936253483614214,'1488963321701171204-1178839081222115328',1178839081222115328,1686025129141,'',0,'bruh2',0),
(7,1665936253483614215,'1488963321701171204-1178839081222115328',1178839081222115328,1686025129142,'',1665936253483614214,'replying to bruh2',0), (7,1665936253483614215,'1488963321701171204-1178839081222115328',1178839081222115328,1686025129142,'',1665936253483614214,'replying to bruh2',0),
(8,1665936253483614216,'1488963321701171204-1178839081222115328',1488963321701171204,1686025129143,'',0,'This conversation is totally fake lol',0), (8,1665936253483614216,'1488963321701171204-1178839081222115328',1488963321701171204,1686025129143,'',0,'This conversation is totally fake lol',0),
(9,1665936253483614217,'1488963321701171204-1178839081222115328',1178839081222115328,1686025129144,'',0,'exactly',0), (9,1665937253483614217,'1488963321701171204-1178839081222115328',1178839081222115328,1686025129144,'',0,'exactly',0),
(36,1766248283901776125,'1458284524761075714-1488963321701171204',1458284524761075714,1709941380913,'',0,'',0), (36,1766248283901776125,'1458284524761075714-1488963321701171204',1458284524761075714,1709941380913,'',0,'',0),
(15,1766255994668191902,'1458284524761075714-1488963321701171204',1458284524761075714,1709943219300,'',0,'You wrote this?',0), (15,1766255994668191902,'1458284524761075714-1488963321701171204',1458284524761075714,1709943219300,'',0,'You wrote this?',0),
(46,1766595519000760325,'1458284524761075714-1488963321701171204',1458284524761075714,1710024168245,'',0,'This looks pretty good huh',0); (46,1766595519000760325,'1458284524761075714-1488963321701171204',1458284524761075714,1710024168245,'',0,'This looks pretty good huh',0);