Skip to content

Commit

Permalink
Update photo archive only with statuses with media
Browse files Browse the repository at this point in the history
  • Loading branch information
techxplorer committed Sep 28, 2024
1 parent 07aa652 commit ec07f46
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/lib/archive.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,34 @@ class Archive {

}

/**
* Check to see if a status has a media.
* @param {object} status An object representing a status.
* @returns {boolean} True, if the status has the tag, otherwise false.
* @throws {TypeError} When the parameters are incorrect.
*/
statusHasMedia( status ) {

if ( typeof status !== "object" ) {
throw new TypeError( "The status parameter must be an object" );
}

if ( status.media_attachments === undefined ) {
return false;
}

if ( !Array.isArray( status.media_attachments ) ) {
return false;
}

if ( status.media_attachments.length === 0 ) {
return false;
}

return true;

}

}

export default Archive;
4 changes: 4 additions & 0 deletions src/lib/photo-archive.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ class PhotoArchive extends Archive {
}
}

if ( !this.statusHasMedia( status ) ) {
continue;
}

const newContent = [];
newContent.push( "---" );
newContent.push( this.contentCreator.makeFrontMatter( status, defaultCategories ) );
Expand Down
63 changes: 63 additions & 0 deletions tests/lib/archive.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,67 @@ describe( "Archive", () => {
} );
} );

describe( "statusHasMedia", () => {
it( "should throw an error if the parameters are incorrect", () => {
const archive = new Archive(
testPassArchivePath
);

assert.throws( () => {
archive.statusHasMedia(
undefined
);
},
{
name: "TypeError",
message: /status parameter/
} );
} );

} );

it( "should return false if the status doesn't have any media", () => {
const archive = new Archive(
testPassArchivePath
);

assert.ok(
archive.statusHasMedia( {} ) === false
);

assert.ok(
archive.statusHasMedia( {
media_attachments: undefined
} ) === false
);

assert.ok(
archive.statusHasMedia( {
media_attachments: ""
} ) === false
);

assert.ok(
archive.statusHasMedia( {
media_attachments: []
} ) === false
);
} );

it( "should return true if the status does had media", () => {
const archive = new Archive(
testPassArchivePath
);

const testStatus = JSON.parse(
readFileSync(
testStatusFile
)
);

assert.ok(
archive.statusHasMedia( testStatus ) === true
);
} );

} );
46 changes: 46 additions & 0 deletions tests/lib/photo-archive.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ const testNewStatuses = [
"112546982904162819.json"
];

const testNoMediaStatuses = [
"112793425453345288.json"
];

/**
* Helper function to tidy the archive directory.
*/
Expand Down Expand Up @@ -502,6 +506,48 @@ describe( "PhotoArchive", () => {
);

} );

it( "should only add statuses that have media", async() => {

// setup the media archive first
const mediaArchive = new MediaArchive(
testPassMediaArchive
);

let statusCount = await mediaArchive.getContentsCount();

assert.equal(
statusCount,
0
);

const { nockDone } = await nockBack( "media-attachment.json" );

const addedMedia = await mediaArchive.addMedia( testPassMediaUrl );

nockDone();

assert.equal(
addedMedia,
1
);

const photoArchive = new PhotoArchive(
testPassArchivePath
);

const photoCount = await photoArchive.addContent(
testNoMediaStatuses,
testPassStatusArchivePath,
testPassMediaArchive
);

assert.equal(
photoCount,
0
);

} );
} );

} );

0 comments on commit ec07f46

Please sign in to comment.