Skip to content

Commit

Permalink
route client user to clientuser
Browse files Browse the repository at this point in the history
  • Loading branch information
storycraft committed May 1, 2020
1 parent c65010e commit 76e42fa
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/packet/loco-bson-packet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export abstract class LocoBsonRequestPacket implements LocoRequestPacket {
}

submitResponseTicket<T extends LocoResponsePacket>(): Promise<T> {
let promise = new Promise<T>((resolve, reject) => this.resolveList.push(resolve));
let promise = new Promise<T>((resolve, reject) => { this.resolveList.push(resolve) });
return promise;
}

Expand Down
6 changes: 2 additions & 4 deletions src/talk/chat/chat-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { JsonUtil } from "../../util/json-util";
export namespace ChatBuilder {
export type BuiltMessage = {
'text': string,
'extra': string
'extra': any
};

export function buildMessage(...textFormat: (string | ChatContent)[]): BuiltMessage {
Expand Down Expand Up @@ -77,11 +77,9 @@ export namespace ChatBuilder {
extra['mentions'] = mentions;
}

let extraText = JsonUtil.stringifyLoseless(extra);

return {
'text': text,
'extra': extraText
'extra': extra
}
}

Expand Down
24 changes: 17 additions & 7 deletions src/talk/chat/template/message-template.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { MessageType } from "../message-type";
import { ChatAttachment, SharpAttachment, EmoticonAttachment } from "../attachment/chat-attachment";
import { ChatAttachment, SharpAttachment, EmoticonAttachment, ChatContent } from "../attachment/chat-attachment";
import { JsonUtil } from "../../../util/json-util";
import { ChatBuilder } from "../chat-builder";

/*
* Created on Fri Jan 03 2020
Expand All @@ -21,11 +22,17 @@ export interface MessageTemplate {

export class AttachmentTemplate implements MessageTemplate {

private packetText: string;
private textExtra: any;

constructor(
private attachment: ChatAttachment,
private text: string = '' //required for some attachment
...textFormat: (string | ChatContent)[]
) {
let msg = ChatBuilder.buildMessage(...textFormat);

this.packetText = msg.text;
this.textExtra = msg.extra;
}

get Attachment() {
Expand All @@ -37,11 +44,14 @@ export class AttachmentTemplate implements MessageTemplate {
}

get Text() {
return this.text;
return this.packetText;
}

set Text(text) {
this.text = text;
setText(...textFormat: (string | ChatContent)[]) {
let msg = ChatBuilder.buildMessage(...textFormat);

this.packetText = msg.text;
this.textExtra = msg.extra;
}

get Valid() {
Expand All @@ -53,11 +63,11 @@ export class AttachmentTemplate implements MessageTemplate {
}

getPacketText() {
return this.text;
return this.packetText;
}

getPacketExtra() {
return JsonUtil.stringifyLoseless(this.attachment.toJsonAttachment());
return JsonUtil.stringifyLoseless({ ...this.textExtra, ...this.attachment.toJsonAttachment() });
}

}
Expand Down
17 changes: 15 additions & 2 deletions src/talk/room/chat-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { ChatBuilder } from "../chat/chat-builder";
import { PacketMessageNotiReadReq } from "../../packet/loco-noti-read";
import { ChatFeed } from "../chat/chat-feed";
import { PacketLeaveReq, PacketLeaveRes } from "../../packet/packet-leave";
import { JsonUtil } from "../../util/json-util";

/*
* Created on Fri Nov 01 2019
Expand Down Expand Up @@ -94,12 +95,14 @@ export class ChatChannel extends EventEmitter {

async sendText(...textFormat: (string | ChatContent)[]): Promise<Chat> {
let { text, extra } = ChatBuilder.buildMessage(...textFormat);

let extraText = JsonUtil.stringifyLoseless(extra);

let userId = this.sessionManager.ClientUser.UserId;

let res = await this.sessionManager.Client.NetworkManager.requestPacketRes<PacketMessageWriteRes>(new PacketMessageWriteReq(this.sessionManager.getNextMessageId(), this.channelId, text, MessageType.Text, false, extra));
let res = await this.sessionManager.Client.NetworkManager.requestPacketRes<PacketMessageWriteRes>(new PacketMessageWriteReq(this.sessionManager.getNextMessageId(), this.channelId, text, MessageType.Text, false, extraText));

let chat = this.sessionManager.chatFromChatlog(new ChatlogStruct(res.LogId, res.PrevLogId, userId, this.channelId, MessageType.Text, text, Math.floor(Date.now() / 1000), extra, res.MessageId));
let chat = this.sessionManager.chatFromChatlog(new ChatlogStruct(res.LogId, res.PrevLogId, userId, this.channelId, MessageType.Text, text, Math.floor(Date.now() / 1000), extraText, res.MessageId));

return chat;
}
Expand Down Expand Up @@ -423,6 +426,9 @@ export class ChannelInfo {
async updateInfo(): Promise<void> {
if (this.pendingInfoReq) return this.pendingInfoReq;

let resolver: () => void | null;
this.pendingInfoReq = new Promise((resolve, reject) => resolver = resolve);

let networkManager = this.channel.Client.NetworkManager;

let info = await networkManager.requestChannelInfo(this.channel.ChannelId);
Expand All @@ -432,6 +438,8 @@ export class ChannelInfo {
this.updateFromStruct(info, this.openLinkInfo);

await this.updateOpenInfo();

resolver!();
}

async updateOpenInfo(): Promise<void> {
Expand All @@ -447,12 +455,17 @@ export class ChannelInfo {
protected async updateMemberInfo(chatInfo: ChatInfoStruct): Promise<void> {
if (this.pendingUserInfoReq) return this.pendingUserInfoReq;

let resolver: () => void | null;
this.pendingUserInfoReq = new Promise((resolve, reject) => resolver = resolve);

let networkManager = this.channel.Client.NetworkManager;

let infoList = await networkManager.requestMemberInfo(this.channel.ChannelId);
let activeInfoList = await networkManager.requestSpecificMemberInfo(this.channel.ChannelId, chatInfo.MemberList.map((item) => item.UserId));

this.initMemberList(infoList.slice().concat(activeInfoList));

resolver!();
}

}
4 changes: 3 additions & 1 deletion src/talk/session/session-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ export class SessionManager {
}

getUser(id: Long): ChatUser {
if (this.clientUser.UserId.equals(id)) return this.clientUser;

if (this.hasUser(id)) return this.userMap.get(id.toString())!;

let user = new ChatUser(id);
Expand All @@ -83,7 +85,7 @@ export class SessionManager {
}

hasUser(id: Long) {
return this.userMap.has(id.toString());
return this.userMap.has(id.toString()) || this.clientUser.UserId.equals(id);
}

async getUserFrom(chatChannel: ChatChannel, id: Long): Promise<ChatUser | null> {
Expand Down
15 changes: 15 additions & 0 deletions src/talk/user/chat-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Long } from "bson";
import { MemberStruct } from "../struct/member-struct";
import { ClientSettingsStruct } from "../struct/client-settings-struct";
import { OpenLinkStruct, OpenMemberStruct } from "../struct/open-link-struct";
import { UserType } from "./user-type";

/*
* Created on Fri Nov 01 2019
Expand Down Expand Up @@ -77,6 +78,8 @@ export class UserInfo implements ChatUserInfoBase {

private lastInfoCache: number;

private userType: UserType;

constructor() {
this.infoLoaded = false;
this.accountId = 0;
Expand All @@ -87,6 +90,7 @@ export class UserInfo implements ChatUserInfoBase {
this.originalProfileImageURL = '';

this.lastInfoCache = -1;
this.userType = UserType.Undefined;
}

get AccountId() {
Expand Down Expand Up @@ -125,7 +129,16 @@ export class UserInfo implements ChatUserInfoBase {
return this.openChatToken;
}

get UserType() {
return this.userType;
}

isOpenUser(): boolean {
if (this.openChatToken) return true;
return false;
}

hasOpenProfile(): boolean {
if (this.profileLinkId) return true;
return false;
}
Expand Down Expand Up @@ -156,6 +169,8 @@ export class UserInfo implements ChatUserInfoBase {
if (memberStruct.ProfileLinkId !== Long.ZERO) {
this.profileLinkId = memberStruct.ProfileLinkId;
}

this.userType = memberStruct.Type;
}

}
Expand Down
1 change: 1 addition & 0 deletions src/talk/user/user-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export enum UserType {
NotFriend = -100,
Deactivated = 9,
Friend = 100,
OpenProfile = 1000,

}

0 comments on commit 76e42fa

Please sign in to comment.