Changeset View
Changeset View
Standalone View
Standalone View
keyserver/src/fetchers/link-fetchers.js
// @flow | // @flow | ||||
import type { | import type { | ||||
InviteLink, | |||||
InviteLinkVerificationRequest, | InviteLinkVerificationRequest, | ||||
InviteLinkVerificationResponse, | InviteLinkVerificationResponse, | ||||
} from 'lib/types/link-types.js'; | } from 'lib/types/link-types.js'; | ||||
import { dbQuery, SQL } from '../database/database.js'; | import { dbQuery, SQL } from '../database/database.js'; | ||||
import { Viewer } from '../session/viewer.js'; | import { Viewer } from '../session/viewer.js'; | ||||
async function verifyInviteLink( | async function verifyInviteLink( | ||||
Show All 38 Lines | const query = SQL` | ||||
WHERE i.name = ${secret} | WHERE i.name = ${secret} | ||||
AND i.community = ${communityID} | AND i.community = ${communityID} | ||||
AND c.community IS NULL | AND c.community IS NULL | ||||
`; | `; | ||||
const [result] = await dbQuery(query); | const [result] = await dbQuery(query); | ||||
return result.length === 1; | return result.length === 1; | ||||
} | } | ||||
export { verifyInviteLink, checkIfInviteLinkIsValid }; | async function fetchPrimaryInviteLinks( | ||||
viewer: Viewer, | |||||
): Promise<$ReadOnlyArray<InviteLink>> { | |||||
const query = SQL` | |||||
SELECT i.name, i.role, i.community, i.expiration_time AS expirationTime, | |||||
i.limit_of_uses AS limitOfUses, i.number_of_uses AS numberOfUses, | |||||
i.\`primary\` | |||||
FROM invite_links i | |||||
INNER JOIN memberships m | |||||
ON i.community = m.thread AND m.user = ${viewer.userID} | |||||
WHERE i.\`primary\` = 1 AND m.role > 0 | |||||
`; | |||||
const [result] = await dbQuery(query); | |||||
return result.map(row => ({ | |||||
name: row.name, | |||||
primary: row.primary, | |||||
role: row.role.toString(), | |||||
communityID: row.community.toString(), | |||||
expirationTime: row.expirationTime, | |||||
limitOfUses: row.limitOfUses, | |||||
numberOfUses: row.numberOfUses, | |||||
})); | |||||
} | |||||
export { verifyInviteLink, checkIfInviteLinkIsValid, fetchPrimaryInviteLinks }; |