Skip to content

Commit

Permalink
feat: 实现离线消息
Browse files Browse the repository at this point in the history
  • Loading branch information
leewei0923 committed Nov 28, 2022
1 parent 8d2b69d commit 6a21913
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/ttalk/dto/message.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { IsNotEmpty } from 'class-validator';

export class SaveMessageDto {
@ApiProperty({ example: '' })
@IsNotEmpty()
remote_id: string;

@IsNotEmpty()
user_account: string;

Expand Down
16 changes: 16 additions & 0 deletions src/ttalk/dto/ttalk.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,19 @@ export class PullInBlacklist {
@IsNotEmpty()
blacklist?: boolean;
}

/**
* 查找用户的信息
*/

export class LoadLatestMessageDto {
@ApiProperty()
@IsNotEmpty()
user_account: string; // 申请人账号

@IsNotEmpty()
friend_account: string; // 被申请人账号

@IsNotEmpty()
create_time: string;
}
12 changes: 6 additions & 6 deletions src/ttalk/events.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { SaveMessageDto } from './dto/message.dto';
import { message_record } from './entities/message_record.entity.mysql';
import { offline_events_record } from './entities/offline_events_record.entity.mysql';
import { OfflineEventsName } from './types';
import { nanoid } from 'nanoid';

@WebSocketGateway(3102, { cors: true })
export class EventsGateway {
Expand Down Expand Up @@ -249,6 +248,7 @@ export class EventsGateway {
@SubscribeMessage('messaging')
async handleMessing(client: Socket, payload: SaveMessageDto) {
const {
remote_id,
user_account,
friend_account,
message,
Expand All @@ -257,7 +257,6 @@ export class EventsGateway {
read_flag,
} = payload;
const curDate = dayjs().format('YYYY-MM-DD HH:mm');
const message_id = nanoid();

// 先查询是否在线,在线将信息存入数据库,然后把信息发送给朋友
// 离线状态,保存数据,将信息存入离线记录
Expand All @@ -280,7 +279,7 @@ export class EventsGateway {
mood_state: payload.mood_state,
message_style: payload.message_style,
read_flag: payload.read_flag,
message_id,
message_id: remote_id,
create_time: curDate,
update_time: curDate,
};
Expand All @@ -301,7 +300,7 @@ export class EventsGateway {

// 将信息存入数据库
this.MessageRecordRepository.save({
message_id,
message_id: remote_id,
user_account,
friend_account,
mood_state,
Expand Down Expand Up @@ -344,16 +343,17 @@ export class EventsGateway {
}

// 如果查找到已经存在的消息,更新update否则更新时间
if (typeof res !== 'object') {
if (res === null) {
this.EventRecordRepository.save({
user_account,
friend_account,
event_type: event,
create_time: curDate,
update_time: curDate,
end_flag: false,
});
} else {
const queryCode = `UPDATE offline_events_record SET update_time = ${curDate} where user_account = '${res.user_account}' AND friend_account = '${res.friend_account}' AND id = '${res.id}'`;
const queryCode = `UPDATE offline_events_record SET update_time = '${curDate}' where user_account = '${res.user_account}' AND friend_account = '${res.friend_account}' AND id = '${res.id}'`;
this.EventRecordRepository.query(queryCode);
}
});
Expand Down
19 changes: 19 additions & 0 deletions src/ttalk/ttalk.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
AddFriendDto,
checkOnlineDto,
getAndUpdateDto,
LoadLatestMessageDto,
PullInBlacklist,
} from './dto/ttalk.dto';
import { SaveMessageDto, updateFlagDto } from './dto/message.dto';
Expand Down Expand Up @@ -174,4 +175,22 @@ export class TtalkController {
};
}
}

/**
* 加载事件
*/
@UseGuards(AuthGuard('jwt'))
@Post('/loadLatestEvents')
loadLatestEvents(@Body() data: { account: string }) {
return this.ttalkService.loadLatestEvent(data.account);
}

/**
* 加载最新消息
*/
@UseGuards(AuthGuard('jwt'))
@Post('/loadLatestMessages')
loadLatestMessages(@Body() data: LoadLatestMessageDto) {
return this.ttalkService.loadLatestMessage(data);
}
}
46 changes: 46 additions & 0 deletions src/ttalk/ttalk.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import {
AddFriendDto,
checkOnlineDto,
getAndUpdateDto,
LoadLatestMessageDto,
PullInBlacklist,
} from './dto/ttalk.dto';
import { ttalk_user_concat } from './entities/user_concat.entity.mysql';
import dayjs from 'dayjs';
import { ttalk_online } from './entities/online.entity.mysql';
import { SaveMessageDto, updateFlagDto } from './dto/message.dto';
import { message_record } from './entities/message_record.entity.mysql';
import { offline_events_record } from './entities/offline_events_record.entity.mysql';

@Injectable()
export class TtalkService {
Expand All @@ -29,6 +31,8 @@ export class TtalkService {
private TTalkOnlineRepository: Repository<ttalk_online>,
@Inject('MESSAGE_RECORD_REPOSITORY')
private MessageRecordRepository: Repository<message_record>,
@Inject('OFFLINE_EVENTS_RECORD_REPOSITORY')
private OfflineEventRecordRepository: Repository<offline_events_record>,
) {}

async existUser(account: string): Promise<number> {
Expand Down Expand Up @@ -455,4 +459,46 @@ export class TtalkService {
},
};
}

/**
* 加载最新的事件
*/

async loadLatestEvent(account: string) {
const res = await this.OfflineEventRecordRepository.find({
where: {
friend_account: account,
end_flag: false,
},
});

const queryCode = `UPDATE offline_events_record SET end_flag = true WHERE friend_account = '${account}'`;

this.OfflineEventRecordRepository.query(queryCode);

return {
code: 200,
msg: '加载成功',
status: 'ok',
info: res,
};
}

/**
* 加载最新消息
*/

async loadLatestMessage(data: LoadLatestMessageDto) {
const { user_account, friend_account, create_time } = data;

const queryCode = `SELECT message_id, user_account, friend_account, mood_state, message_style, message, create_time, read_flag FROM message_record WHERE user_account = '${user_account}' AND friend_account = '${friend_account}' AND create_time >= '${create_time}'`;
const res = await this.MessageRecordRepository.query(queryCode);

return {
code: 200,
msg: '加载成功',
status: 'ok',
info: res,
};
}
}

0 comments on commit 6a21913

Please sign in to comment.