2022-07-01 00:52:03 +00:00
|
|
|
// Copyright 2022 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2022-11-11 04:10:30 +00:00
|
|
|
import { omit } from 'lodash';
|
2022-07-21 00:07:09 +00:00
|
|
|
import type { ThunkAction } from 'redux-thunk';
|
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
import type { ReadonlyDeep } from 'type-fest';
|
2022-07-21 00:07:09 +00:00
|
|
|
import type { StateType as RootStateType } from '../reducer';
|
|
|
|
import type { StoryDistributionWithMembersType } from '../../sql/Interface';
|
2023-08-10 16:43:33 +00:00
|
|
|
import type { StoryDistributionIdString } from '../../types/StoryDistributionId';
|
|
|
|
import type { ServiceIdString } from '../../types/ServiceId';
|
2022-07-21 00:07:09 +00:00
|
|
|
import * as log from '../../logging/log';
|
2024-07-22 18:16:33 +00:00
|
|
|
import { DataReader, DataWriter } from '../../sql/Client';
|
2022-11-10 15:51:39 +00:00
|
|
|
import { MY_STORY_ID } from '../../types/Stories';
|
2023-08-10 16:43:33 +00:00
|
|
|
import { generateStoryDistributionId } from '../../types/StoryDistributionId';
|
2022-10-13 16:14:50 +00:00
|
|
|
import { deleteStoryForEveryone } from '../../util/deleteStoryForEveryone';
|
2022-07-21 00:07:09 +00:00
|
|
|
import { replaceIndex } from '../../util/replaceIndex';
|
|
|
|
import { storageServiceUploadJob } from '../../services/storage';
|
2022-12-09 17:35:34 +00:00
|
|
|
import type { BoundActionCreatorsMapObject } from '../../hooks/useBoundActions';
|
2022-07-01 00:52:03 +00:00
|
|
|
import { useBoundActions } from '../../hooks/useBoundActions';
|
|
|
|
|
|
|
|
// State
|
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
export type StoryDistributionListDataType = ReadonlyDeep<{
|
2023-08-10 16:43:33 +00:00
|
|
|
id: StoryDistributionIdString;
|
2022-07-21 00:07:09 +00:00
|
|
|
deletedAtTimestamp?: number;
|
2022-07-01 00:52:03 +00:00
|
|
|
name: string;
|
|
|
|
allowsReplies: boolean;
|
|
|
|
isBlockList: boolean;
|
2023-08-10 16:43:33 +00:00
|
|
|
memberServiceIds: Array<ServiceIdString>;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2022-07-01 00:52:03 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
export type StoryDistributionListStateType = ReadonlyDeep<{
|
2022-07-01 00:52:03 +00:00
|
|
|
distributionLists: Array<StoryDistributionListDataType>;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2022-07-01 00:52:03 +00:00
|
|
|
|
|
|
|
// Actions
|
|
|
|
|
2022-07-21 00:07:09 +00:00
|
|
|
const ALLOW_REPLIES_CHANGED = 'storyDistributionLists/ALLOW_REPLIES_CHANGED';
|
|
|
|
const CREATE_LIST = 'storyDistributionLists/CREATE_LIST';
|
2022-11-11 04:10:30 +00:00
|
|
|
export const DELETE_LIST = 'storyDistributionLists/DELETE_LIST';
|
|
|
|
export const HIDE_MY_STORIES_FROM =
|
|
|
|
'storyDistributionLists/HIDE_MY_STORIES_FROM';
|
|
|
|
export const MODIFY_LIST = 'storyDistributionLists/MODIFY_LIST';
|
2022-07-21 00:07:09 +00:00
|
|
|
const RESET_MY_STORIES = 'storyDistributionLists/RESET_MY_STORIES';
|
2022-11-11 04:10:30 +00:00
|
|
|
export const VIEWERS_CHANGED = 'storyDistributionLists/VIEWERS_CHANGED';
|
2022-07-21 00:07:09 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type AllowRepliesChangedActionType = ReadonlyDeep<{
|
2022-07-21 00:07:09 +00:00
|
|
|
type: typeof ALLOW_REPLIES_CHANGED;
|
|
|
|
payload: {
|
|
|
|
listId: string;
|
|
|
|
allowsReplies: boolean;
|
|
|
|
};
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2022-07-01 00:52:03 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type CreateListActionType = ReadonlyDeep<{
|
2022-07-01 00:52:03 +00:00
|
|
|
type: typeof CREATE_LIST;
|
|
|
|
payload: StoryDistributionListDataType;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2022-07-01 00:52:03 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type DeleteListActionType = ReadonlyDeep<{
|
2022-07-21 00:07:09 +00:00
|
|
|
type: typeof DELETE_LIST;
|
|
|
|
payload: {
|
|
|
|
listId: string;
|
|
|
|
deletedAtTimestamp: number;
|
|
|
|
};
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2022-07-21 00:07:09 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type HideMyStoriesFromActionType = ReadonlyDeep<{
|
2022-07-21 00:07:09 +00:00
|
|
|
type: typeof HIDE_MY_STORIES_FROM;
|
2023-08-10 16:43:33 +00:00
|
|
|
payload: Array<ServiceIdString>;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2022-07-21 00:07:09 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type ModifyDistributionListType = ReadonlyDeep<
|
2023-08-10 16:43:33 +00:00
|
|
|
Omit<StoryDistributionListDataType, 'memberServiceIds'> & {
|
|
|
|
membersToAdd: Array<ServiceIdString>;
|
|
|
|
membersToRemove: Array<ServiceIdString>;
|
2023-01-13 20:07:26 +00:00
|
|
|
}
|
|
|
|
>;
|
2022-07-21 00:07:09 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
export type ModifyListActionType = ReadonlyDeep<{
|
2022-07-01 00:52:03 +00:00
|
|
|
type: typeof MODIFY_LIST;
|
2022-07-21 00:07:09 +00:00
|
|
|
payload: ModifyDistributionListType;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2022-07-21 00:07:09 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type ResetMyStoriesActionType = ReadonlyDeep<{
|
2022-07-21 00:07:09 +00:00
|
|
|
type: typeof RESET_MY_STORIES;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2022-07-21 00:07:09 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type ViewersChangedActionType = ReadonlyDeep<{
|
2022-07-21 00:07:09 +00:00
|
|
|
type: typeof VIEWERS_CHANGED;
|
|
|
|
payload: {
|
|
|
|
listId: string;
|
2023-08-10 16:43:33 +00:00
|
|
|
memberServiceIds: Array<ServiceIdString>;
|
2022-07-21 00:07:09 +00:00
|
|
|
};
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2022-07-01 00:52:03 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
export type StoryDistributionListsActionType = ReadonlyDeep<
|
2022-07-21 00:07:09 +00:00
|
|
|
| AllowRepliesChangedActionType
|
2022-07-01 00:52:03 +00:00
|
|
|
| CreateListActionType
|
2022-07-21 00:07:09 +00:00
|
|
|
| DeleteListActionType
|
|
|
|
| HideMyStoriesFromActionType
|
|
|
|
| ModifyListActionType
|
|
|
|
| ResetMyStoriesActionType
|
2023-01-13 20:07:26 +00:00
|
|
|
| ViewersChangedActionType
|
|
|
|
>;
|
2022-07-01 00:52:03 +00:00
|
|
|
|
|
|
|
// Action Creators
|
|
|
|
|
2022-07-21 00:07:09 +00:00
|
|
|
function allowsRepliesChanged(
|
|
|
|
listId: string,
|
|
|
|
allowsReplies: boolean
|
|
|
|
): ThunkAction<void, RootStateType, null, AllowRepliesChangedActionType> {
|
|
|
|
return async dispatch => {
|
2024-07-24 00:31:40 +00:00
|
|
|
const storyDistribution =
|
|
|
|
await DataReader.getStoryDistributionWithMembers(listId);
|
2022-07-21 00:07:09 +00:00
|
|
|
|
|
|
|
if (!storyDistribution) {
|
|
|
|
log.warn(
|
|
|
|
'storyDistributionLists.allowsRepliesChanged: No story found for id',
|
|
|
|
listId
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (storyDistribution.allowsReplies === allowsReplies) {
|
|
|
|
log.warn(
|
|
|
|
'storyDistributionLists.allowsRepliesChanged: story already has the same value',
|
|
|
|
{ listId, allowsReplies }
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.modifyStoryDistribution({
|
2022-07-21 00:07:09 +00:00
|
|
|
...storyDistribution,
|
|
|
|
allowsReplies,
|
|
|
|
storageNeedsSync: true,
|
|
|
|
});
|
|
|
|
|
2024-09-30 22:23:32 +00:00
|
|
|
storageServiceUploadJob({
|
|
|
|
reason: 'distributionLists/allowsRepliesChanged',
|
|
|
|
});
|
2022-07-21 00:07:09 +00:00
|
|
|
|
|
|
|
log.info(
|
|
|
|
'storyDistributionLists.allowsRepliesChanged: allowsReplies has changed',
|
|
|
|
listId
|
|
|
|
);
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: ALLOW_REPLIES_CHANGED,
|
|
|
|
payload: {
|
|
|
|
listId,
|
|
|
|
allowsReplies,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-01 00:52:03 +00:00
|
|
|
function createDistributionList(
|
2022-07-21 00:07:09 +00:00
|
|
|
name: string,
|
2023-08-10 16:43:33 +00:00
|
|
|
memberServiceIds: Array<ServiceIdString>,
|
2022-07-21 00:07:09 +00:00
|
|
|
storageServiceDistributionListRecord?: StoryDistributionWithMembersType,
|
|
|
|
shouldSave = true
|
2022-12-09 17:35:34 +00:00
|
|
|
): ThunkAction<
|
2023-08-10 16:43:33 +00:00
|
|
|
Promise<StoryDistributionIdString>,
|
2022-12-09 17:35:34 +00:00
|
|
|
RootStateType,
|
|
|
|
string,
|
|
|
|
CreateListActionType
|
|
|
|
> {
|
2022-07-21 00:07:09 +00:00
|
|
|
return async dispatch => {
|
|
|
|
const storyDistribution: StoryDistributionWithMembersType = {
|
|
|
|
allowsReplies: true,
|
2023-08-10 16:43:33 +00:00
|
|
|
id: generateStoryDistributionId(),
|
2022-07-21 00:07:09 +00:00
|
|
|
isBlockList: false,
|
2023-08-10 16:43:33 +00:00
|
|
|
members: memberServiceIds,
|
2022-07-21 00:07:09 +00:00
|
|
|
name,
|
|
|
|
senderKeyInfo: undefined,
|
|
|
|
storageNeedsSync: true,
|
|
|
|
...(storageServiceDistributionListRecord || {}),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (shouldSave) {
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.createNewStoryDistribution(storyDistribution);
|
2022-07-21 00:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (storyDistribution.storageNeedsSync) {
|
2024-09-30 22:23:32 +00:00
|
|
|
storageServiceUploadJob({ reason: 'createDistributionList' });
|
2022-07-21 00:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: CREATE_LIST,
|
|
|
|
payload: {
|
|
|
|
allowsReplies: Boolean(storyDistribution.allowsReplies),
|
|
|
|
deletedAtTimestamp: storyDistribution.deletedAtTimestamp,
|
|
|
|
id: storyDistribution.id,
|
|
|
|
isBlockList: Boolean(storyDistribution.isBlockList),
|
2023-08-10 16:43:33 +00:00
|
|
|
memberServiceIds,
|
2022-07-21 00:07:09 +00:00
|
|
|
name: storyDistribution.name,
|
|
|
|
},
|
|
|
|
});
|
2022-12-09 17:35:34 +00:00
|
|
|
|
|
|
|
return storyDistribution.id;
|
2022-07-21 00:07:09 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteDistributionList(
|
|
|
|
listId: string
|
|
|
|
): ThunkAction<void, RootStateType, unknown, DeleteListActionType> {
|
2022-10-13 16:14:50 +00:00
|
|
|
return async (dispatch, getState) => {
|
2022-07-21 00:07:09 +00:00
|
|
|
const deletedAtTimestamp = Date.now();
|
|
|
|
|
2024-07-24 00:31:40 +00:00
|
|
|
const storyDistribution =
|
|
|
|
await DataReader.getStoryDistributionWithMembers(listId);
|
2022-07-21 00:07:09 +00:00
|
|
|
|
|
|
|
if (!storyDistribution) {
|
|
|
|
log.warn('No story distribution found for id', listId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.modifyStoryDistributionWithMembers(
|
2022-07-29 00:13:22 +00:00
|
|
|
{
|
|
|
|
...storyDistribution,
|
2022-08-10 16:31:15 +00:00
|
|
|
deletedAtTimestamp,
|
2022-07-29 00:13:22 +00:00
|
|
|
name: '',
|
|
|
|
storageNeedsSync: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
toAdd: [],
|
|
|
|
toRemove: storyDistribution.members,
|
|
|
|
}
|
|
|
|
);
|
2022-07-21 00:07:09 +00:00
|
|
|
|
2022-10-13 16:14:50 +00:00
|
|
|
const { stories } = getState().stories;
|
|
|
|
const storiesToDelete = stories.filter(
|
|
|
|
story => story.storyDistributionListId === listId
|
|
|
|
);
|
|
|
|
await Promise.all(
|
|
|
|
storiesToDelete.map(story => deleteStoryForEveryone(stories, story))
|
|
|
|
);
|
|
|
|
|
2022-07-21 00:07:09 +00:00
|
|
|
log.info(
|
|
|
|
'storyDistributionLists.deleteDistributionList: list deleted',
|
|
|
|
listId
|
|
|
|
);
|
|
|
|
|
2024-09-30 22:23:32 +00:00
|
|
|
storageServiceUploadJob({ reason: 'deleteDistributionList' });
|
2022-07-21 00:07:09 +00:00
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: DELETE_LIST,
|
|
|
|
payload: {
|
|
|
|
listId,
|
|
|
|
deletedAtTimestamp,
|
|
|
|
},
|
|
|
|
});
|
2022-07-01 00:52:03 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function modifyDistributionList(
|
2022-07-21 00:07:09 +00:00
|
|
|
distributionList: ModifyDistributionListType
|
2022-07-01 00:52:03 +00:00
|
|
|
): ModifyListActionType {
|
|
|
|
return {
|
|
|
|
type: MODIFY_LIST,
|
|
|
|
payload: distributionList,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-21 00:07:09 +00:00
|
|
|
function hideMyStoriesFrom(
|
2023-08-10 16:43:33 +00:00
|
|
|
memberServiceIds: Array<ServiceIdString>
|
2022-07-21 00:07:09 +00:00
|
|
|
): ThunkAction<void, RootStateType, null, HideMyStoriesFromActionType> {
|
|
|
|
return async dispatch => {
|
2024-07-24 00:31:40 +00:00
|
|
|
const myStories =
|
|
|
|
await DataReader.getStoryDistributionWithMembers(MY_STORY_ID);
|
2022-07-21 00:07:09 +00:00
|
|
|
|
|
|
|
if (!myStories) {
|
|
|
|
log.error(
|
|
|
|
'storyDistributionLists.hideMyStoriesFrom: Could not find My Stories!'
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
const toAdd = new Set<ServiceIdString>(memberServiceIds);
|
2022-07-21 00:07:09 +00:00
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.modifyStoryDistributionWithMembers(
|
2022-07-21 00:07:09 +00:00
|
|
|
{
|
|
|
|
...myStories,
|
|
|
|
isBlockList: true,
|
|
|
|
storageNeedsSync: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
toAdd: Array.from(toAdd),
|
2023-08-16 20:54:39 +00:00
|
|
|
toRemove: myStories.members.filter(serviceId => !toAdd.has(serviceId)),
|
2022-07-21 00:07:09 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2024-09-30 22:23:32 +00:00
|
|
|
storageServiceUploadJob({
|
|
|
|
reason: 'storyDistributionLists/hideMyStoriesFrom',
|
|
|
|
});
|
2022-07-21 00:07:09 +00:00
|
|
|
|
2022-12-21 18:41:48 +00:00
|
|
|
await window.storage.put('hasSetMyStoriesPrivacy', true);
|
2022-08-23 17:24:55 +00:00
|
|
|
|
2022-07-21 00:07:09 +00:00
|
|
|
dispatch({
|
|
|
|
type: HIDE_MY_STORIES_FROM,
|
2023-08-10 16:43:33 +00:00
|
|
|
payload: memberServiceIds,
|
2022-07-21 00:07:09 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-11-11 04:10:30 +00:00
|
|
|
function removeMembersFromDistributionList(
|
2022-07-21 00:07:09 +00:00
|
|
|
listId: string,
|
2023-08-10 16:43:33 +00:00
|
|
|
memberServiceIds: Array<ServiceIdString>
|
2022-11-11 04:10:30 +00:00
|
|
|
): ThunkAction<void, RootStateType, null, ModifyListActionType> {
|
2022-07-21 00:07:09 +00:00
|
|
|
return async dispatch => {
|
2023-08-10 16:43:33 +00:00
|
|
|
if (!memberServiceIds.length) {
|
2022-07-21 00:07:09 +00:00
|
|
|
log.warn(
|
2023-08-16 20:54:39 +00:00
|
|
|
'storyDistributionLists.removeMembersFromDistributionList cannot remove a member without serviceId',
|
2022-07-21 00:07:09 +00:00
|
|
|
listId
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-07-24 00:31:40 +00:00
|
|
|
const storyDistribution =
|
|
|
|
await DataReader.getStoryDistributionWithMembers(listId);
|
2022-07-21 00:07:09 +00:00
|
|
|
|
|
|
|
if (!storyDistribution) {
|
|
|
|
log.warn(
|
2022-11-11 04:10:30 +00:00
|
|
|
'storyDistributionLists.removeMembersFromDistributionList: No story found for id',
|
2022-07-21 00:07:09 +00:00
|
|
|
listId
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
let toAdd: Array<ServiceIdString> = [];
|
|
|
|
let toRemove: Array<ServiceIdString> = memberServiceIds;
|
2022-11-11 04:10:30 +00:00
|
|
|
let { isBlockList } = storyDistribution;
|
|
|
|
|
|
|
|
// My Story is set to 'All Signal Connections' or is already an exclude list
|
|
|
|
if (
|
|
|
|
listId === MY_STORY_ID &&
|
|
|
|
(storyDistribution.members.length === 0 || isBlockList)
|
|
|
|
) {
|
|
|
|
isBlockList = true;
|
2023-08-10 16:43:33 +00:00
|
|
|
toAdd = memberServiceIds;
|
2022-11-11 04:10:30 +00:00
|
|
|
toRemove = [];
|
|
|
|
|
|
|
|
// The user has now configured My Stories
|
2022-12-21 18:41:48 +00:00
|
|
|
await window.storage.put('hasSetMyStoriesPrivacy', true);
|
2022-11-11 04:10:30 +00:00
|
|
|
}
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.modifyStoryDistributionWithMembers(
|
2022-07-21 00:07:09 +00:00
|
|
|
{
|
|
|
|
...storyDistribution,
|
2022-11-11 04:10:30 +00:00
|
|
|
isBlockList,
|
2022-07-21 00:07:09 +00:00
|
|
|
storageNeedsSync: true,
|
|
|
|
},
|
|
|
|
{
|
2022-11-11 04:10:30 +00:00
|
|
|
toAdd,
|
|
|
|
toRemove,
|
2022-07-21 00:07:09 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
log.info(
|
2022-11-11 04:10:30 +00:00
|
|
|
'storyDistributionLists.removeMembersFromDistributionList: removed',
|
2022-07-21 00:07:09 +00:00
|
|
|
{
|
|
|
|
listId,
|
2023-08-10 16:43:33 +00:00
|
|
|
memberServiceIds,
|
2022-07-21 00:07:09 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2024-09-30 22:23:32 +00:00
|
|
|
storageServiceUploadJob({ reason: 'removeMembersFromDistributionList' });
|
2022-07-21 00:07:09 +00:00
|
|
|
|
|
|
|
dispatch({
|
2022-11-11 04:10:30 +00:00
|
|
|
type: MODIFY_LIST,
|
2022-07-21 00:07:09 +00:00
|
|
|
payload: {
|
2022-11-11 04:10:30 +00:00
|
|
|
...omit(storyDistribution, ['members']),
|
|
|
|
isBlockList,
|
|
|
|
storageNeedsSync: true,
|
|
|
|
membersToAdd: toAdd,
|
|
|
|
membersToRemove: toRemove,
|
2022-07-21 00:07:09 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function setMyStoriesToAllSignalConnections(): ThunkAction<
|
|
|
|
void,
|
|
|
|
RootStateType,
|
|
|
|
null,
|
|
|
|
ResetMyStoriesActionType
|
|
|
|
> {
|
|
|
|
return async dispatch => {
|
2024-07-24 00:31:40 +00:00
|
|
|
const myStories =
|
|
|
|
await DataReader.getStoryDistributionWithMembers(MY_STORY_ID);
|
2022-07-21 00:07:09 +00:00
|
|
|
|
|
|
|
if (!myStories) {
|
|
|
|
log.error(
|
|
|
|
'storyDistributionLists.setMyStoriesToAllSignalConnections: Could not find My Stories!'
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (myStories.isBlockList || myStories.members.length > 0) {
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.modifyStoryDistributionWithMembers(
|
2022-07-21 00:07:09 +00:00
|
|
|
{
|
|
|
|
...myStories,
|
|
|
|
isBlockList: true,
|
|
|
|
storageNeedsSync: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
toAdd: [],
|
|
|
|
toRemove: myStories.members,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2024-09-30 22:23:32 +00:00
|
|
|
storageServiceUploadJob({ reason: 'setMyStoriesToAllSignalConnections' });
|
2022-07-21 00:07:09 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 18:41:48 +00:00
|
|
|
await window.storage.put('hasSetMyStoriesPrivacy', true);
|
2022-08-23 17:24:55 +00:00
|
|
|
|
2022-07-21 00:07:09 +00:00
|
|
|
dispatch({
|
|
|
|
type: RESET_MY_STORIES,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateStoryViewers(
|
|
|
|
listId: string,
|
2023-08-10 16:43:33 +00:00
|
|
|
memberServiceIds: Array<ServiceIdString>
|
2022-07-21 00:07:09 +00:00
|
|
|
): ThunkAction<void, RootStateType, null, ViewersChangedActionType> {
|
|
|
|
return async dispatch => {
|
2024-07-24 00:31:40 +00:00
|
|
|
const storyDistribution =
|
|
|
|
await DataReader.getStoryDistributionWithMembers(listId);
|
2022-07-21 00:07:09 +00:00
|
|
|
|
|
|
|
if (!storyDistribution) {
|
|
|
|
log.warn(
|
|
|
|
'storyDistributionLists.updateStoryViewers: No story found for id',
|
|
|
|
listId
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
const existingServiceIds = new Set<ServiceIdString>(
|
|
|
|
storyDistribution.members
|
|
|
|
);
|
2023-08-10 16:43:33 +00:00
|
|
|
const toAdd: Array<ServiceIdString> = [];
|
2022-07-21 00:07:09 +00:00
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
memberServiceIds.forEach(serviceId => {
|
|
|
|
if (!existingServiceIds.has(serviceId)) {
|
|
|
|
toAdd.push(serviceId);
|
2022-07-21 00:07:09 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
const updatedServiceIds = new Set<ServiceIdString>(memberServiceIds);
|
2023-08-10 16:43:33 +00:00
|
|
|
const toRemove: Array<ServiceIdString> = [];
|
2022-07-21 00:07:09 +00:00
|
|
|
|
2023-08-16 20:54:39 +00:00
|
|
|
storyDistribution.members.forEach(serviceId => {
|
|
|
|
if (!updatedServiceIds.has(serviceId)) {
|
|
|
|
toRemove.push(serviceId);
|
2022-07-21 00:07:09 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
await DataWriter.modifyStoryDistributionWithMembers(
|
2022-07-21 00:07:09 +00:00
|
|
|
{
|
|
|
|
...storyDistribution,
|
|
|
|
isBlockList: false,
|
|
|
|
storageNeedsSync: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
toAdd,
|
|
|
|
toRemove,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2024-09-30 22:23:32 +00:00
|
|
|
storageServiceUploadJob({ reason: 'updateStoryViewers' });
|
2022-07-21 00:07:09 +00:00
|
|
|
|
2022-11-10 15:51:39 +00:00
|
|
|
if (listId === MY_STORY_ID) {
|
2022-12-21 18:41:48 +00:00
|
|
|
await window.storage.put('hasSetMyStoriesPrivacy', true);
|
2022-08-23 17:24:55 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 00:07:09 +00:00
|
|
|
dispatch({
|
|
|
|
type: VIEWERS_CHANGED,
|
|
|
|
payload: {
|
|
|
|
listId,
|
2023-08-10 16:43:33 +00:00
|
|
|
memberServiceIds,
|
2022-07-21 00:07:09 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-07-21 20:01:12 +00:00
|
|
|
function removeMemberFromAllDistributionLists(
|
2023-08-10 16:43:33 +00:00
|
|
|
member: ServiceIdString
|
2023-07-21 20:01:12 +00:00
|
|
|
): ThunkAction<void, RootStateType, null, ModifyListActionType> {
|
|
|
|
return async dispatch => {
|
|
|
|
const logId = `removeMemberFromAllDistributionLists(${member})`;
|
2024-07-22 18:16:33 +00:00
|
|
|
const lists = await DataReader.getAllStoryDistributionsWithMembers();
|
2023-07-21 20:01:12 +00:00
|
|
|
|
|
|
|
const listsWithMember = lists.filter(({ members }) =>
|
|
|
|
members.includes(member)
|
|
|
|
);
|
|
|
|
log.info(
|
|
|
|
`${logId}: removing ${member} from ${listsWithMember.length} lists`
|
|
|
|
);
|
|
|
|
|
|
|
|
for (const { id } of listsWithMember) {
|
|
|
|
dispatch(removeMembersFromDistributionList(id, [member]));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-01 00:52:03 +00:00
|
|
|
export const actions = {
|
2022-07-21 00:07:09 +00:00
|
|
|
allowsRepliesChanged,
|
2022-07-01 00:52:03 +00:00
|
|
|
createDistributionList,
|
2022-07-21 00:07:09 +00:00
|
|
|
deleteDistributionList,
|
|
|
|
hideMyStoriesFrom,
|
2022-07-01 00:52:03 +00:00
|
|
|
modifyDistributionList,
|
2022-11-11 04:10:30 +00:00
|
|
|
removeMembersFromDistributionList,
|
2023-07-21 20:01:12 +00:00
|
|
|
removeMemberFromAllDistributionLists,
|
2022-07-21 00:07:09 +00:00
|
|
|
setMyStoriesToAllSignalConnections,
|
|
|
|
updateStoryViewers,
|
2022-07-01 00:52:03 +00:00
|
|
|
};
|
|
|
|
|
2022-12-09 17:35:34 +00:00
|
|
|
export const useStoryDistributionListsActions =
|
|
|
|
(): BoundActionCreatorsMapObject<typeof actions> => useBoundActions(actions);
|
2022-07-01 00:52:03 +00:00
|
|
|
|
|
|
|
// Reducer
|
|
|
|
|
|
|
|
export function getEmptyState(): StoryDistributionListStateType {
|
|
|
|
return {
|
|
|
|
distributionLists: [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-21 00:07:09 +00:00
|
|
|
function replaceDistributionListData(
|
2023-01-13 20:07:26 +00:00
|
|
|
distributionLists: ReadonlyArray<StoryDistributionListDataType>,
|
2022-07-21 00:07:09 +00:00
|
|
|
listId: string,
|
|
|
|
getNextDistributionListData: (
|
|
|
|
list: StoryDistributionListDataType
|
|
|
|
) => Partial<StoryDistributionListDataType>
|
|
|
|
): Array<StoryDistributionListDataType> | undefined {
|
|
|
|
const listIndex = distributionLists.findIndex(list => list.id === listId);
|
|
|
|
|
|
|
|
if (listIndex < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return replaceIndex(distributionLists, listIndex, {
|
|
|
|
...distributionLists[listIndex],
|
|
|
|
...getNextDistributionListData(distributionLists[listIndex]),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-01 00:52:03 +00:00
|
|
|
export function reducer(
|
|
|
|
state: Readonly<StoryDistributionListStateType> = getEmptyState(),
|
|
|
|
action: Readonly<StoryDistributionListsActionType>
|
|
|
|
): StoryDistributionListStateType {
|
|
|
|
if (action.type === MODIFY_LIST) {
|
|
|
|
const { payload } = action;
|
|
|
|
|
2022-07-21 00:07:09 +00:00
|
|
|
const { membersToAdd, membersToRemove, ...distributionListDetails } =
|
|
|
|
payload;
|
|
|
|
|
|
|
|
const listIndex = state.distributionLists.findIndex(
|
|
|
|
list => list.id === distributionListDetails.id
|
|
|
|
);
|
|
|
|
if (listIndex >= 0) {
|
|
|
|
const existingDistributionList = state.distributionLists[listIndex];
|
2023-08-10 16:43:33 +00:00
|
|
|
const memberServiceIds = new Set<ServiceIdString>(
|
|
|
|
existingDistributionList.memberServiceIds
|
2022-11-11 04:10:30 +00:00
|
|
|
);
|
2023-08-16 20:54:39 +00:00
|
|
|
membersToAdd.forEach(serviceId => memberServiceIds.add(serviceId));
|
|
|
|
membersToRemove.forEach(serviceId => memberServiceIds.delete(serviceId));
|
2022-07-21 00:07:09 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
distributionLists: replaceIndex(state.distributionLists, listIndex, {
|
|
|
|
...existingDistributionList,
|
|
|
|
...distributionListDetails,
|
2023-08-10 16:43:33 +00:00
|
|
|
memberServiceIds: Array.from(memberServiceIds),
|
2022-07-21 00:07:09 +00:00
|
|
|
}),
|
|
|
|
};
|
2022-07-01 00:52:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2022-07-21 00:07:09 +00:00
|
|
|
distributionLists: [
|
|
|
|
...state.distributionLists,
|
|
|
|
{
|
|
|
|
...distributionListDetails,
|
2023-08-10 16:43:33 +00:00
|
|
|
memberServiceIds: membersToAdd,
|
2022-07-21 00:07:09 +00:00
|
|
|
},
|
|
|
|
],
|
2022-07-01 00:52:03 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === CREATE_LIST) {
|
|
|
|
return {
|
|
|
|
distributionLists: [...state.distributionLists, action.payload],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-21 00:07:09 +00:00
|
|
|
if (action.type === DELETE_LIST) {
|
|
|
|
const distributionLists = replaceDistributionListData(
|
|
|
|
state.distributionLists,
|
|
|
|
action.payload.listId,
|
|
|
|
() => ({
|
|
|
|
deletedAtTimestamp: action.payload.deletedAtTimestamp,
|
2023-08-10 16:43:33 +00:00
|
|
|
memberServiceIds: [],
|
2022-07-21 00:07:09 +00:00
|
|
|
name: '',
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
return distributionLists ? { distributionLists } : state;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === HIDE_MY_STORIES_FROM) {
|
|
|
|
const distributionLists = replaceDistributionListData(
|
|
|
|
state.distributionLists,
|
2022-11-10 15:51:39 +00:00
|
|
|
MY_STORY_ID,
|
2022-07-21 00:07:09 +00:00
|
|
|
() => ({
|
|
|
|
isBlockList: true,
|
2023-08-10 16:43:33 +00:00
|
|
|
memberServiceIds: action.payload,
|
2022-07-21 00:07:09 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
return distributionLists ? { distributionLists } : state;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === ALLOW_REPLIES_CHANGED) {
|
|
|
|
const distributionLists = replaceDistributionListData(
|
|
|
|
state.distributionLists,
|
|
|
|
action.payload.listId,
|
|
|
|
() => ({
|
|
|
|
allowsReplies: action.payload.allowsReplies,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
return distributionLists ? { distributionLists } : state;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === VIEWERS_CHANGED) {
|
|
|
|
const distributionLists = replaceDistributionListData(
|
|
|
|
state.distributionLists,
|
|
|
|
action.payload.listId,
|
|
|
|
() => ({
|
|
|
|
isBlockList: false,
|
2023-08-10 16:43:33 +00:00
|
|
|
memberServiceIds: Array.from(new Set(action.payload.memberServiceIds)),
|
2022-07-21 00:07:09 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
return distributionLists ? { distributionLists } : state;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === RESET_MY_STORIES) {
|
|
|
|
const distributionLists = replaceDistributionListData(
|
|
|
|
state.distributionLists,
|
2022-11-10 15:51:39 +00:00
|
|
|
MY_STORY_ID,
|
2022-07-21 00:07:09 +00:00
|
|
|
() => ({
|
2022-08-23 17:24:55 +00:00
|
|
|
isBlockList: true,
|
2023-08-10 16:43:33 +00:00
|
|
|
memberServiceIds: [],
|
2022-07-21 00:07:09 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
return distributionLists ? { distributionLists } : state;
|
|
|
|
}
|
|
|
|
|
2022-07-01 00:52:03 +00:00
|
|
|
return state;
|
|
|
|
}
|