From 39372d8e0a979e7d95a4487a15e6e74c588933bd Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Mon, 9 May 2022 15:17:05 -0700 Subject: [PATCH] sorting conversations by latest --- net/server/internal/api_getChannels.go | 4 ++-- .../Contacts/Channels/useChannels.hook.js | 22 +++++++++++++++++-- net/web/src/context/useCardContext.hook.js | 2 +- net/web/src/context/useChannelContext.hook.js | 4 ++-- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/net/server/internal/api_getChannels.go b/net/server/internal/api_getChannels.go index fefccb7f..3f4f5629 100644 --- a/net/server/internal/api_getChannels.go +++ b/net/server/internal/api_getChannels.go @@ -67,7 +67,7 @@ func GetChannels(w http.ResponseWriter, r *http.Request) { } } else { if err := store.DB.Preload("Channel.Topics", func(db *gorm.DB) *gorm.DB { - return store.DB.Order("topics.id DESC").Limit(1) + return store.DB.Order("topics.id DESC") }).Preload("Channel.Cards.CardSlot").Preload("Channel.Groups.GroupSlot").Where("account_id = ? AND channel_id != 0", account.ID).Find(&slots).Error; err != nil { ErrResponse(w, http.StatusInternalServerError, err) return @@ -110,7 +110,7 @@ func GetChannels(w http.ResponseWriter, r *http.Request) { } } else { if err := store.DB.Preload("Channel.Topics", func(db *gorm.DB) *gorm.DB { - return store.DB.Order("topics.id DESC").Limit(1) + return store.DB.Order("topics.id DESC") }).Preload("Channel.Cards").Preload("Channel.Groups.Cards").Where("account_id = ? AND channel_id != 0", account.ID).Find(&slots).Error; err != nil { ErrResponse(w, http.StatusInternalServerError, err) return diff --git a/net/web/src/User/SideBar/Contacts/Channels/useChannels.hook.js b/net/web/src/User/SideBar/Contacts/Channels/useChannels.hook.js index 2c202659..14e3b3db 100644 --- a/net/web/src/User/SideBar/Contacts/Channels/useChannels.hook.js +++ b/net/web/src/User/SideBar/Contacts/Channels/useChannels.hook.js @@ -58,12 +58,30 @@ export function useChannels() { card.state.cards.forEach((value, key, map) => { cardChannels.current.push(...Array.from(value.channels.values())); }); - updateState({ channels: [ ...channels.current, ...cardChannels.current ]}); + let merged = [ ...channels.current, ...cardChannels.current ]; + merged.sort((a, b) => { + if (a?.data?.channelSummary?.lastTopic?.created > b?.data?.channelSummary?.lastTopic?.created) { + return -1; + } + return 1; + }); + updateState({ channels: merged }); }, [card]) useEffect(() => { channels.current = Array.from(channel.state.channels.values()); - updateState({ channels: [ ...channels.current, ...cardChannels.current ]}); + let merged = [ ...channels.current, ...cardChannels.current ]; + merged.sort((a, b) => { + if (a?.data?.channelSummary?.lastTopic?.created > b?.data?.channelSummary?.lastTopic?.created) { + return -1; + } + return 1; + }); +for (let i = 0; i < merged.length; i++) { + console.log(merged[i]); + console.log(merged[i]?.data?.channelSummary?.lastTopic?.created); +} + updateState({ channels: merged }); }, [channel]) return { state, actions }; diff --git a/net/web/src/context/useCardContext.hook.js b/net/web/src/context/useCardContext.hook.js index f4589b0c..232cd666 100644 --- a/net/web/src/context/useCardContext.hook.js +++ b/net/web/src/context/useCardContext.hook.js @@ -124,7 +124,7 @@ export function useCardContext() { } cur.data.detailRevision = channel.data.detailRevision; } - if (cur.data.detailRevision != channel.data.detailRevision) { + if (cur.data.topicRevision != channel.data.topicRevision) { if (channel.data.channelSummary != null) { cur.data.channelSummary = channel.data.channelSummary; } diff --git a/net/web/src/context/useChannelContext.hook.js b/net/web/src/context/useChannelContext.hook.js index b3c5861a..1e58b4c9 100644 --- a/net/web/src/context/useChannelContext.hook.js +++ b/net/web/src/context/useChannelContext.hook.js @@ -41,8 +41,8 @@ export function useChannelContext() { cur.data.detailRevision = channel.data.detailRevision; } if (cur.data.topicRevision != channel.data.topicRevision) { - if (channel.data.channelDetail != null) { - cur.data.channelDetail = channel.data.channelDetail; + if (channel.data.channelSummary != null) { + cur.data.channelSummary = channel.data.channelSummary; } else { let summary = await getChannelSummary(access.current, channel.id);