diff --git a/ts/state/ducks/stories.ts b/ts/state/ducks/stories.ts index 58f705fbc..5668dd4c5 100644 --- a/ts/state/ducks/stories.ts +++ b/ts/state/ducks/stories.ts @@ -105,6 +105,10 @@ function markStoryRead( return; } + if (!isDownloaded(matchingStory.attachment)) { + return; + } + if (matchingStory.readStatus !== ReadStatus.Unread) { return; } diff --git a/ts/state/selectors/stories.ts b/ts/state/selectors/stories.ts index 7a6660aa8..c167aad2d 100644 --- a/ts/state/selectors/stories.ts +++ b/ts/state/selectors/stories.ts @@ -21,6 +21,32 @@ export const shouldShowStoriesView = createSelector( ({ isShowingStoriesView }): boolean => isShowingStoriesView ); +function getNewestStory(x: ConversationStoryType): StoryViewType { + return x.stories[x.stories.length - 1]; +} + +function sortByRecencyAndUnread( + a: ConversationStoryType, + b: ConversationStoryType +): number { + const storyA = getNewestStory(a); + const storyB = getNewestStory(b); + + if (storyA.isUnread && storyB.isUnread) { + return storyA.timestamp > storyB.timestamp ? -1 : 1; + } + + if (storyB.isUnread) { + return 1; + } + + if (storyA.isUnread) { + return -1; + } + + return storyA.timestamp > storyB.timestamp ? -1 : 1; +} + export const getStories = createSelector( getConversationSelector, getStoriesState, @@ -99,10 +125,11 @@ export const getStories = createSelector( }); }); - // Reversing so that the story list is in DESC order return { - hiddenStories: Array.from(hiddenStoriesById.values()).reverse(), - stories: Array.from(storiesById.values()).reverse(), + hiddenStories: Array.from(hiddenStoriesById.values()).sort( + sortByRecencyAndUnread + ), + stories: Array.from(storiesById.values()).sort(sortByRecencyAndUnread), }; } );