Skip to content

Commit

Permalink
feat: create user group for old users
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed Sep 18, 2019
1 parent e1f3a17 commit ee1423d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/database/helpers/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface Group extends GroupRecord {

export function createDefaultFriendsGroup(who: PersonIdentifier) {
return createUserGroupDatabase(
new GroupIdentifier(who.network, PreDefinedVirtualGroupNames.friends, GroupType.virtual, who.userId),
GroupIdentifier.getDefaultFriendsGroupIdentifier(who),
// Put the raw special name in, then UI can display in their own language.
PreDefinedVirtualGroupNames.friends,
)
Expand Down
15 changes: 15 additions & 0 deletions src/database/migrate/create.user.group.for.old.users.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { queryUserGroupDatabase } from '../group'
import { getMyIdentitiesDB } from '../people'
import { GroupIdentifier } from '../type'
import { createDefaultFriendsGroup } from '../helpers/group'

/**
* If an identity has no default user group, create one
*/
export default async function createUserGroupForOldUsers() {
const ids = await getMyIdentitiesDB()
for (const id of ids) {
const g = await queryUserGroupDatabase(GroupIdentifier.getDefaultFriendsGroupIdentifier(id.identifier))
if (!g) createDefaultFriendsGroup(id.identifier)
}
}
2 changes: 2 additions & 0 deletions src/database/migrate/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import avatar from './old.avatar.1'
import localKeys from './old.localKeys.1'
import keys from './old.keystore.1'
import createUserGroupForOldUsers from './create.user.group.for.old.users'

createUserGroupForOldUsers()
avatar()
.finally(() => localKeys())
.finally(() => keys())
3 changes: 3 additions & 0 deletions src/database/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ export enum PreDefinedVirtualGroupNames {
}
@serializable('GroupIdentifier')
export class GroupIdentifier extends Identifier {
static getDefaultFriendsGroupIdentifier(who: PersonIdentifier) {
return new GroupIdentifier(who.network, PreDefinedVirtualGroupNames.friends, GroupType.virtual, who.userId)
}
constructor(public network: string, public groupID: string, public type: GroupType, public belongs?: string) {
super()
noSlash(network)
Expand Down

0 comments on commit ee1423d

Please sign in to comment.