Skip to content

Commit

Permalink
initial attempt to fix chat fileter for chat edit
Browse files Browse the repository at this point in the history
  • Loading branch information
nkonev committed Jan 24, 2025
1 parent 96541de commit 5580ff3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
16 changes: 7 additions & 9 deletions chat/db/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 (
Expand All @@ -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 (
Expand All @@ -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())
Expand Down
5 changes: 2 additions & 3 deletions chat/handlers/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
26 changes: 17 additions & 9 deletions frontend/src/ChatList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -562,15 +567,16 @@ export default {
searchString: this.searchString,
pageSize: PAGE_SIZE,
chatId: dto.id,
edgeChatId: this.startingFromItemIdTop,
}, {
params: {
reverse: false
},
signal: this.requestAbortController.signal
}).then(({data}) => {
if (data.found) {
this.removeTopElementToMatchSizeIfNeed();
this.addItem(dto);
// TODO /fresh
this.performMarking();
} else {
console.log("Skipping adding", dto)
Expand All @@ -582,7 +588,6 @@ export default {
searchString: this.searchString,
pageSize: PAGE_SIZE,
chatId: dto.id,
edgeChatId: this.startingFromItemIdTop,
}, {
params: {
reverse: false
Expand All @@ -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);
}
})
},
Expand All @@ -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) {
Expand Down

0 comments on commit 5580ff3

Please sign in to comment.