-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1467 from ecency/feature/added-old-draft-migrator
Added 3speak old video migration from edit or draft
- Loading branch information
Showing
4 changed files
with
55 additions
and
0 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
46 changes: 46 additions & 0 deletions
46
src/common/pages/submit/hooks/three-speak-migration-adapter.ts
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,46 @@ | ||
import { useThreeSpeakManager } from "./three-speak-manager"; | ||
import { useThreeSpeakVideo } from "../../../api/threespeak"; | ||
import { useEffect } from "react"; | ||
|
||
const SPEAK_VIDEO_PATTERN = | ||
/\[\!\[\]\(https:\/\/ipfs-3speak\.b-cdn\.net\/ipfs\/.*\)\]\(https:\/\/3speak\.tv\/watch\?v=.*\/.*\)\[Source\]\(https:\/\/ipfs-3speak\.b-cdn\.net\/ipfs\/.*\)/g; | ||
|
||
interface Props { | ||
body: string; | ||
setBody: (v: string) => void; | ||
} | ||
|
||
/** | ||
* This adapter hook translates old 3speak attached posts to new format | ||
* It needs for drafts or post editing | ||
*/ | ||
export function useThreeSpeakMigrationAdapter({ body, setBody }: Props) { | ||
const { attach } = useThreeSpeakManager(); | ||
const { data: videoList } = useThreeSpeakVideo("all"); | ||
|
||
useEffect(() => { | ||
let nextBody = body; | ||
if (body) { | ||
try { | ||
const videosInPost = body.match(SPEAK_VIDEO_PATTERN); | ||
if (videosInPost && videosInPost.length > 0) { | ||
videosInPost?.forEach((video) => { | ||
const v = video.matchAll(/.*watch\?v=(.+)\/(.+)\)\[Source\]/g); | ||
const [_, username, permlink] = v.next().value; | ||
|
||
const existingVideo = videoList.find( | ||
(video) => video.permlink === permlink && video.owner === username | ||
); | ||
if (existingVideo) { | ||
nextBody = nextBody.replace(video, `[3speak](${existingVideo._id})`); | ||
setTimeout(() => attach(existingVideo), 1); // Drop from callstack to end | ||
} | ||
}); | ||
} | ||
setBody(nextBody); | ||
} catch (e) { | ||
console.error("[Old 3Speak video migration error]: Failed to migrate old 3speak video", e); | ||
} | ||
} | ||
}, [body, videoList]); | ||
} |
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