Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions app/lib/database/services/Role.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import database from '..';
import { TAppDatabase } from '../interfaces';
import { ROLES_TABLE } from '../model';
import { TRoleModel } from '../../../definitions';

const getCollection = (db: TAppDatabase) => db.get(ROLES_TABLE);

export const getRoleById = async (id: string): Promise<TRoleModel | null> => {
const db = database.active;
const roleCollection = getCollection(db);
try {
const result = await roleCollection.find(id);
return result;
} catch (error) {
return null;
}
};
36 changes: 19 additions & 17 deletions app/lib/encryption/encryption.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,15 +240,15 @@ class Encryption {
msg,
tmsg
});
if (message._hasPendingUpdate) {
console.log(message);
return;
try {
Comment thread
gerzonc marked this conversation as resolved.
return message.prepareUpdate(
protectedFunction(m => {
Object.assign(m, newMessage);
})
);
} catch {
return null;
}
return message.prepareUpdate(
protectedFunction(m => {
Object.assign(m, newMessage);
})
);
})
);

Expand Down Expand Up @@ -281,15 +281,15 @@ class Encryption {
subsToDecrypt.map(async sub => {
const { rid, lastMessage } = sub;
const newSub = await this.decryptSubscription({ rid, lastMessage });
if (sub._hasPendingUpdate) {
console.log(sub);
return;
try {
return sub.prepareUpdate(
protectedFunction(m => {
Object.assign(m, newSub);
})
);
} catch {
return null;
}
return sub.prepareUpdate(
protectedFunction(m => {
Object.assign(m, newSub);
})
);
})
);

Expand Down Expand Up @@ -352,13 +352,15 @@ class Encryption {
);
// If the subscription already exists but doesn't have the E2EKey yet
} else if (!subRecord.E2EKey && subscription.E2EKey) {
if (!subRecord._hasPendingUpdate) {
try {
// Let's update the subscription with the received E2EKey
batch.push(
subRecord.prepareUpdate(s => {
s.E2EKey = subscription.E2EKey;
})
);
} catch (e) {
log(e);
}
}

Expand Down
38 changes: 18 additions & 20 deletions app/lib/methods/getRoles.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';

import database from '../database';
import { getRoleById } from '../database/services/Role';
import log from '../../utils/log';
import { store as reduxStore } from '../auxStore';
import { removeRoles, setRoles as setRolesAction, updateRoles } from '../../actions/roles';
Expand All @@ -20,41 +21,38 @@ export async function onRolesChanged(ddpMessage) {
const db = database.active;
const rolesCollection = db.get('roles');
try {
const rolesRecord = await rolesCollection.find(_id);
try {
Comment thread
gerzonc marked this conversation as resolved.
await db.action(async () => {
await rolesRecord.update(u => {
const roleRecord = await getRoleById(_id);
if (roleRecord) {
await db.write(async () => {
await roleRecord.update(u => {
u.description = description;
});
});
} catch (e) {
log(e);
}
reduxStore.dispatch(updateRoles(_id, description));
} catch (err) {
try {
await db.action(async () => {
} else {
await db.write(async () => {
await rolesCollection.create(post => {
post._raw = sanitizedRaw({ id: _id, description }, rolesCollection.schema);
});
});
} catch (e) {
log(e);
}
reduxStore.dispatch(updateRoles(_id, description || _id));
} catch (e) {
log(e);
}
}
if (/removed/.test(type)) {
const db = database.active;
const rolesCollection = db.get('roles');
try {
const rolesRecord = await rolesCollection.find(_id);
await db.action(async () => {
await rolesRecord.destroyPermanently();
});
reduxStore.dispatch(removeRoles(_id));
} catch (err) {
console.log(err);
const roleRecord = await getRoleById(_id);
if (roleRecord) {
await db.write(async () => {
await roleRecord.destroyPermanently();
});
reduxStore.dispatch(removeRoles(_id));
}
} catch (e) {
log(e);
}
}
}
Expand Down
91 changes: 50 additions & 41 deletions app/lib/methods/subscriptions/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import log from '../../../utils/log';
import protectedFunction from '../helpers/protectedFunction';
import buildMessage from '../helpers/buildMessage';
import database from '../../database';
import { getMessageById } from '../../database/services/Message';
import { getThreadById } from '../../database/services/Thread';
import { getThreadMessageById } from '../../database/services/ThreadMessage';
import reduxStore from '../../createStore';
import { addUserTyping, clearUserTyping, removeUserTyping } from '../../../actions/usersTyping';
import debounce from '../../../utils/debounce';
Expand Down Expand Up @@ -170,75 +173,81 @@ export default class RoomSubscription {

// Create or update message
try {
const messageRecord = await msgCollection.find(message._id);
if (!messageRecord._hasPendingUpdate) {
const update = messageRecord.prepareUpdate(
let operation = null;
const messageRecord = await getMessageById(message._id);
if (messageRecord) {
operation = messageRecord.prepareUpdate(
protectedFunction(m => {
Object.assign(m, message);
})
);
this._messagesBatch[message._id] = update;
} else {
operation = msgCollection.prepareCreate(
protectedFunction(m => {
m._raw = sanitizedRaw({ id: message._id }, msgCollection.schema);
m.subscription.id = this.rid;
Object.assign(m, message);
})
);
}
} catch {
const create = msgCollection.prepareCreate(
protectedFunction(m => {
m._raw = sanitizedRaw({ id: message._id }, msgCollection.schema);
m.subscription.id = this.rid;
Object.assign(m, message);
})
);
this._messagesBatch[message._id] = create;
this._messagesBatch[message._id] = operation;
} catch (e) {
log(e);
}

// Create or update thread
if (message.tlm) {
try {
const threadRecord = await threadsCollection.find(message._id);
if (!threadRecord._hasPendingUpdate) {
const updateThread = threadRecord.prepareUpdate(
let operation = null;
const threadRecord = await getThreadById(message._id);
if (threadRecord) {
operation = threadRecord.prepareUpdate(
protectedFunction(t => {
Object.assign(t, message);
})
);
this._threadsBatch[message._id] = updateThread;
} else {
operation = threadsCollection.prepareCreate(
protectedFunction(t => {
t._raw = sanitizedRaw({ id: message._id }, threadsCollection.schema);
t.subscription.id = this.rid;
Object.assign(t, message);
})
);
}
} catch {
const createThread = threadsCollection.prepareCreate(
protectedFunction(t => {
t._raw = sanitizedRaw({ id: message._id }, threadsCollection.schema);
t.subscription.id = this.rid;
Object.assign(t, message);
})
);
this._threadsBatch[message._id] = createThread;
this._threadsBatch[message._id] = operation;
} catch (e) {
log(e);
}
}

// Create or update thread message
if (message.tmid) {
try {
const threadMessageRecord = await threadMessagesCollection.find(message._id);
if (!threadMessageRecord._hasPendingUpdate) {
const updateThreadMessage = threadMessageRecord.prepareUpdate(
let operation = null;
const threadMessageRecord = await getThreadMessageById(message._id);
if (threadMessageRecord) {
operation = threadMessageRecord.prepareUpdate(
protectedFunction(tm => {
Object.assign(tm, message);
tm.rid = message.tmid;
delete tm.tmid;
})
);
} else {
operation = threadMessagesCollection.prepareCreate(
protectedFunction(tm => {
tm._raw = sanitizedRaw({ id: message._id }, threadMessagesCollection.schema);
Object.assign(tm, message);
tm.subscription.id = this.rid;
tm.rid = message.tmid;
delete tm.tmid;
})
);
this._threadMessagesBatch[message._id] = updateThreadMessage;
}
} catch {
const createThreadMessage = threadMessagesCollection.prepareCreate(
protectedFunction(tm => {
tm._raw = sanitizedRaw({ id: message._id }, threadMessagesCollection.schema);
Object.assign(tm, message);
tm.subscription.id = this.rid;
tm.rid = message.tmid;
delete tm.tmid;
})
);
this._threadMessagesBatch[message._id] = createThreadMessage;
this._threadMessagesBatch[message._id] = operation;
} catch (e) {
log(e);
}
}

Expand Down
Loading