Skip to content

Commit

Permalink
feat: create a default friend user group when create new identity
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed Sep 18, 2019
1 parent 6f5ab01 commit e1f3a17
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ServeType extends Group | Person> {
/** Omit myself in the UI and the selected result */
Expand Down Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion src/database/group.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference path="./global.d.ts" />
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 {
Expand Down
11 changes: 10 additions & 1 deletion src/database/helpers/group.ts
Original file line number Diff line number Diff line change
@@ -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,
)
}
21 changes: 0 additions & 21 deletions src/database/migrate/old.keystore.1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,6 @@ async function queryPeopleCryptoKey(): Promise<Exporting[]> {
//#region Store & Read CryptoKey
//#endregion

//#region Generate a new private key

/**
* @deprecated
*/
async function generateNewKey(whoami: PersonIdentifier): Promise<People.PersonRecordPublicPrivate> {
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'
Expand Down
5 changes: 4 additions & 1 deletion src/database/people.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
* @type {Record<string, CryptoKey>} Record of <userId, CryptoKey>
* @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
/**
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/database/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
34 changes: 17 additions & 17 deletions src/social-network-provider/facebook.com/UI/collectPeople.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -27,6 +26,7 @@ function findPeopleInfo() {
const id = getPersonIdentifierAtFacebook(a, true)
if (!id) return
Services.Crypto.verifyOthersProve(text, id.identifier)
return id
}
tryFindBioKey()
return {
Expand All @@ -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(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ export async function fetchPostContentFacebook(post: PostIdentifier<PersonIdenti
}
}
// Path 2: fetch by tab task
// TODO: on iOS, this should run the Path 1 in the domain of m.facebook.com
// const tabId = await getActiveTab()
return tasks(getPostUrlAtFacebook(post, 'open'), {
// runAtTabID: tabId,
}).getPostContent(post)
return tasks(getPostUrlAtFacebook(post, 'open')).getPostContent(post)
}

async function getActiveTab() {
Expand Down

0 comments on commit e1f3a17

Please sign in to comment.