refactor: unified messaging inbox content to payload

add compatible code for old message type
pull/90/head
moonrailgun 2 years ago
parent 3fdd036007
commit cf2b4e4d2e

@ -37,9 +37,10 @@ export function createSocket(url: string, token: string): Promise<Socket> {
export function bindSocketEvent(socket: Socket): void { export function bindSocketEvent(socket: Socket): void {
socket.on('notify:chat.inbox.append', (inboxItem: InboxItem) => { socket.on('notify:chat.inbox.append', (inboxItem: InboxItem) => {
if (inboxItem.type === 'message') { if (inboxItem.type === 'message') {
const payload = inboxItem.message ?? inboxItem.payload;
showNotification({ showNotification({
title: inboxItem.message?.converseId ?? '', title: payload.converseId ?? '',
body: inboxItem.message?.messageSnippet ?? '', body: payload.messageSnippet ?? '',
}); });
} }
}); });

@ -144,7 +144,11 @@ export type {
GroupMember, GroupMember,
GroupPanelFeature, GroupPanelFeature,
} from './model/group'; } from './model/group';
export type { InboxItem } from './model/inbox'; export type {
BasicInboxItem,
MessageInboxItem,
InboxItem,
} from './model/inbox';
export { export {
sendMessage, sendMessage,
recallMessage, recallMessage,

@ -3,21 +3,37 @@ import { request } from '../api/request';
/** /**
* *
*/ */
export interface InboxItem { export interface BasicInboxItem {
_id: string; _id: string;
userId: string; userId: string;
readed: boolean; readed: boolean;
type: string;
payload?: object;
createdAt: string;
updatedAt: string;
}
export interface MessageInboxItem extends BasicInboxItem {
type: 'message'; type: 'message';
/**
* @deprecated
*/
message?: { message?: {
groupId?: string; groupId?: string;
converseId: string; converseId: string;
messageId: string; messageId: string;
messageSnippet: string; messageSnippet: string;
}; };
createdAt: string; payload: {
updatedAt: string; groupId?: string;
converseId: string;
messageId: string;
messageSnippet: string;
};
} }
export type InboxItem = MessageInboxItem;
/** /**
* *
*/ */

@ -4,19 +4,24 @@ import { JumpToConverseButton } from '@/components/JumpToButton';
import { LoadingSpinner } from '@/components/LoadingSpinner'; import { LoadingSpinner } from '@/components/LoadingSpinner';
import { Problem } from '@/components/Problem'; import { Problem } from '@/components/Problem';
import React from 'react'; import React from 'react';
import { InboxItem, model, showErrorToasts, useAsync } from 'tailchat-shared'; import {
MessageInboxItem,
model,
showErrorToasts,
useAsync,
} from 'tailchat-shared';
interface Props { interface Props {
info: InboxItem; info: MessageInboxItem;
} }
export const InboxMessageContent: React.FC<Props> = React.memo((props) => { export const InboxMessageContent: React.FC<Props> = React.memo((props) => {
const info = props.info; const info = props.info;
const message = info.message; const payload = info.message ?? info.payload;
if (!message) { if (!payload) {
return <Problem />; return <Problem />;
} }
const { groupId, converseId, messageId } = message; const { groupId, converseId, messageId } = payload;
return ( return (
<div className="w-full relative"> <div className="w-full relative">

@ -33,20 +33,20 @@ export const InboxSidebar: React.FC = React.memo(() => {
const renderInbox = (item: InboxItem) => { const renderInbox = (item: InboxItem) => {
if (item.type === 'message') { if (item.type === 'message') {
const message: Partial<model.inbox.InboxItem['message']> = const payload: Partial<model.inbox.InboxItem['payload']> =
item.message ?? {}; item.message ?? item.payload ?? {};
let title: React.ReactNode = ''; let title: React.ReactNode = '';
if (isValidStr(message.groupId)) { if (isValidStr(payload.groupId)) {
title = <GroupName groupId={message.groupId} />; title = <GroupName groupId={payload.groupId} />;
} else if (isValidStr(message.converseId)) { } else if (isValidStr(payload.converseId)) {
title = <ConverseName converseId={message.converseId} />; title = <ConverseName converseId={payload.converseId} />;
} }
return ( return (
<InboxSidebarItem <InboxSidebarItem
key={item._id} key={item._id}
title={title} title={title}
desc={getMessageRender(message.messageSnippet ?? '')} desc={getMessageRender(payload.messageSnippet ?? '')}
source={'Tailchat'} source={'Tailchat'}
readed={item.readed} readed={item.readed}
to={buildLink(item._id)} to={buildLink(item._id)}

@ -1,18 +1,33 @@
/** /**
* *
*/ */
export interface InboxItem { export interface BasicInboxItem {
_id: string; _id: string;
userId: string; userId: string;
readed: boolean; readed: boolean;
type: 'message'; // will be more type: string;
payload?: Record<string, any>;
createdAt: string;
updatedAt: string;
}
export interface MessageInboxItem extends BasicInboxItem {
type: 'message';
/**
* @deprecated
*/
message?: { message?: {
groupId?: string; groupId?: string;
converseId: string; converseId: string;
messageId: string; messageId: string;
messageSnippet: string; messageSnippet: string;
}; };
payload?: Record<string, any>; payload: {
createdAt: string; groupId?: string;
updatedAt: string; converseId: string;
messageId: string;
messageSnippet: string;
};
} }
export type InboxItem = MessageInboxItem;

@ -55,6 +55,9 @@ export class Inbox extends TimeStamps implements Base {
}) })
type: string; type: string;
/**
* @deprecated please use payload
*/
@prop() @prop()
message?: InboxMessage; message?: InboxMessage;
@ -62,7 +65,7 @@ export class Inbox extends TimeStamps implements Base {
* *
*/ */
@prop() @prop()
payload?: object; payload?: InboxMessage | object;
/** /**
* *

@ -44,7 +44,10 @@ interface InboxMessageStruct {
messageSnippet: string; messageSnippet: string;
} }
export interface InboxStruct { /**
*
*/
export interface BasicInboxItem {
_id: string; _id: string;
userId: string; userId: string;
type: string; type: string;
@ -60,3 +63,24 @@ export interface InboxStruct {
*/ */
readed: boolean; readed: boolean;
} }
export interface MessageInboxItem extends BasicInboxItem {
type: 'message';
/**
* @deprecated
*/
message?: {
groupId?: string;
converseId: string;
messageId: string;
messageSnippet: string;
};
payload: {
groupId?: string;
converseId: string;
messageId: string;
messageSnippet: string;
};
}
export type InboxStruct = MessageInboxItem;

@ -50,7 +50,7 @@ class GetuiService extends TcService {
async (inboxItem: InboxStruct, ctx) => { async (inboxItem: InboxStruct, ctx) => {
if (inboxItem.type === 'message') { if (inboxItem.type === 'message') {
const userId = inboxItem.userId; const userId = inboxItem.userId;
const message = inboxItem.message; const message = inboxItem.payload;
let title = 'new'; let title = 'new';
if (message.groupId) { if (message.groupId) {

@ -32,12 +32,15 @@ class InboxService extends TcService {
if (payload.type === 'add') { if (payload.type === 'add') {
await Promise.all( await Promise.all(
mentions.map((userId) => { mentions.map((userId) => {
return ctx.call('chat.inbox.appendMessage', { return ctx.call('chat.inbox.append', {
userId, userId,
type: 'message',
payload: {
groupId: payload.groupId, groupId: payload.groupId,
converseId: payload.converseId, converseId: payload.converseId,
messageId: payload.messageId, messageId: payload.messageId,
messageSnippet: payload.content, messageSnippet: payload.content,
},
}); });
}) })
); );
@ -65,16 +68,6 @@ class InboxService extends TcService {
payload: 'any', payload: 'any',
}, },
}); });
this.registerAction('appendMessage', this.appendMessage, {
visibility: 'public',
params: {
userId: { type: 'string', optional: true },
groupId: { type: 'string', optional: true },
converseId: 'string',
messageId: 'string',
messageSnippet: 'string',
},
});
this.registerAction('removeMessage', this.removeMessage, { this.registerAction('removeMessage', this.removeMessage, {
visibility: 'public', visibility: 'public',
params: { params: {
@ -120,45 +113,6 @@ class InboxService extends TcService {
return true; return true;
} }
/**
*
*/
async appendMessage(
ctx: TcContext<{
userId?: string;
groupId?: string;
converseId: string;
messageId: string;
messageSnippet: string;
}>
) {
const {
userId = ctx.meta.userId,
groupId,
converseId,
messageId,
messageSnippet,
} = ctx.params;
const doc = await this.adapter.model.create({
userId,
type: 'message',
message: {
groupId,
converseId,
messageId,
messageSnippet,
},
});
const inboxItem = await this.transformDocuments(ctx, {}, doc);
await this.notifyUsersInboxAppend(ctx, [userId], inboxItem);
await this.emitInboxAppendEvent(ctx, inboxItem);
return true;
}
async removeMessage( async removeMessage(
ctx: TcContext<{ ctx: TcContext<{
userId?: string; userId?: string;
@ -177,7 +131,7 @@ class InboxService extends TcService {
await this.adapter.model.remove({ await this.adapter.model.remove({
userId, userId,
type: 'message', type: 'message',
message: { payload: {
groupId, groupId,
converseId, converseId,
messageId, messageId,

Loading…
Cancel
Save