base implementation done, now onto mobile app

This commit is contained in:
user
2026-03-01 07:39:49 +02:00
parent 8197c775ba
commit eb2f82247b
9 changed files with 872 additions and 64 deletions

View File

@@ -1,4 +1,4 @@
import {
import type {
ListMobileDeviceMediaFilters,
ListMobileDeviceSMSFilters,
ListMobileDevicesFilters,
@@ -8,13 +8,13 @@ import {
PingMobileDevice,
RegisterMobileDevice,
} from "./data";
import { FlowExecCtx } from "@core/flow.execution.context";
import type { FlowExecCtx } from "@core/flow.execution.context";
import { traceResultAsync } from "@core/observability";
import { MobileRepository } from "./repository";
import { errAsync } from "neverthrow";
import { db } from "@pkg/db";
import { settings } from "@core/settings";
import { mobileErrors } from "./errors";
import { errAsync } from "neverthrow";
import { db } from "@pkg/db";
export class MobileController {
constructor(
@@ -24,14 +24,20 @@ export class MobileController {
registerDevice(fctx: FlowExecCtx, payload: RegisterMobileDevice) {
return traceResultAsync({
name: "logic.mobile.controller.register",
name: "mobile.register",
fctx,
attributes: { "app.mobile.external_device_id": payload.externalDeviceId },
attributes: {
"app.mobile.external_device_id": payload.externalDeviceId,
},
fn: () =>
this.mobileRepo
.findAdminOwnerId(fctx, this.defaultAdminEmail)
.andThen((ownerUserId) =>
this.mobileRepo.upsertDevice(fctx, payload, ownerUserId),
this.mobileRepo.upsertDevice(
fctx,
payload,
ownerUserId,
),
),
});
}
@@ -42,12 +48,13 @@ export class MobileController {
payload?: PingMobileDevice,
) {
return traceResultAsync({
name: "logic.mobile.controller.ping",
name: "mobile.ping",
fctx,
attributes: { "app.mobile.external_device_id": externalDeviceId },
fn: () =>
this.mobileRepo.getDeviceByExternalId(fctx, externalDeviceId).andThen(
(device) => {
this.mobileRepo
.getDeviceByExternalId(fctx, externalDeviceId)
.andThen((device) => {
const pingAt = payload?.pingAt
? new Date(payload.pingAt as Date | string)
: new Date();
@@ -59,9 +66,12 @@ export class MobileController {
),
);
}
return this.mobileRepo.touchDevicePing(fctx, device.id, pingAt);
},
),
return this.mobileRepo.touchDevicePing(
fctx,
device.id,
pingAt,
);
}),
});
}
@@ -71,24 +81,27 @@ export class MobileController {
messages: MobileSMSInput[],
) {
return traceResultAsync({
name: "logic.mobile.controller.sms_sync",
name: "mobile.sms.sync",
fctx,
attributes: {
"app.mobile.external_device_id": externalDeviceId,
"app.mobile.sms.received_count": messages.length,
},
fn: () =>
this.mobileRepo.getDeviceByExternalId(fctx, externalDeviceId).andThen(
(device) =>
this.mobileRepo
.getDeviceByExternalId(fctx, externalDeviceId)
.andThen((device) =>
this.mobileRepo
.syncSMS(fctx, device.id, messages)
.andThen((syncResult) =>
this.mobileRepo.touchDevicePing(fctx, device.id).map(() => ({
...syncResult,
deviceId: device.id,
})),
this.mobileRepo
.touchDevicePing(fctx, device.id)
.map(() => ({
...syncResult,
deviceId: device.id,
})),
),
),
),
});
}
@@ -98,24 +111,27 @@ export class MobileController {
assets: MobileMediaAssetInput[],
) {
return traceResultAsync({
name: "logic.mobile.controller.media_sync",
name: "mobile.media.sync",
fctx,
attributes: {
"app.mobile.external_device_id": externalDeviceId,
"app.mobile.media.received_count": assets.length,
},
fn: () =>
this.mobileRepo.getDeviceByExternalId(fctx, externalDeviceId).andThen(
(device) =>
this.mobileRepo
.getDeviceByExternalId(fctx, externalDeviceId)
.andThen((device) =>
this.mobileRepo
.syncMediaAssets(fctx, device.id, assets)
.andThen((syncResult) =>
this.mobileRepo.touchDevicePing(fctx, device.id).map(() => ({
...syncResult,
deviceId: device.id,
})),
this.mobileRepo
.touchDevicePing(fctx, device.id)
.map(() => ({
...syncResult,
deviceId: device.id,
})),
),
),
),
});
}
@@ -125,7 +141,7 @@ export class MobileController {
pagination: MobilePagination,
) {
return traceResultAsync({
name: "logic.mobile.controller.list_devices",
name: "mobile.devices.list",
fctx,
attributes: { "app.user.id": filters.ownerUserId },
fn: () => this.mobileRepo.listDevices(fctx, filters, pagination),
@@ -134,10 +150,14 @@ export class MobileController {
getDeviceDetail(fctx: FlowExecCtx, deviceId: number, ownerUserId: string) {
return traceResultAsync({
name: "logic.mobile.controller.get_device_detail",
name: "mobile.device.detail",
fctx,
attributes: { "app.user.id": ownerUserId, "app.mobile.device_id": deviceId },
fn: () => this.mobileRepo.getDeviceDetail(fctx, deviceId, ownerUserId),
attributes: {
"app.user.id": ownerUserId,
"app.mobile.device_id": deviceId,
},
fn: () =>
this.mobileRepo.getDeviceDetail(fctx, deviceId, ownerUserId),
});
}
@@ -147,7 +167,7 @@ export class MobileController {
pagination: MobilePagination,
) {
return traceResultAsync({
name: "logic.mobile.controller.list_device_sms",
name: "mobile.device.sms.list",
fctx,
attributes: { "app.mobile.device_id": filters.deviceId },
fn: () => this.mobileRepo.listDeviceSMS(fctx, filters, pagination),
@@ -160,40 +180,54 @@ export class MobileController {
pagination: MobilePagination,
) {
return traceResultAsync({
name: "logic.mobile.controller.list_device_media",
name: "mobile.device.media.list",
fctx,
attributes: { "app.mobile.device_id": filters.deviceId },
fn: () => this.mobileRepo.listDeviceMedia(fctx, filters, pagination),
fn: () =>
this.mobileRepo.listDeviceMedia(fctx, filters, pagination),
});
}
deleteMediaAsset(fctx: FlowExecCtx, mediaAssetId: number, ownerUserId: string) {
deleteMediaAsset(
fctx: FlowExecCtx,
mediaAssetId: number,
ownerUserId: string,
) {
return traceResultAsync({
name: "logic.mobile.controller.delete_media_asset",
name: "mobile.media.delete",
fctx,
attributes: {
"app.user.id": ownerUserId,
"app.mobile.media_asset_id": mediaAssetId,
},
fn: () => this.mobileRepo.deleteMediaAsset(fctx, mediaAssetId, ownerUserId),
fn: () =>
this.mobileRepo.deleteMediaAsset(
fctx,
mediaAssetId,
ownerUserId,
),
});
}
deleteDevice(fctx: FlowExecCtx, deviceId: number, ownerUserId: string) {
return traceResultAsync({
name: "logic.mobile.controller.delete_device",
name: "mobile.device.delete",
fctx,
attributes: { "app.user.id": ownerUserId, "app.mobile.device_id": deviceId },
attributes: {
"app.user.id": ownerUserId,
"app.mobile.device_id": deviceId,
},
fn: () => this.mobileRepo.deleteDevice(fctx, deviceId, ownerUserId),
});
}
resolveDeviceByExternalId(fctx: FlowExecCtx, externalDeviceId: string) {
return traceResultAsync({
name: "logic.mobile.controller.resolve_device",
name: "mobile.device.resolve",
fctx,
attributes: { "app.mobile.external_device_id": externalDeviceId },
fn: () => this.mobileRepo.getDeviceByExternalId(fctx, externalDeviceId),
fn: () =>
this.mobileRepo.getDeviceByExternalId(fctx, externalDeviceId),
});
}
}