| 1 | import type { PluginPlatformType } from './types/baseTypes' |
| 2 | import type { PlatAccountInfo } from './types/plat.type' |
| 3 | import type { SocialAccount } from '@/api/accounts/account.types' |
| 4 | import { AccountStatus } from '@/app/config/accountConfig' |
| 5 | import { PlatType } from '@/app/config/platConfig' |
| 6 | import { PLUGIN_SUPPORTED_PLATFORMS } from './types/baseTypes' |
| 7 | |
| 8 | export type PluginAccountStatusMap = Partial<Record<PluginPlatformType, PlatAccountInfo | null>> |
| 9 | |
| 10 | export function isPluginSupportedPlatform(platform: PlatType): platform is PluginPlatformType { |
| 11 | return PLUGIN_SUPPORTED_PLATFORMS.includes(platform as PluginPlatformType) |
| 12 | } |
| 13 | |
| 14 | export function getXhsLoginStatus(account?: PlatAccountInfo | null) { |
| 15 | if (!account || account.type !== PlatType.Xhs) { |
| 16 | return null |
| 17 | } |
| 18 | |
| 19 | return account.xhsLoginStatus ?? null |
| 20 | } |
| 21 | |
| 22 | export function getWxSphLoginStatus(account?: PlatAccountInfo | null) { |
| 23 | if (!account || account.type !== PlatType.WxSph) { |
| 24 | return null |
| 25 | } |
| 26 | |
| 27 | return account.wxSphLoginStatus ?? null |
| 28 | } |
| 29 | export function isPluginPlatformAccountReady(account?: PlatAccountInfo | null): account is PlatAccountInfo { |
| 30 | if (!account) { |
| 31 | return false |
| 32 | } |
| 33 | |
| 34 | const xhsLoginStatus = getXhsLoginStatus(account) |
| 35 | if (xhsLoginStatus) { |
| 36 | return xhsLoginStatus.home && xhsLoginStatus.creator |
| 37 | } |
| 38 | |
| 39 | const wxSphLoginStatus = getWxSphLoginStatus(account) |
| 40 | if (wxSphLoginStatus) { |
| 41 | return wxSphLoginStatus.channels |
| 42 | } |
| 43 | |
| 44 | return true |
| 45 | } |
| 46 | |
| 47 | export function mergePluginAccountStatus( |
| 48 | accountList: SocialAccount[], |
| 49 | platformAccounts: PluginAccountStatusMap, |
| 50 | ) { |
| 51 | const accountMap = new Map<string, SocialAccount>() |
| 52 | |
| 53 | const mergedAccountList = accountList.map((account) => { |
| 54 | if (!isPluginSupportedPlatform(account.type)) { |
| 55 | accountMap.set(account.id, account) |
| 56 | return account |
| 57 | } |
| 58 | |
| 59 | if (!Object.hasOwn(platformAccounts, account.type)) { |
| 60 | accountMap.set(account.id, account) |
| 61 | return account |
| 62 | } |
| 63 | |
| 64 | const platformAccount = platformAccounts[account.type] |
| 65 | const shouldBeOnline = !!platformAccount |
| 66 | && isPluginPlatformAccountReady(platformAccount) |
| 67 | && platformAccount.uid === account.uid |
| 68 | const status = shouldBeOnline ? AccountStatus.USABLE : AccountStatus.DISABLE |
| 69 | const mergedAccount = account.status === status ? account : { ...account, status } |
| 70 | |
| 71 | accountMap.set(mergedAccount.id, mergedAccount) |
| 72 | return mergedAccount |
| 73 | }) |
| 74 | |
| 75 | return { |
| 76 | accountList: mergedAccountList, |
| 77 | accountMap, |
| 78 | } |
| 79 | } |
| 80 |