interactions copypasta removal
This commit is contained in:
parent
5d55cbb2ae
commit
7329c694e4
1 changed files with 137 additions and 147 deletions
|
|
@ -305,34 +305,66 @@ export const useStatusesStore = defineStore('statuses', {
|
||||||
},
|
},
|
||||||
|
|
||||||
// Actions
|
// Actions
|
||||||
/// Favorite
|
requestInteract({ name, id, optimisticCall, apiCall, argument, value }) {
|
||||||
favorite(id) {
|
const oldValue = !value // Assumption
|
||||||
this.setFavorited({ id, value: true })
|
|
||||||
|
|
||||||
favorite({
|
const apiArgs = (() => {
|
||||||
id: status.id,
|
switch (name) {
|
||||||
|
case 'emoji':
|
||||||
|
return { emoji: argument }
|
||||||
|
case 'bookmark':
|
||||||
|
return { folder_id: argument }
|
||||||
|
default:
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
})()
|
||||||
|
|
||||||
|
// Optimistic
|
||||||
|
optimisticCall(id, value, argument)
|
||||||
|
|
||||||
|
return apiCall({
|
||||||
|
id,
|
||||||
|
...apiArgs,
|
||||||
credentials: useOAuthStore().token,
|
credentials: useOAuthStore().token,
|
||||||
}).then(({ data: status, timestamp }) => {
|
})
|
||||||
|
.then(({ data: status, timestamp }) => {
|
||||||
this.addNewStatuses({
|
this.addNewStatuses({
|
||||||
statuses: [status],
|
statuses: [status],
|
||||||
timestamp,
|
timestamp,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
optimisticCall(id, oldValue, argument)
|
||||||
|
|
||||||
|
useInterfaceStore().pushGlobalNotice({
|
||||||
|
level: 'error',
|
||||||
|
messageKey: 'status.react_error',
|
||||||
|
messageArgs: [error],
|
||||||
|
timeout: 5000,
|
||||||
|
})
|
||||||
|
})
|
||||||
|
},
|
||||||
|
|
||||||
|
/// Favorite
|
||||||
|
favorite(id) {
|
||||||
|
return this.requestInteract({
|
||||||
|
name: 'favorite',
|
||||||
|
id,
|
||||||
|
apiCall: favorite,
|
||||||
|
optimisticCall: this.setFavorited,
|
||||||
|
value: true,
|
||||||
|
})
|
||||||
},
|
},
|
||||||
unfavorite(id) {
|
unfavorite(id) {
|
||||||
this.setFavorited({ id, value: false })
|
return this.requestInteract({
|
||||||
|
name: 'favorite',
|
||||||
unfavorite({
|
|
||||||
id,
|
id,
|
||||||
credentials: useOAuthStore().token,
|
apiCall: unfavorite,
|
||||||
}).then(({ data: status, timestamp }) => {
|
optimisticCall: this.setFavorited,
|
||||||
this.addNewStatuses({
|
value: false,
|
||||||
statuses: [status],
|
|
||||||
timestamp,
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
setFavorited({ id, value }) {
|
setFavorited(id, value) {
|
||||||
const newStatus = this.allStatuses.get(id)
|
const newStatus = this.allStatuses.get(id)
|
||||||
|
|
||||||
if (newStatus.favorited !== value) {
|
if (newStatus.favorited !== value) {
|
||||||
|
|
@ -348,33 +380,25 @@ export const useStatusesStore = defineStore('statuses', {
|
||||||
|
|
||||||
/// Reprööt
|
/// Reprööt
|
||||||
retweet(id) {
|
retweet(id) {
|
||||||
this.setRetweeted({ id, value: true })
|
return this.requestInteract({
|
||||||
|
name: 'retweet',
|
||||||
retweet({
|
|
||||||
id,
|
id,
|
||||||
credentials: useOAuthStore().token,
|
apiCall: retweet,
|
||||||
}).then(({ data: status, timestamp }) => {
|
optimisticCall: this.setRetweeted,
|
||||||
this.addNewStatuses({
|
value: true,
|
||||||
statuses: [status],
|
|
||||||
timestamp,
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
unretweet(id) {
|
unretweet(id) {
|
||||||
this.setRetweeted({ id, value: false })
|
return this.requestInteract({
|
||||||
|
name: 'retweet',
|
||||||
unretweet({
|
|
||||||
id,
|
id,
|
||||||
credentials: useOAuthStore().token,
|
apiCall: unretweet,
|
||||||
}).then(({ data: status, timestamp }) => {
|
optimisticCall: this.setRetweeted,
|
||||||
this.addNewStatuses({
|
value: false,
|
||||||
statuses: [status],
|
|
||||||
timestamp,
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
setRetweeted({ statusId, value }) {
|
setRetweeted(id, value) {
|
||||||
const newStatus = this.allStatuses.get(statusId)
|
const newStatus = this.allStatuses.get(id)
|
||||||
|
|
||||||
if (newStatus.repeated !== value) {
|
if (newStatus.repeated !== value) {
|
||||||
if (value) {
|
if (value) {
|
||||||
|
|
@ -389,185 +413,151 @@ export const useStatusesStore = defineStore('statuses', {
|
||||||
|
|
||||||
// React
|
// React
|
||||||
reactWithEmoji(id, emoji) {
|
reactWithEmoji(id, emoji) {
|
||||||
this.addOwnReaction({ id, emoji })
|
return this.requestInteract({
|
||||||
|
name: 'emoji',
|
||||||
reactWithEmoji({
|
|
||||||
id,
|
id,
|
||||||
emoji,
|
apiCall: reactWithEmoji,
|
||||||
credentials: useOAuthStore().token,
|
optimisticCall: this.setOwnReaction,
|
||||||
}).then(({ data: status, timestamp }) => {
|
argument: emoji,
|
||||||
this.addNewStatuses({
|
value: true,
|
||||||
statuses: [status],
|
|
||||||
timestamp,
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
unreactWithEmoji(id, emoji) {
|
unreactWithEmoji(id, emoji) {
|
||||||
this.removeOwnReaction({ id, emoji })
|
return this.requestInteract({
|
||||||
|
name: 'emoji',
|
||||||
unreactWithEmoji({
|
|
||||||
id,
|
id,
|
||||||
emoji,
|
apiCall: unreactWithEmoji,
|
||||||
credentials: useOAuthStore().token,
|
optimisticCall: this.setOwnReaction,
|
||||||
}).then(({ data: status, timestamp }) => {
|
argument: emoji,
|
||||||
this.addNewStatuses({
|
value: false,
|
||||||
statuses: [status],
|
|
||||||
timestamp,
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
addOwnReaction(id, emoji) {
|
setOwnReaction(id, value, emoji) {
|
||||||
const currentUser = useUsersStore().currentUser
|
const currentUser = useUsersStore().currentUser
|
||||||
const status = this.allStatuses.get(id)
|
const status = this.allStatuses.get(id)
|
||||||
const reactionIndex = status.emoji_reactions.findIndex(
|
const reactionIndex = status.emoji_reactions.findIndex(
|
||||||
(react) => react.name === emoji,
|
(react) => react.name === emoji,
|
||||||
)
|
)
|
||||||
|
const reactionPresent = reactionIndex >= 0
|
||||||
|
if (!value && !reactionPresent) return
|
||||||
|
|
||||||
const reaction = status.emoji_reactions[reactionIndex] || {
|
const reaction = status.emoji_reactions[reactionIndex] || {
|
||||||
name: emoji,
|
name: emoji,
|
||||||
count: 0,
|
count: 0,
|
||||||
accounts: [],
|
accounts: [],
|
||||||
}
|
}
|
||||||
|
|
||||||
const newReaction = {
|
const count = value ? reaction.count + 1 : reaction.count - 1
|
||||||
...reaction,
|
|
||||||
count: reaction.count + 1,
|
|
||||||
me: true,
|
|
||||||
accounts: [...reaction.accounts, currentUser],
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update count of existing reaction if it exists, otherwise append at the end
|
const accounts = value
|
||||||
if (reactionIndex >= 0) {
|
? [...reaction.accounts, currentUser]
|
||||||
status.emoji_reactions[reactionIndex] = newReaction
|
: accounts.filter((acc) => acc.id !== currentUser.id)
|
||||||
} else {
|
|
||||||
status.emoji_reactions.push(newReaction)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
removeOwnReaction(id, emoji) {
|
|
||||||
const currentUser = useUsersStore().currentUser
|
|
||||||
const status = this.allStatuses.get(id)
|
|
||||||
const reactionIndex = status.emoji_reactions.findIndex(
|
|
||||||
(react) => react.name === emoji,
|
|
||||||
)
|
|
||||||
if (reactionIndex < 0) return
|
|
||||||
|
|
||||||
const reaction = status.emoji_reactions[reactionIndex]
|
|
||||||
const accounts = reaction.accounts || []
|
|
||||||
|
|
||||||
const newReaction = {
|
const newReaction = {
|
||||||
...reaction,
|
...reaction,
|
||||||
count: reaction.count - 1,
|
count,
|
||||||
me: false,
|
me: value,
|
||||||
accounts: accounts.filter((acc) => acc.id !== currentUser.id),
|
accounts,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newReaction.count > 0) {
|
if (reactionPresent && count > 0) {
|
||||||
status.emoji_reactions[reactionIndex] = newReaction
|
status.emoji_reactions[reactionIndex] = newReaction
|
||||||
} else {
|
} else if (count === 0) {
|
||||||
status.emoji_reactions = status.emoji_reactions.filter(
|
status.emoji_reactions = status.emoji_reactions.filter(
|
||||||
(r) => r.name !== emoji,
|
(r) => r.name !== emoji,
|
||||||
)
|
)
|
||||||
|
} else {
|
||||||
|
status.emoji_reactions.push(newReaction)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
/// Bookmark
|
/// Bookmark
|
||||||
bookmark(id, bookmark_folder_id) {
|
bookmark(id, bookmark_folder_id) {
|
||||||
this.setBookmarked({ id, value: true, bookmark_folder_id })
|
return this.requestInteract({
|
||||||
|
name: 'bookmark',
|
||||||
bookmarkStatus({
|
|
||||||
id,
|
id,
|
||||||
folder_id: bookmark_folder_id,
|
apiCall: bookmarkStatus,
|
||||||
credentials: useOAuthStore().token,
|
optimisticCall: this.setBookmarked,
|
||||||
}).then(({ data: status, timestamp }) => {
|
argument: bookmark_folder_id,
|
||||||
this.addNewStatuses({
|
value: false,
|
||||||
statuses: [status],
|
|
||||||
timestamp,
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
unbookmark(id) {
|
unbookmark(id) {
|
||||||
this.setBookmarked({ id, value: false })
|
return this.requestInteract({
|
||||||
|
name: 'bookmark',
|
||||||
unbookmarkStatus({
|
|
||||||
id,
|
id,
|
||||||
credentials: useOAuthStore().token,
|
apiCall: unbookmarkStatus,
|
||||||
}).then(({ data: status, timestamp }) => {
|
optimisticCall: this.setBookmarked,
|
||||||
this.addNewStatuses({
|
value: false,
|
||||||
statuses: [status],
|
|
||||||
timestamp,
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
setBookmarked({ id, value, bookmark_folder_id }) {
|
setBookmarked(id, value, bookmark_folder_id) {
|
||||||
const status = this.allStatuses.get(id)
|
const status = this.allStatuses.get(id)
|
||||||
status.bookmarked = value
|
status.bookmarked = value
|
||||||
|
|
||||||
|
// When unbookmarking we don't specify folder so we wanna keep
|
||||||
|
// reference to the folder even when setting bookmarked to false
|
||||||
|
// the proper reference will be updated when api call resolves
|
||||||
|
if (bookmark_folder_id) {
|
||||||
status.bookmark_folder_id = value ? bookmark_folder_id : null
|
status.bookmark_folder_id = value ? bookmark_folder_id : null
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
/// Mute
|
/// Mute
|
||||||
muteConversation(id) {
|
muteConversation(id) {
|
||||||
return muteConversation({
|
return this.requestInteract({
|
||||||
|
name: 'mute',
|
||||||
id,
|
id,
|
||||||
credentials: useOAuthStore().token,
|
apiCall: muteConversation,
|
||||||
|
optimisticCall: this.setMutedStatus,
|
||||||
|
value: false,
|
||||||
})
|
})
|
||||||
.then(({ data: status, timestamp }) => {
|
|
||||||
this.addNewStatuses({
|
|
||||||
statuses: [status],
|
|
||||||
timestamp,
|
|
||||||
})
|
|
||||||
return status
|
|
||||||
})
|
|
||||||
.then((status) => this.setMutedStatus(status))
|
|
||||||
},
|
},
|
||||||
unmuteConversation(id) {
|
unmuteConversation(id) {
|
||||||
return unmuteConversation({
|
return this.requestInteract({
|
||||||
|
name: 'mute',
|
||||||
id,
|
id,
|
||||||
credentials: useOAuthStore().token,
|
apiCall: unmuteConversation,
|
||||||
|
optimisticCall: this.setMutedStatus,
|
||||||
|
value: false,
|
||||||
})
|
})
|
||||||
.then(({ data: status, timestamp }) => {
|
|
||||||
this.addNewStatuses({
|
|
||||||
statuses: [status],
|
|
||||||
timestamp,
|
|
||||||
})
|
|
||||||
return status
|
|
||||||
})
|
|
||||||
.then((status) => this.setMutedStatus(status))
|
|
||||||
},
|
},
|
||||||
setMutedStatus({ id, thread_muted }) {
|
setMutedStatus(id, value) {
|
||||||
// Setting thread_muted flag on all other known statuses
|
// Setting thread_muted flag on all other known statuses
|
||||||
// belonging to same conversation
|
// belonging to same conversation
|
||||||
const newStatus = this.allStatuses.get(id)
|
const newStatus = this.allStatuses.get(id)
|
||||||
newStatus.thread_muted = thread_muted
|
newStatus.thread_muted = value
|
||||||
|
|
||||||
if (newStatus.thread_muted !== undefined) {
|
if (newStatus.thread_muted !== undefined) {
|
||||||
this.conversations
|
this.conversations
|
||||||
.get(newStatus.statusnet_conversation_id)
|
.get(newStatus.statusnet_conversation_id)
|
||||||
.forEach((status) => {
|
.forEach((status) => {
|
||||||
status.thread_muted = thread_muted
|
status.thread_muted = value
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
/// Pin
|
/// Pin
|
||||||
pinStatus(id) {
|
pinStatus(id) {
|
||||||
return pinOwnStatus({
|
return this.requestInteract({
|
||||||
|
name: 'pin',
|
||||||
id,
|
id,
|
||||||
credentials: useOAuthStore().token,
|
apiCall: pinOwnStatus,
|
||||||
}).then(({ data: status, timestamp }) => {
|
optimisticCall: () => {
|
||||||
this.addNewStatuses({
|
/* no-op */
|
||||||
statuses: [status],
|
},
|
||||||
timestamp,
|
value: true,
|
||||||
})
|
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
unpinStatus(id) {
|
unpinStatus(id) {
|
||||||
return unpinOwnStatus({
|
return this.requestInteract({
|
||||||
|
name: 'pin',
|
||||||
id,
|
id,
|
||||||
credentials: useOAuthStore().token,
|
apiCall: unpinOwnStatus,
|
||||||
}).then(({ data: status, timestamp }) => {
|
optimisticCall: () => {
|
||||||
this.addNewStatuses({
|
/* no-op */
|
||||||
statuses: [status],
|
},
|
||||||
timestamp,
|
value: false,
|
||||||
})
|
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue