Skip to content

Commit

Permalink
feat!: add missing API elements
Browse files Browse the repository at this point in the history
- add autoproxy settings
- add pronouns and pronoun privacy to systems
- add proxy privacy, webhook avatars, tts, autoproxy status, message count, and last message timestamp to members
- add keep proxy to member guild configs
- add private info toggle to system settings
- remove autoproxy config from system settings
- add name and metadata privacy to groups
- add guild to messages
- various rearrangements to keep props in the same order as the API
- fix issues with directly setting `private` and `public` as values for privacy objects

BREAKING CHANGE: autoproxy settings have been moved out of the SystemGuildSettings object and moved into their own object
  • Loading branch information
greysdawn committed Aug 28, 2023
1 parent d1383e1 commit 5769eb7
Show file tree
Hide file tree
Showing 12 changed files with 261 additions and 79 deletions.
40 changes: 39 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Switch, {ISwitch} from './structures/switch';
import Message, {IMessage} from './structures/message';
import SystemConfig, {ISystemConfig} from './structures/systemConfig';
import SystemGuildSettings, {ISystemGuildSettings} from './structures/systemGuildSettings';
import SystemAutoproxySettings, {ISystemAutoproxySettings} from './structures/systemAutoproxySettings';
import MemberGuildSettings, {IMemberGuildSettings} from './structures/memberGuildSettings';

import APIError from './structures/apiError';
Expand Down Expand Up @@ -153,7 +154,7 @@ class PKAPI {
return new SystemConfig(this, resp.data);
}

async getSystemGuildSettings(data: { token?: string, guild: string}) {
async getSystemGuildSettings(data: { token?: string, guild: string }) {
if(this.version < 2) throw new Error("Guild settings are only available for API version 2.");

var token = this.#token || data.token;
Expand Down Expand Up @@ -190,6 +191,43 @@ class PKAPI {
return new SystemGuildSettings(this, {...resp.data, guild: data.guild});
}

async getSystemAutoproxySettings(data: { token?: string, guild: string }) {
if(this.version < 2) throw new Error("Autoproxy settings are only available for API version 2.");

var token = this.#token || data.token;
if(!token) throw new Error("Getting autoproxy settings requires a token.");
if(!data.guild) throw new Error("Must provide a guild ID.");

try {
var resp = await this.handle(ROUTES[this.#_version].GET_SYSTEM_AUTOPROXY_SETTINGS(data.guild), {token});
} catch(e) {
throw e;
}

return new SystemAutoproxySettings(this, {...resp.data, guild: data.guild});
}

async patchSystemAutoproxySettings(data: RequestData<ISystemAutoproxySettings>) {
if(this.version < 2) throw new Error("Autoproxy settings are only available for API version 2.");

var token = this.#token || data.token;
if(!token) throw new Error("PATCH requires a token.");
if(!data.guild) throw new Error("Must provide a guild ID.");

try {
var settings = data instanceof SystemAutoproxySettings ? data : new SystemAutoproxySettings(this, data);
var body = await settings.verify();
var resp = await this.handle(
ROUTES[this.#_version].PATCH_SYSTEM_AUTOPROXY_SETTINGS(data.guild),
{token, body}
);
} catch(e) {
throw e;
}

return new SystemAutoproxySettings(this, {...resp.data, guild: data.guild});
}

/*
** MEMBER FUNCTIONS
*/
Expand Down
5 changes: 5 additions & 0 deletions lib/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,13 @@ const ROUTES: any = {

GET_SYSTEM_CONFIG: () => ({method: 'GET', route: `/systems/@me/settings`}),
PATCH_SYSTEM_CONFIG: () => ({method: 'PATCH', route: `/systems/@me/settings`}),

GET_SYSTEM_GUILD_SETTINGS: (gid: string) => ({method: 'GET', route: `/systems/@me/guilds/${gid}`}),
PATCH_SYSTEM_GUILD_SETTINGS: (gid: string) => ({method: 'PATCH', route: `/systems/@me/guilds/${gid}`}),

GET_SYSTEM_AUTOPROXY_SETTINGS: (gid: string) => ({method: 'GET', route: `/systems/@me/autoproxy?guild_id=${gid}`}),
PATCH_SYSTEM_AUTOPROXY_SETTINGS: (gid: string) => ({method: 'PATCH', route: `/systems/@me/autoproxy?guild_id=${gid}`}),

GET_MEMBER_GUILD_SETTINGS: (mid: string, gid: string) => ({method: 'GET', route: `/members/${mid}/guilds/${gid}`}),
PATCH_MEMBER_GUILD_SETTINGS: (mid: string, gid: string) => ({method: 'PATCH', route: `/members/${mid}/guilds/${gid}`}),

Expand Down
14 changes: 10 additions & 4 deletions lib/structures/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,29 @@ import { validatePrivacy } from '../utils';
import Member from './member';

export const enum GroupPrivacyKeys {
Description = 'description_privacy',
Icon = 'icon_privacy',
List = 'list_privacy',
Visibility = 'visibility',
Name = 'name_privacy',
Description = 'description_privacy',
Icon = 'icon_privacy',
List = 'list_privacy',
Metadata = 'metadata_privacy',
Visibility = 'visibility',
}

const pKeys = [
GroupPrivacyKeys.Name,
GroupPrivacyKeys.Description,
GroupPrivacyKeys.Icon,
GroupPrivacyKeys.List,
GroupPrivacyKeys.Metadata,
GroupPrivacyKeys.Visibility
]

export interface GroupPrivacy {
name_privacy?: string;
description_privacy?: string;
icon_privacy?: string;
list_privacy?: string;
metadata_privacy?: string;
visibility?: string;
}

Expand Down
108 changes: 72 additions & 36 deletions lib/structures/member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ const enum MemberPrivacyKeys {
Birthday = 'birthday_privacy',
Pronouns = 'pronoun_privacy',
Avatar = 'avatar_privacy',
Metadata = 'metadata_privacy'
Metadata = 'metadata_privacy',
Proxy = 'proxy_privacy'
}

const pKeys = [
Expand All @@ -49,7 +50,8 @@ const pKeys = [
MemberPrivacyKeys.Birthday,
MemberPrivacyKeys.Pronouns,
MemberPrivacyKeys.Avatar,
MemberPrivacyKeys.Metadata
MemberPrivacyKeys.Metadata,
MemberPrivacyKeys.Proxy
]

export interface MemberPrivacy {
Expand All @@ -60,6 +62,7 @@ export interface MemberPrivacy {
pronoun_privacy?: string;
avatar_privacy?: string;
metadata_privacy?: string;
proxy_privacy?: string;
}

const KEYS: any = {
Expand All @@ -75,19 +78,33 @@ const KEYS: any = {
test: (d: string) => !d.length || d.length <= 100,
err: "Display name must be 100 characters or less"
},
description: {
test: (d: string) => !d.length || d.length < 1000,
err: "Description must be 1000 characters or less"
},
pronouns: {
test: (p: string) => !p.length || p.length <= 100,
err: "Pronouns must be 100 characters or less"
},
color: {
test: (c: string | Instance) => { c = tc(c); return c.isValid() },
err: "Color must be a valid hex code",
transform: (c: string | Instance) => { c = tc(c); return c.toHex() }
},
birthday: {
test: (d: string | Date) => {
if(d instanceof Date) return true;
else {
var d2 = parser.parseDate(d);
return d2 && !isNaN(d2.valueOf())
}
},
err: "Birthday must be a valid date",
transform: (d: string | Date) => {
if(!d) return d;
var date;
if(!(d instanceof Date)) date = parser.parseDate(d);
else date = d;
return formatDate(date!);
},
init: (d: string | Date) => d ? new Date(d) : d
},
pronouns: {
test: (p: string) => !p.length || p.length <= 100,
err: "Pronouns must be 100 characters or less"
},
avatar_url: {
test: async (a: string) => {
if(!validUrl.isWebUri(a)) return false;
Expand All @@ -99,6 +116,17 @@ const KEYS: any = {
},
err: "Avatar URL must be a valid image and less than 256 characters"
},
webhook_avatar_url: {
test: async (a: string) => {
if(!validUrl.isWebUri(a)) return false;
try {
var data = await axios.head(a);
if(data.headers["content-type"]?.startsWith("image")) return true;
return false;
} catch(e) { return false; }
},
err: "Webhook avatar URL must be a valid image and less than 256 characters"
},
banner: {
test: async (a: string) => {
if(!validUrl.isWebUri(a)) return false;
Expand All @@ -110,23 +138,12 @@ const KEYS: any = {
},
err: "Banner URL must be a valid image and less than 256 characters"
},
birthday: {
test: (d: string | Date) => {
if(d instanceof Date) return true;
else {
var d2 = parser.parseDate(d);
return d2 && !isNaN(d2.valueOf())
}
},
err: "Birthday must be a valid date",
transform: (d: string | Date) => {
if(!d) return d;
var date;
if(!(d instanceof Date)) date = parser.parseDate(d);
else date = d;
return formatDate(date!);
},
init: (d: string | Date) => d ? new Date(d) : d
description: {
test: (d: string) => !d.length || d.length < 1000,
err: "Description must be 1000 characters or less"
},
created: {
init: (d: string | Date) => new Date(d)
},
proxy_tags: {
test: (p: ProxyTag[]) => Array.isArray(p) && !p.some(t => !hasKeys(t, ['prefix', 'suffix'])),
Expand All @@ -136,7 +153,16 @@ const KEYS: any = {
test: (v: any) => typeof v == "boolean",
err: "Keep proxy must be a boolean (true or false)"
},
created: {
tts: {
test: (v: any) => typeof v == "boolean",
err: "TTS must be a boolean (true or false)"
},
autoproxy_enabled: {
test: (v: any) => typeof v == "boolean",
err: "Autoproxy status must be a boolean (true or false)"
},
message_count: { },
last_message_timestamp: {
init: (d: string | Date) => new Date(d)
},
privacy: {
Expand All @@ -155,15 +181,20 @@ export interface IMember {
system: string;
name: string;
display_name?: string;
description?: string;
pronouns?: string;
color?: string;
birthday?: Date | string;
pronouns?: string;
avatar_url?: string;
webhook_avatar_url?: string;
banner?: string;
birthday?: Date | string;
description?: string;
created: Date | string;
proxy_tags?: ProxyTag[];
keep_proxy?: boolean;
created: Date | string;
tts?: boolean;
autoproxy_enabled?: boolean;
message_count?: number;
last_message_timestamp?: Date;
privacy: MemberPrivacy;

groups?: Map<string, Group>;
Expand All @@ -180,15 +211,20 @@ export default class Member implements IMember {
system: string = '';
name: string = '';
display_name?: string;
description?: string;
pronouns?: string;
color?: string;
birthday?: Date | string;
pronouns?: string;
avatar_url?: string;
webhook_avatar_url?: string;
banner?: string;
birthday?: Date | string;
description?: string;
created: Date | string = '';
proxy_tags?: ProxyTag[];
keep_proxy?: boolean;
created: Date | string = '';
tts?: boolean;
autoproxy_enabled?: boolean;
message_count?: number;
last_message_timestamp?: Date;
privacy: MemberPrivacy = {};

groups?: Map<string, Group>;
Expand Down
5 changes: 5 additions & 0 deletions lib/structures/memberGuildSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const KEYS: any = {
} catch(e) { return false; }
},
err: "Avatar URL must be a valid image and less than 256 characters"
},
keep_proxy: {
transform: (v?: any) => v ? true : false
}
}

Expand All @@ -30,6 +33,7 @@ export interface IMemberGuildSettings {
member: string;
display_name?: string;
avatar_url?: string;
keep_proxy?: boolean;
}

export default class MemberGuildSettings implements IMemberGuildSettings {
Expand All @@ -40,6 +44,7 @@ export default class MemberGuildSettings implements IMemberGuildSettings {
member = '';
display_name?: string;
avatar_url?: string;
keep_proxy?: boolean;

constructor(api: API, data: Partial<MemberGuildSettings>) {
this.#api = api;
Expand Down
3 changes: 3 additions & 0 deletions lib/structures/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const KEYS: any = {
original: { },
sender: { },
channel: { },
guild: { },
system: {
init: (s: Partial<System>, api: API) => s ? new System(api, s) : null
},
Expand All @@ -25,6 +26,7 @@ export interface IMessage {
original?: string;
sender: string;
channel: string;
guild: string;
system?: string | System;
member?: string | Member;
}
Expand All @@ -39,6 +41,7 @@ export default class Message implements IMessage {
original?: string;
sender: string = '';
channel: string = '';
guild: string = '';
system?: string | System;
member?: string | Member;

Expand Down
Loading

0 comments on commit 5769eb7

Please sign in to comment.