-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1079 from dm3-org/DSFE3-Halt-delivery
Dsfe3 halt delivery
- Loading branch information
Showing
39 changed files
with
1,080 additions
and
259 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
-- CreateTable | ||
CREATE TABLE "HaltedMessage" ( | ||
"id" TEXT NOT NULL, | ||
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"encryptedEnvelopContainer" TEXT NOT NULL, | ||
"encryptedContactName" TEXT NOT NULL, | ||
"ownerId" TEXT NOT NULL, | ||
|
||
CONSTRAINT "HaltedMessage_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "HaltedMessage" ADD CONSTRAINT "HaltedMessage_ownerId_fkey" FOREIGN KEY ("ownerId") REFERENCES "Account"("id") ON DELETE RESTRICT ON UPDATE CASCADE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
Warnings: | ||
- You are about to drop the column `encryptedContactName` on the `HaltedMessage` table. All the data in the column will be lost. | ||
*/ | ||
-- AlterTable | ||
ALTER TABLE "HaltedMessage" DROP COLUMN "encryptedContactName"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
Warnings: | ||
- You are about to drop the `HaltedMessage` table. If the table is not empty, all the data it contains will be lost. | ||
*/ | ||
-- DropForeignKey | ||
ALTER TABLE "HaltedMessage" DROP CONSTRAINT "HaltedMessage_ownerId_fkey"; | ||
|
||
-- AlterTable | ||
ALTER TABLE "EncryptedMessage" ADD COLUMN "isHalted" BOOLEAN NOT NULL DEFAULT false; | ||
|
||
-- DropTable | ||
DROP TABLE "HaltedMessage"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
packages/backend/src/persistence/storage/postgres/haltedMessage/clearHaltedMessage.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { PrismaClient } from '@prisma/client'; | ||
import { clearHaltedMessage } from './clearHaltedMessage'; | ||
import { getOrCreateAccount } from '../utils/getOrCreateAccount'; | ||
import { addMessageBatch } from '../addMessageBatch'; | ||
|
||
// Mock the PrismaClient | ||
const mockDb = new PrismaClient(); | ||
|
||
describe('deleteHaltedMessage', () => { | ||
let prismaClient: PrismaClient; | ||
|
||
beforeEach(async () => { | ||
prismaClient = new PrismaClient(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await prismaClient.encryptedMessage.deleteMany({}); | ||
await prismaClient.conversation.deleteMany({}); | ||
await prismaClient.account.deleteMany({}); | ||
|
||
prismaClient.$disconnect(); | ||
}); | ||
it('should return false if account does not exist', async () => { | ||
const result = await clearHaltedMessage(mockDb)( | ||
'bob.eth', | ||
'bob.eth', | ||
'messageId', | ||
); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should return false if message does not exist', async () => { | ||
const result = await clearHaltedMessage(mockDb)( | ||
'existing', | ||
'existing', | ||
'messageId', | ||
); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should return true if message is successfully deleted', async () => { | ||
const account = await getOrCreateAccount(prismaClient, 'bob.eth'); | ||
//create message first | ||
const messageRecord1 = { | ||
messageId: 'messageId1', | ||
createdAt: 123, | ||
encryptedEnvelopContainer: 'encryptedEnvelopContainer', | ||
isHalted: true, | ||
}; | ||
const messageRecord2 = { | ||
messageId: 'messageId2', | ||
createdAt: 456, | ||
encryptedEnvelopContainer: 'encryptedEnvelopContainer', | ||
isHalted: false, | ||
}; | ||
|
||
await addMessageBatch(prismaClient)('bob.eth', 'alice.eth', [ | ||
messageRecord1, | ||
messageRecord2, | ||
]); | ||
|
||
const result = await clearHaltedMessage(mockDb)( | ||
'bob.eth', | ||
'bob.eth', | ||
'messageId1', | ||
); | ||
expect(result).toBe(true); | ||
}); | ||
it('should rename to aliasName', async () => { | ||
const account = await getOrCreateAccount(prismaClient, 'bob.eth'); | ||
//create message first | ||
const messageRecord1 = { | ||
messageId: 'messageId1', | ||
createdAt: 123, | ||
encryptedEnvelopContainer: 'encryptedEnvelopContainer', | ||
isHalted: true, | ||
}; | ||
const messageRecord2 = { | ||
messageId: 'messageId2', | ||
createdAt: 456, | ||
encryptedEnvelopContainer: 'encryptedEnvelopContainer', | ||
isHalted: false, | ||
}; | ||
|
||
await addMessageBatch(prismaClient)('bob.eth', 'alice.eth', [ | ||
messageRecord1, | ||
messageRecord2, | ||
]); | ||
|
||
const beforeClearHaltedMessage = | ||
await prismaClient.encryptedMessage.findFirst({ | ||
where: { | ||
id: 'messageId1', | ||
}, | ||
}); | ||
|
||
expect(beforeClearHaltedMessage?.encryptedContactName).toBe( | ||
'alice.eth', | ||
); | ||
|
||
const result = await clearHaltedMessage(mockDb)( | ||
'bob.eth', | ||
'0x123.addr.dm3.eth', | ||
'messageId1', | ||
); | ||
|
||
const message = await prismaClient.encryptedMessage.findFirst({ | ||
where: { | ||
id: 'messageId1', | ||
}, | ||
}); | ||
|
||
expect(message?.encryptedContactName).toBe('0x123.addr.dm3.eth'); | ||
expect(result).toBe(true); | ||
}); | ||
}); |
63 changes: 63 additions & 0 deletions
63
packages/backend/src/persistence/storage/postgres/haltedMessage/clearHaltedMessage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { PrismaClient } from '@prisma/client'; | ||
|
||
export const clearHaltedMessage = | ||
(db: PrismaClient) => | ||
/** | ||
* | ||
* @param ensName the ensName the messages have been stored originally i.E alice.eth | ||
* @param aliasName the aliasNamespace the cleint uses after resolving the ensName i.E Alice. ie addr.user.dm3.eth | ||
* @param messageId the messageId | ||
* @returns | ||
*/ | ||
async (ensName: string, aliasName: string, messageId: string) => { | ||
//Find the account first we want to get the messages for | ||
const account = await db.account.findFirst({ | ||
where: { | ||
id: ensName, | ||
}, | ||
}); | ||
//If the contact does not exist, there is no message that can be deleted | ||
if (!account) { | ||
console.log('cleatHaltedMessages: account not found'); | ||
return false; | ||
} | ||
|
||
const message = await db.encryptedMessage.findFirst({ | ||
where: { | ||
id: messageId, | ||
ownerId: account.id, | ||
}, | ||
}); | ||
|
||
if (!message) { | ||
console.log(`cleatHaltedMessages: message ${messageId} not found`); | ||
return false; | ||
} | ||
|
||
try { | ||
await db.encryptedMessage.update({ | ||
where: { | ||
id: messageId, | ||
}, | ||
data: { | ||
//Message is no longer halted | ||
isHalted: false, | ||
//Use alias name | ||
encryptedContactName: aliasName, | ||
}, | ||
}); | ||
|
||
await db.conversation.update({ | ||
where: { | ||
id: message.conversationId, | ||
}, | ||
data: { | ||
encryptedContactName: aliasName, | ||
}, | ||
}); | ||
return true; | ||
} catch (e) { | ||
console.error('clear halted error', e); | ||
return false; | ||
} | ||
}; |
Oops, something went wrong.