2020-11-07 18:45:02 +02:00
|
|
|
import BaseModel from '../BaseModel';
|
2023-10-21 17:07:44 +02:00
|
|
|
import { SqlQuery } from '../services/database/types';
|
2021-10-05 18:47:38 +02:00
|
|
|
import BaseItem from './BaseItem';
|
2018-03-13 01:40:43 +02:00
|
|
|
|
2018-06-17 17:59:06 +02:00
|
|
|
// - If is_associated = 1, note_resources indicates which note_id is currently associated with the given resource_id
|
|
|
|
// - If is_associated = 0, note_resources indicates which note_id *was* associated with the given resource_id
|
|
|
|
// - last_seen_time tells the last time that reosurce was associated with this note.
|
|
|
|
// - If last_seen_time is 0, it means the resource has never been associated with any note.
|
|
|
|
|
2020-11-07 18:45:02 +02:00
|
|
|
export default class NoteResource extends BaseModel {
|
2023-03-06 16:22:01 +02:00
|
|
|
public static tableName() {
|
2018-03-13 01:40:43 +02:00
|
|
|
return 'note_resources';
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public static modelType() {
|
2018-03-13 01:40:43 +02:00
|
|
|
return BaseModel.TYPE_NOTE_RESOURCE;
|
|
|
|
}
|
|
|
|
|
2021-01-29 20:45:11 +02:00
|
|
|
public static async applySharedStatusToLinkedResources() {
|
|
|
|
const queries: SqlQuery[] = [];
|
|
|
|
|
|
|
|
queries.push({ sql: `
|
|
|
|
UPDATE resources
|
|
|
|
SET is_shared = 0
|
|
|
|
` });
|
|
|
|
|
|
|
|
queries.push({ sql: `
|
|
|
|
UPDATE resources
|
|
|
|
SET is_shared = 1
|
|
|
|
WHERE id IN (
|
|
|
|
SELECT DISTINCT note_resources.resource_id
|
|
|
|
FROM notes JOIN note_resources ON notes.id = note_resources.note_id
|
|
|
|
WHERE notes.is_shared = 1
|
|
|
|
)
|
|
|
|
` });
|
|
|
|
|
|
|
|
await this.db().transactionExecBatch(queries);
|
|
|
|
}
|
|
|
|
|
2021-05-13 18:57:37 +02:00
|
|
|
// public static async updateResourceShareIds() {
|
|
|
|
// // Find all resources where share_id is different from parent note
|
|
|
|
// // share_id. Then update share_id on all these resources. Essentially it
|
|
|
|
// // makes it match the resource share_id to the note share_id.
|
|
|
|
|
|
|
|
// const sql = `
|
|
|
|
// SELECT r.id, n.share_id
|
|
|
|
// FROM note_resources nr
|
|
|
|
// LEFT JOIN resources r ON nr.resource_id = r.id
|
|
|
|
// LEFT JOIN notes n ON nr.note_id = n.id
|
|
|
|
// WHERE n.share_id != r.share_id`;
|
|
|
|
|
|
|
|
// const rows = await this.db().selectAll(sql);
|
|
|
|
|
|
|
|
// const updatedTime = Date.now();
|
|
|
|
// const queries: SqlQuery[] = [];
|
|
|
|
|
|
|
|
// for (const row of rows) {
|
|
|
|
// queries.push({
|
|
|
|
// sql: `
|
|
|
|
// UPDATE resources
|
|
|
|
// SET share_id = ?, updated_time = ?
|
|
|
|
// WHERE id = ?`,
|
|
|
|
// params: [
|
|
|
|
// row.share_id || '',
|
|
|
|
// updatedTime,
|
|
|
|
// row.id,
|
|
|
|
// ],
|
|
|
|
// });
|
|
|
|
// }
|
|
|
|
|
|
|
|
// await this.db().transactionExecBatch(queries);
|
|
|
|
// }
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public static async associatedNoteIds(resourceId: string): Promise<string[]> {
|
2020-11-07 18:45:02 +02:00
|
|
|
const rows = await this.modelSelectAll('SELECT note_id FROM note_resources WHERE resource_id = ? AND is_associated = 1', [resourceId]);
|
2020-11-12 21:13:28 +02:00
|
|
|
return rows.map((r: any) => r.note_id);
|
2020-11-07 18:45:02 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public static async setAssociatedResources(noteId: string, resourceIds: string[]) {
|
2018-03-15 20:08:46 +02:00
|
|
|
const existingRows = await this.modelSelectAll('SELECT * FROM note_resources WHERE note_id = ?', [noteId]);
|
|
|
|
|
|
|
|
const notProcessedResourceIds = resourceIds.slice();
|
|
|
|
const queries = [];
|
|
|
|
for (let i = 0; i < existingRows.length; i++) {
|
|
|
|
const row = existingRows[i];
|
|
|
|
const resourceIndex = resourceIds.indexOf(row.resource_id);
|
|
|
|
|
|
|
|
if (resourceIndex >= 0) {
|
|
|
|
queries.push({ sql: 'UPDATE note_resources SET last_seen_time = ?, is_associated = 1 WHERE id = ?', params: [Date.now(), row.id] });
|
|
|
|
notProcessedResourceIds.splice(notProcessedResourceIds.indexOf(row.resource_id), 1);
|
|
|
|
} else {
|
|
|
|
queries.push({ sql: 'UPDATE note_resources SET is_associated = 0 WHERE id = ?', params: [row.id] });
|
|
|
|
}
|
|
|
|
}
|
2018-03-13 01:40:43 +02:00
|
|
|
|
2018-03-15 20:08:46 +02:00
|
|
|
for (let i = 0; i < notProcessedResourceIds.length; i++) {
|
|
|
|
queries.push({ sql: 'INSERT INTO note_resources (note_id, resource_id, is_associated, last_seen_time) VALUES (?, ?, ?, ?)', params: [noteId, notProcessedResourceIds[i], 1, Date.now()] });
|
2018-03-13 01:40:43 +02:00
|
|
|
}
|
|
|
|
|
2018-03-16 19:39:44 +02:00
|
|
|
await this.db().transactionExecBatch(queries);
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public static async addOrphanedResources() {
|
2018-03-16 19:39:44 +02:00
|
|
|
const missingResources = await this.db().selectAll('SELECT id FROM resources WHERE id NOT IN (SELECT DISTINCT resource_id FROM note_resources)');
|
|
|
|
const queries = [];
|
|
|
|
for (let i = 0; i < missingResources.length; i++) {
|
2018-03-16 22:17:52 +02:00
|
|
|
const id = missingResources[i].id;
|
2021-10-05 18:47:38 +02:00
|
|
|
|
|
|
|
// If the resource is not associated with any note, and has never
|
|
|
|
// been synced, it means it's a local resource that was removed from
|
|
|
|
// a note (or the note was deleted). In which case, we set a
|
|
|
|
// "last_seen_time", so that it can be considered an orphan reosurce
|
|
|
|
// that can be auto-deleted.
|
|
|
|
//
|
|
|
|
// https://github.com/laurent22/joplin/issues/932#issuecomment-933736405
|
|
|
|
|
|
|
|
const hasBeenSynced = await BaseItem.itemHasBeenSynced(id);
|
|
|
|
const lastSeenTime = hasBeenSynced ? 0 : Date.now();
|
|
|
|
|
|
|
|
queries.push({
|
|
|
|
sql: 'INSERT INTO note_resources (note_id, resource_id, is_associated, last_seen_time) VALUES (?, ?, ?, ?)',
|
2023-08-22 12:58:53 +02:00
|
|
|
params: ['', id, 0, lastSeenTime] },
|
2021-10-05 18:47:38 +02:00
|
|
|
);
|
2018-03-16 19:39:44 +02:00
|
|
|
}
|
|
|
|
await this.db().transactionExecBatch(queries);
|
2018-03-13 01:40:43 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public static async remove(noteId: string) {
|
2018-03-15 20:08:46 +02:00
|
|
|
await this.db().exec({ sql: 'UPDATE note_resources SET is_associated = 0 WHERE note_id = ?', params: [noteId] });
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public static async orphanResources(expiryDelay: number = null) {
|
2018-05-27 13:50:12 +02:00
|
|
|
if (expiryDelay === null) expiryDelay = 1000 * 60 * 60 * 24 * 10;
|
2018-03-15 20:08:46 +02:00
|
|
|
const cutOffTime = Date.now() - expiryDelay;
|
2019-07-29 15:43:53 +02:00
|
|
|
const output = await this.modelSelectAll(
|
|
|
|
`
|
2018-03-16 19:39:44 +02:00
|
|
|
SELECT resource_id, sum(is_associated)
|
|
|
|
FROM note_resources
|
|
|
|
GROUP BY resource_id
|
|
|
|
HAVING sum(is_associated) <= 0
|
|
|
|
AND last_seen_time < ?
|
2018-06-17 17:59:06 +02:00
|
|
|
AND last_seen_time != 0
|
2019-07-29 15:43:53 +02:00
|
|
|
`,
|
2023-08-22 12:58:53 +02:00
|
|
|
[cutOffTime],
|
2019-07-29 15:43:53 +02:00
|
|
|
);
|
2020-11-12 21:13:28 +02:00
|
|
|
return output.map((r: any) => r.resource_id);
|
2018-03-13 01:40:43 +02:00
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public static async deleteByResource(resourceId: string) {
|
2018-03-16 19:39:44 +02:00
|
|
|
await this.db().exec('DELETE FROM note_resources WHERE resource_id = ?', [resourceId]);
|
|
|
|
}
|
2018-03-13 01:40:43 +02:00
|
|
|
}
|