Max 1e6a74b8ce
feat(frontend): observe posts (#8293)
* After creating the post, the author of it automatically observes it to get notifications when there are interactions

* a user that comments a post, automatically observes that post to get notifications when there are more interactions on that post

* mutation that switches the state of the obeservation of a post on and off

* remove duplicate code

* fix unit tests

* add metric observed users count to posts

* change naming

* Add follow post entry to post menu

* Add FollowButton (WIP), show unfollow in menu when already followed

* Follow/unfollow post => observe

* Update slashed bell

* Add requests to observe/unobserve posts

* Add ObserveButton functionality

* Rename isObservedByMe

* Add observingUsersCount; simplify ObserveButton and menu entries

* Fix locales

* Add snapshot test for ObserveButton (WIP)

* Remove empty routes push

* Add test for ObserveButton

* Add test for ContentMenu, improve ObserveButton test

* Remove unneeded fields from PostQuery

---------

Co-authored-by: Moriz Wahl <moriz.wahl@gmx.de>
2025-04-04 11:54:43 +00:00

265 lines
6.8 KiB
Vue

<template>
<dropdown class="content-menu" :placement="placement" offset="5">
<template #default="{ toggleMenu }">
<slot name="button" :toggleMenu="toggleMenu">
<base-button
data-test="content-menu-button"
icon="ellipsis-v"
size="small"
circle
@click.prevent="toggleMenu()"
/>
</slot>
</template>
<template #popover="{ toggleMenu }">
<div class="content-menu-popover">
<ds-menu :routes="routes">
<template #menuitem="item">
<ds-menu-item
:route="item.route"
:parents="item.parents"
@click.stop.prevent="openItem(item.route, toggleMenu)"
>
<base-icon :name="item.route.icon" />
{{ item.route.label }}
</ds-menu-item>
</template>
</ds-menu>
</div>
</template>
</dropdown>
</template>
<script>
import Dropdown from '~/components/Dropdown'
export default {
name: 'ContentMenu',
components: {
Dropdown,
},
props: {
placement: { type: String, default: 'top-end' },
resource: { type: Object, required: true },
isOwner: { type: Boolean, default: false },
resourceType: {
type: String,
required: true,
validator: (value) => {
return value.match(/(contribution|comment|organization|user)/)
},
},
modalsData: {
type: Object,
required: false,
default: () => {
return {}
},
},
},
computed: {
routes() {
const routes = []
if (this.resourceType === 'contribution') {
if (this.isOwner) {
routes.push({
label: this.$t(`post.menu.edit`),
name: 'post-edit-id',
params: {
id: this.resource.id,
},
icon: 'edit',
})
routes.push({
label: this.$t(`post.menu.delete`),
callback: () => {
this.openModal('confirm', 'delete')
},
icon: 'trash',
})
}
if (this.isAdmin && !this.resource.group) {
if (!this.resource.pinnedBy) {
routes.push({
label: this.$t(`post.menu.pin`),
callback: () => {
this.$emit('pinPost', this.resource)
},
icon: 'link',
})
} else {
routes.push({
label: this.$t(`post.menu.unpin`),
callback: () => {
this.$emit('unpinPost', this.resource)
},
icon: 'unlink',
})
}
}
if (this.resource.isObservedByMe) {
routes.push({
label: this.$t(`post.menu.unobserve`),
callback: () => {
this.$emit('toggleObservePost', this.resource.id, false)
},
icon: 'bell-slashed',
})
} else {
routes.push({
label: this.$t(`post.menu.observe`),
callback: () => {
this.$emit('toggleObservePost', this.resource.id, true)
},
icon: 'bell',
})
}
}
if (this.isOwner && this.resourceType === 'comment') {
routes.push({
label: this.$t(`comment.menu.edit`),
callback: () => {
this.$emit('editComment')
},
icon: 'edit',
})
routes.push({
label: this.$t(`comment.menu.delete`),
callback: () => {
this.openModal('confirm', 'delete')
},
icon: 'trash',
})
}
if (!this.isOwner) {
routes.push({
label: this.$t(`report.${this.resourceType}.title`),
callback: () => {
this.openModal('report')
},
icon: 'flag',
})
}
if (!this.isOwner && this.isModerator) {
if (!this.resource.disabled) {
routes.push({
label: this.$t(`disable.${this.resourceType}.title`),
callback: () => {
this.openModal('disable')
},
icon: 'eye-slash',
})
} else {
routes.push({
label: this.$t(`release.${this.resourceType}.title`),
callback: () => {
this.openModal('release')
},
icon: 'eye',
})
}
}
if (this.resourceType === 'user') {
if (this.isOwner) {
routes.push({
label: this.$t(`settings.name`),
path: '/settings',
icon: 'edit',
})
} else {
if (this.resource.isMuted) {
routes.push({
label: this.$t(`settings.muted-users.unmute`),
callback: () => {
this.$emit('unmute', this.resource)
},
icon: 'microphone',
})
} else {
routes.push({
label: this.$t(`settings.muted-users.mute`),
callback: () => {
this.$emit('mute', this.resource)
},
icon: 'microphone-slash',
})
}
if (this.resource.isBlocked) {
routes.push({
label: this.$t(`settings.blocked-users.unblock`),
callback: () => {
this.$emit('unblock', this.resource)
},
icon: 'user-plus',
})
} else {
routes.push({
label: this.$t(`settings.blocked-users.block`),
callback: () => {
this.$emit('block', this.resource)
},
icon: 'user-times',
})
}
if (this.isAdmin === true) {
routes.push({
label: this.$t(`settings.deleteUserAccount.name`),
callback: () => {
this.$emit('delete', this.resource)
},
icon: 'trash',
})
}
}
}
return routes
},
isModerator() {
return this.$store.getters['auth/isModerator']
},
isAdmin() {
return this.$store.getters['auth/isAdmin']
},
},
methods: {
openItem(route, toggleMenu) {
if (route.callback) {
route.callback()
} else {
this.$router.push(route)
}
toggleMenu()
},
openModal(dialog, modalDataName = null) {
this.$store.commit('modal/SET_OPEN', {
name: dialog,
data: {
type: this.resourceType,
resource: this.resource,
modalData: modalDataName ? this.modalsData[modalDataName] : {},
},
})
},
},
}
</script>
<style lang="scss">
.content-menu-popover {
nav {
margin-top: -$space-xx-small;
margin-bottom: -$space-xx-small;
margin-left: -$space-x-small;
margin-right: -$space-x-small;
}
}
</style>