Skip to content

Commit

Permalink
Create a section in the history panel to reset scene activity (#5168)
Browse files Browse the repository at this point in the history
Co-authored-by: WithoutPants <[email protected]>
  • Loading branch information
ikmckenz and WithoutPants authored Aug 29, 2024
1 parent 68738bd commit 96fdd94
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 1 deletion.
7 changes: 7 additions & 0 deletions graphql/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,13 @@ type Mutation {
"Sets the resume time point (if provided) and adds the provided duration to the scene's play duration"
sceneSaveActivity(id: ID!, resume_time: Float, playDuration: Float): Boolean!

"Resets the resume time point and play duration"
sceneResetActivity(
id: ID!
reset_resume: Boolean
reset_duration: Boolean
): Boolean!

"Increments the play count for the scene. Returns the new play count value."
sceneIncrementPlayCount(id: ID!): Int!
@deprecated(reason: "Use sceneAddPlay instead")
Expand Down
18 changes: 18 additions & 0 deletions internal/api/resolver_mutation_scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,24 @@ func (r *mutationResolver) SceneSaveActivity(ctx context.Context, id string, res
return ret, nil
}

func (r *mutationResolver) SceneResetActivity(ctx context.Context, id string, resetResume *bool, resetDuration *bool) (ret bool, err error) {
sceneID, err := strconv.Atoi(id)
if err != nil {
return false, fmt.Errorf("converting id: %w", err)
}

if err := r.withTxn(ctx, func(ctx context.Context) error {
qb := r.repository.Scene

ret, err = qb.ResetActivity(ctx, sceneID, utils.IsTrue(resetResume), utils.IsTrue(resetDuration))
return err
}); err != nil {
return false, err
}

return ret, nil
}

// deprecated
func (r *mutationResolver) SceneIncrementPlayCount(ctx context.Context, id string) (ret int, err error) {
sceneID, err := strconv.Atoi(id)
Expand Down
21 changes: 21 additions & 0 deletions pkg/models/mocks/SceneReaderWriter.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/models/repository_scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ type SceneWriter interface {
OHistoryWriter
ViewHistoryWriter
SaveActivity(ctx context.Context, sceneID int, resumeTime *float64, playDuration *float64) (bool, error)
ResetActivity(ctx context.Context, sceneID int, resetResume bool, resetDuration bool) (bool, error)
}

// SceneReaderWriter provides all scene methods.
Expand Down
24 changes: 24 additions & 0 deletions pkg/sqlite/scene.go
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,30 @@ func (qb *SceneStore) SaveActivity(ctx context.Context, id int, resumeTime *floa
return true, nil
}

func (qb *SceneStore) ResetActivity(ctx context.Context, id int, resetResume bool, resetDuration bool) (bool, error) {
if err := qb.tableMgr.checkIDExists(ctx, id); err != nil {
return false, err
}

record := goqu.Record{}

if resetResume {
record["resume_time"] = 0.0
}

if resetDuration {
record["play_duration"] = 0.0
}

if len(record) > 0 {
if err := qb.tableMgr.updateByID(ctx, id, record); err != nil {
return false, err
}
}

return true, nil
}

func (qb *SceneStore) GetURLs(ctx context.Context, sceneID int) ([]string, error) {
return scenesURLsTableMgr.get(ctx, sceneID)
}
Expand Down
12 changes: 12 additions & 0 deletions ui/v2.5/graphql/mutations/scene.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ mutation SceneSaveActivity(
)
}

mutation SceneResetActivity(
$id: ID!
$reset_resume: Boolean!
$reset_duration: Boolean!
) {
sceneResetActivity(
id: $id
reset_resume: $reset_resume
reset_duration: $reset_duration
)
}

mutation SceneAddPlay($id: ID!, $times: [Timestamp!]) {
sceneAddPlay(id: $id, times: $times) {
count
Expand Down
85 changes: 84 additions & 1 deletion ui/v2.5/src/components/Scenes/SceneDetails/SceneHistoryPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import {
useSceneIncrementPlayCount,
useSceneResetO,
useSceneResetPlayCount,
useSceneResetActivity,
} from "src/core/StashService";
import * as GQL from "src/core/generated-graphql";
import { useToast } from "src/hooks/Toast";
import { TextField } from "src/utils/field";
import TextUtils from "src/utils/text";

Expand Down Expand Up @@ -72,9 +74,19 @@ const History: React.FC<{

const HistoryMenu: React.FC<{
hasHistory: boolean;
showResetResumeDuration: boolean;
onAddDate: () => void;
onClearDates: () => void;
}> = ({ hasHistory, onAddDate, onClearDates }) => {
resetResume: () => void;
resetDuration: () => void;
}> = ({
hasHistory,
showResetResumeDuration,
onAddDate,
onClearDates,
resetResume,
resetDuration,
}) => {
const intl = useIntl();

return (
Expand All @@ -101,6 +113,22 @@ const HistoryMenu: React.FC<{
<FormattedMessage id="actions.clear_date_data" />
</Dropdown.Item>
)}
{showResetResumeDuration && (
<Dropdown.Item
className="bg-secondary text-white"
onClick={() => resetResume()}
>
<FormattedMessage id="actions.reset_resume_time" />
</Dropdown.Item>
)}
{showResetResumeDuration && (
<Dropdown.Item
className="bg-secondary text-white"
onClick={() => resetDuration()}
>
<FormattedMessage id="actions.reset_play_duration" />
</Dropdown.Item>
)}
</Dropdown.Menu>
</Dropdown>
);
Expand Down Expand Up @@ -142,6 +170,7 @@ interface ISceneHistoryProps {

export const SceneHistoryPanel: React.FC<ISceneHistoryProps> = ({ scene }) => {
const intl = useIntl();
const Toast = useToast();

const [dialogs, setDialogs] = React.useState({
playHistory: false,
Expand All @@ -160,6 +189,8 @@ export const SceneHistoryPanel: React.FC<ISceneHistoryProps> = ({ scene }) => {
const [incrementOCount] = useSceneIncrementO(scene.id);
const [decrementOCount] = useSceneDecrementO(scene.id);
const [resetO] = useSceneResetO(scene.id);
const [resetResume] = useSceneResetActivity(scene.id, true, false);
const [resetDuration] = useSceneResetActivity(scene.id, false, true);

function dateStringToISOString(time: string) {
const date = TextUtils.stringToFuzzyDateTime(time);
Expand Down Expand Up @@ -221,6 +252,52 @@ export const SceneHistoryPanel: React.FC<ISceneHistoryProps> = ({ scene }) => {
});
}

async function handleResetResume() {
try {
await resetResume({
variables: {
id: scene.id,
reset_resume: true,
reset_duration: false,
},
});

Toast.success(
intl.formatMessage(
{ id: "toast.updated_entity" },
{
entity: intl.formatMessage({ id: "scene" }).toLocaleLowerCase(),
}
)
);
} catch (e) {
Toast.error(e);
}
}

async function handleResetDuration() {
try {
await resetDuration({
variables: {
id: scene.id,
reset_resume: false,
reset_duration: true,
},
});

Toast.success(
intl.formatMessage(
{ id: "toast.updated_entity" },
{
entity: intl.formatMessage({ id: "scene" }).toLocaleLowerCase(),
}
)
);
} catch (e) {
Toast.error(e);
}
}

function maybeRenderDialogs() {
return (
<>
Expand Down Expand Up @@ -296,8 +373,11 @@ export const SceneHistoryPanel: React.FC<ISceneHistoryProps> = ({ scene }) => {
</Button>
<HistoryMenu
hasHistory={playHistory.length > 0}
showResetResumeDuration={true}
onAddDate={() => setDialogPartial({ addPlay: true })}
onClearDates={() => setDialogPartial({ playHistory: true })}
resetResume={() => handleResetResume()}
resetDuration={() => handleResetDuration()}
/>
</span>
</h5>
Expand Down Expand Up @@ -336,8 +416,11 @@ export const SceneHistoryPanel: React.FC<ISceneHistoryProps> = ({ scene }) => {
</Button>
<HistoryMenu
hasHistory={oHistory.length > 0}
showResetResumeDuration={false}
onAddDate={() => setDialogPartial({ addO: true })}
onClearDates={() => setDialogPartial({ oHistory: true })}
resetResume={() => handleResetResume()}
resetDuration={() => handleResetDuration()}
/>
</span>
</h5>
Expand Down
15 changes: 15 additions & 0 deletions ui/v2.5/src/core/StashService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,21 @@ export const useSceneResetO = (id: string) =>
},
});

export const useSceneResetActivity = (
id: string,
reset_resume: boolean,
reset_duration: boolean
) =>
GQL.useSceneResetActivityMutation({
variables: { id, reset_resume, reset_duration },
update(cache, result) {
if (!result.data?.sceneResetActivity) return;

evictTypeFields(cache, sceneMutationImpactedTypeFields);
evictQueries(cache, sceneMutationImpactedQueries);
},
});

export const useSceneGenerateScreenshot = () =>
GQL.useSceneGenerateScreenshotMutation();

Expand Down
2 changes: 2 additions & 0 deletions ui/v2.5/src/locales/en-GB.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@
"remove_from_gallery": "Remove from Gallery",
"rename_gen_files": "Rename generated files",
"rescan": "Rescan",
"reset_play_duration": "Reset play duration",
"reset_resume_time": "Reset resume time",
"reset_cover": "Restore Default Cover",
"reshuffle": "Reshuffle",
"running": "running",
Expand Down

0 comments on commit 96fdd94

Please sign in to comment.