Skip to content

Commit

Permalink
feat: support for multi char
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Feb 8, 2023
1 parent c813c08 commit 54f76d2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 89 deletions.
86 changes: 12 additions & 74 deletions src/models/site.model.ts
Expand Up @@ -100,25 +100,17 @@ export const getUserSites = async (params: GetUserSitesParams) => {
}

export type GetAccountSitesParams = {
account: GeneralAccount
handle: string
unidata?: Unidata
}

export const getAccountSites = (
params: GetAccountSitesParams,
): Promise<Profile[] | null> => {
switch (params.account.type) {
case "email":
return getUserSites({
handle: params.account.character.handle,
unidata: params.unidata,
})
case "wallet":
return getUserSites({
address: params.account.address,
unidata: params.unidata,
})
}
return getUserSites({
handle: params.handle,
unidata: params.unidata,
})
}

export const getSite = async (input: string, customUnidata?: Unidata) => {
Expand Down Expand Up @@ -210,27 +202,15 @@ export const getSites = async (input: string[]) => {

export const getSubscription = async (
siteId: string,
account: GeneralAccount,
handle: string,
customUnidata?: Unidata,
) => {
const links = await (() => {
switch (account.type) {
case "email":
return (customUnidata || unidata).links.get({
source: "Crossbell Link",
identity: account.character.handle,
platform: "Crossbell",
filter: { to: siteId },
})
case "wallet":
return (customUnidata || unidata).links.get({
source: "Crossbell Link",
identity: account.address,
platform: "Ethereum",
filter: { to: siteId },
})
}
})()
const links = await (customUnidata || unidata).links.get({
source: "Crossbell Link",
identity: handle,
platform: "Crossbell",
filter: { to: siteId },
})

return !!links?.list?.length
}
Expand Down Expand Up @@ -387,27 +367,6 @@ export async function createSite(
)
}

export async function subscribeToSite(
input: {
userId: string
siteId: string
},
customUnidata?: Unidata,
) {
return (customUnidata || unidata).links.set(
{
source: "Crossbell Link",
identity: input.userId,
platform: "Ethereum",
action: "add",
},
{
to: input.siteId,
type: "follow",
},
)
}

export async function subscribeToSites(
input: {
user: Profile
Expand All @@ -427,27 +386,6 @@ export async function subscribeToSites(
}
}

export async function unsubscribeFromSite(
input: {
userId: string
siteId: string
},
customUnidata?: Unidata,
) {
return (customUnidata || unidata).links.set(
{
source: "Crossbell Link",
identity: input.userId,
platform: "Ethereum",
action: "remove",
},
{
to: input.siteId,
type: "follow",
},
)
}

const xLogOperatorPermissions: CharacterOperatorPermission[] = [
CharacterOperatorPermission.SET_NOTE_URI,
CharacterOperatorPermission.DELETE_NOTE,
Expand Down
35 changes: 20 additions & 15 deletions src/queries/site.ts
Expand Up @@ -13,14 +13,15 @@ import { useUnidata } from "./unidata"
export const useAccountSites = () => {
const unidata = useUnidata()
const account = useAccountState((s) => s.computed.account)
const address = account?.type === "email" ? account.email : account?.address
const handle =
account?.type === "email" ? account.character?.handle : account?.handle

return useQuery(["getUserSites", address], async () => {
if (!account) {
return useQuery(["getUserSites", handle], async () => {
if (!account || !handle) {
return []
}

return siteModel.getAccountSites({ account, unidata })
return siteModel.getAccountSites({ handle, unidata })
})
}

Expand All @@ -45,18 +46,18 @@ export const useGetSites = (input: string[]) => {

export const useGetSubscription = (siteId: string | undefined) => {
const account = useAccountState((s) => s.computed.account)
const handle =
account?.type === "email" ? account.character?.handle : account?.handle
const unidata = useUnidata()

return useQuery(
["getSubscription", siteId, account?.characterId],
async () => {
if (!account || !siteId) {
return false
}
console.log(["getSubscription", siteId, handle])
return useQuery(["getSubscription", siteId, handle], async () => {
if (!handle || !siteId) {
return false
}

return siteModel.getSubscription(siteId, account, unidata)
},
)
return siteModel.getSubscription(siteId, handle, unidata)
})
}

export const useGetSiteSubscriptions = (data: { siteId: string }) => {
Expand Down Expand Up @@ -135,7 +136,9 @@ export function useSubscribeToSite() {
queryClient.invalidateQueries([
"getSubscription",
variables.siteId,
account?.characterId,
account?.type === "email"
? account?.character?.handle
: account?.handle,
]),
])
},
Expand Down Expand Up @@ -187,7 +190,9 @@ export function useUnsubscribeFromSite() {
queryClient.invalidateQueries([
"getSubscription",
variables.siteId,
account?.characterId,
account?.type === "email"
? account?.character?.handle
: account?.handle,
]),
])
},
Expand Down

0 comments on commit 54f76d2

Please sign in to comment.