Skip to content

Commit

Permalink
feat(chat): introduce auditMode, room deletions on leave and remove u…
Browse files Browse the repository at this point in the history
…nique name requirement from rooms (#794)

feat(chat): delete room when all participants have left
fix(chat): room deletion not deleting associated messages
refactor(chat): allow rooms with non-unique names
feat(chat): introduce audit mode to keep chats and rooms after deletion
feat(chat): add participant log to keep a history of who joins or leaves a room
refactor(grpc-sdk): add missing types in query
fix(chat): remove "any" from query
fix(authorization): remove "any" from query
chore: codefactor issues
  • Loading branch information
kkopanidis authored Nov 16, 2023
1 parent 97fcb77 commit 1fc4484
Show file tree
Hide file tree
Showing 13 changed files with 222 additions and 62 deletions.
4 changes: 3 additions & 1 deletion libraries/grpc-sdk/src/types/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ type operators =
| '$regex'
| '$options'
| '$like'
| '$ilike';
| '$ilike'
| '$exists';
type arrayOperators = '$in' | '$nin';
type conditionOperators = '$or' | '$and';

Expand All @@ -19,6 +20,7 @@ type simpleQuery<T> = {
};

type mixedQuery<T> =
| { $exists: boolean }
| { [key in operators]?: documentValues<T> }
| { [key in arrayOperators]?: documentValues<T>[] };

Expand Down
6 changes: 3 additions & 3 deletions modules/authorization/src/migrations/actorIndex.migration.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import ConduitGrpcSdk, { Query } from '@conduitplatform/grpc-sdk';
import { Query } from '@conduitplatform/grpc-sdk';
import { ActorIndex } from '../models';

export const migrateActorIndex = async (grpcSdk: ConduitGrpcSdk) => {
const query: Query<any> = {
export const migrateActorIndex = async () => {
const query: Query<ActorIndex> = {
$or: [
{ entityType: '' },
{ entityId: '' },
Expand Down
8 changes: 4 additions & 4 deletions modules/authorization/src/migrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import { migratePermission } from './permission.migration';

export async function runMigrations(grpcSdk: ConduitGrpcSdk) {
await Promise.all([
migrateObjectIndex(grpcSdk),
migrateActorIndex(grpcSdk),
migrateRelationships(grpcSdk),
migratePermission(grpcSdk),
migrateObjectIndex(),
migrateActorIndex(),
migrateRelationships(),
migratePermission(),
]);
}
6 changes: 3 additions & 3 deletions modules/authorization/src/migrations/objectIndex.migration.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import ConduitGrpcSdk, { Query } from '@conduitplatform/grpc-sdk';
import { Query } from '@conduitplatform/grpc-sdk';
import { ObjectIndex } from '../models';

export const migrateObjectIndex = async (grpcSdk: ConduitGrpcSdk) => {
const query: Query<any> = {
export const migrateObjectIndex = async () => {
const query: Query<ObjectIndex> = {
$or: [
{ entityType: '' },
{ entityId: '' },
Expand Down
6 changes: 3 additions & 3 deletions modules/authorization/src/migrations/permission.migration.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import ConduitGrpcSdk, { Query } from '@conduitplatform/grpc-sdk';
import { Query } from '@conduitplatform/grpc-sdk';
import { Permission } from '../models';

export const migratePermission = async (grpcSdk: ConduitGrpcSdk) => {
const query: Query<any> = {
export const migratePermission = async () => {
const query: Query<Permission> = {
$or: [
{ resourceType: '' },
{ resourceId: '' },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import ConduitGrpcSdk, { Query } from '@conduitplatform/grpc-sdk';
import { Query } from '@conduitplatform/grpc-sdk';
import { Relationship } from '../models';

export const migrateRelationships = async (grpcSdk: ConduitGrpcSdk) => {
const query: Query<any> = {
export const migrateRelationships = async () => {
const query: Query<Relationship> = {
$or: [
{ resourceType: '' },
{ resourceId: '' },
Expand Down
5 changes: 5 additions & 0 deletions modules/chat/src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export default {
format: 'Boolean',
default: false,
},
auditMode: {
doc: 'When audit is enabled, deleted rooms and messages are not actually deleted, but marked as deleted',
format: 'Boolean',
default: false,
},
explicit_room_joins: {
enabled: {
doc: 'Defines whether users should explicitly accept an invitation before being introduced into a chat room',
Expand Down
17 changes: 17 additions & 0 deletions modules/chat/src/migrations/chatMessage.migrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Query } from '@conduitplatform/grpc-sdk';
import { ChatMessage } from '../models';

export const migrateChatMessages = async () => {
const query: Query<ChatMessage> = {
$or: [{ deleted: '' }, { deleted: { $exists: false } }],
};
let chatMessages = await ChatMessage.getInstance().findMany(query, undefined, 0, 100);
while (chatMessages.length === 0) {
for (const chatMessage of chatMessages) {
await ChatMessage.getInstance().findByIdAndUpdate(chatMessage._id, {
deleted: false,
});
}
chatMessages = await ChatMessage.getInstance().findMany(query, undefined, 0, 100);
}
};
18 changes: 18 additions & 0 deletions modules/chat/src/migrations/chatRoom.migrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Query } from '@conduitplatform/grpc-sdk';
import { ChatRoom } from '../models';

export const migrateChatRoom = async () => {
const query: Query<ChatRoom> = {
$or: [{ deleted: '' }, { deleted: { $exists: false } }],
};
let chatRooms = await ChatRoom.getInstance().findMany(query, undefined, 0, 100);
while (chatRooms.length === 0) {
for (const chatRoom of chatRooms) {
await ChatRoom.getInstance().findByIdAndUpdate(chatRoom._id, {
deleted: false,
participantsLog: [],
});
}
chatRooms = await ChatRoom.getInstance().findMany(query, undefined, 0, 100);
}
};
5 changes: 4 additions & 1 deletion modules/chat/src/migrations/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import ConduitGrpcSdk from '@conduitplatform/grpc-sdk';
import { migrateChatRoom } from './chatRoom.migrate';
import { migrateChatMessages } from './chatMessage.migrate';

export async function runMigrations(grpcSdk: ConduitGrpcSdk) {
// ...
await migrateChatRoom();
await migrateChatMessages();
}
45 changes: 40 additions & 5 deletions modules/chat/src/models/ChatRoom.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,34 @@ const schema: ConduitModel = {
required: true,
},
],
creator: {
type: TYPE.Relation,
model: 'User',
},
participantsLog: [
{
type: {
action: {
type: TYPE.String,
enum: ['add', 'remove', 'create', 'join', 'leave'],
required: true,
},
user: {
type: TYPE.Relation,
model: 'User',
required: true,
},
timestamp: {
type: TYPE.Date,
required: true,
},
},
},
],
deleted: {
type: TYPE.Boolean,
default: false,
},
createdAt: TYPE.Date,
updatedAt: TYPE.Date,
};
Expand All @@ -33,11 +61,18 @@ const collectionName = undefined;

export class ChatRoom extends ConduitActiveSchema<ChatRoom> {
private static _instance: ChatRoom;
_id!: string;
name!: string;
participants!: string[] | User[];
createdAt!: Date;
updatedAt!: Date;
_id: string;
name: string;
creator?: string | User;
participants: string[] | User[];
participantsLog: {
action: 'add' | 'remove' | 'create' | 'join' | 'leave';
user: string | User;
timestamp: Date;
}[];
deleted: boolean;
createdAt: Date;
updatedAt: Date;

private constructor(database: DatabaseProvider) {
super(database, ChatRoom.name, schema, modelOptions, collectionName);
Expand Down
19 changes: 12 additions & 7 deletions modules/chat/src/models/Message.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ const schema: ConduitModel = {
required: true,
},
],
deleted: {
type: TYPE.Boolean,
default: false,
},
createdAt: TYPE.Date,
updatedAt: TYPE.Date,
};
Expand All @@ -44,13 +48,14 @@ const collectionName = undefined;

export class ChatMessage extends ConduitActiveSchema<ChatMessage> {
private static _instance: ChatMessage;
_id!: string;
message!: string;
senderUser!: string | User;
room!: string | ChatRoom;
readBy!: string[] | User[];
createdAt!: Date;
updatedAt!: Date;
_id: string;
message: string;
senderUser: string | User;
room: string | ChatRoom;
readBy: string[] | User[];
deleted: boolean;
createdAt: Date;
updatedAt: Date;

private constructor(database: DatabaseProvider) {
super(database, ChatMessage.name, schema, modelOptions, collectionName);
Expand Down
Loading

0 comments on commit 1fc4484

Please sign in to comment.