diff --git a/src/components/shared/SelectPeopleAndGroups/SelectPeopleAndGroupsUI.tsx b/src/components/shared/SelectPeopleAndGroups/SelectPeopleAndGroupsUI.tsx index 1bb016194afa..5d964ac724c8 100644 --- a/src/components/shared/SelectPeopleAndGroups/SelectPeopleAndGroupsUI.tsx +++ b/src/components/shared/SelectPeopleAndGroups/SelectPeopleAndGroupsUI.tsx @@ -5,6 +5,7 @@ import { Person, Group } from '../../../database' import { useCurrentIdentity } from '../../DataSource/useActivatedUI' import { PersonOrGroupInList } from './PersonOrGroupInList' import { PersonOrGroupInChip } from './PersonOrGroupInChip' +import { PersonIdentifier, GroupIdentifier } from '../../../database/type' type PersonOrGroup = Group | Person interface SelectPeopleAndGroupsUIProps { /** Omit myself in the UI and the selected result */ @@ -153,8 +154,8 @@ function FrozenChip(item: PersonOrGroup) { } export function isPerson(x: PersonOrGroup): x is Person { - return 'fingerprint' in x + return x.identifier instanceof PersonIdentifier } export function isGroup(x: PersonOrGroup): x is Group { - return !isPerson(x) + return x.identifier instanceof GroupIdentifier } diff --git a/src/database/group.ts b/src/database/group.ts index 59fff1f08366..9dfd53f2bed2 100644 --- a/src/database/group.ts +++ b/src/database/group.ts @@ -1,6 +1,6 @@ /// import { openDB, DBSchema } from 'idb/with-async-ittr' -import { GroupIdentifier, PersonIdentifier, GroupType, PreDefinedVirtualGroupType, Identifier } from './type' +import { GroupIdentifier, PersonIdentifier, Identifier } from './type' //#region Schema interface GroupRecordBase { diff --git a/src/database/helpers/group.ts b/src/database/helpers/group.ts index 297d02eebe42..a5acc30bd308 100644 --- a/src/database/helpers/group.ts +++ b/src/database/helpers/group.ts @@ -1,5 +1,14 @@ -import { GroupRecord } from '../group' +import { GroupRecord, createUserGroupDatabase } from '../group' +import { PersonIdentifier, GroupIdentifier, PreDefinedVirtualGroupNames, GroupType } from '../type' export interface Group extends GroupRecord { avatar?: string } + +export function createDefaultFriendsGroup(who: PersonIdentifier) { + return createUserGroupDatabase( + new GroupIdentifier(who.network, PreDefinedVirtualGroupNames.friends, GroupType.virtual, who.userId), + // Put the raw special name in, then UI can display in their own language. + PreDefinedVirtualGroupNames.friends, + ) +} diff --git a/src/database/migrate/old.keystore.1.ts b/src/database/migrate/old.keystore.1.ts index f70cc8c3cb5e..b9316dbcb8b6 100644 --- a/src/database/migrate/old.keystore.1.ts +++ b/src/database/migrate/old.keystore.1.ts @@ -43,27 +43,6 @@ async function queryPeopleCryptoKey(): Promise { //#region Store & Read CryptoKey //#endregion -//#region Generate a new private key - -/** - * @deprecated - */ -async function generateNewKey(whoami: PersonIdentifier): Promise { - const has = await People.queryMyIdentityAtDB(whoami) - if (has) throw new TypeError('You already have a key-pair!') - - const mine = await crypto.subtle.generateKey({ name: 'ECDH', namedCurve: 'K-256' }, true, ['deriveKey']) - const record: People.PersonRecordPublicPrivate = { - identifier: whoami, - groups: [], - publicKey: mine.publicKey, - privateKey: mine.privateKey, - } - await People.storeMyIdentityDB(record) - return record -} -//#endregion - import { deleteDB } from 'idb/with-async-ittr' import * as People from '../people' import { PersonIdentifier } from '../type' diff --git a/src/database/people.ts b/src/database/people.ts index b2f39d0aca73..721cbaaa52e6 100644 --- a/src/database/people.ts +++ b/src/database/people.ts @@ -18,12 +18,14 @@ * @type {Record} Record of * @keys outline, string, which means network. */ -import { GroupIdentifier, Identifier, PersonIdentifier } from './type' +import { GroupIdentifier, Identifier, PersonIdentifier, GroupType, PreDefinedVirtualGroupNames } from './type' import { DBSchema, openDB } from 'idb/with-async-ittr' import { CryptoKeyToJsonWebKey, JsonWebKeyToCryptoKey } from '../utils/type-transform/CryptoKey-JsonWebKey' import { MessageCenter } from '../utils/messages' import { personRecordToPerson } from './helpers/person' import { isIdentifierArrayEquals } from '../utils/equality' +import { createUserGroupDatabase } from './group' +import { createDefaultFriendsGroup } from './helpers/group' //#region Type and utils /** @@ -247,6 +249,7 @@ export async function generateMyIdentityDB(identifier: PersonIdentifier): Promis publicKey: key.publicKey, privateKey: key.privateKey, }) + await createDefaultFriendsGroup(identifier).catch(console.error) MessageCenter.emit('identityUpdated', undefined) } //#endregion diff --git a/src/database/type.ts b/src/database/type.ts index 9cd61f508db4..d313420f4954 100644 --- a/src/database/type.ts +++ b/src/database/type.ts @@ -80,8 +80,8 @@ export enum GroupType { */ real = 'real', } -export enum PreDefinedVirtualGroupType { - friends = 'friends', +export enum PreDefinedVirtualGroupNames { + friends = '_default_friends_group_', } @serializable('GroupIdentifier') export class GroupIdentifier extends Identifier { diff --git a/src/social-network-provider/facebook.com/UI/collectPeople.ts b/src/social-network-provider/facebook.com/UI/collectPeople.ts index c13595456565..eb63350fad8a 100644 --- a/src/social-network-provider/facebook.com/UI/collectPeople.ts +++ b/src/social-network-provider/facebook.com/UI/collectPeople.ts @@ -15,8 +15,7 @@ function findPeopleInfo() { /** * @var text * @example - * "...r Xoogler, MaskBook: A80TOj...eW9yqf - * Google - Softwa..." + * "...r Xoogler, MaskBook: A80TOj...eW9yqf Google - Softwa..." */ const text = node.innerText @@ -27,6 +26,7 @@ function findPeopleInfo() { const id = getPersonIdentifierAtFacebook(a, true) if (!id) return Services.Crypto.verifyOthersProve(text, id.identifier) + return id } tryFindBioKey() return { @@ -36,22 +36,22 @@ function findPeopleInfo() { }) .startWatch() } +enum Status { + Friend, + Unknown, + NonFriend, +} +/** + * Ack: + * If `#pagelet_timeline_profile_actions > * > *` have 4 children, they are not friend. + * If have 6 children, they are friend. + */ +const isFriend = new LiveSelector().querySelectorAll('#pagelet_timeline_profile_actions > * > *').replace(arr => { + if (arr.length === 6) return [Status.Friend] + else if (arr.length === 4) return [Status.NonFriend] + return [Status.Unknown] +}) function detectIfFriend() { - enum Status { - Friend, - Unknown, - NonFriend, - } - /** - * Ack: - * If `#pagelet_timeline_profile_actions > * > *` have 4 children, they are not friend. - * If have 6 children, they are friend. - */ - const isFriend = new LiveSelector().querySelectorAll('#pagelet_timeline_profile_actions > * > *').replace(arr => { - if (arr.length === 6) return [Status.Friend] - else if (arr.length === 4) return [Status.NonFriend] - return [Status.Unknown] - }) // TODO: finish this, store it into the database, also do this in twitter new MutationObserverWatcher(isFriend) .useForeach(() => { diff --git a/src/social-network-provider/facebook.com/Worker/fetchPostContent.ts b/src/social-network-provider/facebook.com/Worker/fetchPostContent.ts index e76b199aa323..5a04e1db27aa 100644 --- a/src/social-network-provider/facebook.com/Worker/fetchPostContent.ts +++ b/src/social-network-provider/facebook.com/Worker/fetchPostContent.ts @@ -32,11 +32,7 @@ export async function fetchPostContentFacebook(post: PostIdentifier