forked from calzoneman/sync
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
46311bd
commit b80a532
Showing
5 changed files
with
89 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { createMySQLDuplicateKeyUpdate } from '../util/on-duplicate-key-update'; | ||
const Switches = require('../switches'); | ||
|
||
const Media = require('cytube-mediaquery/lib/media'); | ||
|
||
// TODO: these fullname-vs-shortcode hacks really need to be abolished | ||
function mediaquery2cytube(type) { | ||
switch (type) { | ||
case 'youtube': | ||
return 'yt'; | ||
default: | ||
throw new Error(`mediaquery2cytube: no mapping for ${type}`); | ||
} | ||
} | ||
|
||
function cytube2mediaquery(type) { | ||
switch (type) { | ||
case 'yt': | ||
return 'youtube'; | ||
default: | ||
throw new Error(`cytube2mediaquery: no mapping for ${type}`); | ||
} | ||
} | ||
|
||
class MetadataCacheDB { | ||
constructor(db) { | ||
this.db = db; | ||
} | ||
|
||
async put(media) { | ||
if (!Switches.isActive('ytCache')) return; | ||
|
||
media = new Media(media); | ||
media.type = mediaquery2cytube(media.type); | ||
return this.db.runTransaction(async tx => { | ||
let insert = tx.table('media_metadata_cache') | ||
.insert({ | ||
id: media.id, | ||
type: media.type, | ||
metadata: JSON.stringify(media) | ||
}); | ||
let update = tx.raw(createMySQLDuplicateKeyUpdate( | ||
['metadata'] | ||
)); | ||
|
||
return tx.raw(insert.toString() + update.toString()); | ||
}); | ||
} | ||
|
||
async get(id, type) { | ||
if (!Switches.isActive('ytCache')) return null; | ||
|
||
return this.db.runTransaction(async tx => { | ||
let row = await tx.table('media_metadata_cache') | ||
.where({ id, type }) | ||
.first(); | ||
|
||
if (row === undefined || row === null) { | ||
return null; | ||
} | ||
|
||
let metadata = JSON.parse(row.metadata); | ||
metadata.type = cytube2mediaquery(metadata.type); | ||
return new Media(metadata); | ||
}); | ||
} | ||
} | ||
|
||
export { MetadataCacheDB }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters