Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for forwarding chathistory message. #256

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
const rules = {
semi: ["error", "always"],
quotes: ["error", "double"],
'space-before-function-paren': ["error", "never"],
"space-before-function-paren": ["error", {
"anonymous": "never",
"named": "never",
"asyncArrow": "always"
}],
'array-bracket-spacing': ["error", "never"]
}

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wechaty-puppet-padlocal",
"version": "1.20.1",
"version": "1.20.2",
"description": "Puppet PadLocal for Wechaty",
"type": "module",
"exports": {
Expand Down
2 changes: 1 addition & 1 deletion src/cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ nodeCleanup((exitCode, signal) => {
const puppets = RunningPuppets.slice();

Promise.all(
puppets.map(async(puppet) => {
puppets.map(async (puppet) => {
await puppet.stop();
}),
).finally(() => {
Expand Down
4 changes: 2 additions & 2 deletions src/padlocal/events/event-friendship.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const isNeedVerify = (message: PadLocal.Message.AsObject): boolean => {
});
};

const isReceive = async(message: PadLocal.Message.AsObject): Promise<ReceiveXmlSchema | null> => {
const isReceive = async (message: PadLocal.Message.AsObject): Promise<ReceiveXmlSchema | null> => {
if (message.type !== WechatMessageType.VerifyMsg && message.type !== WechatMessageType.VerifyMsgEnterprise) {
return null;
}
Expand All @@ -67,7 +67,7 @@ const isReceive = async(message: PadLocal.Message.AsObject): Promise<ReceiveXmlS
return null;
};

export default async(_puppet: PUPPET.Puppet, message: PadLocal.Message.AsObject): Promise<EventPayload> => {
export default async (_puppet: PUPPET.Puppet, message: PadLocal.Message.AsObject): Promise<EventPayload> => {
if (isConfirm(message)) {
return {
contactId: message.fromusername,
Expand Down
2 changes: 1 addition & 1 deletion src/padlocal/events/event-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import type * as PUPPET from "wechaty-puppet";
import type PadLocal from "padlocal-client-ts/dist/proto/padlocal_pb.js";
import type { EventPayload } from "./event.js";

export default async(_puppet: PUPPET.Puppet, message: PadLocal.Message.AsObject): Promise<EventPayload> => {
export default async (_puppet: PUPPET.Puppet, message: PadLocal.Message.AsObject): Promise<EventPayload> => {
return message;
};
2 changes: 1 addition & 1 deletion src/padlocal/events/event-room-invite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const ROOM_OTHER_INVITE_TITLE_EN = [/Group Chat Invitation/];
const ROOM_OTHER_INVITE_LIST_ZH = [/^"(.+)"邀请你加入群聊(.*),进入可查看详情。/];
const ROOM_OTHER_INVITE_LIST_EN = [/"(.+)" invited you to join the group chat "(.+)"\. Enter to view details\./];

export default async(_puppet: PUPPET.Puppet, message: PadLocal.Message.AsObject): Promise<EventPayload> => {
export default async (_puppet: PUPPET.Puppet, message: PadLocal.Message.AsObject): Promise<EventPayload> => {
let appMsgPayload: AppMessagePayload;
try {
appMsgPayload = await parseAppmsgMessagePayload(message.content);
Expand Down
10 changes: 5 additions & 5 deletions src/padlocal/events/event-room-join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const OTHER_JOIN_VIA_OTHER_QRCODE_REGEX_LIST = [
/^"(.+)" joined the group chat via the QR Code shared by "(.+)"/,
];

export default async(puppet: PUPPET.Puppet, message: PadLocal.Message.AsObject): Promise<EventPayload> => {
export default async (puppet: PUPPET.Puppet, message: PadLocal.Message.AsObject): Promise<EventPayload> => {
const roomId = message.fromusername;
if (!isRoomId(roomId)) {
return null;
Expand All @@ -57,7 +57,7 @@ export default async(puppet: PUPPET.Puppet, message: PadLocal.Message.AsObject):
const youInviteOther = createSysmsgTemplateRunner<PUPPET.payloads.EventRoomJoin>(
sysmsgTemplatePayload,
[...YOU_INVITE_OTHER_REGEX_LIST, ...OTHER_JOIN_VIA_YOUR_QRCODE_REGEX_LIST],
async(templateLinkList) => {
async (templateLinkList) => {
// the first item MUST be others profile link
const inviteeList = templateLinkList[0]!.payload as SysmsgTemplateLinkProfile;
// filter other empty userName, in case the user is not your friend
Expand All @@ -77,7 +77,7 @@ export default async(puppet: PUPPET.Puppet, message: PadLocal.Message.AsObject):
const otherInviteYou = createSysmsgTemplateRunner<PUPPET.payloads.EventRoomJoin>(
sysmsgTemplatePayload,
OTHER_INVITE_YOU_REGEX_LIST,
async(templateLinkList) => {
async (templateLinkList) => {
// the first must invitor
const inviter = templateLinkList[0]!.payload as SysmsgTemplateLinkProfile;

Expand All @@ -97,7 +97,7 @@ export default async(puppet: PUPPET.Puppet, message: PadLocal.Message.AsObject):
const otherInviteOther = createSysmsgTemplateRunner<PUPPET.payloads.EventRoomJoin>(
sysmsgTemplatePayload,
[...OTHER_INVITE_YOU_AND_OTHER_REGEX_LIST, ...OTHER_INVITE_OTHER_REGEX_LIST],
async(templateLinkList, matchedRegexIndex) => {
async (templateLinkList, matchedRegexIndex) => {
// the first item is invitor
const inviter = templateLinkList[0]!.payload as SysmsgTemplateLinkProfile;

Expand Down Expand Up @@ -126,7 +126,7 @@ export default async(puppet: PUPPET.Puppet, message: PadLocal.Message.AsObject):
const otherJoinViaQrCode = createSysmsgTemplateRunner<PUPPET.payloads.EventRoomJoin>(
sysmsgTemplatePayload,
OTHER_JOIN_VIA_OTHER_QRCODE_REGEX_LIST,
async(templateLinkList) => {
async (templateLinkList) => {
// the first item is invitee
const inviteeList = templateLinkList[0]!.payload as SysmsgTemplateLinkProfile;
// filter other empty userName, in case the user is not your friend
Expand Down
8 changes: 4 additions & 4 deletions src/padlocal/events/event-room-leave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export function isRoomLeaveDebouncing(roomId: string, removeeId: string): boolea
return roomLeaveDebounceMap.get(key) !== undefined;
}

export default async(puppet: PUPPET.Puppet, message: PadLocal.Message.AsObject): Promise<EventPayload> => {
export default async (puppet: PUPPET.Puppet, message: PadLocal.Message.AsObject): Promise<EventPayload> => {
const roomId = message.fromusername;
if (!isRoomId(roomId)) {
return null;
Expand All @@ -60,7 +60,7 @@ export default async(puppet: PUPPET.Puppet, message: PadLocal.Message.AsObject):
* /^(你)将"(.+)"移出了群聊/,
* 我移除别人是 10002: https://gist.github.com/padlocal/5676b96ad0ca918fdd53849417eff422
*/
const youRemoveOther = async() => {
const youRemoveOther = async () => {
const sysmsgTemplatePayload = await parseSysmsgSysmsgTemplateMessagePayload(message);
if (!sysmsgTemplatePayload) {
return null;
Expand All @@ -69,7 +69,7 @@ export default async(puppet: PUPPET.Puppet, message: PadLocal.Message.AsObject):
return await parseSysmsgTemplate<PUPPET.payloads.EventRoomLeave>(
sysmsgTemplatePayload,
YOU_REMOVE_OTHER_REGEX_LIST,
async(templateLinkList) => {
async (templateLinkList) => {
// the first item MUST be removed profile link
const removeeList = templateLinkList[0]!.payload as SysmsgTemplateLinkProfile;
// filter other empty userName, in case the user is not your friend
Expand All @@ -88,7 +88,7 @@ export default async(puppet: PUPPET.Puppet, message: PadLocal.Message.AsObject):
* /^(你)被"([^"]+?)"移出群聊/,
* // 我被别人移除是 10000:https://gist.github.com/padlocal/60be89334d4d743937f07023da20291e
*/
const otherRemoveYou = async() => {
const otherRemoveYou = async () => {
if (message.type !== WechatMessageType.Sys) {
return null;
}
Expand Down
12 changes: 6 additions & 6 deletions src/padlocal/events/event-room-topic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ const YOU_CHANGE_TOPIC_REGEX_LIST = [
/^(You) changed the group name to "(.+)"$/,
];

type TopicChange = {changerId: string, newTopic: string};
type TopicChange = { changerId: string, newTopic: string };

export default async(puppet: PUPPET.Puppet, message: PadLocal.Message.AsObject): Promise<EventPayload> => {
export default async (puppet: PUPPET.Puppet, message: PadLocal.Message.AsObject): Promise<EventPayload> => {
const roomId = message.fromusername;
if (!isRoomId(roomId)) {
return null;
Expand All @@ -28,12 +28,12 @@ export default async(puppet: PUPPET.Puppet, message: PadLocal.Message.AsObject):
/**
* 1. Message payload "you change the room topic" is plain text with type 10000 : https://gist.github.com/padlocal/0c7bb4f5d51e7e94a0efa108bebb4645
*/
const youChangeTopic = async() => {
const youChangeTopic = async () => {
if (message.type !== WechatMessageType.Sys) {
return null;
}

return parseTextWithRegexList(message.content, YOU_CHANGE_TOPIC_REGEX_LIST, async(_, match) => {
return parseTextWithRegexList(message.content, YOU_CHANGE_TOPIC_REGEX_LIST, async (_, match) => {
const newTopic = match[2];

return {
Expand All @@ -46,7 +46,7 @@ export default async(puppet: PUPPET.Puppet, message: PadLocal.Message.AsObject):
/**
* 2. Message payload "others change room topic" is xml text with type 10002: https://gist.github.com/padlocal/3480ada677839c8c11578d47e820e893
*/
const otherChangeTopic = async() => {
const otherChangeTopic = async () => {
const sysmsgTemplatePayload = await parseSysmsgSysmsgTemplateMessagePayload(message);
if (!sysmsgTemplatePayload) {
return null;
Expand All @@ -55,7 +55,7 @@ export default async(puppet: PUPPET.Puppet, message: PadLocal.Message.AsObject):
return parseSysmsgTemplate<TopicChange>(
sysmsgTemplatePayload,
OTHER_CHANGE_TOPIC_REGEX_LIST,
async(templateLinkList) => {
async (templateLinkList) => {
// the first item MUST be changers profile link
const changerList = templateLinkList[0]!.payload as SysmsgTemplateLinkProfile;
const changerId = changerList[0]!.userName;
Expand Down
8 changes: 4 additions & 4 deletions src/padlocal/messages/sysmsg/message-revokemsg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ const OTHER_REVOKE_REGEX_LIST = [
export async function parseRevokeMsgMessagePayload(revokeMsgXmlSchema: RevokeMsgXmlSchema): Promise<RevokeMsgMessagePayload> {
let nickName: string | undefined;

const youRevoke = async() => parseTextWithRegexList<RevokeMsgType>(revokeMsgXmlSchema.replacemsg, YOU_REVOKE_REGEX_LIST, async() => "You");
const otherRevoke = async() => parseTextWithRegexList<RevokeMsgType>(revokeMsgXmlSchema.replacemsg, OTHER_REVOKE_REGEX_LIST, async(_, match) => {
const youRevoke = async () => parseTextWithRegexList<RevokeMsgType>(revokeMsgXmlSchema.replacemsg, YOU_REVOKE_REGEX_LIST, async () => "You");
const otherRevoke = async () => parseTextWithRegexList<RevokeMsgType>(revokeMsgXmlSchema.replacemsg, OTHER_REVOKE_REGEX_LIST, async (_, match) => {
nickName = match[1];
return "Other";
});
Expand All @@ -49,7 +49,7 @@ export async function parseRevokeMsgMessagePayload(revokeMsgXmlSchema: RevokeMsg
};
}

export async function getRevokeOriginalMessage(puppet: PUPPET.Puppet, revokemsgPayload:RevokeMsgMessagePayload): Promise<PUPPET.payloads.Message | null> {
export async function getRevokeOriginalMessage(puppet: PUPPET.Puppet, revokemsgPayload: RevokeMsgMessagePayload): Promise<PUPPET.payloads.Message | null> {
const messageIdList = await puppet.messageSearch({ id: revokemsgPayload.originalMessageId });
if (messageIdList.length) {
return puppet.messagePayload(messageIdList[0]!);
Expand All @@ -58,7 +58,7 @@ export async function getRevokeOriginalMessage(puppet: PUPPET.Puppet, revokemsgP
return null;
}

export async function getRevokeOperatorIdForRoomMessage(puppet: PUPPET.Puppet, revokemsgPayload:RevokeMsgMessagePayload) : Promise<string | null> {
export async function getRevokeOperatorIdForRoomMessage(puppet: PUPPET.Puppet, revokemsgPayload: RevokeMsgMessagePayload): Promise<string | null> {
if (isRoomId(revokemsgPayload.session) || isIMRoomId(revokemsgPayload.session)) {
const contactIdList = await puppet.roomMemberSearch(revokemsgPayload.session, revokemsgPayload.operatorNickName!);
if (contactIdList.length) {
Expand Down
8 changes: 4 additions & 4 deletions src/padlocal/messages/sysmsg/message-sysmsgtemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function toList(list: any): any[] {
export async function parseSysmsgTemplateMessagePayload(sysmsgTemplateXml: SysmsgTemplateXmlSchema): Promise<SysmsgTemplateMessagePayload> {
const linkList = toList(sysmsgTemplateXml.content_template.link_list.link);

const allLinkList = linkList.map((link): SysmsgTemplateLink => {
const allLinkList = linkList.map((link): SysmsgTemplateLink => {
const type = link.$.type as SysmsgTemplateLinkType;
let payload: SysmsgTemplateLinkPayload | undefined;

Expand Down Expand Up @@ -122,12 +122,12 @@ export async function parseSysmsgTemplateMessagePayload(sysmsgTemplateXml: Sysms

export type SysmsgTemplateHandler<T> = (templateLinkList: SysmsgTemplateLink[], matchedRegexIndex: number) => Promise<T>;

export async function parseSysmsgTemplate<T>(sysmsgTemplatePayload: SysmsgTemplateMessagePayload, regexList: RegExp[], handler: SysmsgTemplateHandler<T>) : Promise<T | null> {
return parseTextWithRegexList(sysmsgTemplatePayload.template, regexList, async(matchedRegexIndex) => {
export async function parseSysmsgTemplate<T>(sysmsgTemplatePayload: SysmsgTemplateMessagePayload, regexList: RegExp[], handler: SysmsgTemplateHandler<T>): Promise<T | null> {
return parseTextWithRegexList(sysmsgTemplatePayload.template, regexList, async (matchedRegexIndex) => {
return handler(sysmsgTemplatePayload.templateLinkList, matchedRegexIndex);
});
}

export function createSysmsgTemplateRunner<T>(sysmsgTemplatePayload: SysmsgTemplateMessagePayload, regexList: RegExp[], handler: SysmsgTemplateHandler<T>): Runner<T> {
return async() => parseSysmsgTemplate<T>(sysmsgTemplatePayload, regexList, handler);
return async () => parseSysmsgTemplate<T>(sysmsgTemplatePayload, regexList, handler);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { log } from "wechaty-puppet";
import { LOGPRE, MessageParser, MessageParserContext } from "./message-parser.js";
import { AppMessageType, parseAppmsgMessagePayload } from "../../messages/message-appmsg.js";

export const appMsgParser: MessageParser = async(padLocalMessage: PadLocal.Message.AsObject, ret: PUPPET.payloads.Message, context: MessageParserContext) => {
export const appMsgParser: MessageParser = async (padLocalMessage: PadLocal.Message.AsObject, ret: PUPPET.payloads.Message, context: MessageParserContext) => {
if (ret.type !== PUPPET.types.Message.Attachment) {
return ret;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AppMessageType, parseAppmsgMessagePayload, ReferMsgPayload } from "../.
import { WechatMessageType } from "../../types.js";
import type { MessageParser, MessageParserContext } from "./message-parser.js";

export const referMsgParser: MessageParser = async(_padLocalMessage: PadLocal.Message.AsObject, ret: PUPPET.payloads.Message, context: MessageParserContext) => {
export const referMsgParser: MessageParser = async (_padLocalMessage: PadLocal.Message.AsObject, ret: PUPPET.payloads.Message, context: MessageParserContext) => {
if (!context.appMessagePayload || context.appMessagePayload.type !== AppMessageType.ReferMsg) {
return ret;
}
Expand Down
2 changes: 1 addition & 1 deletion src/padlocal/schema-mapper/message/message-parser-room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ async function roomMessageSentBySelf(padLocalMessage: PadLocal.Message.AsObject,
* @param ret
* @param context
*/
export const roomParser: MessageParser = async(padLocalMessage: PadLocal.Message.AsObject, ret: PUPPET.payloads.Message, context: MessageParserContext) => {
export const roomParser: MessageParser = async (padLocalMessage: PadLocal.Message.AsObject, ret: PUPPET.payloads.Message, context: MessageParserContext) => {
await roomMessageSentByOthers(padLocalMessage, ret);
await roomMessageSentBySelf(padLocalMessage, ret);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type * as PUPPET from "wechaty-puppet";
import type PadLocal from "padlocal-client-ts/dist/proto/padlocal_pb.js";
import type { MessageParser, MessageParserContext } from "./message-parser";

export const singleChatParser: MessageParser = async(padLocalMessage: PadLocal.Message.AsObject, ret: PUPPET.payloads.Message, context: MessageParserContext) => {
export const singleChatParser: MessageParser = async (padLocalMessage: PadLocal.Message.AsObject, ret: PUPPET.payloads.Message, context: MessageParserContext) => {
if (!context.isRoomMessage) {
ret.talkerId = padLocalMessage.fromusername;
ret.listenerId = padLocalMessage.tousername;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { RevokeMsgMessagePayload } from "../../messages/sysmsg/message-revo
* @param ret
* @param context
*/
export const sysmsgParser: MessageParser = async(padLocalMessage: PadLocal.Message.AsObject, ret: PUPPET.payloads.Message, context: MessageParserContext) => {
export const sysmsgParser: MessageParser = async (padLocalMessage: PadLocal.Message.AsObject, ret: PUPPET.payloads.Message, context: MessageParserContext) => {
const sysmsgPayload = await parseSysmsgMessagePayload(padLocalMessage);
if (!sysmsgPayload) {
return ret;
Expand Down
2 changes: 1 addition & 1 deletion src/padlocal/schema-mapper/message/message-parser-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const TypeMappings: { [key: number]: PUPPET.types.Message; } = {
[WechatMessageType.SysNotice]: PUPPET.types.Message.Unknown,
};

export const typeParser: MessageParser = async(padLocalMessage: PadLocal.Message.AsObject, ret: PUPPET.payloads.Message, _context: MessageParserContext) => {
export const typeParser: MessageParser = async (padLocalMessage: PadLocal.Message.AsObject, ret: PUPPET.payloads.Message, _context: MessageParserContext) => {
const wechatMessageType = padLocalMessage.type as WechatMessageType;
let type: PUPPET.types.Message | undefined = TypeMappings[wechatMessageType];

Expand Down
Loading