From 5580ff3ce3fc449a25b879f5d640a3a2cbafc014 Mon Sep 17 00:00:00 2001 From: Nikita Konev Date: Fri, 24 Jan 2025 12:03:04 +0300 Subject: [PATCH] initial attempt to fix chat fileter for chat edit --- chat/db/chat.go | 16 +++++++--------- chat/handlers/chat.go | 5 ++--- frontend/src/ChatList.vue | 26 +++++++++++++++++--------- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/chat/db/chat.go b/chat/db/chat.go index eb805e308..e2e5afc29 100644 --- a/chat/db/chat.go +++ b/chat/db/chat.go @@ -1260,7 +1260,7 @@ func (db *DB) GetUserChatNotificationSettings(ctx context.Context, userId, chatI } // see also getRowNumbers -func (tx *Tx) ChatFilter(ctx context.Context, participantId int64, chatId int64, edgeChatId *int64, pageSize int, reverse bool, searchString string, additionalFoundUserIds []int64) (bool, error) { +func (tx *Tx) ChatFilter(ctx context.Context, participantId int64, chatId int64, pageSize int, reverse bool, searchString string, additionalFoundUserIds []int64) (bool, error) { orderDirection := "desc" if reverse { @@ -1275,7 +1275,7 @@ func (tx *Tx) ChatFilter(ctx context.Context, participantId int64, chatId int64, var row *sql.Row if searchString != "" { row = tx.QueryRowContext(ctx, fmt.Sprintf(` - with first_page as ( + with a_page as ( select inn3.* from ( select inn2.* from ( select id, rn FROM ( @@ -1286,17 +1286,16 @@ func (tx *Tx) ChatFilter(ctx context.Context, participantId int64, chatId int64, ) inn2 limit $4 ) inn3 ) - select exists (select * from first_page where id = $5) -- chat id to probe - and (($6::bigint is null) or exists (select * from first_page where id = $6)) + select exists (select * from a_page where id = $5) `, chat_order, orderDirection, chatFrom(true), getChatSearchClause(additionalFoundUserIds)), - participantId, searchStringWithPercents, searchString, pageSize, chatId, edgeChatId) + participantId, searchStringWithPercents, searchString, pageSize, chatId) // last line: // edge on the screen - here we ensure that this is the first page, in (1, 2) means the first place for the toppest element or the second place after sorting // checking ($6::bigint is null) is needed for the case no items on the screen so frontend has edgeChatId == null // casing to bigint needed because of https://github.com/jackc/pgx/issues/281 } else { row = tx.QueryRowContext(ctx, fmt.Sprintf(` - with first_page as ( + with a_page as ( select inn3.* from ( select inn2.* from ( select id, rn FROM ( @@ -1307,10 +1306,9 @@ func (tx *Tx) ChatFilter(ctx context.Context, participantId int64, chatId int64, ) inn2 limit $2 ) inn3 ) - select exists (select * from first_page where id = $3) -- chat id to probe - and (($4::bigint is null) or exists (select * from first_page where id = $4)) + select exists (select * from a_page where id = $3) `, chat_order, orderDirection, chatFrom(true), chat_where), - participantId, pageSize, chatId, edgeChatId) + participantId, pageSize, chatId) } if row.Err() != nil { tx.lgr.WithTracing(ctx).Errorf("Error during get Search %v", row.Err()) diff --git a/chat/handlers/chat.go b/chat/handlers/chat.go index 52e07b6bc..eadf1a9cd 100644 --- a/chat/handlers/chat.go +++ b/chat/handlers/chat.go @@ -221,8 +221,7 @@ func (ch *ChatHandler) HasNewMessages(c echo.Context) error { type ChatFilterDto struct { SearchString string `json:"searchString"` PageSize int `json:"pageSize"` - ChatId int64 `json:"chatId"` // id of probe element - EdgeChatId *int64 `json:"edgeChatId"` // edge chatId on the this page on screen + ChatId int64 `json:"chatId"` // id of probe element } func (ch *ChatHandler) Filter(c echo.Context) error { @@ -243,7 +242,7 @@ func (ch *ChatHandler) Filter(c echo.Context) error { var additionalFoundUserIds = ch.getAdditionalUserIds(c.Request().Context(), searchString) return db.Transact(c.Request().Context(), ch.db, func(tx *db.Tx) error { - found, err := tx.ChatFilter(c.Request().Context(), userPrincipalDto.UserId, bindTo.ChatId, bindTo.EdgeChatId, bindTo.PageSize, reverse, searchString, additionalFoundUserIds) + found, err := tx.ChatFilter(c.Request().Context(), userPrincipalDto.UserId, bindTo.ChatId, bindTo.PageSize, reverse, searchString, additionalFoundUserIds) if err != nil { return err } diff --git a/frontend/src/ChatList.vue b/frontend/src/ChatList.vue index 92c0ad353..e4eac98da 100644 --- a/frontend/src/ChatList.vue +++ b/frontend/src/ChatList.vue @@ -536,6 +536,11 @@ export default { return false } }, + removeTopElementToMatchSizeIfNeed() { + if (this.items.length >= PAGE_SIZE) { + this.items.slice(0, -1); + } + }, addItem(dto) { console.log("Adding item", dto); this.transformItem(dto); @@ -562,7 +567,6 @@ export default { searchString: this.searchString, pageSize: PAGE_SIZE, chatId: dto.id, - edgeChatId: this.startingFromItemIdTop, }, { params: { reverse: false @@ -570,7 +574,9 @@ export default { signal: this.requestAbortController.signal }).then(({data}) => { if (data.found) { + this.removeTopElementToMatchSizeIfNeed(); this.addItem(dto); + // TODO /fresh this.performMarking(); } else { console.log("Skipping adding", dto) @@ -582,7 +588,6 @@ export default { searchString: this.searchString, pageSize: PAGE_SIZE, chatId: dto.id, - edgeChatId: this.startingFromItemIdTop, }, { params: { reverse: false @@ -596,12 +601,14 @@ export default { const changedDto = this.applyState(this.items[idxOf], dto); // preserve online and isInVideo this.changeItem(changedDto); } else { + this.removeTopElementToMatchSizeIfNeed(); this.addItem(dto); // used to/along with redraw a public chat when user leaves from it } + // TODO /fresh this.performMarking(); } else { - console.log("Not found for editing, removing from the current view", dto); - this.removeItem(dto); + console.log("Not matched after editing, removing from the current view", dto); + this.onDeleteChat(dto); } }) }, @@ -613,11 +620,12 @@ export default { } }, onDeleteChat(dto) { - if (this.hasItem(dto)) { - this.removeItem(dto); - } else { - console.log("Item was not been removed", dto); - } + if (this.hasItem(dto)) { + this.removeItem(dto); + // TODO /fresh + } else { + console.log("Item was not been removed", dto); + } }, // does should change items list (new item added to visible part or not for example) hasItem(item) {