better gameinvite

This commit is contained in:
Robin Appelman 2020-12-25 22:27:30 +01:00
commit 3656419a80
4 changed files with 301 additions and 35 deletions

View file

@ -5,6 +5,7 @@ import UPNG from "@pdf-lib/upng";
const GIFEncoder = require("gif-encoder");
import {WritableStreamBuffer} from 'stream-buffers';
import {Util} from "mx-puppet-bridge";
import * as SteamID from "steamid";
function isBBCode(field: BBCodeField): field is BBCodeNode {
return field['tag'] !== undefined;
@ -45,8 +46,7 @@ async function apngToGif(sourceUrl: string): Promise<Buffer> {
return output.getContents();
}
async function formatBBCode(steam: Steam, puppetId: number, node: BBCodeNode): Promise<ImageMessage | TextMessage> {
async function formatBBCode(steam: Steam, puppetId: number, node: BBCodeNode, fromSteamId?: SteamID): Promise<ImageMessage | TextMessage> {
if (node.tag === 'img') {
return {
kind: "image",
@ -69,22 +69,45 @@ async function formatBBCode(steam: Steam, puppetId: number, node: BBCodeNode): P
kind: "image",
urlOrBuffer: gif
};
} else if (node.tag === 'gameinvite') {
let game = await steam.getProduct(puppetId, node.attrs['appid']);
if (steam.getSteamId(puppetId) === fromSteamId) {
return {
kind: "text",
body: "",
};
}
return {
kind: "text",
body: `You were invited to play ${game.appinfo.common.name}`
};
} else {
return {kind: "text", body: `[${node.tag}]`};
}
}
export async function exportMessageForSending(steam: Steam, puppetId: number, message: IIncomingFriendMessage | IIncomingChatMessage): Promise<(TextMessage | ImageMessage)[]> {
export async function exportMessageForSending(
steam: Steam,
puppetId: number,
message: IIncomingFriendMessage | IIncomingChatMessage,
fromSteamId?: SteamID
): Promise<(TextMessage | ImageMessage)[]> {
if (message.message_bbcode_parsed) {
let parts = await Promise.all(message.message_bbcode_parsed.map(node => {
if (isBBCode(node)) {
return formatBBCode(steam, puppetId, node);
return formatBBCode(steam, puppetId, node, message['steamid_friend']);
} else {
return {kind: "text", body: node} as TextMessage;
}
}));
return parts.reduce((merged, part) => {
if (part.kind === "text" && part.body === "") {
return merged;
}
if (merged.length === 0) {
merged.push(part);
} else {

View file

@ -74,7 +74,8 @@ export class Steam {
}
}
async getProduct(p: ISteamPuppet, appId: string): Promise<AppInfo> {
public async getProduct(puppetId: number, appId: string): Promise<AppInfo> {
const p = this.puppets[puppetId];
let app = p.knownApps.get(appId);
if (app) {
return app;
@ -140,6 +141,10 @@ export class Steam {
}
}
public getSteamId(puppetId: number): SteamID | null {
return this.puppets[puppetId].client.steamID;
}
public async newPuppet(puppetId: number, data: IPuppetParams) {
log.info(`Adding new Puppet: puppetId=${puppetId}`);
if (this.puppets[puppetId]) {
@ -291,12 +296,17 @@ export class Steam {
const p = this.puppets[puppetId];
log.verbose("Got chat message from steam to pass on");
let sendParams = await this.getChatMessageSendParams(puppetId, message, fromSteamId);
let sendParams = await this.getChatMessageSendParams(puppetId, message);
await this.sendMessage(p, puppetId, sendParams, message);
}
public async sendMessage(puppet: ISteamPuppet, puppetId: number, sendParams: IReceiveParams, incoming: IIncomingFriendMessage | IIncomingChatMessage) {
public async sendMessage(
puppet: ISteamPuppet,
puppetId: number,
sendParams: IReceiveParams,
incoming: IIncomingFriendMessage | IIncomingChatMessage
) {
const parts = await exportMessageForSending(this, puppetId, incoming);
for (let part of parts) {