Skip to content

Commit

Permalink
Sort stories when they were read at
Browse files Browse the repository at this point in the history
  • Loading branch information
josh-signal committed Sep 21, 2022
1 parent b04fbb6 commit af5a496
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 7 deletions.
1 change: 1 addition & 0 deletions ts/model-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ export type MessageAttributesType = {
sendHQImages?: boolean;

// Should only be present for incoming messages and errors
readAt?: number;
readStatus?: ReadStatus;
// Used for all kinds of notifications, as well as incoming messages
seenStatus?: SeenStatus;
Expand Down
1 change: 1 addition & 0 deletions ts/services/MessageUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function markReadOrViewed(

const nextMessageAttributes: MessageAttributesType = {
...messageAttrs,
readAt: timestamp,
readStatus: newReadStatus,
seenStatus: SeenStatus.Seen,
};
Expand Down
1 change: 1 addition & 0 deletions ts/services/storyLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export function getStoryDataFromMessageAttributes(
'conversationId',
'deletedForEveryone',
'reactions',
'readAt',
'readStatus',
'sendStateByConversationId',
'source',
Expand Down
17 changes: 14 additions & 3 deletions ts/state/ducks/stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export type StoryDataType = {
| 'conversationId'
| 'deletedForEveryone'
| 'reactions'
| 'readAt'
| 'readStatus'
| 'sendStateByConversationId'
| 'source'
Expand Down Expand Up @@ -140,7 +141,10 @@ type LoadStoryRepliesActionType = {

type MarkStoryReadActionType = {
type: typeof MARK_STORY_READ;
payload: string;
payload: {
messageId: string;
readAt: number;
};
};

type QueueStoryDownloadActionType = {
Expand Down Expand Up @@ -456,7 +460,10 @@ function markStoryRead(

dispatch({
type: MARK_STORY_READ,
payload: messageId,
payload: {
messageId,
readAt: storyReadDate,
},
});
};
}
Expand Down Expand Up @@ -1157,6 +1164,7 @@ export function reducer(
'expireTimer',
'messageId',
'reactions',
'readAt',
'readStatus',
'sendStateByConversationId',
'source',
Expand Down Expand Up @@ -1228,12 +1236,15 @@ export function reducer(
}

if (action.type === MARK_STORY_READ) {
const { messageId, readAt } = action.payload;

return {
...state,
stories: state.stories.map(story => {
if (story.messageId === action.payload) {
if (story.messageId === messageId) {
return {
...story,
readAt,
readStatus: ReadStatus.Viewed,
};
}
Expand Down
22 changes: 18 additions & 4 deletions ts/state/selectors/stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ function sortByRecencyAndUnread(
return -1;
}

if (storyA.storyView.readAt && storyB.storyView.readAt) {
return storyA.storyView.readAt > storyB.storyView.readAt ? -1 : 1;
}

return storyA.storyView.timestamp > storyB.storyView.timestamp ? -1 : 1;
}

Expand Down Expand Up @@ -145,10 +149,19 @@ export function getStoryView(
'title',
]);

const { attachment, timestamp, expirationStartTimestamp, expireTimer } = pick(
story,
['attachment', 'timestamp', 'expirationStartTimestamp', 'expireTimer']
);
const {
attachment,
expirationStartTimestamp,
expireTimer,
readAt,
timestamp,
} = pick(story, [
'attachment',
'expirationStartTimestamp',
'expireTimer',
'readAt',
'timestamp',
]);

const { sendStateByConversationId } = story;
let sendState: Array<StorySendStateType> | undefined;
Expand Down Expand Up @@ -182,6 +195,7 @@ export function getStoryView(
isHidden: Boolean(sender.hideStory),
isUnread: story.readStatus === ReadStatus.Unread,
messageId: story.messageId,
readAt,
sender,
sendState,
timestamp,
Expand Down
1 change: 1 addition & 0 deletions ts/types/Stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export type StoryViewType = {
isHidden?: boolean;
isUnread?: boolean;
messageId: string;
readAt?: number;
sender: Pick<
ConversationType,
| 'acceptedMessageRequest'
Expand Down

0 comments on commit af5a496

Please sign in to comment.