diff --git a/application/api/chat.1/send.js b/application/api/chat.1/send.js index 8e8ae2ce..33aa61c0 100644 --- a/application/api/chat.1/send.js +++ b/application/api/chat.1/send.js @@ -1,3 +1,8 @@ -async ({ room, message }) => { - domain.chat.send(room, message); -}; +({ + access: 'public', + + method: async ({ room, message }) => { + domain.chat.send(room, message); + return true; + }, +}); diff --git a/application/api/chat.1/subscribe.js b/application/api/chat.1/subscribe.js index f98f7fea..82d5f7e3 100644 --- a/application/api/chat.1/subscribe.js +++ b/application/api/chat.1/subscribe.js @@ -1,7 +1,13 @@ -async ({ room }) => { - const clients = domain.chat.getRoom(room); - clients.add(context.client); - context.client.on('close', () => { - clients.delete(context.client); - }); -}; +({ + access: 'public', + + method: async ({ room }) => { + const clients = domain.chat.getRoom(room); + clients.add(context.client); + context.client.on('close', () => { + clients.delete(context.client); + if (clients.size === 0) domain.chat.dropRoom(room); + }); + return true; + }, +}); diff --git a/application/api/chat.1/unsubscribe.js b/application/api/chat.1/unsubscribe.js new file mode 100644 index 00000000..4306e92d --- /dev/null +++ b/application/api/chat.1/unsubscribe.js @@ -0,0 +1,11 @@ +({ + access: 'public', + + method: async ({ room }) => { + const clients = domain.chat.rooms.get(room); + if (!clients) throw new Error(`Room ${room} is not found`); + clients.delete(context.client); + if (clients.size === 0) domain.chat.dropRoom(room); + return true; + }, +}); diff --git a/application/domain/chat.js b/application/domain/chat.js index 6a17c928..94aa7dc6 100644 --- a/application/domain/chat.js +++ b/application/domain/chat.js @@ -9,8 +9,13 @@ return room; }, + dropRoom(name) { + domain.chat.rooms.delete(name); + }, + send(name, message) { - const room = domain.chat.getRoom(name); + const room = domain.chat.rooms.get(name); + if (!room) throw new Error(`Room ${name} is not found`); for (const client of room) { client.emit('chat/message', { room: name, message }); } diff --git a/application/domain/chat.test.js b/application/domain/chat.test.js index bdd4d400..113071f2 100644 --- a/application/domain/chat.test.js +++ b/application/domain/chat.test.js @@ -10,7 +10,7 @@ }); await t.test('Send message with domain.chat.send', async () => { - const result = domain.chat.send('Marcus', 'Hello there'); + const result = domain.chat.send('example1', 'Hello there'); node.assert.strictEqual(result, undefined); }); }, diff --git a/application/domain/tests/api.js b/application/domain/tests/api.js new file mode 100644 index 00000000..b82015ce --- /dev/null +++ b/application/domain/tests/api.js @@ -0,0 +1,16 @@ +({ + async cases(t, metacom) { + if (t) return; + const res = await metacom.api.auth.signin({ + login: 'marcus', + password: 'marcus', + }); + node.assert.strictEqual(res.status, 'logged'); + node.assert.strictEqual(typeof res.token, 'string'); + + await t.test(`Call example.add({ a, b })`, async () => { + const res = await metacom.api.example.add({ a: 10, b: 20 }); + node.assert.strictEqual(res, 30); + }); + }, +}); diff --git a/application/domain/tests/api.test.js b/application/domain/tests/api.test.js deleted file mode 100644 index 7b1955dc..00000000 --- a/application/domain/tests/api.test.js +++ /dev/null @@ -1,16 +0,0 @@ -({ - name: 'API test', - - async run() { - const url = 'http://127.0.0.1:8001/api'; - const metacom = metarhia.metacom.Metacom.create(url); - await metacom.load('auth', 'example'); - - const res = await metacom.api.auth.signin({ - login: 'marcus', - password: 'marcus', - }); - console.log({ res }); - //node.assert.strictEqual(data.size, size, msg); - }, -}); diff --git a/application/domain/tests/http.test.js b/application/domain/tests/http.test.js new file mode 100644 index 00000000..7f2c59d2 --- /dev/null +++ b/application/domain/tests/http.test.js @@ -0,0 +1,10 @@ +({ + name: 'Metacom over HTTP', + + async run(t) { + const url = 'http://127.0.0.1:8001/api'; + const metacom = metarhia.metacom.Metacom.create(url); + await metacom.load('auth', 'example'); + await domain.tests.api.cases(t, metacom); + }, +}); diff --git a/application/domain/tests/ws.test.js b/application/domain/tests/ws.test.js new file mode 100644 index 00000000..0d283164 --- /dev/null +++ b/application/domain/tests/ws.test.js @@ -0,0 +1,10 @@ +({ + name: 'Metacom over Websocket', + + async run(t) { + const url = 'ws://127.0.0.1:8001/api'; + const metacom = metarhia.metacom.Metacom.create(url); + await metacom.load('auth', 'example'); + await domain.tests.api.cases(t, metacom); + }, +});