Add kling (#200)
This commit is contained in:
1 parent
e12c9d96cc
commit
5f0ec79e83
43 files changed
+2718
No files matched your search
@@ -0,0 +1,196 @@
|
||||
import { applicationOperator, credentialOperator, klingOperator, serviceOperator } from '@/operators';
|
||||
import { IKlingState } from './models';
|
||||
import { ActionContext } from 'vuex';
|
||||
import { IRootState } from '../common/models';
|
||||
import { IApplication, ICredential, IKlingConfig, IKlingTask, IService } from '@/models';
|
||||
import { Status } from '@/models/common';
|
||||
import { KLING_SERVICE_ID } from '@/constants';
|
||||
import { mergeAndSortLists } from '@/utils/merge';
|
||||
import { getFinalApplication } from '@/utils';
|
||||
|
||||
export const resetAll = ({ commit }: ActionContext<IKlingState, IRootState>): void => {
|
||||
commit('resetAll');
|
||||
};
|
||||
|
||||
export const setApplication = async ({ commit, dispatch }: any, payload: IApplication): Promise<void> => {
|
||||
console.debug('set application', payload);
|
||||
commit('setApplication', payload);
|
||||
if (!payload) {
|
||||
console.debug('application is null, return');
|
||||
return;
|
||||
}
|
||||
const credential = payload?.credentials?.find((credential) => credential?.host === window.location.origin);
|
||||
if (credential) {
|
||||
console.debug('credential exists, set credential', credential);
|
||||
commit('setCredential', credential);
|
||||
} else {
|
||||
console.debug('credential not exists, start to create credential for application', payload);
|
||||
await dispatch('createCredential');
|
||||
}
|
||||
};
|
||||
|
||||
export const setApplications = async ({ commit }: any, payload: IApplication[]): Promise<void> => {
|
||||
console.debug('set applications', payload);
|
||||
commit('setApplications', payload);
|
||||
};
|
||||
|
||||
export const setService = async ({ commit }: any, payload: IService): Promise<void> => {
|
||||
console.debug('set service', payload);
|
||||
commit('setService', payload);
|
||||
};
|
||||
|
||||
export const setCredential = async ({ commit }: any, payload: ICredential): Promise<void> => {
|
||||
console.debug('set credential', payload);
|
||||
commit('setCredential', payload);
|
||||
};
|
||||
|
||||
export const createCredential = async ({ commit, state }: any): Promise<ICredential | undefined> => {
|
||||
const application = state.application;
|
||||
console.debug('prepare to create credential for application', application);
|
||||
if (!application) {
|
||||
console.error('Application not found');
|
||||
return undefined;
|
||||
}
|
||||
console.debug('creating create credential for application', application);
|
||||
const { data: credential } = await credentialOperator.create({
|
||||
application_id: application?.id,
|
||||
host: window.location.origin
|
||||
});
|
||||
console.debug('created credential success', credential);
|
||||
commit('setCredential', credential);
|
||||
console.debug('end createCredential');
|
||||
return credential;
|
||||
};
|
||||
|
||||
export const getService = async ({
|
||||
commit,
|
||||
state
|
||||
}: ActionContext<IKlingState, IRootState>): Promise<IService | undefined> => {
|
||||
state.status.getService = Status.Request;
|
||||
try {
|
||||
const { data: service } = await serviceOperator.get(KLING_SERVICE_ID);
|
||||
state.status.getService = Status.Success;
|
||||
commit('setService', service);
|
||||
return service;
|
||||
} catch (error) {
|
||||
state.status.getService = Status.Error;
|
||||
commit('setService', undefined);
|
||||
}
|
||||
};
|
||||
|
||||
export const getApplications = async ({
|
||||
commit,
|
||||
state,
|
||||
rootState
|
||||
}: ActionContext<IKlingState, IRootState>): Promise<IApplication[] | undefined> => {
|
||||
console.debug('start to get applications for chat');
|
||||
state.status.getApplications = Status.Request;
|
||||
const currentApplication = state.application;
|
||||
console.debug('current application', currentApplication);
|
||||
try {
|
||||
const { data: applications } = await applicationOperator.getAll({
|
||||
user_id: rootState?.user?.id,
|
||||
service_id: KLING_SERVICE_ID
|
||||
});
|
||||
state.status.getApplications = Status.Success;
|
||||
commit('setApplications', applications.items);
|
||||
const finalApplication = getFinalApplication(applications.items, currentApplication);
|
||||
if (finalApplication) {
|
||||
console.debug('set final application', finalApplication, finalApplication?.type);
|
||||
commit('setApplication', finalApplication);
|
||||
} else {
|
||||
console.debug('set application undefined', undefined);
|
||||
commit('setApplication', undefined);
|
||||
}
|
||||
return applications.items;
|
||||
} catch (error) {
|
||||
console.error('get applications failed', error);
|
||||
state.status.getApplications = Status.Error;
|
||||
commit('setApplications', undefined);
|
||||
commit('setApplication', undefined);
|
||||
}
|
||||
};
|
||||
|
||||
export const setConfig = ({ commit }: any, payload: IKlingConfig) => {
|
||||
commit('setConfig', payload);
|
||||
};
|
||||
|
||||
export const setTasks = ({ commit }: any, payload: any) => {
|
||||
commit('setTasks', payload);
|
||||
};
|
||||
|
||||
export const setTasksItems = ({ commit }: any, payload: IKlingTask[]) => {
|
||||
commit('setTasksItems', payload);
|
||||
};
|
||||
|
||||
export const setTasksTotal = ({ commit }: any, payload: number) => {
|
||||
commit('setTasksTotal', payload);
|
||||
};
|
||||
|
||||
export const setTasksActive = ({ commit }: any, payload: IKlingTask) => {
|
||||
commit('setTasksActive', payload);
|
||||
};
|
||||
|
||||
export const getTasks = async (
|
||||
{ commit, state, rootState }: ActionContext<IKlingState, IRootState>,
|
||||
{
|
||||
offset,
|
||||
limit,
|
||||
createdAtMin,
|
||||
createdAtMax
|
||||
}: { offset?: number; limit?: number; createdAtMin?: number; createdAtMax?: number }
|
||||
): Promise<IKlingTask[]> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
console.debug('start to get tasks', offset, limit);
|
||||
const credential = state.credential;
|
||||
const token = credential?.token;
|
||||
if (!token) {
|
||||
return reject('no token');
|
||||
}
|
||||
klingOperator
|
||||
.tasks(
|
||||
{
|
||||
userId: rootState?.user?.id,
|
||||
createdAtMin,
|
||||
createdAtMax,
|
||||
type: 'videos'
|
||||
},
|
||||
{
|
||||
token
|
||||
}
|
||||
)
|
||||
.then((response) => {
|
||||
console.debug('get videos tasks success', response.data.items);
|
||||
// merge with existing tasks
|
||||
const existingItems = state?.tasks?.items || [];
|
||||
console.debug('existing items', existingItems);
|
||||
const newItems = response.data.items || [];
|
||||
console.debug('new items', newItems);
|
||||
// sort and de-duplicate using created_at
|
||||
const mergedItems = mergeAndSortLists(existingItems, newItems);
|
||||
commit('setTasksItems', mergedItems);
|
||||
commit('setTasksTotal', response.data.count);
|
||||
resolve(response.data.items);
|
||||
})
|
||||
.catch((error) => {
|
||||
return reject(error);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export default {
|
||||
setService,
|
||||
getService,
|
||||
resetAll,
|
||||
setCredential,
|
||||
setConfig,
|
||||
setApplication,
|
||||
setApplications,
|
||||
getApplications,
|
||||
setTasks,
|
||||
setTasksItems,
|
||||
setTasksTotal,
|
||||
setTasksActive,
|
||||
getTasks,
|
||||
createCredential
|
||||
};
|
||||
@@ -0,0 +1,14 @@
|
||||
import { Module } from 'vuex';
|
||||
import { IKlingState } from './models';
|
||||
import actions from './actions';
|
||||
import mutations from './mutations';
|
||||
import state from './state';
|
||||
|
||||
export const kling: Module<IKlingState, any> = {
|
||||
namespaced: true,
|
||||
state,
|
||||
mutations,
|
||||
actions
|
||||
};
|
||||
|
||||
export default kling;
|
||||
@@ -0,0 +1,22 @@
|
||||
import { IApplication, ICredential, IService, Status } from '@/models';
|
||||
import { IKlingConfig, IKlingTask } from '@/models';
|
||||
|
||||
export interface IKlingState {
|
||||
application: IApplication | undefined;
|
||||
applications: IApplication[] | undefined;
|
||||
service: IService | undefined;
|
||||
credential: ICredential | undefined;
|
||||
config: IKlingConfig | undefined;
|
||||
tasks:
|
||||
| {
|
||||
items: IKlingTask[] | undefined;
|
||||
total: number | undefined;
|
||||
active: IKlingTask | undefined;
|
||||
}
|
||||
| undefined;
|
||||
status: {
|
||||
getService: Status;
|
||||
getApplications: Status;
|
||||
getTasks: Status;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
import { IApplication, ICredential, IKlingConfig, IKlingTask, IService } from '@/models';
|
||||
import { IKlingState } from './models';
|
||||
|
||||
export const resetAll = (state: IKlingState): void => {
|
||||
state.service = undefined;
|
||||
state.application = undefined;
|
||||
state.config = undefined;
|
||||
state.credential = undefined;
|
||||
state.tasks = undefined;
|
||||
};
|
||||
|
||||
export const setService = (state: IKlingState, payload: IService): void => {
|
||||
state.service = payload;
|
||||
};
|
||||
|
||||
export const setCredential = (state: IKlingState, payload: ICredential): void => {
|
||||
state.credential = payload;
|
||||
};
|
||||
|
||||
export const setApplication = (state: IKlingState, payload: IApplication): void => {
|
||||
state.application = payload;
|
||||
};
|
||||
|
||||
export const setApplications = (state: IKlingState, payload: IApplication[]): void => {
|
||||
state.applications = payload;
|
||||
};
|
||||
|
||||
export const setConfig = (state: IKlingState, payload: IKlingConfig): void => {
|
||||
state.config = payload;
|
||||
};
|
||||
|
||||
export const setTasksItems = (state: IKlingState, payload: IKlingTask[]): void => {
|
||||
const newPayload = {
|
||||
...state.tasks,
|
||||
items: payload
|
||||
} as typeof state.tasks;
|
||||
state.tasks = newPayload;
|
||||
};
|
||||
|
||||
export const setTasksTotal = (state: IKlingState, payload: number): void => {
|
||||
const newPayload = {
|
||||
...state.tasks,
|
||||
total: payload
|
||||
} as typeof state.tasks;
|
||||
state.tasks = newPayload;
|
||||
};
|
||||
|
||||
export const setTasksActive = (state: IKlingState, payload: IKlingTask): void => {
|
||||
const newPayload = {
|
||||
...state.tasks,
|
||||
active: payload
|
||||
} as typeof state.tasks;
|
||||
state.tasks = newPayload;
|
||||
};
|
||||
|
||||
export const setTasks = (state: IKlingState, payload: any): void => {
|
||||
state.tasks = payload;
|
||||
};
|
||||
|
||||
export default {
|
||||
setTasks,
|
||||
setApplication,
|
||||
setApplications,
|
||||
setConfig,
|
||||
setCredential,
|
||||
setService,
|
||||
setTasksActive,
|
||||
setTasksItems,
|
||||
setTasksTotal,
|
||||
resetAll
|
||||
};
|
||||
@@ -0,0 +1 @@
|
||||
export default ['kling.credential', 'kling.application', 'kling.applications'];
|
||||
@@ -0,0 +1,18 @@
|
||||
import { IKlingState } from './models';
|
||||
import { Status } from '@/models';
|
||||
|
||||
export default (): IKlingState => {
|
||||
return {
|
||||
service: undefined,
|
||||
application: undefined,
|
||||
applications: undefined,
|
||||
tasks: undefined,
|
||||
credential: undefined,
|
||||
config: undefined,
|
||||
status: {
|
||||
getService: Status.None,
|
||||
getApplications: Status.None,
|
||||
getTasks: Status.None
|
||||
}
|
||||
};
|
||||
};
|
||||
Reference in new issue
Block a user