聊天:增加用户信息等字段

This commit is contained in:
liangtongchuan
2021-03-17 18:05:48 +08:00
parent 76aee57e67
commit 94ca8614b4
5 changed files with 43 additions and 8 deletions

View File

@@ -3,6 +3,7 @@ import {Application, BackendSession} from 'pinus';
import { resResult } from '../../../pubUtils/util'; import { resResult } from '../../../pubUtils/util';
import { DEFAULT_MSG_PER_PAGE, STATUS } from '../../../consts'; import { DEFAULT_MSG_PER_PAGE, STATUS } from '../../../consts';
import { createAccuseData, createGroupMsg, createPrivateMsg, getPrivateMessages, pushGroupMsgToRoom, pushMsgToRole, updatePrivateMsgReadInfo } from '../../../services/chatService'; import { createAccuseData, createGroupMsg, createPrivateMsg, getPrivateMessages, pushGroupMsgToRoom, pushMsgToRole, updatePrivateMsgReadInfo } from '../../../services/chatService';
import { getSimpleRoleInfo } from '../../../services/roleService';
export default function(app: Application) { export default function(app: Application) {
@@ -129,7 +130,8 @@ export class ChatHandler {
const roleId = session.get('roleId'); const roleId = session.get('roleId');
const { targetRoleId, fromSeqId = Infinity, count = DEFAULT_MSG_PER_PAGE } = msg; const { targetRoleId, fromSeqId = Infinity, count = DEFAULT_MSG_PER_PAGE } = msg;
const msgs = await getPrivateMessages(roleId, targetRoleId, fromSeqId, count); const msgs = await getPrivateMessages(roleId, targetRoleId, fromSeqId, count);
return resResult(STATUS.SUCCESS, { targetRoleId, msgs }); const targetRoleInfo = await getSimpleRoleInfo(targetRoleId);
return resResult(STATUS.SUCCESS, { targetRoleId, msgs, targetRoleInfo });
} }
/** /**

View File

@@ -3,7 +3,7 @@ import { Application, ChannelService } from 'pinus';
import { resResult } from '../../../pubUtils/util'; import { resResult } from '../../../pubUtils/util';
import { ON_ADD_CHANNEL_ROUTE, ON_GROUP_MSG_ROUTE, ON_LEAVE_CHANNEL_ROUTE, STATUS } from '../../../consts'; import { ON_ADD_CHANNEL_ROUTE, ON_GROUP_MSG_ROUTE, ON_LEAVE_CHANNEL_ROUTE, STATUS } from '../../../consts';
import { PrivateMessageType } from '../../../db/PrivateMessage'; import { PrivateMessageType } from '../../../db/PrivateMessage';
import { addUserToChannel } from '../../../services/roleService'; import { addUserToChannel, getSimpleRoleInfo } from '../../../services/roleService';
import { ChannelUser } from '../../../domain/ChannelUser'; import { ChannelUser } from '../../../domain/ChannelUser';
export default function (app: Application) { export default function (app: Application) {
@@ -151,7 +151,8 @@ export class ChatRemote {
public async sendGroupMsg(roomId: string, msg: Partial<GroupMessageType>) { public async sendGroupMsg(roomId: string, msg: Partial<GroupMessageType>) {
let channel = this.channelService.getChannel(roomId, false); let channel = this.channelService.getChannel(roomId, false);
if (!channel) return; if (!channel) return;
channel.pushMessage(ON_GROUP_MSG_ROUTE, resResult(STATUS.SUCCESS, msg)); const roleInfo = await getSimpleRoleInfo(msg.roleId);
channel.pushMessage(ON_GROUP_MSG_ROUTE, resResult(STATUS.SUCCESS, { ...msg, roleInfo }));
} }
/** /**

View File

@@ -1,5 +1,5 @@
import { PrivateChatRec } from './../db/ChatInfo'; import { PrivateChatRec } from './../db/ChatInfo';
import { RoleModel } from './../db/Role'; import { RoleModel, RoleUpdate } from './../db/Role';
import { GroupMessageModel } from './../db/GroupMessage'; import { GroupMessageModel } from './../db/GroupMessage';
import { CounterModel } from './../db/Counter'; import { CounterModel } from './../db/Counter';
import { STATUS } from './../consts/statusCode'; import { STATUS } from './../consts/statusCode';
@@ -12,6 +12,7 @@ import { getRoleOnlineInfo } from './redisService';
import { ChatInfoModel } from '../db/ChatInfo'; import { ChatInfoModel } from '../db/ChatInfo';
import { channelServer } from './chatChannelService'; import { channelServer } from './chatChannelService';
import { AccuseRecModel, AccueseParam } from '../db/AccuseRec'; import { AccuseRecModel, AccueseParam } from '../db/AccuseRec';
import { getSimpleRoleInfo, getSimpleRoleInfos } from './roleService';
export * from './chatChannelService'; export * from './chatChannelService';
export * from './sysChatService'; export * from './sysChatService';
@@ -127,7 +128,8 @@ export async function pushMsgToRole(msg: PrivateMessageType | GroupMessageType)
const targetRoleId = msg.targetRoleId!; const targetRoleId = msg.targetRoleId!;
const { sid } = await getRoleOnlineInfo(targetRoleId); const { sid } = await getRoleOnlineInfo(targetRoleId);
if (sid) { if (sid) {
pinus.app.get('channelService').pushMessageByUids(ON_PRIVATE_MSG_ROUTE, resResult(STATUS.SUCCESS, msg), [{uid: targetRoleId, sid}]); const roleInfo = await getSimpleRoleInfo(msg.roleId);
pinus.app.get('channelService').pushMessageByUids(ON_PRIVATE_MSG_ROUTE, resResult(STATUS.SUCCESS, { ...msg, roleInfo }), [{ uid: targetRoleId, sid }]);
} }
} }
@@ -141,7 +143,8 @@ export async function pushGroupMsgToRoom(msg: GroupMessageType) {
if (!msg) return; if (!msg) return;
const roomId = msg.roomId!; const roomId = msg.roomId!;
const channelSid = await channelServer(roomId); const channelSid = await channelServer(roomId);
await pinus.app.rpc.chat.chatRemote.sendGroupMsg.toServer(channelSid, roomId, msg); const roleInfo = await getSimpleRoleInfo(msg.roleId);
await pinus.app.rpc.chat.chatRemote.sendGroupMsg.toServer(channelSid, roomId, { ...msg, roleInfo });
} }
/** /**
@@ -158,6 +161,10 @@ export async function getPrivateMessages(roleId: string, targetRoleId: string, f
return result; return result;
} }
function sendFromRole(msg: GroupMessageParam) {
return msg.source === MSG_SOURCE.ROLE_SEND_TEXT;
}
/** /**
* @description 最近群聊消息 * @description 最近群聊消息
* @param {string} roomId 群聊房间标识 * @param {string} roomId 群聊房间标识
@@ -165,7 +172,20 @@ export async function getPrivateMessages(roleId: string, targetRoleId: string, f
* @returns * @returns
*/ */
async function recentGroupMsgs(roomId: string, count?: number) { async function recentGroupMsgs(roomId: string, count?: number) {
const result = await GroupMessageModel.getMsgs(roomId, Infinity, count || RECENT_GROUP_MSGS_CNT); const msgs = await GroupMessageModel.getMsgs(roomId, Infinity, count || RECENT_GROUP_MSGS_CNT);
const roleIds = msgs
.map(msg => { return sendFromRole(msg) ? msg.roleId : null })
.filter(roleId => !!roleId);
const roleInfos = await getSimpleRoleInfos(roleIds);
const result = roleInfos ?
msgs.map(msg => {
for (let roleInfo of roleInfos) {
if (roleInfo.roleId === msg.roleId) {
return { ...msg, roleInfo };
}
}
})
: msgs;
return result || []; return result || [];
} }

View File

@@ -90,3 +90,15 @@ export function addUserToChannel(channel: Channel, user: ChannelUser) {
channel.add(uid, sid); channel.add(uid, sid);
} }
} }
export async function getSimpleRoleInfo(roleId: string) {
if (!roleId) return null;
let role = await RoleModel.findByRoleId(roleId, ROLE_SELECT.SHOW_SIMPLE, true);
return role;
}
export async function getSimpleRoleInfos(roleIds: string[]) {
if (!roleIds || !roleIds.length) return null;
let roles = await RoleModel.findRoleByField('roleId', roleIds, ROLE_SELECT.SHOW_SIMPLE, true);
return roles;
}

View File

@@ -53,7 +53,7 @@ export async function pushGuildNoticeUpdateMsg(roleId: string, roleName: string,
export async function pushGuildUpStructureMsg(roleId: string, roleName: string, guildInfo: Partial<GuildType>) { export async function pushGuildUpStructureMsg(roleId: string, roleName: string, guildInfo: Partial<GuildType>) {
const { code, structure } = guildInfo; const { code, structure } = guildInfo;
if (!code || !structure || !isArray(structure)) return null; if (!code || !structure || !isArray(structure)) return null;
const guild = pick(guildInfo, ['code', 'structure']); const guild = pick(guildInfo, ['code', 'structure', 'name']);
const content = JSON.stringify({ roleId, roleName, guild }); const content = JSON.stringify({ roleId, roleName, guild });
const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.GUILD, code, MSG_TYPE.RICH_TEXT, MSG_SOURCE.GUILD_STRUCTURE_LV_UP, content, null, null); const msgData = await createGroupMsg(roleId, roleName, CHANNEL_PREFIX.GUILD, code, MSG_TYPE.RICH_TEXT, MSG_SOURCE.GUILD_STRUCTURE_LV_UP, content, null, null);
await pushGroupMsgToRoom(msgData); await pushGroupMsgToRoom(msgData);