Add deviceId and deviceName to drafts, skip loading media for other devies

This commit is contained in:
Eric Bailey
2026-01-30 14:30:41 -06:00
parent 84c5d0c3c6
commit e7449e95a9
7 changed files with 78 additions and 12 deletions
+16
View File
@@ -0,0 +1,16 @@
import * as Device from 'expo-device'
import * as env from '#/env'
export function getDeviceName(): string {
const deviceName = Device.deviceName
if (env.IS_ANDROID) {
return deviceName || 'Android'
} else if (env.IS_IOS) {
// we need an entitlement to get the real device name on iOS, so just
// return a generic name for now
return 'iOS'
} else {
return 'Web' // could append browser info here
}
}
+2 -2
View File
@@ -138,7 +138,7 @@ import {
type RestoredVideo,
} from './drafts/state/api'
import {
loadDraft,
loadDraftMedia,
useCleanupPublishedDraftMutation,
useSaveDraftMutation,
} from './drafts/state/queries'
@@ -482,7 +482,7 @@ export const ComposePost = ({
})
// Load local media files for the draft
const {loadedMedia} = await loadDraft(draftSummary.draft)
const {loadedMedia} = await loadDraftMedia(draftSummary.draft)
// Extract original localRefs for orphan detection on save
const originalLocalRefs = extractLocalRefs(draftSummary.draft)
+2 -2
View File
@@ -1,11 +1,11 @@
/**
* Type converters for Draft API - convert between ComposerState and server Draft types.
*/
import {Platform} from 'react-native'
import {type AppBskyDraftDefs, AtUri, RichText} from '@atproto/api'
import {nanoid} from 'nanoid/non-secure'
import {resolveLink} from '#/lib/api/resolve'
import {getDeviceName} from '#/lib/deviceName'
import {getImageDim} from '#/lib/media/manip'
import {mimeToExt} from '#/lib/media/video/util'
import {type ComposerImage} from '#/state/gallery'
@@ -69,7 +69,7 @@ export async function composerStateToDraft(state: ComposerState): Promise<{
const draft: AppBskyDraftDefs.Draft = {
$type: 'app.bsky.draft.defs#draft',
deviceId: getDeviceId(),
platform: Platform.OS,
deviceName: getDeviceName().slice(0, 100), // max length of 100 in lex
posts,
threadgateAllow: threadgateAllowUISettingToAllowRecordValue(
state.thread.threadgate,
+14 -7
View File
@@ -8,6 +8,7 @@ import {
import {isNetworkError} from '#/lib/strings/errors'
import {useAgent} from '#/state/session'
import {type ComposerState} from '#/view/com/composer/state/composer'
import {getDeviceId} from '#/analytics/identifiers'
import {composerStateToDraft, draftViewToSummary} from './api'
import {logger} from './logger'
import * as storage from './storage'
@@ -42,11 +43,17 @@ export function useDraftsQuery() {
* Load a draft's local media for editing.
* Takes the full Draft object (from DraftSummary) to avoid re-fetching.
*/
export async function loadDraft(draft: AppBskyDraftDefs.Draft): Promise<{
export async function loadDraftMedia(draft: AppBskyDraftDefs.Draft): Promise<{
loadedMedia: Map<string, string>
}> {
// Load local media files
const loadedMedia = new Map<string, string>()
// can't load media from another device
if (draft.deviceId && draft.deviceId !== getDeviceId()) {
return {loadedMedia}
}
for (const post of draft.posts) {
// Load images
if (post.embedImages) {
@@ -54,10 +61,10 @@ export async function loadDraft(draft: AppBskyDraftDefs.Draft): Promise<{
try {
const url = await storage.loadMediaFromLocal(img.localRef.path)
loadedMedia.set(img.localRef.path, url)
} catch (e) {
logger.debug('Failed to load draft image', {
} catch (e: any) {
logger.error('Failed to load draft image', {
path: img.localRef.path,
error: e,
safeMessage: e.message,
})
}
}
@@ -68,10 +75,10 @@ export async function loadDraft(draft: AppBskyDraftDefs.Draft): Promise<{
try {
const url = await storage.loadMediaFromLocal(vid.localRef.path)
loadedMedia.set(vid.localRef.path, url)
} catch (e) {
logger.debug('Failed to load draft video', {
} catch (e: any) {
logger.error('Failed to load draft video', {
path: vid.localRef.path,
error: e,
safeMessage: e.message,
})
}
}