diff --git a/src/api.ts b/src/api.ts index 3c8e92a..4a2049f 100644 --- a/src/api.ts +++ b/src/api.ts @@ -10,12 +10,8 @@ import { eventUrnToMessageID, urnID } from './util' export type SendMessageResolveFunction = (value: Message[]) => void export default class LinkedIn implements PlatformAPI { - private currentUser = null - user: CurrentUser - private cookies: any - private searchedUsers: User[] private realTimeApi: null | LinkedInRealTime = null @@ -29,36 +25,31 @@ export default class LinkedIn implements PlatformAPI { readonly api = new LinkedInAPI() + private afterAuth = async (cookies: any) => { + this.api.setLoginState(CookieJar.fromJSON(cookies)) + const currentUser = await this.api.getCurrentUser() + if (!currentUser) throw new ReAuthError() + this.user = mapCurrentUser(currentUser) + } + init = async (serialized: { cookies: any }) => { const { cookies } = serialized || {} if (!cookies) return - this.cookies = cookies - - await this.api.setLoginState(CookieJar.fromJSON(cookies)) - this.currentUser = await this.api.getCurrentUser() - if (!this.currentUser) throw new ReAuthError() - - this.user = mapCurrentUser(this.currentUser) + await this.afterAuth(cookies) } login = async ({ cookieJarJSON }): Promise => { if (!cookieJarJSON?.cookies?.some(({ key }) => key === LinkedInAuthCookieName)) return { type: 'error', errorMessage: 'No authentication cookie was found' } - - await this.api.setLoginState(CookieJar.fromJSON(cookieJarJSON)) - - this.currentUser = await this.api.getCurrentUser() - this.user = mapCurrentUser(this.currentUser) - this.cookies = cookieJarJSON - + await this.afterAuth(cookieJarJSON) return { type: 'success' } } - serializeSession = () => ({ cookies: this.cookies }) + serializeSession = () => ({ cookies: this.api.cookieJar.toJSON() }) logout = () => this.api.logout() - getCurrentUser = () => mapCurrentUser(this.currentUser) + getCurrentUser = () => this.user updateThreadSeenMap = (threadID: string, participantID: string, messageID: string, seenAt: string) => { if (!this.threadSeenMap.has(threadID)) this.threadSeenMap.set(threadID, new Map()) diff --git a/src/lib/linkedin.ts b/src/lib/linkedin.ts index 127360c..d2c469e 100644 --- a/src/lib/linkedin.ts +++ b/src/lib/linkedin.ts @@ -49,7 +49,7 @@ export default class LinkedInAPI { // key is threadID, values are participantIDs conversationsParticipants: Record = {} - setLoginState = async (cookieJar: CookieJar) => { + setLoginState = (cookieJar: CookieJar) => { if (!cookieJar) throw TypeError() this.cookieJar = cookieJar }