create chat document
This commit is contained in:
@@ -3,6 +3,7 @@ import PouchDB from '@/api/PouchDB'
|
|||||||
import type {
|
import type {
|
||||||
BaseDocument,
|
BaseDocument,
|
||||||
QRCodeDocument,
|
QRCodeDocument,
|
||||||
|
ChatDocument,
|
||||||
MessageDocument,
|
MessageDocument,
|
||||||
WebPushSubscriptionDocument,
|
WebPushSubscriptionDocument,
|
||||||
PublicSettingsDocument,
|
PublicSettingsDocument,
|
||||||
@@ -18,6 +19,7 @@ export const getLocalDb = <T extends BaseDocument>(dbName: DB_NAMES) => new Pouc
|
|||||||
export const qrCodeDbRemote = getRemoteDb<QRCodeDocument>(DB_NAMES.QR)
|
export const qrCodeDbRemote = getRemoteDb<QRCodeDocument>(DB_NAMES.QR)
|
||||||
export const qrCodeDbLocal = getLocalDb<QRCodeDocument>(DB_NAMES.QR)
|
export const qrCodeDbLocal = getLocalDb<QRCodeDocument>(DB_NAMES.QR)
|
||||||
export const messagesDbLocal = getLocalDb<MessageDocument>(DB_NAMES.MESSAGES)
|
export const messagesDbLocal = getLocalDb<MessageDocument>(DB_NAMES.MESSAGES)
|
||||||
|
export const chatsDbRemote = getRemoteDb<ChatDocument>(DB_NAMES.CHATS)
|
||||||
export const webPushSubscriptionsDbRemote = getRemoteDb<
|
export const webPushSubscriptionsDbRemote = getRemoteDb<
|
||||||
WebPushSubscriptionDocument<PushSubscriptionJSON>
|
WebPushSubscriptionDocument<PushSubscriptionJSON>
|
||||||
>(DB_NAMES.WEB_PUSH_SUBSCRIPTIONS)
|
>(DB_NAMES.WEB_PUSH_SUBSCRIPTIONS)
|
||||||
|
|||||||
@@ -44,8 +44,8 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { ref } from 'vue'
|
import { ref } from 'vue'
|
||||||
import { useRouter } from 'vue-router'
|
import { useRouter } from 'vue-router'
|
||||||
import { messagesDbLocal as db } from '@/api/dbs'
|
import { chatsDbRemote, messagesDbLocal as db } from '@/api/dbs'
|
||||||
import type { MessageDocument, QRCodeDocument } from '@hereconnect/types'
|
import type { ChatDocument, MessageDocument, QRCodeDocument } from '@hereconnect/types'
|
||||||
import { ROUTE_NAMES } from '@/constants/routes'
|
import { ROUTE_NAMES } from '@/constants/routes'
|
||||||
import { getUserUuid } from '@/utils/getUserUuid'
|
import { getUserUuid } from '@/utils/getUserUuid'
|
||||||
import { getBrowserUuid } from '@/utils/getBrowserUuid'
|
import { getBrowserUuid } from '@/utils/getBrowserUuid'
|
||||||
@@ -69,22 +69,49 @@ async function sendMessage(content: string) {
|
|||||||
try {
|
try {
|
||||||
loading.value = true
|
loading.value = true
|
||||||
|
|
||||||
const chat = (new Date().getTime() - new Date(doc.created_at).getTime()).toString(36)
|
const date = new Date()
|
||||||
|
const created_at = date.toISOString()
|
||||||
|
const chat = (date.getTime() - new Date(doc.created_at).getTime()).toString(36)
|
||||||
|
const user_uuid = getUserUuid()
|
||||||
|
const browser_uuid = getBrowserUuid()
|
||||||
|
|
||||||
|
const chatGuestDoc: ChatDocument = {
|
||||||
|
_id: `chat:${user_uuid}:guest:${doc.uri}:${chat}`,
|
||||||
|
type: 'chat',
|
||||||
|
qr_code_uri: doc.uri,
|
||||||
|
role: 'guest',
|
||||||
|
chat,
|
||||||
|
created_at,
|
||||||
|
user_uuid,
|
||||||
|
}
|
||||||
|
|
||||||
|
const chatOwnerDoc: ChatDocument = {
|
||||||
|
_id: `chat:${doc.user_uuid}:owner:${doc.uri}:${chat}`,
|
||||||
|
type: 'chat',
|
||||||
|
qr_code_uri: doc.uri,
|
||||||
|
role: 'owner',
|
||||||
|
chat,
|
||||||
|
created_at,
|
||||||
|
user_uuid: doc.user_uuid,
|
||||||
|
}
|
||||||
|
|
||||||
const message: MessageDocument = {
|
const message: MessageDocument = {
|
||||||
_id: `message:${doc.uri}:${chat}:${new Date().toISOString()}`,
|
_id: `message:${doc.uri}:${chat}:${created_at}`,
|
||||||
type: 'message',
|
type: 'message',
|
||||||
qr_code_uri: doc.uri,
|
qr_code_uri: doc.uri,
|
||||||
chat,
|
chat,
|
||||||
created_at: new Date().toISOString(),
|
created_at,
|
||||||
from: 'guest',
|
from: 'guest',
|
||||||
user_uuid: getUserUuid(),
|
user_uuid,
|
||||||
browser_uuid: getBrowserUuid(),
|
browser_uuid,
|
||||||
body: content.trim(),
|
body: content.trim(),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
|
chatsDbRemote.bulkDocs([chatGuestDoc, chatOwnerDoc]),
|
||||||
// Сохраняем сообщение в локальную базу данных
|
// Сохраняем сообщение в локальную базу данных
|
||||||
await db.put(message)
|
db.put(message),
|
||||||
|
])
|
||||||
|
|
||||||
success.value = true
|
success.value = true
|
||||||
error.value = null
|
error.value = null
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
export enum DB_NAMES {
|
export enum DB_NAMES {
|
||||||
QR = 'qr',
|
QR = 'qr',
|
||||||
|
CHATS = 'chats',
|
||||||
MESSAGES = 'messages',
|
MESSAGES = 'messages',
|
||||||
WEB_PUSH_SUBSCRIPTIONS = 'web_push_subscriptions',
|
WEB_PUSH_SUBSCRIPTIONS = 'web_push_subscriptions',
|
||||||
PUBLIC_SETTINGS = 'public_settings',
|
PUBLIC_SETTINGS = 'public_settings',
|
||||||
|
|||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import { type BaseDocument } from "./BaseDocument";
|
||||||
|
import type { Role } from "./Role";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Интерфейс для документа чата.
|
||||||
|
* Используется для хранения чатов, отправленных через QR‑код.
|
||||||
|
*/
|
||||||
|
export interface ChatDocument extends BaseDocument {
|
||||||
|
/** Тип документа, всегда 'chat' */
|
||||||
|
type: "chat";
|
||||||
|
|
||||||
|
/** Идентификатор QR‑кода, к которому относится чат */
|
||||||
|
qr_code_uri: string;
|
||||||
|
|
||||||
|
/** Идентификатор чата */
|
||||||
|
chat: string;
|
||||||
|
|
||||||
|
/** Роль пользователя в чате */
|
||||||
|
role: Role;
|
||||||
|
|
||||||
|
/** Идентификатор пользователя, которому принадлежит чат */
|
||||||
|
user_uuid: string;
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
export * from "./Role";
|
export * from "./Role";
|
||||||
export * from "./BaseDocument";
|
export * from "./BaseDocument";
|
||||||
|
export * from "./ChatDocument";
|
||||||
export * from "./MessageDocument";
|
export * from "./MessageDocument";
|
||||||
export * from "./PublicSettingsDocument";
|
export * from "./PublicSettingsDocument";
|
||||||
export * from "./QRCodeDocument";
|
export * from "./QRCodeDocument";
|
||||||
|
|||||||
@@ -17,6 +17,10 @@ const dbsSecurity = {
|
|||||||
admins: { roles: ["_admin"] },
|
admins: { roles: ["_admin"] },
|
||||||
members: { roles: [] },
|
members: { roles: [] },
|
||||||
},
|
},
|
||||||
|
chats: {
|
||||||
|
admins: { roles: ["_admin"] },
|
||||||
|
members: { roles: [] },
|
||||||
|
},
|
||||||
qr: {
|
qr: {
|
||||||
admins: { roles: ["_admin"] },
|
admins: { roles: ["_admin"] },
|
||||||
members: { roles: [] },
|
members: { roles: [] },
|
||||||
|
|||||||
Reference in New Issue
Block a user