Skip to content

Commit

Permalink
chore: inject posts according to the WholePostVisibility settings
Browse files Browse the repository at this point in the history
  • Loading branch information
guanbinrui committed Aug 31, 2020
1 parent 1ce64ab commit b798fdc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/components/InjectedComponents/PostDummy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,33 @@ import React from 'react'
import { usePostInfoDetails } from '../DataSource/usePostInfo'
import { DefaultTypedMessageRenderer } from './TypedMessageRenderer'
import { PluginUI } from '../../plugins/plugin'
import { makeTypedMessageCompound, isTypedMessageSuspended } from '../../protocols/typed-message'
import { makeTypedMessageCompound, isTypedMessageSuspended, isTypedMessageBuiltIn } from '../../protocols/typed-message'
import { useValueRef } from '../../utils/hooks/useValueRef'
import { currentWholePostVisibilitySettings, WholePostVisibility } from '../../settings/settings'

export interface PostDummyProps {}

export function PostDummy(props: PostDummyProps) {
const postMessage = usePostInfoDetails('parsedPostContent')
const parsedPostContent = usePostInfoDetails('parsedPostContent')
const postPayload = usePostInfoDetails('postPayload')
const wholePostVisibilitySettings = useValueRef(currentWholePostVisibilitySettings)

const processedPostMessage = Array.from(PluginUI.values()).reduce(
(x, plugin) => (plugin.postMessageProcessor ? plugin.postMessageProcessor(x) : x),
postMessage,
parsedPostContent,
)

// render dummy for posts which enhanced by plugins
if (
wholePostVisibilitySettings === WholePostVisibility.enhancedOnly &&
processedPostMessage.items.every(isTypedMessageBuiltIn)
)
return null

// render dummy for posts which encrypted by maskbook
if (wholePostVisibilitySettings === WholePostVisibility.encryptedOnly && !postPayload.ok) return null

// render dummy for all posts
return (
<DefaultTypedMessageRenderer
message={makeTypedMessageCompound(processedPostMessage.items.filter((x) => !isTypedMessageSuspended(x)))}
Expand Down
4 changes: 4 additions & 0 deletions src/protocols/typed-message/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ export interface TypedMessageSuspended<T extends TypedMessage = TypedMessage> ex
readonly tag?: string
}

export function isTypedMessageBuiltIn(x: TypedMessage) {
return ['text', 'anchor', 'unknown', 'compound', 'image', 'empty', 'suspended'].includes(x.type)
}

export function isTypedMessageText(x: TypedMessage): x is TypedMessageText {
return x.type === 'text'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export function collectPostsFacebook(this: SocialNetworkUI) {
info.postMetadataImages.add(url)
nextTypedMessage.push(makeTypedMessageImage(url))
}
// parse post content
info.parsedPostContent.value = makeTypedMessageCompound(nextTypedMessage)
}
collectPostInfo()
Expand Down

0 comments on commit b798fdc

Please sign in to comment.