-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
186 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { FrontingEntry, Member, System, Tag } from "../entities"; | ||
import { PartialBy } from "../../types"; | ||
import { newSystem } from "../tables/system"; | ||
import { getTables } from ".."; | ||
import { newTag } from "../tables/tags"; | ||
import { newMember } from "../tables/members"; | ||
import { newFrontingEntry } from "../tables/frontingEntries"; | ||
|
||
export async function importPluralKit(pkExport: any){ | ||
// WIPE AMPERSAND | ||
await Promise.all(getTables().map(async x => x.clear())); | ||
|
||
// SYSTEM | ||
const systemInfo: PartialBy<System, "uuid"> = { | ||
name: pkExport.name, | ||
description: pkExport.description, | ||
} | ||
if (pkExport.avatar_url) { | ||
try{ | ||
const request = await fetch(pkExport.avatar_url); | ||
systemInfo.image = new File([await request.blob()], pkExport.avatar_url.split("/").pop()); | ||
}catch(e) { | ||
// whatever | ||
} | ||
} | ||
await newSystem(systemInfo); | ||
|
||
// TAGS | ||
const tagMapping = new Map<string, string>(); | ||
for (const pkGroup of pkExport.groups) { | ||
const tag: PartialBy<Tag, "uuid"> = { | ||
name: pkGroup.display_name || pkGroup.name, | ||
description: pkGroup.description, | ||
color: pkGroup.color ? "#" + pkGroup.color : undefined, | ||
type: "member" | ||
}; | ||
const uuid = await newTag(tag); | ||
if(!uuid) return false; | ||
tagMapping.set(pkGroup.id, uuid); | ||
} | ||
|
||
// MEMBERS | ||
const memberMapping = new Map<string, string>(); | ||
for (const pkMember of pkExport.members) { | ||
const member: PartialBy<Member, "uuid"> = { | ||
name: pkMember.display_name || pkMember.name, | ||
description: pkMember.description, | ||
pronouns: pkMember.pronouns, | ||
color: pkMember.color ? "#" + pkMember.color : undefined, | ||
isArchived: false, | ||
isCustomFront: false, | ||
dateCreated: new Date(pkMember.created), | ||
tags: pkExport.groups.filter(x => x.members.includes(pkMember.id)).map(x => tagMapping.get(x.id)) | ||
} | ||
if (pkMember.avatar_url) { | ||
try{ | ||
const request = await fetch(pkMember.avatar_url); | ||
member.image = new File([await request.blob()], pkMember.avatar_url.split("/").pop()); | ||
}catch(e){ | ||
// whatever, again | ||
} | ||
} | ||
const uuid = await newMember(member); | ||
if(!uuid) return false; | ||
memberMapping.set(pkMember.id, uuid); | ||
} | ||
|
||
// FRONTING ENTRIES | ||
const frontingEntries = new Map<string, PartialBy<FrontingEntry, "uuid">>(); | ||
for(const pkSwitch of pkExport.switches.sort((a, b) => new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime())){ | ||
const date = new Date(pkSwitch.timestamp); | ||
|
||
// check who left | ||
for(const id of frontingEntries.keys()){ | ||
if(!pkSwitch.members.includes(id)){ | ||
const frontingEntry = frontingEntries.get(id)!; | ||
frontingEntry.endTime = date; | ||
await newFrontingEntry(frontingEntry); | ||
frontingEntries.delete(id); | ||
} | ||
} | ||
|
||
// check who's new | ||
for(const id of pkSwitch.members){ | ||
if(!frontingEntries.has(id)){ | ||
const frontingEntry: PartialBy<FrontingEntry, "uuid"> = { | ||
member: memberMapping.get(id)!, | ||
startTime: date, | ||
isMainFronter: false | ||
} | ||
frontingEntries.set(id, frontingEntry); | ||
} | ||
} | ||
|
||
} | ||
for(const frontingEntry of frontingEntries.values()){ | ||
await newFrontingEntry(frontingEntry); | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters