change emoji reactions to use new format

This commit is contained in:
Shpuld Shpuldson 2020-01-26 15:45:12 +02:00
parent b10b92a876
commit a018ea622c
7 changed files with 99 additions and 37 deletions

View file

@ -150,7 +150,7 @@ const conversation = {
if (!id) return if (!id) return
this.highlight = id this.highlight = id
this.$store.dispatch('fetchFavsAndRepeats', id) this.$store.dispatch('fetchFavsAndRepeats', id)
this.$store.dispatch('fetchEmojiReactions', id) this.$store.dispatch('fetchEmojiReactionsBy', id)
}, },
getHighlight () { getHighlight () {
return this.isExpanded ? this.highlight : null return this.isExpanded ? this.highlight : null

View file

@ -4,12 +4,17 @@ const EmojiReactions = {
props: ['status'], props: ['status'],
computed: { computed: {
emojiReactions () { emojiReactions () {
return this.status.emojiReactions console.log(this.status.emoji_reactions)
return this.status.emoji_reactions
} }
}, },
methods: { methods: {
reactedWith (emoji) { reactedWith (emoji) {
return this.status.reactedWithEmoji.includes(emoji) // return []
const user = this.$store.state.users.currentUser
const reaction = this.status.emoji_reactions.find(r => r.emoji === emoji)
console.log(reaction)
return reaction.accounts && reaction.accounts.find(u => u.id === user.id)
}, },
reactWith (emoji) { reactWith (emoji) {
this.$store.dispatch('reactWithEmoji', { id: this.status.id, emoji }) this.$store.dispatch('reactWithEmoji', { id: this.status.id, emoji })

View file

@ -1,14 +1,14 @@
<template> <template>
<div class="emoji-reactions"> <div class="emoji-reactions">
<button <button
v-for="(users, emoji) in emojiReactions" v-for="(reaction) in emojiReactions"
:key="emoji" :key="reaction.emoji"
class="emoji-reaction btn btn-default" class="emoji-reaction btn btn-default"
:class="{ 'picked-reaction': reactedWith(emoji) }" :class="{ 'picked-reaction': reactedWith(reaction.emoji) }"
@click="emojiOnClick(emoji, $event)" @click="emojiOnClick(reaction.emoji, $event)"
> >
<span v-if="users">{{ users.length }}</span> <span>{{ reaction.count }}</span>
<span>{{ emoji }}</span> <span>{{ reaction.emoji }}</span>
</button> </button>
</div> </div>
</template> </template>

View file

@ -355,7 +355,6 @@
</transition> </transition>
<EmojiReactions <EmojiReactions
v-if="isFocused"
:status="status" :status="status"
/> />

View file

@ -10,10 +10,7 @@ import {
first, first,
last, last,
isArray, isArray,
omitBy, omitBy
flow,
filter,
keys
} from 'lodash' } from 'lodash'
import { set } from 'vue' import { set } from 'vue'
import apiService from '../services/api/api.service.js' import apiService from '../services/api/api.service.js'
@ -534,33 +531,48 @@ export const mutations = {
newStatus.fave_num = newStatus.favoritedBy.length newStatus.fave_num = newStatus.favoritedBy.length
newStatus.favorited = !!newStatus.favoritedBy.find(({ id }) => currentUser.id === id) newStatus.favorited = !!newStatus.favoritedBy.find(({ id }) => currentUser.id === id)
}, },
addEmojiReactions (state, { id, emojiReactions, currentUser }) { addEmojiReactionsBy (state, { id, emojiReactions, currentUser }) {
const status = state.allStatusesObject[id] const status = state.allStatusesObject[id]
set(status, 'emojiReactions', emojiReactions) set(status, 'emoji_reactions', emojiReactions)
const reactedWithEmoji = flow(
keys,
filter(reaction => find(reaction, { id: currentUser.id }))
)(emojiReactions)
set(status, 'reactedWithEmoji', reactedWithEmoji)
}, },
addOwnReaction (state, { id, emoji, currentUser }) { addOwnReaction (state, { id, emoji, currentUser }) {
const status = state.allStatusesObject[id] const status = state.allStatusesObject[id]
status.emojiReactions = status.emojiReactions || {} const reactionIndex = findIndex(status.emoji_reactions, { emoji })
const listOfUsers = (status.emojiReactions && status.emojiReactions[emoji]) || [] const reaction = status.emoji_reactions[reactionIndex] || { emoji, count: 0, accounts: [] }
const hasSelfAlready = !!find(listOfUsers, { id: currentUser.id })
if (!hasSelfAlready) { const newReaction = {
set(status.emojiReactions, emoji, listOfUsers.concat([{ id: currentUser.id }])) ...reaction,
set(status, 'reactedWithEmoji', [...status.reactedWithEmoji, emoji]) count: reaction.count + 1,
accounts: [
...reaction.accounts,
currentUser
]
}
// Update count of existing reaction if it exists, otherwise append at the end
if (reactionIndex >= 0) {
set(status.emoji_reactions, reactionIndex, newReaction)
} else {
set(status, 'emoji_reactions', [...status.emoji_reactions, newReaction])
} }
}, },
removeOwnReaction (state, { id, emoji, currentUser }) { removeOwnReaction (state, { id, emoji, currentUser }) {
const status = state.allStatusesObject[id] const status = state.allStatusesObject[id]
const listOfUsers = status.emojiReactions[emoji] || [] const reactionIndex = findIndex(status.emoji_reactions, { emoji })
const hasSelfAlready = !!find(listOfUsers, { id: currentUser.id }) if (reactionIndex < 0) return
if (hasSelfAlready) {
const newUsers = filter(listOfUsers, user => user.id !== currentUser.id) const reaction = status.emoji_reactions[reactionIndex]
set(status.emojiReactions, emoji, newUsers)
set(status, 'reactedWithEmoji', status.reactedWithEmoji.filter(e => e !== emoji)) const newReaction = {
...reaction,
count: reaction.count - 1,
accounts: reaction.accounts.filter(acc => acc.id === currentUser.id)
}
if (newReaction.count > 0) {
set(status.emoji_reactions, reactionIndex, newReaction)
} else {
set(status, 'emoji_reactions', status.emoji_reactions.filter(r => r.emoji !== emoji))
} }
}, },
updateStatusWithPoll (state, { id, poll }) { updateStatusWithPoll (state, { id, poll }) {
@ -672,7 +684,7 @@ const statuses = {
commit('addOwnReaction', { id, emoji, currentUser }) commit('addOwnReaction', { id, emoji, currentUser })
rootState.api.backendInteractor.reactWithEmoji({ id, emoji }).then( rootState.api.backendInteractor.reactWithEmoji({ id, emoji }).then(
status => { status => {
dispatch('fetchEmojiReactions', id) dispatch('fetchEmojiReactionsBy', id)
} }
) )
}, },
@ -681,14 +693,14 @@ const statuses = {
commit('removeOwnReaction', { id, emoji, currentUser }) commit('removeOwnReaction', { id, emoji, currentUser })
rootState.api.backendInteractor.unreactWithEmoji({ id, emoji }).then( rootState.api.backendInteractor.unreactWithEmoji({ id, emoji }).then(
status => { status => {
dispatch('fetchEmojiReactions', id) dispatch('fetchEmojiReactionsBy', id)
} }
) )
}, },
fetchEmojiReactions ({ rootState, commit }, id) { fetchEmojiReactionsBy ({ rootState, commit }, id) {
rootState.api.backendInteractor.fetchEmojiReactions({ id }).then( rootState.api.backendInteractor.fetchEmojiReactions({ id }).then(
emojiReactions => { emojiReactions => {
commit('addEmojiReactions', { id, emojiReactions, currentUser: rootState.users.currentUser }) commit('addEmojiReactionsBy', { id, emojiReactions, currentUser: rootState.users.currentUser })
} }
) )
}, },

View file

@ -233,6 +233,7 @@ export const parseStatus = (data) => {
output.statusnet_html = addEmojis(data.content, data.emojis) output.statusnet_html = addEmojis(data.content, data.emojis)
output.tags = data.tags output.tags = data.tags
output.emoji_reactions = [{ emoji: 'A', count: 5 }] // data.pleroma.emoji_reactions
if (data.pleroma) { if (data.pleroma) {
const { pleroma } = data const { pleroma } = data

View file

@ -241,6 +241,51 @@ describe('Statuses module', () => {
}) })
}) })
describe('emojiReactions', () => {
it('increments count in existing reaction', () => {
const state = defaultState()
const status = makeMockStatus({ id: '1' })
status.emoji_reactions = [ { emoji: '😂', count: 1, accounts: [] } ]
mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
mutations.addOwnReaction(state, { id: '1', emoji: '😂', currentUser: { id: 'me' } })
expect(state.allStatusesObject['1'].emoji_reactions[0].count).to.eql(2)
expect(state.allStatusesObject['1'].emoji_reactions[0].accounts[0].id).to.eql('me')
})
it('adds a new reaction', () => {
const state = defaultState()
const status = makeMockStatus({ id: '1' })
status.emoji_reactions = []
mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
mutations.addOwnReaction(state, { id: '1', emoji: '😂', currentUser: { id: 'me' } })
expect(state.allStatusesObject['1'].emoji_reactions[0].count).to.eql(1)
expect(state.allStatusesObject['1'].emoji_reactions[0].accounts[0].id).to.eql('me')
})
it('decreases count in existing reaction', () => {
const state = defaultState()
const status = makeMockStatus({ id: '1' })
status.emoji_reactions = [ { emoji: '😂', count: 2, accounts: [{ id: 'me' }] } ]
mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
mutations.removeOwnReaction(state, { id: '1', emoji: '😂', currentUser: {} })
expect(state.allStatusesObject['1'].emoji_reactions[0].count).to.eql(1)
expect(state.allStatusesObject['1'].emoji_reactions[0].accounts).to.eql([])
})
it('removes a reaction', () => {
const state = defaultState()
const status = makeMockStatus({ id: '1' })
status.emoji_reactions = [{ emoji: '😂', count: 1, accounts: [{ id: 'me' }] }]
mutations.addNewStatuses(state, { statuses: [status], showImmediately: true, timeline: 'public' })
mutations.removeOwnReaction(state, { id: '1', emoji: '😂', currentUser: {} })
expect(state.allStatusesObject['1'].emoji_reactions.length).to.eql(0)
})
})
describe('showNewStatuses', () => { describe('showNewStatuses', () => {
it('resets the minId to the min of the visible statuses when adding new to visible statuses', () => { it('resets the minId to the min of the visible statuses when adding new to visible statuses', () => {
const state = defaultState() const state = defaultState()