-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(formatters): ⚡ Add thread & friend list formatters WIP
In this commit, a lot has been disabled, commented or deleted. However, thread & friend list are functioning. I hope this won't be a disaster. Mentioning #3
- Loading branch information
1 parent
40bc001
commit 46a0cb3
Showing
8 changed files
with
199 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,80 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ | ||
|
||
import { ThreadInfo } from '../types/threads'; | ||
import { UserInfo } from '../types/users'; | ||
import { ThreadInfo, ThreadParticipant } from '../types/threads'; | ||
import { UserID, UserInfo } from '../types/users'; | ||
|
||
export function formatSingleFriend(originalFriend: any): UserInfo { | ||
// TODO: CRITICAL: finish this | ||
return originalFriend; | ||
export function formatSingleFriend(originalFriend: any): UserInfo | null { | ||
const original = originalFriend?.node?.sts_info?.direct_nav_result; | ||
if (!original) return null; | ||
// single friend can be also FB event or site: | ||
if (original.type.toUpperCase() !== 'FRIEND') return null; | ||
|
||
return { | ||
userId: parseInt(original.ent_id), | ||
fullName: original.title, | ||
profilePictureUrlSmall: original.img_url, | ||
profileUrl: original.link_url | ||
}; | ||
} | ||
|
||
export function formatSingleThread(originalThread: any, currentUserId: UserID): ThreadInfo { | ||
const original = originalThread.node; | ||
if (!original) | ||
throw new Error( | ||
`There was an unknown response. Contact the dev team about this (error code F-22). Data: ${JSON.stringify( | ||
originalThread | ||
)}` | ||
); | ||
|
||
const isGroup = original.thread_type.toLowerCase() === 'group'; | ||
const isOneToOne = original.thread_type.toLowerCase() === 'one_to_one'; | ||
if (!isOneToOne && !isGroup) | ||
throw new Error( | ||
`There was an unknown thread type. Contact the dev team about this (error code F-23). Data: ${JSON.stringify( | ||
originalThread | ||
)}` | ||
); | ||
|
||
const common = { | ||
isGroup, | ||
lastUpdated: parseInt(original.updated_time), | ||
participants: formatParticipants(original.all_participants?.edges), | ||
nicknames: formatParticipantCustomisations(original.customization_info?.participant_customizations) | ||
}; | ||
|
||
if (isGroup) | ||
return { | ||
...common, | ||
threadId: parseInt(original.thread_key?.thread_fbid), | ||
threadName: original.name, | ||
imageUrl: original.image?.uri || original.image?.url | ||
}; | ||
|
||
const otherParticipant = common.participants.find(p => p.userId != currentUserId); | ||
return { | ||
...common, | ||
threadId: otherParticipant?.userId as UserID, | ||
threadName: otherParticipant?.fullName as string, | ||
imageUrl: otherParticipant?.profilePictureUrlSmall as string | ||
}; | ||
} | ||
|
||
function formatParticipantCustomisations(array: any[]) { | ||
if (!array) return {}; | ||
const nicknames: Record<UserID, string> = {}; | ||
for (const obj of array) nicknames[parseInt(obj.participant_id)] = obj.nickname; | ||
return nicknames; | ||
} | ||
|
||
export function formatSingleThread(originalThread: any): ThreadInfo { | ||
// TODO: CRITICAL: finish this | ||
return originalThread; | ||
function formatParticipants(array: any[]): ThreadParticipant[] { | ||
if (!array) return []; | ||
return array | ||
.map(one => one.node.messaging_actor) | ||
.map(one => ({ | ||
userId: parseInt(one.id), | ||
fullName: one.name, | ||
shortName: one.short_name, | ||
profilePictureUrlSmall: one.profile_picture?.uri || one.profile_picture?.url | ||
})); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,49 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ | ||
import { UserGender, UserID, UserInfo } from '../types/users'; | ||
// /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
// /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ | ||
// import { UserGender, UserID, UserInfo } from '../types/users'; | ||
|
||
export function formatUserInfoDict(userProfiles: any): Record<UserID, UserInfo> { | ||
const finalObject: Record<UserID, UserInfo> = {}; | ||
for (const key of Object.keys(userProfiles)) { | ||
const parsedKey = parseInt(key); | ||
if (!parsedKey) | ||
throw new Error( | ||
`There was an unknown response. Contact the dev team about this (error code 935520). User profiles: ${JSON.stringify( | ||
userProfiles | ||
)}` | ||
); | ||
// filter out some (possibly) hidden accounts - they nevertheless contain no useful information | ||
if (userProfiles[key].id == 0) continue; | ||
// export function formatUserInfoDict(userProfiles: any): Record<UserID, UserInfo> { | ||
// const finalObject: Record<UserID, UserInfo> = {}; | ||
// for (const key of Object.keys(userProfiles)) { | ||
// const parsedKey = parseInt(key); | ||
// if (!parsedKey) | ||
// throw new Error( | ||
// `There was an unknown response. Contact the dev team about this (error code 935520). User profiles: ${JSON.stringify( | ||
// userProfiles | ||
// )}` | ||
// ); | ||
// // filter out some (possibly) hidden accounts - they nevertheless contain no useful information | ||
// if (userProfiles[key].id == 0) continue; | ||
|
||
finalObject[parsedKey] = { | ||
id: parseInt(userProfiles[key].id), | ||
// finalObject[parsedKey] = { | ||
// id: parseInt(userProfiles[key].id), | ||
|
||
fullName: userProfiles[key].name, | ||
firstName: userProfiles[key].firstName, | ||
alternateName: userProfiles[key].alternateName || undefined, | ||
gender: getGender(userProfiles[key].gender), | ||
// fullName: userProfiles[key].name, | ||
// firstName: userProfiles[key].firstName, | ||
// alternateName: userProfiles[key].alternateName || undefined, | ||
// gender: getGender(userProfiles[key].gender), | ||
|
||
isFriend: userProfiles[key].is_friend, | ||
isBlocked: userProfiles[key].is_blocked, | ||
// isFriend: userProfiles[key].is_friend, | ||
// isBlocked: userProfiles[key].is_blocked, | ||
|
||
thumbSrc: userProfiles[key].thumbSrc, | ||
profileUrl: userProfiles[key].uri, | ||
type: userProfiles[key].type, | ||
vanity: userProfiles[key].vanity || undefined | ||
}; | ||
} | ||
return finalObject; | ||
} | ||
// thumbSrc: userProfiles[key].thumbSrc, | ||
// profileUrl: userProfiles[key].uri, | ||
// type: userProfiles[key].type, | ||
// vanity: userProfiles[key].vanity || undefined | ||
// }; | ||
// } | ||
// return finalObject; | ||
// } | ||
|
||
function getGender(gender: any): UserGender { | ||
switch (gender) { | ||
case 1: | ||
return UserGender.Female; | ||
case 2: | ||
return UserGender.Male; | ||
case 6: | ||
return UserGender.Other; | ||
default: | ||
return UserGender.Unknown; | ||
} | ||
} | ||
// function getGender(gender: any): UserGender { | ||
// switch (gender) { | ||
// case 1: | ||
// return UserGender.Female; | ||
// case 2: | ||
// return UserGender.Male; | ||
// case 6: | ||
// return UserGender.Other; | ||
// default: | ||
// return UserGender.Unknown; | ||
// } | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.