Skip to content

Commit

Permalink
refactor: sns utils (#11345)
Browse files Browse the repository at this point in the history
  • Loading branch information
guanbinrui committed Feb 2, 2024
1 parent e647578 commit b6001e7
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 42 deletions.
1 change: 1 addition & 0 deletions packages/app/src/initialization/plugin-host/enable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ __setSiteAdaptorContext__({
currentNextIDPlatform: undefined,
currentPersonaIdentifier: UNDEFINED,
getPostURL: () => null,
getProfileURL: () => null,
share: undefined,
connectPersona: reject,
getPostIdFromNewPostToast: undefined,
Expand Down
1 change: 1 addition & 0 deletions packages/mask/content-script/site-adaptor-infra/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export async function activateSiteAdaptorUIInner(ui_deferred: SiteAdaptorUI.Defe
currentNextIDPlatform: ui.configuration.nextIDConfig?.platform,
currentPersonaIdentifier: createSubscriptionFromValueRef(currentPersonaIdentifier, signal),
getPostURL: ui.utils.getPostURL || (() => null),
getProfileURL: ui.utils.getProfileURL || (() => null),
share: ui.utils.share,
getPostIdFromNewPostToast: ui.configuration.nextIDConfig?.getPostIdFromNewPostToast,
connectPersona,
Expand Down
18 changes: 15 additions & 3 deletions packages/mask/content-script/site-adaptors/facebook.com/shared.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
import urlcat from 'urlcat'
import type { SiteAdaptor } from '@masknet/types'
import { facebookBase } from './base.js'
import { getPostUrlAtFacebook, isValidFacebookUsername } from './utils/parse-username.js'
import { type PostIdentifier } from '@masknet/shared-base'
import { type ProfileIdentifier, type PostIdentifier } from '@masknet/shared-base'
import { hasPayloadLike } from '../../utils/index.js'
import { createSiteAdaptorSpecializedPostContext } from '../../site-adaptor-infra/utils/create-post-context.js'
import { openWindow } from '@masknet/shared-base-ui'
import { FacebookAdaptor } from '../../../shared/site-adaptors/implementations/facebook.com.js'

function getPostURL(post: PostIdentifier): URL | null {
return new URL(getPostUrlAtFacebook(post))
}
function getProfileURL(profile: ProfileIdentifier): URL | null {
return new URL('https://www.facebook.com')
}
function getShareURL(text: string): URL | null {
return new URL(
urlcat('https://www.facebook.com/sharer/sharer.php', {
quote: text,
u: 'mask.io',
}),
)
}
export const facebookShared: SiteAdaptor.Shared & SiteAdaptor.Base = {
...facebookBase,
utils: {
isValidUsername: (v) => !!isValidFacebookUsername(v),
getPostURL,
getProfileURL,
share(message) {
openWindow(FacebookAdaptor.getShareLinkURL?.(message))
openWindow(getShareURL?.(message))
},
createPostContext: createSiteAdaptorSpecializedPostContext(facebookBase.networkIdentifier, {
hasPayloadLike,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@ import { createSiteAdaptorSpecializedPostContext } from '../../site-adaptor-infr
import { hasPayloadLike } from '../../utils/index.js'
import { instagramBase } from './base.js'

function getProfileURL(): URL | null {
return new URL('https://www.instagram.com/')
}

export const instagramShared: SiteAdaptor.Shared & SiteAdaptor.Base = {
...instagramBase,
utils: {
getProfileURL,
createPostContext: createSiteAdaptorSpecializedPostContext(instagramBase.networkIdentifier, {
hasPayloadLike,
}),
Expand Down
20 changes: 17 additions & 3 deletions packages/mask/content-script/site-adaptors/minds.com/shared.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
import { type PostIdentifier } from '@masknet/shared-base'
import urlcat from 'urlcat'
import { type ProfileIdentifier, type PostIdentifier } from '@masknet/shared-base'
import { openWindow } from '@masknet/shared-base-ui'
import type { SiteAdaptor } from '@masknet/types'
import { createSiteAdaptorSpecializedPostContext } from '../../site-adaptor-infra/utils/create-post-context.js'
import { hasPayloadLike } from '../../utils/index.js'
import { mindsBase } from './base.js'
import { usernameValidator } from './utils/user.js'
import { MindsAdaptor } from '../../../shared/site-adaptors/implementations/minds.com.js'

function getPostURL(post: PostIdentifier): URL {
return new URL(`https://minds.com/newsfeed/${post.postId}`)
}

function getProfileURL(profile: ProfileIdentifier): URL | null {
return new URL('https://www.minds.com')
}

function getShareURL(text: string): URL | null {
return new URL(
urlcat('https://www.minds.com/newsfeed/subscriptions', {
intentUrl: text,
}),
)
}

export const mindsShared: SiteAdaptor.Shared & SiteAdaptor.Base = {
...mindsBase,
utils: {
isValidUsername: usernameValidator,
getPostURL,
getProfileURL,
share(message) {
openWindow(MindsAdaptor.getShareLinkURL?.(message))
openWindow(getShareURL(message))
},
createPostContext: createSiteAdaptorSpecializedPostContext(mindsBase.networkIdentifier, {
hasPayloadLike,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@ import { hasPayloadLike } from '../../utils/index.js'
import { mirrorBase } from './base.js'
import { getUserIdentity } from './utils/user.js'

function getProfileURL() {
return new URL('https://mirror.xyz/dashboard')
}
function getShareURL(text: string) {
return new URL('https://mirror.xyz')
}

export const mirrorShared: SiteAdaptor.Shared & SiteAdaptor.Base = {
...mirrorBase,
utils: {
getProfileURL,
getShareURL,
createPostContext: createSiteAdaptorSpecializedPostContext(mirrorBase.networkIdentifier, {
hasPayloadLike,
}),
Expand Down
14 changes: 11 additions & 3 deletions packages/mask/content-script/site-adaptors/twitter.com/shared.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import urlcat from 'urlcat'
import { type PostIdentifier, ProfileIdentifier } from '@masknet/shared-base'
import { openWindow } from '@masknet/shared-base-ui'
import type { SiteAdaptor } from '@masknet/types'
Expand All @@ -6,19 +7,26 @@ import { hasPayloadLike } from '../../utils/index.js'
import { twitterBase } from './base.js'
import { TwitterDecoder } from '@masknet/encryption'
import { getUserIdentity, usernameValidator } from './utils/user.js'
import { TwitterAdaptor } from '../../../shared/site-adaptors/implementations/twitter.com.js'

function getPostURL(post: PostIdentifier): URL | null {
if (!(post.identifier instanceof ProfileIdentifier)) return null
return new URL(`https://twitter.com/${post.identifier.userId}/status/${post.postId}`)
}
function getProfileURL(profile: ProfileIdentifier): URL | null {
return new URL(`https://twitter.com/${profile.userId}`)
}
function getShareURL(text: string): URL | null {
return new URL(urlcat('https://twitter.com/intent/tweet', { text }))
}
export const twitterShared: SiteAdaptor.Shared & SiteAdaptor.Base = {
...twitterBase,
utils: {
isValidUsername: usernameValidator,
getPostURL,
getProfileURL,
getShareURL,
share(text) {
const url = TwitterAdaptor.getShareLinkURL!(text)
const url = getShareURL(text)
const width = 700
const height = 520
const openedWindow = openWindow(url, 'share', {
Expand All @@ -35,7 +43,7 @@ export const twitterShared: SiteAdaptor.Shared & SiteAdaptor.Base = {
scrollbars: true,
},
})
if (openedWindow === null) {
if (openedWindow === null && url) {
location.assign(url)
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
import urlcat from 'urlcat'
import { EnhanceableSite } from '@masknet/shared-base'
import type { SiteAdaptor } from '../types.js'

const origins = ['https://www.facebook.com/*', 'https://m.facebook.com/*', 'https://facebook.com/*']

export const FacebookAdaptor: SiteAdaptor.Definition = {
name: 'Facebook',
networkIdentifier: EnhanceableSite.Facebook,
declarativePermissions: { origins },
homepage: 'https://www.facebook.com',
isSocialNetwork: true,
sortIndex: 1,
getProfilePage: () => new URL('https://www.facebook.com'),
getShareLinkURL(message) {
const url = urlcat('https://www.facebook.com/sharer/sharer.php', {
quote: message,
u: 'mask.io',
})
return new URL(url)
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { EnhanceableSite } from '@masknet/shared-base'
import type { SiteAdaptor } from '../types.js'

const origins = ['https://www.instagram.com/*', 'https://m.instagram.com/*', 'https://instagram.com/*']

export const InstagramAdaptor: SiteAdaptor.Definition = {
name: 'Instagram',
networkIdentifier: EnhanceableSite.Instagram,
declarativePermissions: { origins },
homepage: 'https://www.instagram.com/',
isSocialNetwork: true,
sortIndex: 3,
getProfilePage: () => new URL('https://www.instagram.com/'),
getShareLinkURL: null,
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { EnhanceableSite } from '@masknet/shared-base'
import urlcat from 'urlcat'
import type { SiteAdaptor } from '../types.js'

const origins = ['https://www.minds.com/*', 'https://minds.com/*', 'https://cdn.minds.com/*']
Expand All @@ -10,11 +9,4 @@ export const MindsAdaptor: SiteAdaptor.Definition = {
homepage: 'https://www.minds.com',
isSocialNetwork: true,
sortIndex: 4,
getProfilePage: () => new URL('https://www.minds.com'),
getShareLinkURL(message) {
const url = urlcat('https://www.minds.com/newsfeed/subscriptions', {
intentUrl: message,
})
return new URL(url)
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,4 @@ export const MirrorAdaptor: SiteAdaptor.Definition = {
homepage: 'https://mirror.xyz',
isSocialNetwork: false,
sortIndex: 2,
getProfilePage: () => new URL('https://mirror.xyz/dashboard'),
getShareLinkURL(message) {
return new URL('https://mirror.xyz')
},
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import urlcat from 'urlcat'
import { EnhanceableSite } from '@masknet/shared-base'
import type { SiteAdaptor } from '../types.js'

Expand All @@ -10,9 +9,4 @@ export const TwitterAdaptor: SiteAdaptor.Definition = {
homepage: 'https://twitter.com',
isSocialNetwork: true,
sortIndex: 0,
getProfilePage: (userId) => new URL(`https://twitter.com/${userId.userId}`),
getShareLinkURL(message) {
const url = urlcat('https://twitter.com/intent/tweet', { text: message })
return new URL(url)
},
}
2 changes: 0 additions & 2 deletions packages/mask/shared/site-adaptors/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ export declare namespace SiteAdaptor {
// Note: if declarativePermissions is no longer sufficient, use "false" to indicate it need a load().
declarativePermissions: DeclarativePermissions
homepage: undefined | string
getProfilePage: null | ((profile: ProfileIdentifier) => URL | null)
getShareLinkURL: null | ((text: string) => URL)
notReadyForProduction?: boolean
/** Whether this provider need to connect persona */
isSocialNetwork?: boolean
Expand Down
5 changes: 4 additions & 1 deletion packages/plugin-infra/src/site-adaptor/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@

import type { Subscription } from 'use-subscription'
import type { IdentityResolved } from '../types.js'
import type { NextIDPlatform, PersonaIdentifier, PostIdentifier } from '@masknet/shared-base'
import type { NextIDPlatform, PersonaIdentifier, PostIdentifier, ProfileIdentifier } from '@masknet/shared-base'

export interface __SiteAdaptorContext__ {
lastRecognizedProfile: Subscription<IdentityResolved | undefined>
currentVisitingProfile: Subscription<IdentityResolved | undefined>
currentNextIDPlatform: NextIDPlatform | undefined
currentPersonaIdentifier: Subscription<PersonaIdentifier | undefined>
getPostURL: (identifier: PostIdentifier) => URL | null
getProfileURL: (identifier: ProfileIdentifier) => URL | null
share: undefined | ((text: string) => void)
getUserIdentity: ((useId: string) => Promise<IdentityResolved | undefined>) | undefined
getPostIdFromNewPostToast: (() => string) | undefined
Expand All @@ -23,6 +24,7 @@ export let currentVisitingProfile: __SiteAdaptorContext__['currentVisitingProfil
export let currentNextIDPlatform: NextIDPlatform | undefined
export let currentPersonaIdentifier: __SiteAdaptorContext__['currentPersonaIdentifier']
export let getPostURL: __SiteAdaptorContext__['getPostURL']
export let getProfileURL: __SiteAdaptorContext__['getProfileURL']
export let share: __SiteAdaptorContext__['share']
export let getUserIdentity: __SiteAdaptorContext__['getUserIdentity']
export let getPostIdFromNewPostToast: __SiteAdaptorContext__['getPostIdFromNewPostToast']
Expand All @@ -36,6 +38,7 @@ export function __setSiteAdaptorContext__(value: __SiteAdaptorContext__) {
currentNextIDPlatform,
currentPersonaIdentifier,
getPostURL,
getProfileURL,
share,
getUserIdentity,
getPostIdFromNewPostToast,
Expand Down
4 changes: 3 additions & 1 deletion packages/types/src/SiteAdaptor.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import type { EncryptPayloadNetwork } from '@masknet/encryption'
import type { EnhanceableSite, PostIdentifier } from '@masknet/shared-base'
import type { EnhanceableSite, PostIdentifier, ProfileIdentifier } from '@masknet/shared-base'
import type { IdentityResolved, PostContext, PostContextCreation } from '@masknet/plugin-infra/content-script'
import type { SiteAdaptorUI } from './SiteAdaptorUI.js'

export declare namespace SiteAdaptor {
export interface Utils {
/** @returns post URL from PostIdentifier */
getPostURL?(post: PostIdentifier): URL | null
getProfileURL?(profile: ProfileIdentifier): URL | null
getShareURL?(text: string): URL | null
getUserIdentity?(userId: string): Promise<IdentityResolved | undefined>
/** Is this username valid in this network */
isValidUsername?(username: string): boolean
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/SiteAdaptorUI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export namespace SiteAdaptorUI {
}
export interface Redirect {
profilePage?(profile: ProfileIdentifier): void
postPage?(post: PostIdentifier): void
newsFeed?(): void
}
export interface Endpoint {
Expand Down

0 comments on commit b6001e7

Please sign in to comment.