...
 
Commits (2)
......@@ -52,6 +52,13 @@ export default class BaseModel {
return this.__list;
}
@action
removeFromList() {
if (this._list) {
this._list.remove(this);
}
}
toPlainObject() {
const plainEntity = toJS(this);
......@@ -92,9 +99,9 @@ export default class BaseModel {
const childs = this.childModels();
Object.getOwnPropertyNames(this).forEach(key => {
if (data[key]) {
if (data[key] !== undefined) {
if (childs[key]) {
if (childs[key] && this[key] && this[key].update) {
// we update the child model
this[key].update(data[key]);
} else {
......
......@@ -21,6 +21,10 @@ export const isApiError = function(err) {
return err instanceof ApiError;
}
export const isApiForbidden = function(err) {
return err instanceof ApiError && err.status == 403;
}
/**
* Api service
*/
......
// @flow
import _ from 'lodash';
import apiService from "./api.service";
import apiService, { isApiForbidden, ApiError } from "./api.service";
import sessionService from "./session.service";
import blockListService from "./block-list.service";
import GroupModel from "../../groups/GroupModel";
......@@ -67,6 +67,15 @@ class EntitiesService {
entitiesStorage.remove(urn);
}
/**
* Delete an entity from the cache
* @param {Array<string>} urn
*/
deleteManyFromCache(urns: Array<string>) {
urns.forEach((urn: string): boolean => this.entities.delete(urn));
entitiesStorage.removeMany(urns);
}
/**
* Get entities from feed
* @param {Array} feed
......@@ -185,7 +194,7 @@ class EntitiesService {
* @param {boolean} asActivities
* @return []
*/
async fetch(urns: Array<string>, abortTag: any, asActivities: boolean = false): Promise<Array<Object>> {
async fetch(urns: Array<string>, abortTag: any, asActivities: boolean = false): Promise<void> {
try {
const response: any = await apiService.get('api/v2/entities/', { urns, as_activities: asActivities ? 1 : 0}, abortTag);
......@@ -193,10 +202,25 @@ class EntitiesService {
for (const entity of response.entities) {
this.addEntity(entity);
}
return response;
} catch (err) {
console.log(err)
// if the server response is a 403
if (isApiForbidden(err)) {
// if the entity exists in the cache, remove the permissions to force the UI update
urns.forEach((urn: string) => {
const cache = this.entities.get(urn);
if (cache) {
// remove permissions
cache.entity.setPermissions({permissions:[]});
// if the entity is attached to a list we remove if from the list
cache.entity.removeFromList();
}
})
// remove it from memory and local storage
this.deleteManyFromCache(urns);
return;
}
throw err;
}
}
......@@ -211,6 +235,7 @@ class EntitiesService {
this.cleanEntity(entity);
const storedEntity = this.getFromCache(entity.urn);
if (storedEntity) {
storedEntity.update(entity);
} else {
......@@ -254,6 +279,7 @@ class EntitiesService {
return ActivityModel.create(entity);
}
}
return ActivityModel.create(entity)
}
}
......
import deviceLog, {LogView, InMemoryAdapter} from 'react-native-device-log';
import deviceLog from 'react-native-device-log';
import * as stacktraceParser from "stacktrace-parser";
import AsyncStorage from '@react-native-community/async-storage';
import storageService from './storage.service';
import settingsService from '../../settings/SettingsService'
import settingsStore from '../../settings/SettingsStore';
import * as Sentry from '@sentry/react-native';
import { isNetworkFail } from '../helpers/abortableFetch';
import { isNetworkFail, isAbort } from '../helpers/abortableFetch';
import { ApiError } from './api.service';
const parseErrorStack = error => {
......@@ -77,7 +75,7 @@ class LogService {
}
// do not log request or api errors < 500
if (!isNetworkFail(error) && (!this.isApiError(error) || this.isUnexpectedError(error))) {
if (!isNetworkFail(error) && (!this.isApiError(error) || this.isUnexpectedError(error)) && !isAbort(error)) {
// report the issue to sentry
Sentry.captureException(error);
......
......@@ -87,6 +87,15 @@ export class EntitiesStorage {
return await this.db.executeSql('DELETE FROM entities WHERE urn=?', [urn]);
}
/**
* Remove many entities
* @param {Array} urns
*/
async removeMany(urns) {
const urnsIn = "('" + urns.join("','") + "')";
return await this.db.executeSql('DELETE FROM entities WHERE urn IN ' + urnsIn);
}
/**
* Remove all entities
*/
......
......@@ -222,11 +222,7 @@ export default class ActivityModel extends BaseModel {
async deleteEntity() {
try {
await deleteItem(this.guid)
if (this._list) {
runInAction(() => {
this._list.remove(this);
});
}
this.removeFromList();
entitiesService.deleteFromCache(this.urn)
} catch (err) {
logService.exception('[ActivityModel]', err);
......
......@@ -125,6 +125,12 @@ class ActivityScreen extends Component {
*/
render() {
if (!this.entityStore.entity && !this.entityStore.errorLoading) return <CenteredLoading />;
if (!this.entityStore.entity.can(FLAG_VIEW, true)) {
this.props.navigation.goBack();
return null;
}
return (
<View style={[CS.flexContainer, CS.backgroundWhite]}>
{
......