Add is_blocked to FileModel (#8842)

* Add is_blocked to FileModel

* fix type in the docs

* use correct type in docs
This commit is contained in:
Elias Nahum 2025-05-07 15:33:04 +08:00 committed by GitHub
parent 974f2419ef
commit f320dd65ad
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 69 additions and 22 deletions

View file

@ -7,6 +7,7 @@ import {
updateLocalFile,
updateLocalFilePath,
getLocalFileInfo,
setFileAsBlocked,
} from './file';
import type ServerDataOperator from '@database/operator/server_data_operator';
@ -72,4 +73,28 @@ describe('files', () => {
expect(file).toBeDefined();
expect(file!.id).toBe(fileInfo.id);
});
it('setFileAsBlocked - handle not found database', async () => {
const {error} = await setFileAsBlocked('foo', fileInfo.id!);
expect(error).toBeTruthy();
});
it('setFileAsBlocked', async () => {
await operator.handleFiles({files: [fileInfo], prepareRecordsOnly: false});
const {error} = await setFileAsBlocked(serverUrl, fileInfo.id!);
expect(error).toBeUndefined();
const {file} = await getLocalFileInfo(serverUrl, fileInfo.id!);
expect(file).toBeDefined();
expect(file!.isBlocked).toBe(true);
});
it('setFileAsBlocked - no file', async () => {
const {error} = await setFileAsBlocked(serverUrl, fileInfo.id!);
expect(error).toBeUndefined();
const {file} = await getLocalFileInfo(serverUrl, fileInfo.id!);
expect(file).toBeUndefined();
});
});

View file

@ -34,6 +34,24 @@ export const updateLocalFilePath = async (serverUrl: string, fileId: string, loc
}
};
export const setFileAsBlocked = async (serverUrl: string, fileId: string) => {
try {
const {database} = DatabaseManager.getServerDatabaseAndOperator(serverUrl);
const file = await getFileById(database, fileId);
if (file) {
await database.write(async () => {
await file.update((r) => {
r.isBlocked = true;
});
});
}
return {error: undefined};
} catch (error) {
logError('Failed setFileAsBlocked', error);
return {error};
}
};
export const getLocalFileInfo = async (serverUrl: string, fileId: string) => {
try {
const {database} = DatabaseManager.getServerDatabaseAndOperator(serverUrl);

View file

@ -8,9 +8,20 @@ import {addColumns, createTable, schemaMigrations, unsafeExecuteSql} from '@nozb
import {MM_TABLES} from '@constants/database';
const {CHANNEL_BOOKMARK, CHANNEL_INFO, DRAFT, POST, CHANNEL, CUSTOM_PROFILE_ATTRIBUTE, CUSTOM_PROFILE_FIELD, SCHEDULED_POST} = MM_TABLES.SERVER;
const {CHANNEL_BOOKMARK, CHANNEL_INFO, DRAFT, FILE, POST, CHANNEL, CUSTOM_PROFILE_ATTRIBUTE, CUSTOM_PROFILE_FIELD, SCHEDULED_POST} = MM_TABLES.SERVER;
export default schemaMigrations({migrations: [
{
toVersion: 10,
steps: [
addColumns({
table: FILE,
columns: [
{name: 'is_blocked', type: 'boolean'},
],
}),
],
},
{
toVersion: 9,
steps: [

View file

@ -56,6 +56,9 @@ export default class FileModel extends Model implements FileModelInterface {
/** width : The width of the file object/image */
@field('width') width!: number;
/** isBlocked : Whether the file is blocked and cannot be opened */
@field('is_blocked') isBlocked!: boolean;
/** post : The related Post record for this file */
@immutableRelation(POST, 'post_id') post!: Relation<PostModel>;

View file

@ -157,6 +157,7 @@ export const transformFileRecord = ({action, database, value}: TransformerArgs<F
file.height = raw?.height || record?.height || 0;
file.imageThumbnail = raw?.mini_preview || record?.imageThumbnail || '';
file.localPath = raw?.localPath || record?.localPath || null;
file.isBlocked = record?.isBlocked ?? false;
};
return prepareBaseRecord({

View file

@ -43,7 +43,7 @@ import {
} from './table_schemas';
export const serverSchema: AppSchema = appSchema({
version: 9,
version: 10,
tables: [
CategorySchema,
CategoryChannelSchema,

View file

@ -19,5 +19,6 @@ export default tableSchema({
{name: 'post_id', type: 'string', isIndexed: true},
{name: 'size', type: 'number'},
{name: 'width', type: 'number'},
{name: 'is_blocked', type: 'boolean'},
],
});

View file

@ -49,7 +49,7 @@ const {
describe('*** Test schema for SERVER database ***', () => {
it('=> The SERVER SCHEMA should strictly match', () => {
expect(serverSchema).toEqual({
version: 9,
version: 10,
unsafeSql: undefined,
tables: {
[CATEGORY]: {
@ -334,6 +334,7 @@ describe('*** Test schema for SERVER database ***', () => {
post_id: {name: 'post_id', type: 'string', isIndexed: true},
size: {name: 'size', type: 'number'},
width: {name: 'width', type: 'number'},
is_blocked: {name: 'is_blocked', type: 'boolean'},
},
columnArray: [
{name: 'extension', type: 'string'},
@ -345,6 +346,7 @@ describe('*** Test schema for SERVER database ***', () => {
{name: 'post_id', type: 'string', isIndexed: true},
{name: 'size', type: 'number'},
{name: 'width', type: 'number'},
{name: 'is_blocked', type: 'boolean'},
],
},
[GROUP]: {

View file

@ -108,6 +108,7 @@ name string
post_id string INDEX FK >- Post.id
size number
width number
is_blocked bool #Determines if a file has been blocked and cannot be opened anymore
Group
-

View file

@ -1,19 +0,0 @@
--- SessionDelegate.swift.orig 2022-03-12 17:34:07.000000000 -0300
+++ SessionDelegate.swift 2022-03-16 15:51:22.000000000 -0300
@@ -292,6 +292,16 @@
return
}
+ let allHeaders = (downloadRequest.response)?.allHeaderFields as? NSDictionary ?? NSDictionary()
+ let sizeString = (allHeaders["X-Uncompressed-Content-Length"] ?? allHeaders["x-uncompressed-content-length"]) as? String
+ if (sizeString != nil) {
+ let size = Int64(sizeString!)
+ downloadRequest.updateDownloadProgress(bytesWritten: bytesWritten,
+ totalBytesExpectedToWrite: size!)
+ return
+
+ }
+
downloadRequest.updateDownloadProgress(bytesWritten: bytesWritten,
totalBytesExpectedToWrite: totalBytesExpectedToWrite)
}

View file

@ -694,6 +694,7 @@ class TestHelperSingleton {
postId: this.generateId(),
post: this.fakeRelation(),
toFileInfo: jest.fn(),
isBlocked: false,
...overwrite,
};
};

View file

@ -45,6 +45,9 @@ declare class FileModel extends Model {
/** post : The related Post record for this file */
post: Relation<PostModel>;
/** isBlocked : Whether the file is blocked and cannot be opened */
isBlocked: boolean;
toFileInfo: (authorId: string) => FileInfo;
}