Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/functions/code-of-conduct/blockUser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import {getAuth} from 'firebase-admin/auth';
import {
checkAuthenticationAndAccess,
BlockUserParams,
Expand All @@ -21,7 +21,7 @@ export const blockUser = functions.https.onCall<BlockUserParams>(
/** The Github client for performing Github actions. */
const github = await getAuthenticatedGithubClient();
/** The user performing the block action */
const actor = await admin.auth().getUser(authRequest.auth.uid);
const actor = await getAuth().getUser(authRequest.auth.uid);
/** The display name of the user. */
const actorName = actor.displayName || actor.email || 'Unknown User';
/** The Firestore Document for the user being blocked. */
Expand Down
18 changes: 12 additions & 6 deletions apps/functions/code-of-conduct/shared.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import * as admin from 'firebase-admin';
import {getAuth} from 'firebase-admin/auth';
import {
DocumentData,
FirestoreDataConverter,
QueryDocumentSnapshot,
getFirestore,
} from 'firebase-admin/firestore';
import {Octokit} from '@octokit/rest';
import {createAppAuth} from '@octokit/auth-app';
import * as functions from 'firebase-functions';
Expand All @@ -19,11 +25,11 @@ export interface UnblockUserParams {
/**
* Convertor to ensure the data types for javascript and firestore storage are in sync.
*/
export const converter: admin.firestore.FirestoreDataConverter<BlockedUser> = {
export const converter: FirestoreDataConverter<BlockedUser> = {
toFirestore: (user: BlockedUser) => {
return user;
},
fromFirestore: (data: admin.firestore.QueryDocumentSnapshot<BlockedUser>) => {
fromFirestore: (data: QueryDocumentSnapshot<BlockedUser>) => {
return {
username: data.get('username'),
context: data.get('context'),
Expand All @@ -38,10 +44,10 @@ export const converter: admin.firestore.FirestoreDataConverter<BlockedUser> = {

/** Get the firestore collection for the blocked users, with the converter already set up. */
export const blockedUsersCollection = () =>
admin.firestore().collection('blockedUsers').withConverter(converter);
getFirestore().collection('blockedUsers').withConverter(converter);

/** A blocked user stored in Firestore. */
export interface BlockedUser extends admin.firestore.DocumentData {
export interface BlockedUser extends DocumentData {
blockedBy: string;
blockedOn: Date;
username: string;
Expand All @@ -66,7 +72,7 @@ export async function checkAuthenticationAndAccess(
throw new functions.https.HttpsError('unauthenticated', 'This action requires authentication');
}

const user = await admin.auth().getUser(context.auth.uid);
const user = await getAuth().getUser(context.auth.uid);
const githubProvider = user.providerData.find((data) => data.providerId === 'github.com');

if (!githubProvider) {
Expand Down
22 changes: 16 additions & 6 deletions apps/functions/code-of-conduct/unblockUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
getAuthenticatedGithubClient,
} from './shared.js';
import {Octokit} from '@octokit/rest';
import * as admin from 'firebase-admin';
import {DocumentSnapshot} from 'firebase-admin/firestore';
import * as functions from 'firebase-functions';

/** Unblocks the provided user from Github, clearing their records from our listing. */
Expand Down Expand Up @@ -41,12 +41,22 @@ export const dailyUnblock = functions.scheduler.onSchedule(
.where('blockUntil', '<', new Date())
.get();

await Promise.all(usersToUnblock.docs.map(async (user) => performUnblock(github, user)));
const results = await Promise.allSettled(
usersToUnblock.docs.map((user) => performUnblock(github, user)),
);
for (const result of results) {
if (result.status === 'rejected') {
functions.logger.error('Failed to unblock user:', result.reason);
}
}
},
);

async function performUnblock(github: Octokit, doc: admin.firestore.DocumentSnapshot<BlockedUser>) {
await github.orgs
.unblockUser({org: 'angular', username: doc.get('username')})
.then(() => doc.ref.delete());
async function performUnblock(github: Octokit, doc: DocumentSnapshot<BlockedUser>): Promise<void> {
const data = doc.data();
if (!data) {
throw new Error(`No blocked user record found for document "${doc.id}".`);
}
await github.orgs.unblockUser({org: 'angular', username: data.username});
await doc.ref.delete();
}
Comment thread
alan-agius4 marked this conversation as resolved.
4 changes: 2 additions & 2 deletions apps/functions/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './code-of-conduct/index.js';
export * from './dns-redirecting/index.js';
import * as admin from 'firebase-admin';
import {initializeApp} from 'firebase-admin/app';

admin.initializeApp();
initializeApp();
2 changes: 1 addition & 1 deletion apps/functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@octokit/rest": "22.0.1",
"@octokit/webhooks-types": "7.6.1",
"@types/node": "24.13.3",
"firebase-admin": "13.10.0",
"firebase-admin": "14.2.0",
"firebase-functions": "7.3.2",
"firebase-tools": "15.25.1"
}
Expand Down
Loading