MCPcopy Create free account
hub / github.com/msgbyte/tailchat / sendMessage

Method sendMessage

server/services/core/chat/message.service.ts:184–272  ·  view source on GitHub ↗

* 发送普通消息

(
    ctx: TcContext<{
      converseId: string;
      groupId?: string;
      content: string;
      plain?: string;
      meta?: object;
    }>
  )

Source from the content-addressed store, hash-verified

182 * 发送普通消息
183 */
184 async sendMessage(
185 ctx: TcContext<{
186 converseId: string;
187 groupId?: string;
188 content: string;
189 plain?: string;
190 meta?: object;
191 }>
192 ) {
193 const { converseId, groupId, content, plain, meta } = ctx.params;
194 const userId = ctx.meta.userId;
195 const t = ctx.meta.t;
196 const isGroupMessage = isValidStr(groupId);
197
198 /**
199 * 鉴权
200 */
201 await this.checkConversePermission(ctx, converseId, groupId); // 鉴权是否能获取到会话内容
202 if (isGroupMessage) {
203 // 是群组消息, 鉴权是否禁言
204 const groupInfo = await call(ctx).getGroupInfo(groupId);
205 const member = groupInfo.members.find((m) => String(m.userId) === userId);
206 if (member) {
207 // 因为有机器人,所以如果没有在成员列表中找到不报错
208 if (new Date(member.muteUntil).valueOf() > new Date().valueOf()) {
209 throw new Error(t('您因为被禁言无法发送消息'));
210 }
211 }
212 }
213
214 const message = await this.adapter.insert({
215 converseId: new Types.ObjectId(converseId),
216 groupId:
217 typeof groupId === 'string' ? new Types.ObjectId(groupId) : undefined,
218 author: new Types.ObjectId(userId),
219 content,
220 meta,
221 });
222
223 const json = await this.transformDocuments(ctx, {}, message);
224
225 if (isGroupMessage) {
226 this.roomcastNotify(ctx, converseId, 'add', json);
227 } else {
228 // 如果是私信的话需要基于用户去推送
229 // 因为用户可能不订阅消息(删除了dmlist)
230 const converseInfo = await call(ctx).getConverseInfo(converseId);
231 if (converseInfo) {
232 const converseMemberIds = converseInfo.members.map((m) => String(m));
233
234 call(ctx)
235 .isUserOnline(converseMemberIds)
236 .then((onlineList) => {
237 _.zip(converseMemberIds, onlineList).forEach(
238 ([memberId, isOnline]) => {
239 if (isOnline) {
240 // 用户在线,则直接推送,通过客户端来创建会话
241 this.unicastNotify(ctx, memberId, 'add', json);

Callers

nothing calls this directly

Calls 9

isValidStrFunction · 0.90
callFunction · 0.85
getGroupInfoMethod · 0.80
findMethod · 0.65
insertMethod · 0.65
emitMethod · 0.65
tFunction · 0.50
callMethod · 0.45

Tested by

no test coverage detected