Skip to content

Commit

Permalink
feat: auto share to friends
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed Oct 30, 2019
1 parent c0df039 commit 4a04f33
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 33 deletions.
23 changes: 22 additions & 1 deletion src/database/group.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// <reference path="./global.d.ts" />
import { DBSchema, openDB } from 'idb/with-async-ittr'
import { GroupIdentifier, Identifier, PersonIdentifier } from './type'
import { MessageCenter } from '../utils/messages'

//#region Schema
interface GroupRecordBase {
Expand Down Expand Up @@ -83,19 +84,39 @@ export async function updateUserGroupDatabase(

const t = (await db).transaction('groups', 'readwrite')
let nextRecord: GroupRecord
const nonDuplicateNewMembers: PersonIdentifier[] = []
if (type === 'replace') {
nextRecord = { ...orig, ...group }
} else if (type === 'append') {
const nextMembers = new Set<string>()
for (const i of orig.members) {
nextMembers.add(i.toText())
}
for (const i of group.members || []) {
if (!nextMembers.has(i.toText())) {
nextMembers.add(i.toText())
nonDuplicateNewMembers.push(i)
}
}
nextRecord = {
identifier: group.identifier,
banned: !orig.banned && !group.banned ? undefined : [...(orig.banned || []), ...(group.banned || [])],
groupName: group.groupName || orig.groupName,
members: orig.members.concat(...(group.members || [])),
members: Array.from(nextMembers).map(x => Identifier.fromString(x) as PersonIdentifier),
}
} else {
nextRecord = type(orig) || orig
}
await t.objectStore('groups').put(GroupRecordIntoDB(nextRecord))
nonDuplicateNewMembers.length &&
MessageCenter.emit(
'joinGroup',
{
group: group.identifier,
newMembers: nonDuplicateNewMembers,
},
true,
)
}

/**
Expand Down
8 changes: 0 additions & 8 deletions src/database/helpers/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ export function createDefaultFriendsGroup(who: PersonIdentifier) {
export async function addPersonToFriendsGroup(group: GroupIdentifier, newMembers: (Person | PersonIdentifier)[]) {
const memberList = newMembers.map(x => (x instanceof PersonIdentifier ? x : x.identifier)) as PersonIdentifier[]
await updateUserGroupDatabase({ identifier: group, members: memberList }, 'append')
MessageCenter.emit(
'joinGroup',
{
group,
newMembers: memberList,
},
true,
)
}
export function removePersonFromFriendsGroup(group: GroupIdentifier, removedFriend: (Person | PersonIdentifier)[]) {
const friendList = removedFriend.map(x => (x instanceof PersonIdentifier ? x : x.identifier)) as PersonIdentifier[]
Expand Down
23 changes: 12 additions & 11 deletions src/database/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,14 @@ const db = openDB<PostDB>('maskbook-post-v2', 3, {
for (const each of oldType) {
newType[each.toText()] = { reason: [{ type: 'direct', at: new Date(0) }] }
}

cursor.update({
const next: PostDBRecord = {
...cursor.value,
recipients: newType,
postBy: PersonIdentifier.unknown,
foundAt: new Date(0),
})
recipientGroups: [],
}
cursor.update(next)
}
}
},
Expand All @@ -129,14 +130,14 @@ export async function updatePostDB(
updateRecord: Partial<PostRecord> & Pick<PostRecord, 'identifier'>,
mode: 'append' | 'override',
): Promise<void> {
const currentRecord =
(await queryPostDB(updateRecord.identifier)) ||
({
identifier: updateRecord.identifier,
recipients: {},
postBy: PersonIdentifier.unknown,
foundAt: new Date(),
} as PostRecord)
const emptyRecord: PostRecord = {
identifier: updateRecord.identifier,
recipients: {},
postBy: PersonIdentifier.unknown,
foundAt: new Date(),
recipientGroups: [],
}
const currentRecord = (await queryPostDB(updateRecord.identifier)) || emptyRecord
const nextRecord: PostRecord = { ...currentRecord, ...updateRecord }
const nextRecipients: PostDBRecord['recipients'] =
mode === 'override' ? toDb(nextRecord).recipients : toDb(currentRecord).recipients
Expand Down
13 changes: 10 additions & 3 deletions src/extension/background-script/Jobs/AutoShareToFriends.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MessageCenter } from '../../../utils/messages'
import { queryUserGroup } from '../PeopleService'
import { PreDefinedVirtualGroupNames } from '../../../database/type'
import { PreDefinedVirtualGroupNames, PersonIdentifier } from '../../../database/type'
import { queryPostsDB } from '../../../database/post'
import { appendShareTarget } from '../CryptoService'

Expand Down Expand Up @@ -31,17 +31,24 @@ export function initAutoShareToFriends() {
console.warn('Post ', id, ' have no CryptoKey, skipping')
continue
}
const notSharedBefore: PersonIdentifier[] = []

data.newMembers.forEach(x => {
if (post.recipients[x.toText()]) {
// skipping
} else notSharedBefore.push(x)
})
appendShareTarget(
-38,
post.postCryptoKey,
post.identifier.postIV,
data.newMembers,
notSharedBefore,
data.group.ownerIdentifier,
group.identifier,
).then(() => {
console.log('Post ', id, ' shared')
}, console.error)
}
setTimeout(() => console.groupEnd(), 1000)
console.groupEnd()
})
}
20 changes: 10 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1160,13 +1160,13 @@
"@hapi/hoek" "8.x.x"

"@holoflows/kit@https://github.com/DimensionDev/holoflows-kit":
version "0.6.0"
resolved "https://github.com/DimensionDev/holoflows-kit#716841ae898119113a3cb3d49d44deff84a4edd4"
version "0.6.1"
resolved "https://github.com/DimensionDev/holoflows-kit#a6ba337fd54a006cf01734f1e6cc2d4e21ea2635"
dependencies:
"@types/lodash-es" "^4.1.4"
async-call-rpc "^1.0.0"
concurrent-lock "^1.0.7"
jsx-jsonml-devtools-renderer "^1.4.0"
jsx-jsonml-devtools-renderer "^1.4.1"
lodash-es "^4.17.15"
memorize-decorator "^0.2.2"
mitt "^1.1.3"
Expand Down Expand Up @@ -9701,10 +9701,10 @@ jsx-ast-utils@^2.1.0, jsx-ast-utils@^2.2.1:
array-includes "^3.0.3"
object.assign "^4.1.0"

jsx-jsonml-devtools-renderer@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/jsx-jsonml-devtools-renderer/-/jsx-jsonml-devtools-renderer-1.4.0.tgz#ab3ca51c913a4e04616154e6242f4286d1b9c253"
integrity sha512-if1QA9ZLdtpYq9CZayD2dcj7pcdS7i2aVX6x0TP+Wuwr58OspyuhR8uimZXRL+KYjZyBr1FnI4fLPbF9bmOOag==
jsx-jsonml-devtools-renderer@^1.4.1:
version "1.4.2"
resolved "https://registry.yarnpkg.com/jsx-jsonml-devtools-renderer/-/jsx-jsonml-devtools-renderer-1.4.2.tgz#600edbe8841b4a88665d47275464d747d78ba6e5"
integrity sha512-G2/5Nh2BXnOWzfXTKwHpSg05ZI9l6yf1VAx7tbgjkoUN6fBdcqev0jgBxD8tQgy+zBBxXI73Uu6qZG4NmefV0g==
dependencies:
csstype "^2.6.6"

Expand Down Expand Up @@ -10688,9 +10688,9 @@ mississippi@^3.0.0:
through2 "^2.0.0"

mitt@^1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/mitt/-/mitt-1.1.3.tgz#528c506238a05dce11cd914a741ea2cc332da9b8"
integrity sha512-mUDCnVNsAi+eD6qA0HkRkwYczbLHJ49z17BGe2PYRhZL4wpZUFZGJHU7/5tmvohoma+Hdn0Vh/oJTiPEmgSruA==
version "1.2.0"
resolved "https://registry.yarnpkg.com/mitt/-/mitt-1.2.0.tgz#cb24e6569c806e31bd4e3995787fe38a04fdf90d"
integrity sha512-r6lj77KlwqLhIUku9UWYes7KJtsczvolZkzp8hbaDPPaE24OmWl5s539Mytlj22siEQKosZ26qCBgda2PKwoJw==

mixed-map@^0.1.0:
version "0.1.1"
Expand Down

0 comments on commit 4a04f33

Please sign in to comment.