pleroma-fe/src/services/status_poster/status_poster.service.js

122 lines
2.6 KiB
JavaScript
Raw Normal View History

2016-10-30 16:53:58 +01:00
import { map } from 'lodash'
2026-01-08 17:26:52 +02:00
2026-08-10 22:26:22 +03:00
import { useStatusesStore } from 'src/stores/statuses.js'
2026-08-20 21:00:36 +03:00
import { useTimelinesStore } from 'src/stores/timelines.js'
import { useOAuthStore } from 'src/stores/oauth.js'
2026-08-10 15:00:59 +03:00
2026-06-13 03:10:00 +03:00
import {
editStatus as apiEditStatus,
postStatus as apiPostStatus,
setMediaDescription as apiSetMediaDescription,
uploadMedia as apiUploadMedia,
2026-06-17 14:36:45 +03:00
} from 'src/api/user.js'
2016-10-30 16:53:58 +01:00
2020-06-28 12:16:41 +03:00
const postStatus = ({
store,
status,
spoilerText,
visibility,
sensitive,
poll,
media = [],
inReplyToStatusId = undefined,
2023-07-12 21:34:19 -04:00
quoteId = undefined,
2020-06-28 12:16:41 +03:00
contentType = 'text/plain',
2020-07-15 16:19:57 +03:00
preview = false,
2026-01-06 16:22:52 +02:00
idempotencyKey = '',
2020-06-28 12:16:41 +03:00
}) => {
2016-10-30 16:53:58 +01:00
const mediaIds = map(media, 'id')
2026-06-13 03:10:00 +03:00
return apiPostStatus({
credentials: useOAuthStore().token
2026-06-13 03:10:00 +03:00
status,
spoilerText,
visibility,
sensitive,
mediaIds,
inReplyToStatusId,
quoteId,
contentType,
poll,
preview,
idempotencyKey,
2026-08-10 22:26:22 +03:00
}).then(({ data, timestamp }) => {
2026-08-20 22:43:34 +03:00
if (!preview) {
2026-08-10 22:26:22 +03:00
useStatusesStore().addNewStatuses({
timestamp,
2026-06-22 20:54:34 +03:00
statuses: [data],
2026-08-20 22:43:34 +03:00
})
useTimelinesStore().addStatusesToTimeline('friends', undefined, {
statuses: [data.id],
2026-06-22 20:54:34 +03:00
showImmediately: true,
noIdUpdate: true, // To prevent missing notices on next pull.
})
2026-08-20 22:43:34 +03:00
}
2026-06-22 20:19:54 +03:00
return data
})
2016-10-30 16:53:58 +01:00
}
2022-06-07 21:31:48 -06:00
const editStatus = ({
store,
statusId,
status,
spoilerText,
sensitive,
poll,
media = [],
2026-01-06 16:22:52 +02:00
contentType = 'text/plain',
2022-06-07 21:31:48 -06:00
}) => {
const mediaIds = map(media, 'id')
return apiEditStatus({
2026-06-13 03:10:00 +03:00
id: statusId,
credentials: useOAuthStore().token
2026-06-13 03:10:00 +03:00
status,
spoilerText,
sensitive,
poll,
mediaIds,
contentType,
})
2026-08-20 21:00:36 +03:00
.then(({ data, timestamp }) => {
useStatusesStore().addNewStatuses({
statuses: [data],
timestamp,
})
useTimelinesStore().addStatusesToTimeline('friends', undefined, {
2026-08-20 22:43:34 +03:00
statuses: [data.id],
showImmediately: true,
noIdUpdate: true, // To prevent missing notices on next pull.
})
2022-06-07 21:31:48 -06:00
return data
})
.catch((err) => {
console.error('Error editing status', err)
return {
2026-01-06 16:22:52 +02:00
error: err.message,
2022-06-07 21:31:48 -06:00
}
})
}
const uploadMedia = ({ store, formData }) => {
const credentials = useOAuthStore().token
return apiUploadMedia({ credentials, formData }).then(({ data }) => data)
}
2020-07-07 09:07:20 +03:00
const setMediaDescription = ({ store, id, description }) => {
const credentials = useOAuthStore().token
return apiSetMediaDescription({ credentials, id, description }).then(
({ data }) => data,
)
2020-07-07 09:07:20 +03:00
}
2016-10-30 16:53:58 +01:00
const statusPosterService = {
postStatus,
2022-06-07 21:31:48 -06:00
editStatus,
2020-07-07 09:07:20 +03:00
uploadMedia,
2026-01-06 16:22:52 +02:00
setMediaDescription,
2016-10-30 16:53:58 +01:00
}
export default statusPosterService