2021-02-25 16:41:44 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
2022-04-04 19:15:29 +00:00
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
2021-02-01 17:18:55 +00:00
|
|
|
import orderBy from 'lodash/orderBy';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
2022-04-07 16:53:07 +00:00
|
|
|
import { setPermissions as setPermissionsAction } from '../../actions/permissions';
|
|
|
|
import { IPermission, TPermissionModel } from '../../definitions';
|
2022-06-06 14:17:51 +00:00
|
|
|
import log from './helpers/log';
|
2022-04-07 16:53:07 +00:00
|
|
|
import { store as reduxStore } from '../store/auxStore';
|
|
|
|
import database from '../database';
|
2022-04-04 19:15:29 +00:00
|
|
|
import sdk from '../services/sdk';
|
2022-04-07 16:53:07 +00:00
|
|
|
import protectedFunction from './helpers/protectedFunction';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { compareServerVersion } from './helpers';
|
2021-02-25 16:41:44 +00:00
|
|
|
|
2022-02-17 13:06:31 +00:00
|
|
|
export const SUPPORTED_PERMISSIONS = [
|
2021-02-25 16:41:44 +00:00
|
|
|
'add-user-to-any-c-room',
|
|
|
|
'add-user-to-any-p-room',
|
|
|
|
'add-user-to-joined-room',
|
[NEW] Add/Create/Remove channel on a team (#3090)
* Added Create Team
* Added actionTypes, actions, ENG strings for Teams and updated NewMessageView
* Added createTeam sagas, createTeam reducer, new Team string and update CreateChannelView
* Remove unnecessary actionTypes, reducers and sagas, e2e tests and navigation to team view
* Minor tweaks
* Show TeamChannelsView only if joined the team
* Minor tweak
* Added AddChannelTeamView
* Added permissions, translations strings for teams, deleteTeamRoom and addTeamRooms, AddExistingChannelView, updated CreateChannelView, TeamChannelsView
* Refactor touch component and update removeRoom and deleteRoom methods
* Minor tweaks
* Minor tweaks for removing channels and addExistingChannelView
* Added missing events and fixed channels list
* Minor tweaks for refactored touch component
* Minor tweaks
* Remove unnecesary changes, update TeamChannelsView, AddExistingChannelView, AddChannelTeamView, createChannel, goRoom and Touchable
* Add screens to ModalStack, events, autoJoin, update createChannel, addRoomsToTeam and Touchable
* Minor tweak
* Update loadMessagesForRoom.js
* Updated schema, tag component, touch, AddChannelTeamView, AddExistingChannelView, ActionSheet Item
* Fix unnecessary changes
* Add i18n, update createChannel, AddExistingChannelTeamView, AddChannelTeamView, RightButton and TeamChannelsView
* Updated styles, added tag story
* Minor tweak
* Minor tweaks
* Auto-join tweak
* Minor tweaks
* Minor tweak on search
* One way to refactor :P
* Next level refactor :)
* Fix create group dm
* Refactor renderItem
* Minor bug fixes
* Fix stories
Co-authored-by: Diego Mello <diegolmello@gmail.com>
2021-05-19 21:14:42 +00:00
|
|
|
'add-team-channel',
|
2021-02-25 16:41:44 +00:00
|
|
|
'archive-room',
|
|
|
|
'auto-translate',
|
|
|
|
'create-invite-links',
|
2021-10-05 13:59:40 +00:00
|
|
|
'create-c',
|
|
|
|
'create-p',
|
|
|
|
'create-d',
|
|
|
|
'start-discussion',
|
2023-10-16 20:55:38 +00:00
|
|
|
'start-discussion-other-user',
|
[NEW] Convert/Move Channel to Team (#3164)
* Added Create Team
* Added actionTypes, actions, ENG strings for Teams and updated NewMessageView
* Added createTeam sagas, createTeam reducer, new Team string and update CreateChannelView
* Remove unnecessary actionTypes, reducers and sagas, e2e tests and navigation to team view
* Minor tweaks
* Show TeamChannelsView only if joined the team
* Minor tweak
* Added AddChannelTeamView
* Added permissions, translations strings for teams, deleteTeamRoom and addTeamRooms, AddExistingChannelView, updated CreateChannelView, TeamChannelsView
* Refactor touch component and update removeRoom and deleteRoom methods
* Minor tweaks
* Minor tweaks for removing channels and addExistingChannelView
* Added missing events and fixed channels list
* Minor tweaks for refactored touch component
* Added SelectListView and logic for leaving team
* Added addTeamMember and removeTeamMember
* Minor tweak
* Added deleteTeam function
* Minor tweak
* Minor tweaks
* Remove unnecesary changes, update TeamChannelsView, AddExistingChannelView, AddChannelTeamView, createChannel, goRoom and Touchable
* Remove unnecesary prop
* Add screens to ModalStack, events, autoJoin, update createChannel, addRoomsToTeam and Touchable
* Minor tweak
* Update loadMessagesForRoom.js
* Updated schema, tag component, touch, AddChannelTeamView, AddExistingChannelView, ActionSheet Item
* Fix unnecessary changes
* Add i18n, update createChannel, AddExistingChannelTeamView, AddChannelTeamView, RightButton and TeamChannelsView
* Updated styles, added tag story
* Minor tweak
* Minor tweaks
* Auto-join tweak
* Minor tweaks
* Minor tweak on search
* Minor refactor to ListItem, add SelectListView to ModalStack, update handleLeaveTeam
* Minor tweaks
* Update SelectListView
* Update handleLeaveTeam, remove unnecessary method, add story
* Minor tweak
* Minor visual tweaks
* Update SelectListView.js
* Update index.js
* Update RoomMembersView
* Updated SelectListView, RoomActionsView, leaveTeam method and string translations
* Update SelectListVIew
* Minor tweak
* Update SelectListView
* Minor tweak
* Minor tweaks
* Fix for List.Item subtitles being pushed down by title's flex
* Minor tweaks
* Update RoomActionsView
* Use showConfirmationAlert and showErrorAlert
* Remove addTeamMember, update removeTeamMember
* Update Alert
* Minor tweaks
* Minor tweaks
* Minor tweak
* Update showActionSheet on RoomMembersView
* Remove team main from query and move code around
* Fetch roles
* Update RoomMembersView and SelectListView
* Update rocketchat.js
* Updated leaveTeam and handleRemoveFromTeam
* Fix validation
* Remove unnecessary function
* Update RoomActionsView
* Update en.json
* updated deleteTeam function and permissions
* Added showConfirmationAlert
* Added string translations for teams
* Fix permission
* Added moveChannelToTeam and convertToTeam functionality
* Fix SelectListView RadioButton
* Fix moveToTeam
* Added searchBar to SelectListVIew
* Update RoomView , SelectListVIew and string translation for error
Co-authored-by: Diego Mello <diegolmello@gmail.com>
2021-06-02 13:44:19 +00:00
|
|
|
'create-team',
|
2021-02-25 16:41:44 +00:00
|
|
|
'delete-c',
|
|
|
|
'delete-message',
|
|
|
|
'delete-p',
|
[NEW] Delete Teams (#3123)
* Added Create Team
* Added actionTypes, actions, ENG strings for Teams and updated NewMessageView
* Added createTeam sagas, createTeam reducer, new Team string and update CreateChannelView
* Remove unnecessary actionTypes, reducers and sagas, e2e tests and navigation to team view
* Minor tweaks
* Show TeamChannelsView only if joined the team
* Minor tweak
* Added AddChannelTeamView
* Added permissions, translations strings for teams, deleteTeamRoom and addTeamRooms, AddExistingChannelView, updated CreateChannelView, TeamChannelsView
* Refactor touch component and update removeRoom and deleteRoom methods
* Minor tweaks
* Minor tweaks for removing channels and addExistingChannelView
* Added missing events and fixed channels list
* Minor tweaks for refactored touch component
* Added SelectListView and logic for leaving team
* Added addTeamMember and removeTeamMember
* Minor tweak
* Added deleteTeam function
* Minor tweak
* Minor tweaks
* Remove unnecesary changes, update TeamChannelsView, AddExistingChannelView, AddChannelTeamView, createChannel, goRoom and Touchable
* Remove unnecesary prop
* Add screens to ModalStack, events, autoJoin, update createChannel, addRoomsToTeam and Touchable
* Minor tweak
* Update loadMessagesForRoom.js
* Updated schema, tag component, touch, AddChannelTeamView, AddExistingChannelView, ActionSheet Item
* Fix unnecessary changes
* Add i18n, update createChannel, AddExistingChannelTeamView, AddChannelTeamView, RightButton and TeamChannelsView
* Updated styles, added tag story
* Minor tweak
* Minor tweaks
* Auto-join tweak
* Minor tweaks
* Minor tweak on search
* Minor refactor to ListItem, add SelectListView to ModalStack, update handleLeaveTeam
* Minor tweaks
* Update SelectListView
* Update handleLeaveTeam, remove unnecessary method, add story
* Minor tweak
* Minor visual tweaks
* Update SelectListView.js
* Update index.js
* Update RoomMembersView
* Updated SelectListView, RoomActionsView, leaveTeam method and string translations
* Update SelectListVIew
* Minor tweak
* Update SelectListView
* Minor tweak
* Minor tweaks
* Fix for List.Item subtitles being pushed down by title's flex
* Minor tweaks
* Update RoomActionsView
* Use showConfirmationAlert and showErrorAlert
* Remove addTeamMember, update removeTeamMember
* Update Alert
* Minor tweaks
* Minor tweaks
* Minor tweak
* Update showActionSheet on RoomMembersView
* Remove team main from query and move code around
* Fetch roles
* Update RoomMembersView and SelectListView
* Update rocketchat.js
* Updated leaveTeam and handleRemoveFromTeam
* Fix validation
* Remove unnecessary function
* Update RoomActionsView
* Update en.json
* updated deleteTeam function and permissions
* Added showConfirmationAlert
* Added string translations for teams
* Fix permission
* Minor tweaks
* Typo
Co-authored-by: Diego Mello <diegolmello@gmail.com>
2021-05-28 14:22:41 +00:00
|
|
|
'delete-team',
|
2021-02-25 16:41:44 +00:00
|
|
|
'edit-message',
|
|
|
|
'edit-room',
|
[NEW] Remove member from team (#3117)
* Added Create Team
* Added actionTypes, actions, ENG strings for Teams and updated NewMessageView
* Added createTeam sagas, createTeam reducer, new Team string and update CreateChannelView
* Remove unnecessary actionTypes, reducers and sagas, e2e tests and navigation to team view
* Minor tweaks
* Show TeamChannelsView only if joined the team
* Minor tweak
* Added AddChannelTeamView
* Added permissions, translations strings for teams, deleteTeamRoom and addTeamRooms, AddExistingChannelView, updated CreateChannelView, TeamChannelsView
* Refactor touch component and update removeRoom and deleteRoom methods
* Minor tweaks
* Minor tweaks for removing channels and addExistingChannelView
* Added missing events and fixed channels list
* Minor tweaks for refactored touch component
* Added SelectListView and logic for leaving team
* Added addTeamMember and removeTeamMember
* Minor tweak
* Minor tweak
* Minor tweaks
* Remove unnecesary changes, update TeamChannelsView, AddExistingChannelView, AddChannelTeamView, createChannel, goRoom and Touchable
* Remove unnecesary prop
* Add screens to ModalStack, events, autoJoin, update createChannel, addRoomsToTeam and Touchable
* Minor tweak
* Update loadMessagesForRoom.js
* Updated schema, tag component, touch, AddChannelTeamView, AddExistingChannelView, ActionSheet Item
* Fix unnecessary changes
* Add i18n, update createChannel, AddExistingChannelTeamView, AddChannelTeamView, RightButton and TeamChannelsView
* Updated styles, added tag story
* Minor tweak
* Minor tweaks
* Auto-join tweak
* Minor tweaks
* Minor tweak on search
* Minor refactor to ListItem, add SelectListView to ModalStack, update handleLeaveTeam
* Minor tweaks
* Update SelectListView
* Update handleLeaveTeam, remove unnecessary method, add story
* Minor tweak
* Minor visual tweaks
* Update SelectListView.js
* Update RoomMembersView
* Updated SelectListView, RoomActionsView, leaveTeam method and string translations
* Update SelectListVIew
* Minor tweak
* Update SelectListView
* Minor tweak
* Minor tweaks
* Fix for List.Item subtitles being pushed down by title's flex
* Minor tweaks
* Update RoomActionsView
* Use showConfirmationAlert and showErrorAlert
* Remove addTeamMember, update removeTeamMember
* Update Alert
* Minor tweaks
* Minor tweaks
* Minor tweak
* Update showActionSheet on RoomMembersView
* Remove team main from query and move code around
* Fetch roles
* Update RoomMembersView and SelectListView
* Updated leaveTeam and handleRemoveFromTeam
* Fix validation
* Remove unnecessary function
* Added confirmationAlert for missing permissions case
Co-authored-by: Diego Mello <diegolmello@gmail.com>
2021-05-26 21:01:06 +00:00
|
|
|
'edit-team-member',
|
[NEW] Add/Create/Remove channel on a team (#3090)
* Added Create Team
* Added actionTypes, actions, ENG strings for Teams and updated NewMessageView
* Added createTeam sagas, createTeam reducer, new Team string and update CreateChannelView
* Remove unnecessary actionTypes, reducers and sagas, e2e tests and navigation to team view
* Minor tweaks
* Show TeamChannelsView only if joined the team
* Minor tweak
* Added AddChannelTeamView
* Added permissions, translations strings for teams, deleteTeamRoom and addTeamRooms, AddExistingChannelView, updated CreateChannelView, TeamChannelsView
* Refactor touch component and update removeRoom and deleteRoom methods
* Minor tweaks
* Minor tweaks for removing channels and addExistingChannelView
* Added missing events and fixed channels list
* Minor tweaks for refactored touch component
* Minor tweaks
* Remove unnecesary changes, update TeamChannelsView, AddExistingChannelView, AddChannelTeamView, createChannel, goRoom and Touchable
* Add screens to ModalStack, events, autoJoin, update createChannel, addRoomsToTeam and Touchable
* Minor tweak
* Update loadMessagesForRoom.js
* Updated schema, tag component, touch, AddChannelTeamView, AddExistingChannelView, ActionSheet Item
* Fix unnecessary changes
* Add i18n, update createChannel, AddExistingChannelTeamView, AddChannelTeamView, RightButton and TeamChannelsView
* Updated styles, added tag story
* Minor tweak
* Minor tweaks
* Auto-join tweak
* Minor tweaks
* Minor tweak on search
* One way to refactor :P
* Next level refactor :)
* Fix create group dm
* Refactor renderItem
* Minor bug fixes
* Fix stories
Co-authored-by: Diego Mello <diegolmello@gmail.com>
2021-05-19 21:14:42 +00:00
|
|
|
'edit-team-channel',
|
2021-02-25 16:41:44 +00:00
|
|
|
'force-delete-message',
|
|
|
|
'mute-user',
|
|
|
|
'pin-message',
|
|
|
|
'post-readonly',
|
|
|
|
'remove-user',
|
[NEW] Add/Create/Remove channel on a team (#3090)
* Added Create Team
* Added actionTypes, actions, ENG strings for Teams and updated NewMessageView
* Added createTeam sagas, createTeam reducer, new Team string and update CreateChannelView
* Remove unnecessary actionTypes, reducers and sagas, e2e tests and navigation to team view
* Minor tweaks
* Show TeamChannelsView only if joined the team
* Minor tweak
* Added AddChannelTeamView
* Added permissions, translations strings for teams, deleteTeamRoom and addTeamRooms, AddExistingChannelView, updated CreateChannelView, TeamChannelsView
* Refactor touch component and update removeRoom and deleteRoom methods
* Minor tweaks
* Minor tweaks for removing channels and addExistingChannelView
* Added missing events and fixed channels list
* Minor tweaks for refactored touch component
* Minor tweaks
* Remove unnecesary changes, update TeamChannelsView, AddExistingChannelView, AddChannelTeamView, createChannel, goRoom and Touchable
* Add screens to ModalStack, events, autoJoin, update createChannel, addRoomsToTeam and Touchable
* Minor tweak
* Update loadMessagesForRoom.js
* Updated schema, tag component, touch, AddChannelTeamView, AddExistingChannelView, ActionSheet Item
* Fix unnecessary changes
* Add i18n, update createChannel, AddExistingChannelTeamView, AddChannelTeamView, RightButton and TeamChannelsView
* Updated styles, added tag story
* Minor tweak
* Minor tweaks
* Auto-join tweak
* Minor tweaks
* Minor tweak on search
* One way to refactor :P
* Next level refactor :)
* Fix create group dm
* Refactor renderItem
* Minor bug fixes
* Fix stories
Co-authored-by: Diego Mello <diegolmello@gmail.com>
2021-05-19 21:14:42 +00:00
|
|
|
'remove-team-channel',
|
2021-02-25 16:41:44 +00:00
|
|
|
'set-leader',
|
|
|
|
'set-moderator',
|
|
|
|
'set-owner',
|
|
|
|
'set-react-when-readonly',
|
|
|
|
'set-readonly',
|
|
|
|
'toggle-room-e2e-encryption',
|
|
|
|
'transfer-livechat-guest',
|
|
|
|
'unarchive-room',
|
|
|
|
'view-broadcast-member-list',
|
|
|
|
'view-privileged-setting',
|
|
|
|
'view-room-administration',
|
|
|
|
'view-statistics',
|
[NEW] Add/Create/Remove channel on a team (#3090)
* Added Create Team
* Added actionTypes, actions, ENG strings for Teams and updated NewMessageView
* Added createTeam sagas, createTeam reducer, new Team string and update CreateChannelView
* Remove unnecessary actionTypes, reducers and sagas, e2e tests and navigation to team view
* Minor tweaks
* Show TeamChannelsView only if joined the team
* Minor tweak
* Added AddChannelTeamView
* Added permissions, translations strings for teams, deleteTeamRoom and addTeamRooms, AddExistingChannelView, updated CreateChannelView, TeamChannelsView
* Refactor touch component and update removeRoom and deleteRoom methods
* Minor tweaks
* Minor tweaks for removing channels and addExistingChannelView
* Added missing events and fixed channels list
* Minor tweaks for refactored touch component
* Minor tweaks
* Remove unnecesary changes, update TeamChannelsView, AddExistingChannelView, AddChannelTeamView, createChannel, goRoom and Touchable
* Add screens to ModalStack, events, autoJoin, update createChannel, addRoomsToTeam and Touchable
* Minor tweak
* Update loadMessagesForRoom.js
* Updated schema, tag component, touch, AddChannelTeamView, AddExistingChannelView, ActionSheet Item
* Fix unnecessary changes
* Add i18n, update createChannel, AddExistingChannelTeamView, AddChannelTeamView, RightButton and TeamChannelsView
* Updated styles, added tag story
* Minor tweak
* Minor tweaks
* Auto-join tweak
* Minor tweaks
* Minor tweak on search
* One way to refactor :P
* Next level refactor :)
* Fix create group dm
* Refactor renderItem
* Minor bug fixes
* Fix stories
Co-authored-by: Diego Mello <diegolmello@gmail.com>
2021-05-19 21:14:42 +00:00
|
|
|
'view-user-administration',
|
|
|
|
'view-all-teams',
|
2021-07-09 18:46:24 +00:00
|
|
|
'view-all-team-channels',
|
2021-08-23 13:42:09 +00:00
|
|
|
'convert-team',
|
|
|
|
'edit-omnichannel-contact',
|
2021-09-22 17:29:26 +00:00
|
|
|
'edit-livechat-room-customfields',
|
2021-12-13 16:27:01 +00:00
|
|
|
'view-canned-responses',
|
2022-11-11 17:28:41 +00:00
|
|
|
'mobile-upload-file',
|
2023-08-21 19:12:34 +00:00
|
|
|
'delete-own-message',
|
|
|
|
'call-management'
|
2022-02-17 13:06:31 +00:00
|
|
|
] as const;
|
2021-02-25 16:41:44 +00:00
|
|
|
|
2022-02-17 13:06:31 +00:00
|
|
|
export async function setPermissions(): Promise<void> {
|
2021-02-25 16:41:44 +00:00
|
|
|
const db = database.active;
|
2022-02-01 13:39:09 +00:00
|
|
|
const permissionsCollection = db.get('permissions');
|
2022-02-17 13:06:31 +00:00
|
|
|
const allPermissions = await permissionsCollection
|
|
|
|
.query(Q.where('id', Q.oneOf(SUPPORTED_PERMISSIONS as unknown as string[])))
|
|
|
|
.fetch();
|
2021-02-25 16:41:44 +00:00
|
|
|
const parsed = allPermissions.reduce((acc, item) => ({ ...acc, [item.id]: item.roles }), {});
|
|
|
|
|
|
|
|
reduxStore.dispatch(setPermissionsAction(parsed));
|
|
|
|
}
|
2019-04-26 20:54:58 +00:00
|
|
|
|
2022-02-17 13:06:31 +00:00
|
|
|
const getUpdatedSince = (allRecords: TPermissionModel[]) => {
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
|
|
|
if (!allRecords.length) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
const ordered = orderBy(
|
|
|
|
allRecords.filter(item => item._updatedAt !== null),
|
|
|
|
['_updatedAt'],
|
|
|
|
['desc']
|
|
|
|
);
|
2022-02-17 13:06:31 +00:00
|
|
|
return new Date(ordered[0]._updatedAt).toISOString();
|
2019-09-16 20:26:32 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
return null;
|
2019-04-26 20:54:58 +00:00
|
|
|
};
|
|
|
|
|
2022-02-17 13:06:31 +00:00
|
|
|
const updatePermissions = async ({
|
|
|
|
update = [],
|
|
|
|
remove = [],
|
|
|
|
allRecords
|
|
|
|
}: {
|
|
|
|
update?: IPermission[];
|
|
|
|
remove?: IPermission[];
|
|
|
|
allRecords: TPermissionModel[];
|
|
|
|
}) => {
|
2019-09-16 20:26:32 +00:00
|
|
|
if (!((update && update.length) || (remove && remove.length))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const db = database.active;
|
2021-02-26 16:25:51 +00:00
|
|
|
const permissionsCollection = db.get('permissions');
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2022-02-17 13:06:31 +00:00
|
|
|
const batch: TPermissionModel[] = [];
|
|
|
|
|
|
|
|
// Delete
|
|
|
|
if (remove?.length) {
|
|
|
|
const filteredPermissionsToDelete = allRecords.filter(i1 => remove.find(i2 => i1.id === i2._id));
|
|
|
|
const permissionsToDelete = filteredPermissionsToDelete.map(permission => permission.prepareDestroyPermanently());
|
|
|
|
batch.push(...permissionsToDelete);
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
|
|
|
|
// Create or update
|
2022-02-17 13:06:31 +00:00
|
|
|
if (update?.length) {
|
|
|
|
const filteredPermissionsToCreate = update.filter(i1 => !allRecords.find(i2 => i1._id === i2.id));
|
|
|
|
const filteredPermissionsToUpdate = allRecords.filter(i1 => update.find(i2 => i1.id === i2._id));
|
|
|
|
const permissionsToCreate = filteredPermissionsToCreate.map(permission =>
|
2021-09-13 20:41:05 +00:00
|
|
|
permissionsCollection.prepareCreate(
|
2022-02-17 13:06:31 +00:00
|
|
|
protectedFunction((p: TPermissionModel) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
p._raw = sanitizedRaw({ id: permission._id }, permissionsCollection.schema);
|
|
|
|
Object.assign(p, permission);
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
2022-02-17 13:06:31 +00:00
|
|
|
const permissionsToUpdate = filteredPermissionsToUpdate.map(permission => {
|
2019-09-16 20:26:32 +00:00
|
|
|
const newPermission = update.find(p => p._id === permission.id);
|
2021-09-13 20:41:05 +00:00
|
|
|
return permission.prepareUpdate(
|
2022-02-17 13:06:31 +00:00
|
|
|
protectedFunction((p: TPermissionModel) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
Object.assign(p, newPermission);
|
|
|
|
})
|
|
|
|
);
|
2019-04-26 20:54:58 +00:00
|
|
|
});
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2022-02-17 13:06:31 +00:00
|
|
|
batch.push(...permissionsToCreate, ...permissionsToUpdate);
|
2019-09-16 20:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-02-17 13:06:31 +00:00
|
|
|
await db.write(async () => {
|
2019-09-16 20:26:32 +00:00
|
|
|
await db.batch(...batch);
|
|
|
|
});
|
2021-02-25 16:41:44 +00:00
|
|
|
return true;
|
2019-09-16 20:26:32 +00:00
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
2019-04-26 20:54:58 +00:00
|
|
|
};
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2022-02-17 13:06:31 +00:00
|
|
|
export function getPermissions(): Promise<void> {
|
2021-09-13 20:41:05 +00:00
|
|
|
return new Promise(async resolve => {
|
2019-06-17 13:57:07 +00:00
|
|
|
try {
|
2022-02-17 13:06:31 +00:00
|
|
|
const serverVersion: string | null = reduxStore.getState().server.version;
|
2019-09-16 20:26:32 +00:00
|
|
|
const db = database.active;
|
2021-02-26 16:25:51 +00:00
|
|
|
const permissionsCollection = db.get('permissions');
|
2019-09-16 20:26:32 +00:00
|
|
|
const allRecords = await permissionsCollection.query().fetch();
|
2022-04-04 19:15:29 +00:00
|
|
|
sdk.subscribe('stream-notify-logged', 'permissions-changed');
|
2019-06-17 13:57:07 +00:00
|
|
|
// if server version is lower than 0.73.0, fetches from old api
|
2022-02-17 13:06:31 +00:00
|
|
|
if (serverVersion && compareServerVersion(serverVersion, 'lowerThan', '0.73.0')) {
|
2019-06-17 13:57:07 +00:00
|
|
|
// RC 0.66.0
|
2022-02-17 13:06:31 +00:00
|
|
|
// @ts-ignore
|
|
|
|
const result: any = await sdk.get('permissions.list');
|
2019-06-17 13:57:07 +00:00
|
|
|
if (!result.success) {
|
|
|
|
return resolve();
|
|
|
|
}
|
2021-02-25 16:41:44 +00:00
|
|
|
const changePermissions = await updatePermissions({ update: result.permissions, allRecords });
|
|
|
|
if (changePermissions) {
|
|
|
|
setPermissions();
|
|
|
|
}
|
2021-02-23 18:36:20 +00:00
|
|
|
return resolve();
|
2022-02-17 13:06:31 +00:00
|
|
|
}
|
2018-06-18 13:30:36 +00:00
|
|
|
|
2022-02-17 13:06:31 +00:00
|
|
|
const params: { updatedSince?: string } = {};
|
|
|
|
const updatedSince = getUpdatedSince(allRecords);
|
|
|
|
if (updatedSince) {
|
|
|
|
params.updatedSince = updatedSince;
|
|
|
|
}
|
|
|
|
// RC 0.73.0
|
|
|
|
const result = await sdk.get('permissions.listAll', params);
|
2019-04-26 20:54:58 +00:00
|
|
|
|
2022-02-17 13:06:31 +00:00
|
|
|
if (!result.success) {
|
2021-02-23 18:36:20 +00:00
|
|
|
return resolve();
|
2019-06-17 13:57:07 +00:00
|
|
|
}
|
2022-02-17 13:06:31 +00:00
|
|
|
|
|
|
|
const changePermissions = await updatePermissions({ update: result.update, remove: result.remove, allRecords });
|
|
|
|
if (changePermissions) {
|
|
|
|
setPermissions();
|
|
|
|
}
|
|
|
|
return resolve();
|
2019-06-17 13:57:07 +00:00
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2019-06-17 13:57:07 +00:00
|
|
|
return resolve();
|
2019-04-26 20:54:58 +00:00
|
|
|
}
|
2019-06-17 13:57:07 +00:00
|
|
|
});
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|