|
| 1 | +import { GetApplicants } from "./../interfaces/rsi/get-applicants.interface"; |
| 2 | +import { GetApplicantsParams } from "./../interfaces/rsi/get-applicants-params.interface"; |
| 3 | +import { |
| 4 | + GetOrgMembersOpts, |
| 5 | + OrgMemberRank, |
| 6 | + OrgMemberVisibility |
| 7 | +} from "../interfaces/api/get-org-members-params.interface"; |
| 8 | +import { Container } from "typedi"; |
| 9 | +import { RSIService } from "../../services/rsi.service"; |
| 10 | +import { GetOrgMembers, OrgMember } from "../interfaces/api/get-org-members.interface"; |
| 11 | +import { GetOrgMembersOptions } from "../interfaces/api/get-org-members-params.interface"; |
| 12 | +import { parse, HTMLElement } from "node-html-parser"; |
| 13 | +import { OrgApplicant } from "../interfaces/rsi/get-applicants.interface"; |
| 14 | + |
| 15 | +/** |
| 16 | + * Represents an RSI Organisation |
| 17 | + */ |
| 18 | +export class Organisation { |
| 19 | + /** main rsi service */ |
| 20 | + protected _rsi: RSIService = Container.get(RSIService); |
| 21 | + |
| 22 | + /** |
| 23 | + * @param SSID unique SSID of the organisation |
| 24 | + */ |
| 25 | + constructor(public readonly SSID: string) {} |
| 26 | + |
| 27 | + /** |
| 28 | + * Get the list of members for this organisation that can be seen by everyone |
| 29 | + * this does not require any specific privilege. |
| 30 | + * |
| 31 | + * this will **NOT** return "HIDDEN" members and will return no personal info for "REDACTED" members |
| 32 | + * |
| 33 | + * @param options fetch options |
| 34 | + * @see `getMembers()` If you have memberlist privilege and want all the members |
| 35 | + */ |
| 36 | + public async getPublicMembers(): Promise<OrgMember[]>; |
| 37 | + public async getPublicMembers(options: GetOrgMembersOptions): Promise<OrgMember[]>; |
| 38 | + public async getPublicMembers(options?: GetOrgMembersOptions): Promise<OrgMember[]> { |
| 39 | + const res = await this._rsi.post<GetOrgMembers>(`api/orgs/getOrgMembers`, { |
| 40 | + ...options, |
| 41 | + symbol: this.SSID |
| 42 | + }); |
| 43 | + |
| 44 | + return this.buildMembersReturn(res.data, options); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Get the list of members for this organisation. |
| 49 | + * You **will** need memberlist privilege for this to work. |
| 50 | + * |
| 51 | + * @param options fetch options |
| 52 | + * @see `getPublicMembers()` for a list of public members if you do not have privilege |
| 53 | + */ |
| 54 | + public async getMembers(): Promise<OrgMember[]>; |
| 55 | + public async getMembers(options: GetOrgMembersOptions): Promise<OrgMember[]>; |
| 56 | + public async getMembers(options?: GetOrgMembersOptions): Promise<OrgMember[]> { |
| 57 | + const res = await this._rsi.post<GetOrgMembers>(`api/orgs/getOrgMembers`, { |
| 58 | + ...options, |
| 59 | + admin_mode: 1, |
| 60 | + symbol: this.SSID |
| 61 | + }); |
| 62 | + |
| 63 | + return this.buildMembersReturn(res.data, options, true); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Get the list of current applicants |
| 68 | + * |
| 69 | + * will fetch the first 500 applicants by default. |
| 70 | + */ |
| 71 | + public async getApplicants(): Promise<GetApplicants>; |
| 72 | + public async getApplicants(options: GetApplicantsParams): Promise<GetApplicants>; |
| 73 | + public async getApplicants( |
| 74 | + options: GetApplicantsParams = { page: 1, pagesize: 500 } |
| 75 | + ): Promise<GetApplicants> { |
| 76 | + const res = await this._rsi.navigate( |
| 77 | + `orgs/${this.SSID}/admin/applications?page=${options.page}&pagesize=${options.pagesize}` |
| 78 | + ); |
| 79 | + |
| 80 | + return this.parseApplicants(res); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Parse the HTML returned by getApplicants() into an array of OrgApplicant |
| 85 | + */ |
| 86 | + protected parseApplicants(resHTML: string): Array<OrgApplicant> { |
| 87 | + const root = parse(resHTML); |
| 88 | + |
| 89 | + |
| 90 | + return root |
| 91 | + .querySelectorAll("ul.applicants-listing li.clearfix") |
| 92 | + .map((li: HTMLElement) => { |
| 93 | + const applicant: OrgApplicant = { |
| 94 | + id: Number( |
| 95 | + (li.querySelectorAll("div.player-cell")[0] as HTMLElement).attributes[ |
| 96 | + "data-app-id" |
| 97 | + ] |
| 98 | + ), |
| 99 | + // no, this is not a typo, they do display the HANDLE in a .nick |
| 100 | + handle: li.querySelectorAll("span.nick")[0].text, |
| 101 | + nick: li.querySelectorAll("a.name")[0].text, |
| 102 | + message: li.querySelectorAll("span.message")[0].text |
| 103 | + }; |
| 104 | + |
| 105 | + return applicant; |
| 106 | + }); |
| 107 | + } |
| 108 | + |
| 109 | + protected async buildMembersReturn( |
| 110 | + res: GetOrgMembers, |
| 111 | + opts?: GetOrgMembersOptions, |
| 112 | + admin = false |
| 113 | + ) { |
| 114 | + const firstPassMembers = this.parseMembers(res); |
| 115 | + |
| 116 | + if (opts && (opts as GetOrgMembersOpts).allMembers) { |
| 117 | + if (firstPassMembers.length < res.totalrows) { |
| 118 | + /** |
| 119 | + * We have to make multiple calls because rsi api |
| 120 | + * does not support a pagesize != 32 ... |
| 121 | + */ |
| 122 | + for (let i = 2; i <= Math.ceil(res.totalrows / 32); i++) { |
| 123 | + // We need to fetch again :( |
| 124 | + const res2 = await this._rsi.post<GetOrgMembers>(`api/orgs/getOrgMembers`, { |
| 125 | + ...opts, |
| 126 | + page: i, |
| 127 | + admin_mode: admin ? 1 : undefined, |
| 128 | + symbol: this.SSID |
| 129 | + }); |
| 130 | + firstPassMembers.push(...this.parseMembers(res2.data)); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return firstPassMembers; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Parse the HTML of a getOrgMembers calls into an OrgMember array |
| 140 | + * @param res the res of getOrgMembers call |
| 141 | + */ |
| 142 | + protected parseMembers(res: GetOrgMembers) { |
| 143 | + const root = parse(res.html); |
| 144 | + |
| 145 | + const members = root.querySelectorAll("li").map(li => { |
| 146 | + const el = li as HTMLElement; |
| 147 | + |
| 148 | + const user: OrgMember = { |
| 149 | + id: Number(el.attributes["data-member-id"]), |
| 150 | + handle: el.attributes["data-member-nickname"], |
| 151 | + monicker: el.attributes["data-member-displayname"], |
| 152 | + avatar: |
| 153 | + el.attributes["data-member-avatar"].length > 5 |
| 154 | + ? el.attributes["data-member-avatar"] |
| 155 | + : null, |
| 156 | + rank: null, |
| 157 | + visibility: null |
| 158 | + }; |
| 159 | + |
| 160 | + const [_, rank] = (el.querySelector("span.ranking-stars") as HTMLElement).classNames |
| 161 | + .join(" ") |
| 162 | + .match(/data([0-9])/); |
| 163 | + |
| 164 | + user["rank"] = OrgMemberRank[rank]; |
| 165 | + |
| 166 | + const [_1, visibility] = (el.querySelector( |
| 167 | + "span.visibility" |
| 168 | + ) as HTMLElement).text.match(/Membership: (.*)/); |
| 169 | + |
| 170 | + user["visibility"] = visibility.substr(0, 1).toUpperCase() as OrgMemberVisibility; |
| 171 | + |
| 172 | + return user; |
| 173 | + }); |
| 174 | + |
| 175 | + return members; |
| 176 | + } |
| 177 | +} |
0 commit comments