diff --git a/pkg/persistence/dm_queries.go b/pkg/persistence/dm_queries.go index 7b4a48e..9bdea1a 100644 --- a/pkg/persistence/dm_queries.go +++ b/pkg/persistence/dm_queries.go @@ -626,3 +626,21 @@ func (p Profile) NextDMPage(c DMCursor) DMChatView { p.fill_dm_contents(&ret.DMTrove) 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 +} diff --git a/pkg/persistence/dm_queries_test.go b/pkg/persistence/dm_queries_test.go index 1b170ce..6c16918 100644 --- a/pkg/persistence/dm_queries_test.go +++ b/pkg/persistence/dm_queries_test.go @@ -266,7 +266,7 @@ func TestGetChatRoomContentsAfterTimestamp(t *testing.T) { chat_view := profile.GetChatRoomContents(room_id, 1686025129141) // 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 require.Len(chat_view.Messages, 4) @@ -279,3 +279,17 @@ func TestGetChatRoomContentsAfterTimestamp(t *testing.T) { 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) +} diff --git a/sample_data/seed_data.sql b/sample_data/seed_data.sql index 3698a8e..d705dcb 100644 --- a/sample_data/seed_data.sql +++ b/sample_data/seed_data.sql @@ -388,10 +388,10 @@ create table chat_room_participants(rowid integer primary key, unique(chat_room_id, user_id) ); INSERT INTO chat_room_participants VALUES - (1,'1458284524761075714-1488963321701171204',1458284524761075714,1665936253483614212,0,0,0,0,0,0,''), - (2,'1458284524761075714-1488963321701171204',1488963321701171204,1665936253483614212,1,0,0,0,1,0,'AT_END'), - (3,'1488963321701171204-1178839081222115328',1488963321701171204,1686075343331,1,0,0,0,1,0,'AT_END'), - (4,'1488963321701171204-1178839081222115328',1178839081222115328,1686075343331,0,0,0,0,0,0,''); + (1,'1458284524761075714-1488963321701171204',1458284524761075714,1766595519000760325,0,0,0,0,0,0,''), + (2,'1458284524761075714-1488963321701171204',1488963321701171204,1766595519000760325,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,1665937253483614217,0,0,0,0,0,0,''); create table chat_messages (rowid integer primary key, 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), (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), - (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), (15,1766255994668191902,'1458284524761075714-1488963321701171204',1458284524761075714,1709943219300,'',0,'You wrote this?',0), (46,1766595519000760325,'1458284524761075714-1488963321701171204',1458284524761075714,1710024168245,'',0,'This looks pretty good huh',0);