Add youtube/twitter rewrite (closes #22)
This commit is contained in:
@@ -106,6 +106,11 @@ let constants = {
|
||||
collatedFiles: []
|
||||
},
|
||||
|
||||
default_user_settings: {
|
||||
rewrite_youtube: "invidio.us",
|
||||
rewrite_twitter: "nitter.net"
|
||||
},
|
||||
|
||||
user_settings: [
|
||||
{
|
||||
name: "language",
|
||||
@@ -166,12 +171,12 @@ let constants = {
|
||||
name: "rewrite_youtube",
|
||||
default: "",
|
||||
boolean: false,
|
||||
replaceEmptyWithDefault: true
|
||||
replaceEmptyWithDefault: false
|
||||
},{
|
||||
name: "rewrite_twitter",
|
||||
default: "",
|
||||
boolean: false,
|
||||
replaceEmptyWithDefault: true
|
||||
replaceEmptyWithDefault: false
|
||||
}
|
||||
],
|
||||
|
||||
|
||||
66
src/lib/structures/BaseUser.js
Normal file
66
src/lib/structures/BaseUser.js
Normal file
@@ -0,0 +1,66 @@
|
||||
const constants = require("../constants")
|
||||
const {proxyProfilePic} = require("../utils/proxyurl")
|
||||
const {structure} = require("../utils/structuretext")
|
||||
|
||||
const rewriters = {
|
||||
rewrite_youtube: ["youtube.com", "www.youtube.com", "youtu.be"],
|
||||
rewrite_twitter: ["twitter.com", "www.twitter.com", "twtr.cm"]
|
||||
}
|
||||
|
||||
class BaseUser {
|
||||
constructor() {
|
||||
/** @type {import("../types").GraphUser} */
|
||||
this.data
|
||||
/** @type {number} */
|
||||
this.cachedAt
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {object} settings
|
||||
* @param {string} settings.rewrite_youtube
|
||||
* @param {string} settings.rewrite_twitter
|
||||
*/
|
||||
getRewriteLink(settings) {
|
||||
if (!this.data.external_url) return null
|
||||
let url
|
||||
try {
|
||||
url = new URL(this.data.external_url)
|
||||
} catch (e) {
|
||||
return null
|
||||
}
|
||||
for (const key of Object.keys(rewriters)) { // for each thing we can rewrite
|
||||
if (key in settings) { // if the settings want to replace it
|
||||
if (rewriters[key].includes(url.host)) { // if the url matches this filter
|
||||
if (settings[key].includes("://")) {
|
||||
[url.protocol, url.host] = settings[key].split("//")
|
||||
} else {
|
||||
url.host = settings[key]
|
||||
url.protocol = "https:"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return url.toString()
|
||||
}
|
||||
|
||||
computeProxyProfilePic() {
|
||||
this.proxyProfilePicture = proxyProfilePic(this.data.profile_pic_url, this.data.id)
|
||||
}
|
||||
|
||||
getStructuredBio() {
|
||||
if (!this.data.biography) return null
|
||||
return structure(this.data.biography)
|
||||
}
|
||||
|
||||
getTtl(scale = 1) {
|
||||
const expiresAt = this.cachedAt + constants.caching.resource_cache_time
|
||||
const ttl = expiresAt - Date.now()
|
||||
return Math.ceil(Math.max(ttl, 0) / scale)
|
||||
}
|
||||
|
||||
export() {
|
||||
return this.data
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = BaseUser
|
||||
@@ -1,11 +1,11 @@
|
||||
const constants = require("../constants")
|
||||
const {proxyProfilePic} = require("../utils/proxyurl")
|
||||
const {structure} = require("../utils/structuretext")
|
||||
const Timeline = require("./Timeline")
|
||||
require("../testimports")(constants, Timeline)
|
||||
const BaseUser = require("./BaseUser")
|
||||
require("../testimports")(constants, Timeline, BaseUser)
|
||||
|
||||
class ReelUser {
|
||||
class ReelUser extends BaseUser {
|
||||
constructor(data) {
|
||||
super()
|
||||
/** @type {import("../types").GraphUser} */
|
||||
this.data = data
|
||||
this.fromReel = true
|
||||
@@ -17,25 +17,6 @@ class ReelUser {
|
||||
this.cachedAt = Date.now()
|
||||
this.computeProxyProfilePic()
|
||||
}
|
||||
|
||||
computeProxyProfilePic() {
|
||||
this.proxyProfilePicture = proxyProfilePic(this.data.profile_pic_url, this.data.id)
|
||||
}
|
||||
|
||||
getStructuredBio() {
|
||||
if (!this.data.biography) return null
|
||||
return structure(this.data.biography)
|
||||
}
|
||||
|
||||
getTtl(scale = 1) {
|
||||
const expiresAt = this.cachedAt + constants.caching.resource_cache_time
|
||||
const ttl = expiresAt - Date.now()
|
||||
return Math.ceil(Math.max(ttl, 0) / scale)
|
||||
}
|
||||
|
||||
export() {
|
||||
return this.data
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ReelUser
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
const constants = require("../constants")
|
||||
const {proxyProfilePic} = require("../utils/proxyurl")
|
||||
const {structure} = require("../utils/structuretext")
|
||||
const Timeline = require("./Timeline")
|
||||
require("../testimports")(constants, Timeline)
|
||||
const BaseUser = require("./BaseUser")
|
||||
require("../testimports")(constants, Timeline, BaseUser)
|
||||
|
||||
class User {
|
||||
class User extends BaseUser {
|
||||
/**
|
||||
* @param {import("../types").GraphUser} data
|
||||
*/
|
||||
constructor(data) {
|
||||
super()
|
||||
this.data = data
|
||||
this.following = data.edge_follow.count
|
||||
this.followedBy = data.edge_followed_by.count
|
||||
@@ -17,25 +17,6 @@ class User {
|
||||
this.cachedAt = Date.now()
|
||||
this.computeProxyProfilePic()
|
||||
}
|
||||
|
||||
computeProxyProfilePic() {
|
||||
this.proxyProfilePicture = proxyProfilePic(this.data.profile_pic_url, this.data.id)
|
||||
}
|
||||
|
||||
getStructuredBio() {
|
||||
if (!this.data.biography) return null
|
||||
return structure(this.data.biography)
|
||||
}
|
||||
|
||||
getTtl(scale = 1) {
|
||||
const expiresAt = this.cachedAt + constants.caching.resource_cache_time
|
||||
const ttl = expiresAt - Date.now()
|
||||
return Math.ceil(Math.max(ttl, 0) / scale)
|
||||
}
|
||||
|
||||
export() {
|
||||
return this.data
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = User
|
||||
|
||||
Reference in New Issue
Block a user