fetch rooms

This commit is contained in:
grimhilt 2023-03-20 14:43:07 +01:00
parent 095efb5440
commit d7029854b4
12 changed files with 160 additions and 91 deletions

3
.gitignore vendored
View File

@ -33,4 +33,5 @@ config.json
*.json *.json
tmp tmp
*test* *test*
*.png *.png
!*/schemas/*

17
back/controllers/rooms.js Normal file
View File

@ -0,0 +1,17 @@
const statusCode = require("../utils/statusCodes").statusCodes;
const { getRooms } = require("../db/api.js");
async function rooms(body, res) {
const { mailboxId, offset, limit } = body;
getRooms(mailboxId).then((rooms) => {
console.log(rooms)
res.status(statusCode.OK).json(rooms);
}).catch((err) => {
console.log(err)
res.status(statusCode.INTERNAL_SERVER_ERROR);
});
}
module.exports = {
rooms,
};

View File

@ -20,7 +20,32 @@ async function getMailboxes() {
return await execQueryAsync(query, values); return await execQueryAsync(query, values);
} }
async function getRooms(mailboxId) {
const query = `
SELECT
app_room.room_id AS id,
app_room.room_name AS roomName,
address.email AS user,
app_room.owner_id AS userId,
app_room.notSeen,
mailbox_message.mailbox_id AS mailboxId
FROM app_room
INNER JOIN message
INNER JOIN mailbox_message
INNER JOIN address
WHERE
message.message_id = app_room.message_id AND
mailbox_message.mailbox_id = 1 AND
mailbox_message.message_id = message.message_id AND
address.address_id = app_room.owner_id
`;
// todo mailboxId
const values = [];
return await execQueryAsync(query, values);
}
module.exports = { module.exports = {
registerMailbox, registerMailbox,
getMailboxes getMailboxes,
getRooms
}; };

View File

@ -9,6 +9,7 @@ addFormats(ajv);
const schema_mailbox = require("../schemas/mailbox_schema.json"); const schema_mailbox = require("../schemas/mailbox_schema.json");
const { addMailbox } = require("../controllers/addMailbox.js"); const { addMailbox } = require("../controllers/addMailbox.js");
const { getMailboxes } = require("../db/api.js"); const { getMailboxes } = require("../db/api.js");
const { rooms } = require("../controllers/rooms.js");
const validate_mailbox = ajv.compile(schema_mailbox); const validate_mailbox = ajv.compile(schema_mailbox);
@ -28,23 +29,11 @@ router.get("/mailboxes", (req, res) => {
* @param {string} token the token of the user * @param {string} token the token of the user
* @return {object} a list of room and their preview (subject) * @return {object} a list of room and their preview (subject)
*/ */
router.get("/{mailboxId}/messages", (req, res) => { router.get("/:mailboxId/rooms", async (req, res) => {
const { mailboxId, offset, limit } = req.params; const { mailboxId, offset, limit } = req.params;
// todo check token // todo check token
// todo use offset // todo use offset
const query = ` await rooms(req.params, res);
SELECT app_room.room_id, app_room.room_name, app_room.owner_id, app_room.notSeen, mailbox_message.mailbox_id, address.email
FROM app_room
INNER JOIN message
INNER JOIN mailbox_message
INNER JOIN address
WHERE
message.message_id = app_room.message_id AND
mailbox_message.mailbox_id = 1 AND
mailbox_message.message_id = message.message_id AND
address.address_id = app_room.owner_id
`;
const values = [mailboxId];
}); });

View File

@ -0,0 +1,14 @@
{
"type": "object",
"properties": {
"email": { "type": "string", "format": "email" },
"pwd": { "type": "string" },
"xoauth": { "type": "string" },
"xoauth2": { "type": "string" },
"host": { "type": "string", "format": "hostname" },
"port": { "type": "number", "maximum": 65535 },
"tls": { "type": "boolean" }
},
"required": ["email", "host", "port", "tls"],
"additionalProperties": false
}

View File

@ -7,4 +7,7 @@ export default {
getMailboxes() { getMailboxes() {
return API().get('/mail/mailboxes'); return API().get('/mail/mailboxes');
}, },
getRooms(mailboxId) {
return API().get(`/mail/${mailboxId}/rooms`);
}
} }

View File

@ -2,65 +2,80 @@ import API from "@/services/imapAPI";
import { createStore } from "vuex"; import { createStore } from "vuex";
const roomsStore = createStore({ const roomsStore = createStore({
state() { state() {
return { return {
rooms: [ rooms: [
{ {
users: "clemnce", id: 0,
object: user: "clemnce",
"Lorem magna minim cillum labore ex eiusmod proident excepteur sint irure ipsum.", userId: 0,
mailbox: 1, name: "Lorem magna minim cillum labore ex eiusmod proident excepteur sint irure ipsum.",
}, mailboxId: 1,
{ },
users: "juliette", {
object: user: "juliette",
"Lorem magna minim cillum labore ex eiusmod proident excepteur sint irure ipsum.", name: "Lorem magna minim cillum labore ex eiusmod proident excepteur sint irure ipsum.",
mailbox: 1, mailboxId: 1,
}, },
{ {
users: "jean", user: "jean",
object: name: "Lorem magna minim cillum labore ex eiusmod proident excepteur sint irure ipsum.",
"Lorem magna minim cillum labore ex eiusmod proident excepteur sint irure ipsum.", mailboxId: 2,
mailbox: 2, },
}, {
{ user: "luc",
users: "luc", name: "Lorem magna minim cillum labore ex eiusmod proident excepteur sint irure ipsum.",
object: mailboxId: 2,
"Lorem magna minim cillum labore ex eiusmod proident excepteur sint irure ipsum.", },
mailbox: 2, ],
}, mailboxes: [],
], activeMailbox: -1,
mailboxes: [], };
activeMailbox: -1 },
}; mutations: {
}, setActiveMailbox(state, payload) {
mutations: { state.activeMailbox = payload;
setActiveMailbox(state, payload) { const mailbox = state.mailboxes.find((mailbox) => mailbox.id == payload);
state.activeMailbox = payload; // todo fetched mailbox all
if (mailbox?.fetched == false) {
mailbox.fetched = true;
console.log("add messages")
API.getRooms(payload).then((res) => {
// todo add if not exist
res.data.forEach((room) => {
state.rooms.push(room);
});
}).catch((err) => {
console.log(err)
});
}
},
addMailboxes(state, payload) {
payload.forEach((mailbox) => {
mailbox.fetched = false;
state.mailboxes.push(mailbox);
});
},
},
getters: {
rooms: (state) => () => {
console.log(state.rooms.length)
if (state.activeMailbox == 0) return state.rooms;
return state.rooms.filter((room) => room.mailboxId == state.activeMailbox);
},
},
actions: {
async fetchMailboxes(context) {
console.log("add mailboxes");
API.getMailboxes()
.then((res) => {
context.commit("addMailboxes", res.data);
})
.catch((err) => {
console.log(err);
});
},
}, },
addMailboxes(state, payload) {
payload.forEach(mailbox => {
state.mailboxes.push(mailbox);
});
}
},
getters: {
rooms: (state) => () => {
if (state.activeMailbox == 0) return state.rooms;
return state.rooms.filter(room => room.mailbox == state.activeMailbox);
}
},
actions: {
async addMailboxes(context) {
console.log("add mailboxes")
API.getMailboxes().then((res) => {
console.log(res.data)
context.commit("addMailboxes", res.data);
}).catch((err) => {
console.log(err)
});
}
}
}); });
export default roomsStore; export default roomsStore;

View File

@ -1,19 +1,19 @@
<template> <template>
<div> <div>
<Mailboxes /> <Mailboxes />
<Users id="users"/> <Rooms id="rooms"/>
</div> </div>
</template> </template>
<script> <script>
import Mailboxes from './mailboxes/Mailboxes' import Mailboxes from './mailboxes/Mailboxes'
import Users from './users/Users.vue' import Rooms from './rooms/Rooms.vue'
export default { export default {
name: 'Sidebar', name: 'Sidebar',
components: { components: {
Mailboxes, Mailboxes,
Users, Rooms,
} }
} }
</script> </script>
@ -24,7 +24,7 @@ div {
height: 100%; height: 100%;
} }
#users { #rooms {
max-width: 300px; max-width: 300px;
min-width: 250px; min-width: 250px;
} }

View File

@ -28,11 +28,10 @@ export default {
computed: { computed: {
...mapState(['mailboxes']) ...mapState(['mailboxes'])
}, },
} created() {
console.log("call api get mailboxes");
if (roomsStore.state.mailboxes.length == 0) { roomsStore.dispatch('fetchMailboxes');
console.log("call api get mailboxes"); }
roomsStore.dispatch('addMailboxes');
} }
</script> </script>

View File

@ -3,8 +3,8 @@
<div id="user"> <div id="user">
<BaseAvatar url="vue.png"/> <BaseAvatar url="vue.png"/>
<div id="content"> <div id="content">
<div id="sender">{{ sender }}</div> <div id="sender">{{ data.user }}</div>
<div id="object">{{ object }}</div> <div id="object">{{ data.roomName }}</div>
</div> </div>
</div> </div>
<ThreadList /> <ThreadList />
@ -18,8 +18,14 @@ import ThreadList from './threads/ThreadList.vue'
export default { export default {
name: 'User', name: 'User',
props: { props: {
sender: String, data: {
object: String id: Number,
roomName: String,
user: String,
userId: Number,
notSeen: Number,
mailboxId: Number
}
}, },
components: { components: {
BaseAvatar, BaseAvatar,

View File

@ -1,20 +1,20 @@
<template> <template>
<div> <div>
<User v-for="(room, index) in rooms()" :key="index" :sender="room.users" :object="room.object" /> <Room v-for="(room, index) in rooms()" :key="index" :data="room" />
</div> </div>
</template> </template>
<script> <script>
import { mapGetters } from 'vuex' import { mapGetters } from 'vuex'
import User from './User' import Room from './Room'
export default { export default {
name: 'Users', name: 'Rooms',
props: { props: {
}, },
components: { components: {
User Room
}, },
computed: { computed: {
...mapGetters(['rooms']) ...mapGetters(['rooms'])