Skip to content

Commit

Permalink
style: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
MliKiowa committed Jun 21, 2024
1 parent 685dc74 commit 114c98f
Show file tree
Hide file tree
Showing 17 changed files with 159 additions and 159 deletions.
80 changes: 40 additions & 40 deletions src/common/utils/MessageUnique.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
import crypto from 'crypto';

class LimitedHashTable<K, V> {
private keyToValue: Map<K, V> = new Map();
private valueToKey: Map<V, K> = new Map();
private maxSize: number;
private KeyQueneList: K[] = [];
private ValueQueneList: V[] = [];
constructor(maxSize: number) {
this.maxSize = maxSize;
}
set(key: K, value: V): void {
this.keyToValue.set(key, value);
this.valueToKey.set(value, key);
if (this.KeyQueneList.length >= this.maxSize || this.ValueQueneList.length >= this.maxSize) {
this.KeyQueneList.shift();
this.ValueQueneList.shift();
}
}
private keyToValue: Map<K, V> = new Map();
private valueToKey: Map<V, K> = new Map();
private maxSize: number;
private KeyQueneList: K[] = [];
private ValueQueneList: V[] = [];
constructor(maxSize: number) {
this.maxSize = maxSize;
}
set(key: K, value: V): void {
this.keyToValue.set(key, value);
this.valueToKey.set(value, key);
if (this.KeyQueneList.length >= this.maxSize || this.ValueQueneList.length >= this.maxSize) {
this.KeyQueneList.shift();
this.ValueQueneList.shift();
}
}

getValue(key: K): V | undefined {
return this.keyToValue.get(key);
}
getValue(key: K): V | undefined {
return this.keyToValue.get(key);
}

getKey(value: V): K | undefined {
return this.valueToKey.get(value);
}
getKey(value: V): K | undefined {
return this.valueToKey.get(value);
}

delete(key: K): void {
const value = this.keyToValue.get(key);
if (value !== undefined) {
this.keyToValue.delete(key);
this.valueToKey.delete(value);
}
delete(key: K): void {
const value = this.keyToValue.get(key);
if (value !== undefined) {
this.keyToValue.delete(key);
this.valueToKey.delete(value);
}
}
}

class MessageUniqueWrapper {
private msgIdMap: LimitedHashTable<number, string> = new LimitedHashTable(1000);
createMsg(MsgId: string) {
let ShortId = parseInt(crypto.createHash('sha1').update('2345').digest('hex').slice(0, 8), 16);
this.msgIdMap.set(ShortId, MsgId);
return ShortId;
}
getMsgIdByShortId(ShortId: number) {
return this.msgIdMap.getValue(ShortId);
}
getShortIdByMsgId(MsgId: string) {
return this.msgIdMap.getKey(MsgId);
}
private msgIdMap: LimitedHashTable<number, string> = new LimitedHashTable(1000);
createMsg(MsgId: string) {
const ShortId = parseInt(crypto.createHash('sha1').update('2345').digest('hex').slice(0, 8), 16);
this.msgIdMap.set(ShortId, MsgId);
return ShortId;
}
getMsgIdByShortId(ShortId: number) {
return this.msgIdMap.getValue(ShortId);
}
getShortIdByMsgId(MsgId: string) {
return this.msgIdMap.getKey(MsgId);
}
}

export const MessageUnique = new MessageUniqueWrapper();
10 changes: 5 additions & 5 deletions src/common/utils/Packet.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// 方案一 MiniApp发包方案
// 前置条件: 处于GUI环境 存在MiniApp

import { NTQQSystemApi } from "@/core";
import { NTQQSystemApi } from '@/core';

// 前排提示: 开发验证仅Win平台开展
export class MiniAppUtil {
static async RunMiniAppWithGUI() {
//process.env.ELECTRON_RUN_AS_NODE = undefined;//没用还是得自己用cpp之类的语言写个程序转发参数
return NTQQSystemApi.BootMiniApp(process.execPath, "miniapp://open/1007?url=https%3A%2F%2Fm.q.qq.com%2Fa%2Fs%2Fedd0a83d3b8afe233dfa07adaaf8033f%3Fscene%3D1007%26min_refer%3D10001");
}
static async RunMiniAppWithGUI() {
//process.env.ELECTRON_RUN_AS_NODE = undefined;//没用还是得自己用cpp之类的语言写个程序转发参数
return NTQQSystemApi.BootMiniApp(process.execPath, 'miniapp://open/1007?url=https%3A%2F%2Fm.q.qq.com%2Fa%2Fs%2Fedd0a83d3b8afe233dfa07adaaf8033f%3Fscene%3D1007%26min_refer%3D10001');
}
}
// 方案二 MiniApp发包方案 替代MiniApp方案
// 前置条件: 无
Expand Down
32 changes: 16 additions & 16 deletions src/common/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,23 +264,23 @@ export function isEqual(obj1: any, obj2: any) {
}

export async function deleteOldFiles(directoryPath: string, daysThreshold: number) {
try {
const files = await fsPromise.readdir(directoryPath);
try {
const files = await fsPromise.readdir(directoryPath);

for (const file of files) {
const filePath = path.join(directoryPath, file);
const stats = await fsPromise.stat(filePath);
const lastModifiedTime = stats.mtimeMs;
const currentTime = Date.now();
const timeDifference = currentTime - lastModifiedTime;
const daysDifference = timeDifference / (1000 * 60 * 60 * 24);
for (const file of files) {
const filePath = path.join(directoryPath, file);
const stats = await fsPromise.stat(filePath);
const lastModifiedTime = stats.mtimeMs;
const currentTime = Date.now();
const timeDifference = currentTime - lastModifiedTime;
const daysDifference = timeDifference / (1000 * 60 * 60 * 24);

if (daysDifference > daysThreshold) {
await fsPromise.unlink(filePath); // Delete the file
//console.log(`Deleted: ${filePath}`);
}
}
} catch (error) {
//console.error('Error deleting files:', error);
if (daysDifference > daysThreshold) {
await fsPromise.unlink(filePath); // Delete the file
//console.log(`Deleted: ${filePath}`);
}
}
} catch (error) {
//console.error('Error deleting files:', error);
}
}
4 changes: 2 additions & 2 deletions src/common/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class RequestUtil {
const formDataParts = [
`------${boundary}\r\n`,
`Content-Disposition: form-data; name="share_image"; filename="${filePath}"\r\n`,
`Content-Type: ` + type + `\r\n\r\n`
'Content-Type: ' + type + '\r\n\r\n'
];

const fileContent = readFileSync(filePath);
Expand Down Expand Up @@ -161,7 +161,7 @@ export class RequestUtil {
try {
if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {
const responseJson = JSON.parse(responseBody) as retType;
resolve(responseJson.result?.url!);
resolve(responseJson.result!.url!);
} else {
reject(new Error(`Unexpected status code: ${res.statusCode}`));
}
Expand Down
22 changes: 11 additions & 11 deletions src/onebot11/action/extends/CreateCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ import { FromSchema, JSONSchema } from 'json-schema-to-ts';
import { selfInfo } from '@/core/data';

const SchemaData = {
type: 'object',
properties: {
rawData: { type: 'string' },
brief: { type: 'string' }
},
required: ['brief', 'rawData'],
type: 'object',
properties: {
rawData: { type: 'string' },
brief: { type: 'string' }
},
required: ['brief', 'rawData'],
} as const satisfies JSONSchema;

type Payload = FromSchema<typeof SchemaData>;

export class CreateCollection extends BaseAction<Payload, any> {
actionName = ActionName.CreateCollection;
PayloadSchema = SchemaData;
protected async _handle(payload: Payload) {
return await NTQQCollectionApi.createCollection(selfInfo.uin, selfInfo.uid, selfInfo.nick, payload.brief, payload.rawData);
}
actionName = ActionName.CreateCollection;
PayloadSchema = SchemaData;
protected async _handle(payload: Payload) {
return await NTQQCollectionApi.createCollection(selfInfo.uin, selfInfo.uid, selfInfo.nick, payload.brief, payload.rawData);
}
}
22 changes: 11 additions & 11 deletions src/onebot11/action/extends/GetCollectionList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ import { FromSchema, JSONSchema } from 'json-schema-to-ts';
import { selfInfo } from '@/core/data';

const SchemaData = {
type: 'object',
properties: {
category: { type: 'number' },
count: { type: 'number' }
},
required: ['category', 'count'],
type: 'object',
properties: {
category: { type: 'number' },
count: { type: 'number' }
},
required: ['category', 'count'],
} as const satisfies JSONSchema;

type Payload = FromSchema<typeof SchemaData>;

export class GetCollectionList extends BaseAction<Payload, any> {
actionName = ActionName.GetCollectionList;
PayloadSchema = SchemaData;
protected async _handle(payload: Payload) {
return await NTQQCollectionApi.getAllCollection(payload.category, payload.count);
}
actionName = ActionName.GetCollectionList;
PayloadSchema = SchemaData;
protected async _handle(payload: Payload) {
return await NTQQCollectionApi.getAllCollection(payload.category, payload.count);
}
}
22 changes: 11 additions & 11 deletions src/onebot11/action/extends/SetLongNick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ import { NTQQUserApi } from '@/core/apis';
import { FromSchema, JSONSchema } from 'json-schema-to-ts';

const SchemaData = {
type: 'object',
properties: {
longNick: { type: 'string' },
},
required: [ 'longNick'],
type: 'object',
properties: {
longNick: { type: 'string' },
},
required: [ 'longNick'],
} as const satisfies JSONSchema;

type Payload = FromSchema<typeof SchemaData>;

export class SetLongNick extends BaseAction<Payload, any> {
actionName = ActionName.SetLongNick;
PayloadSchema = SchemaData;
protected async _handle(payload: Payload) {
let ret = await NTQQUserApi.setLongNick(payload.longNick)
return ret;
}
actionName = ActionName.SetLongNick;
PayloadSchema = SchemaData;
protected async _handle(payload: Payload) {
const ret = await NTQQUserApi.setLongNick(payload.longNick);
return ret;
}
}
38 changes: 19 additions & 19 deletions src/onebot11/action/extends/SetSelfProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@ import { NTQQUserApi } from '@/core/apis';
import { FromSchema, JSONSchema } from 'json-schema-to-ts';

const SchemaData = {
type: 'object',
properties: {
nick: { type: 'string' },
longNick: { type: 'string' },
sex: { type: 'number' }//传Sex值?建议传0
},
required: ['nick', 'longNick', 'sex'],
type: 'object',
properties: {
nick: { type: 'string' },
longNick: { type: 'string' },
sex: { type: 'number' }//传Sex值?建议传0
},
required: ['nick', 'longNick', 'sex'],
} as const satisfies JSONSchema;

type Payload = FromSchema<typeof SchemaData>;

export class SetSelfProfile extends BaseAction<Payload, any | null> {
actionName = ActionName.SetSelfProfile;
PayloadSchema = SchemaData;
protected async _handle(payload: Payload) {
let ret = await NTQQUserApi.modifySelfProfile({
nick: payload.nick,
longNick: payload.longNick,
sex: payload.sex,
birthday: { birthday_year: '', birthday_month: '', birthday_day: '' },
location: undefined
});
return ret;
}
actionName = ActionName.SetSelfProfile;
PayloadSchema = SchemaData;
protected async _handle(payload: Payload) {
const ret = await NTQQUserApi.modifySelfProfile({
nick: payload.nick,
longNick: payload.longNick,
sex: payload.sex,
birthday: { birthday_year: '', birthday_month: '', birthday_day: '' },
location: undefined
});
return ret;
}
}
48 changes: 24 additions & 24 deletions src/onebot11/action/extends/sharePeer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,41 @@ import { BuddyCategoryType } from '@/core/entities/';
import { FromSchema, JSONSchema } from 'json-schema-to-ts';

const SchemaData = {
type: 'object',
properties: {
user_id: { type: 'string' },
group_id: { type: 'string' },
phoneNumber: { type: 'string' },
},
type: 'object',
properties: {
user_id: { type: 'string' },
group_id: { type: 'string' },
phoneNumber: { type: 'string' },
},
} as const satisfies JSONSchema;

type Payload = FromSchema<typeof SchemaData>;


export class sharePeer extends BaseAction<Payload, any> {
actionName = ActionName.SharePeer;
PayloadSchema = SchemaData;
protected async _handle(payload: Payload) {
if (payload.group_id) {
return await NTQQGroupApi.getGroupRecommendContactArkJson(payload.group_id);
} else if (payload.user_id) {
return await NTQQUserApi.getBuddyRecommendContactArkJson(payload.user_id, payload.phoneNumber || '');
}
actionName = ActionName.SharePeer;
PayloadSchema = SchemaData;
protected async _handle(payload: Payload) {
if (payload.group_id) {
return await NTQQGroupApi.getGroupRecommendContactArkJson(payload.group_id);
} else if (payload.user_id) {
return await NTQQUserApi.getBuddyRecommendContactArkJson(payload.user_id, payload.phoneNumber || '');
}
}
}
const SchemaDataGroupEx = {
type: 'object',
properties: {
group_id: { type: 'string' },
},
required: ['group_id']
type: 'object',
properties: {
group_id: { type: 'string' },
},
required: ['group_id']
} as const satisfies JSONSchema;

type PayloadGroupEx = FromSchema<typeof SchemaDataGroupEx>;
export class shareGroupEx extends BaseAction<PayloadGroupEx, any> {
actionName = ActionName.ShareGroupEx;
PayloadSchema = SchemaDataGroupEx;
protected async _handle(payload: PayloadGroupEx) {
return await NTQQGroupApi.getArkJsonGroupShare(payload.group_id);
}
actionName = ActionName.ShareGroupEx;
PayloadSchema = SchemaDataGroupEx;
protected async _handle(payload: PayloadGroupEx) {
return await NTQQGroupApi.getArkJsonGroupShare(payload.group_id);
}
}
Loading

0 comments on commit 114c98f

Please sign in to comment.