Skip to content

Commit

Permalink
Send operation logs at opening a keyboard and flashing a keymap.
Browse files Browse the repository at this point in the history
  • Loading branch information
yoichiro committed Dec 14, 2023
1 parent 1b045ba commit dac2bd6
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
10 changes: 9 additions & 1 deletion firestore.rules
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ service cloud.firestore {
&& (request.auth.uid == resource.data.uid);
}

match /logs/{definitionID}/operations/{operationLogID} {
allow create;
allow update: if false;
allow delete: if false;
allow read: if isAuthenticated()
&& (request.auth.uid == getDefinition(definitionID).data.author_uid);
}

function isAuthenticated() {
return request.auth.uid != null;
}
Expand All @@ -149,4 +157,4 @@ service cloud.firestore {
return get(/databases/$(database)/documents/keyboards/v2/definitions/$(definitionID));
}
}
}
}
12 changes: 10 additions & 2 deletions src/actions/hid.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export const hidActionsThunk = {
dispatch: ThunkDispatch<RootState, undefined, ActionTypes>,
getState: () => RootState
) => {
const { app, entities } = getState();
const { app, entities, storage } = getState();
const keyboard = entities.keyboard!;
const result = await keyboard.open();
if (!result.success) {
Expand All @@ -291,6 +291,10 @@ export const hidActionsThunk = {
product_id: keyboard.getInformation().productId,
product_name: keyboard.getInformation().productName,
});
await storage.instance!.sendOperationLog(
entities.keyboardDefinitionDocument!.id,
'configure/open'
);

const isBleMicroPro = keyboard
.getInformation()
Expand Down Expand Up @@ -487,13 +491,17 @@ export const hidActionsThunk = {
dispatch: ThunkDispatch<RootState, undefined, ActionTypes>,
getState: () => RootState
) => {
const { app, entities } = getState();
const { app, entities, storage } = getState();
const keyboard: IKeyboard = entities.keyboard!;
sendEventToGoogleAnalytics('configure/flash', {
vendor_id: keyboard.getInformation().vendorId,
product_id: keyboard.getInformation().productId,
product_name: keyboard.getInformation().productName,
});
await storage.instance!.sendOperationLog(
entities.keyboardDefinitionDocument!.id,
'configure/flash'
);
const remaps = app.remaps;
for (let layer = 0; layer < remaps.length; layer++) {
const remap = remaps[layer];
Expand Down
31 changes: 31 additions & 0 deletions src/services/provider/Firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
IFirmwareBuildingTask,
BUILDABLE_FIRMWARE_QMK_FIRMWARE_VERSION,
IBuildableFirmwareQmkFirmwareVersion,
IOperationLogType,
} from '../storage/Storage';
import { IAuth, IAuthenticationResult } from '../auth/Auth';
import { IFirmwareCodePlace, IKeyboardFeatures } from '../../store/state';
Expand Down Expand Up @@ -1990,4 +1991,34 @@ export class FirebaseProvider implements IStorage, IAuth {
);
}
}

async sendOperationLog(
keyboardDefinitionId: string,
operation: IOperationLogType
): Promise<void> {
try {
const doc: {
operation: IOperationLogType;
uid?: string;
createdAt: Date;
expireAt: Date;
} = {
operation,
createdAt: new Date(),
// This operation log will be deleted after 90 days.
expireAt: new Date(Date.now() + 90 * 24 * 60 * 60 * 1000),
};
if (this.auth.currentUser !== null) {
doc.uid = this.auth.currentUser.uid;
}
await this.db
.collection('logs')
.doc(keyboardDefinitionId)
.collection('operations')
.add(doc);
} catch (error) {
console.error(error);
// Ignore error.
}
}
}
7 changes: 7 additions & 0 deletions src/services/storage/Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ export type IFirmwareBuildingTask = {
updatedAt: Date;
};

export type IOperationLogType = 'configure/flash' | 'configure/open';

/* eslint-disable no-unused-vars */
export interface IStorage {
fetchKeyboardDefinitionDocumentByDeviceInfo(
Expand Down Expand Up @@ -531,5 +533,10 @@ export interface IStorage {
description: string
): Promise<IEmptyResult>;
fetchAllBuildableFirmwares(): Promise<IResult<IBuildableFirmware[]>>;

sendOperationLog(
keyboardDefinitionId: string,
operation: IOperationLogType
): Promise<void>;
}
/* eslint-enable no-unused-vars */

0 comments on commit dac2bd6

Please sign in to comment.