Skip to content

Commit

Permalink
Merge pull request #1467 from ecency/feature/added-old-draft-migrator
Browse files Browse the repository at this point in the history
Added 3speak old video migration from edit or draft
  • Loading branch information
feruzm authored Sep 23, 2023
2 parents ba5ac22 + f1a536a commit 324bdae
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/common/pages/submit/hooks/three-speak-manager-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createContext } from "react";

export interface ThreeSpeakManagerContext {
videos: Record<string, ThreeSpeakVideo>;
setVideos: (v: Record<string, ThreeSpeakVideo>) => void;
isNsfw: boolean;
setIsNsfw: (v: boolean) => void;
isEditing: boolean;
Expand All @@ -20,6 +21,7 @@ export interface ThreeSpeakManagerContext {

export const ThreeSpeakVideoContext = createContext<ThreeSpeakManagerContext>({
videos: {},
setVideos: () => {},
isNsfw: false,
setIsNsfw: () => {},
clear: () => {},
Expand Down
1 change: 1 addition & 0 deletions src/common/pages/submit/hooks/three-speak-manager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export function ThreeSpeakManager(props: { children: ReactNode }) {
<ThreeSpeakVideoContext.Provider
value={{
videos: videos!!,
setVideos,
isNsfw: isNsfw!!,
setIsNsfw,
isEditing,
Expand Down
46 changes: 46 additions & 0 deletions src/common/pages/submit/hooks/three-speak-migration-adapter.ts
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]);
}
6 changes: 6 additions & 0 deletions src/common/pages/submit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import { SubmitPreviewContent } from "./submit-preview-content";
import { useUpdateApi } from "./api/update";
import "./_index.scss";
import { SubmitVideoAttachments } from "./submit-video-attachments";
import { useThreeSpeakMigrationAdapter } from "./hooks/three-speak-migration-adapter";

interface MatchProps {
match: MatchType;
Expand Down Expand Up @@ -123,6 +124,11 @@ export function Submit(props: PageProps & MatchProps) {
clearAdvanced
} = useAdvancedManager();

useThreeSpeakMigrationAdapter({
body,
setBody
});

useCommunityDetector(props.location, (community) => {
setTags([...tags, community]);
});
Expand Down

0 comments on commit 324bdae

Please sign in to comment.