Skip to content

Commit af736cf

Browse files
Merge pull request #5692 from christianbeeznest/ofaj-21101-16
Social: Fix friends list, update requests, add notifications - refs BT#21101
2 parents 40e0e23 + 7c4a221 commit af736cf

File tree

5 files changed

+120
-32
lines changed

5 files changed

+120
-32
lines changed

assets/vue/components/basecomponents/BaseButton.vue

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
:severity="primeSeverityProperty"
1212
:size="size"
1313
:text="onlyIcon"
14-
:title="onlyIcon ? label : undefined"
14+
:title="tooltip || (onlyIcon ? label : undefined)"
1515
:type="isSubmit ? 'submit' : 'button'"
1616
:loading="isLoading"
1717
@click="$emit('click', $event)"
@@ -44,8 +44,7 @@ const props = defineProps({
4444
required: true,
4545
validator: buttonTypeValidator,
4646
},
47-
// associate this button to a popup through its identifier, this will make this button toggle the popup
48-
popupIdentifier: {
47+
tooltip: {
4948
type: String,
5049
default: "",
5150
},
@@ -67,6 +66,10 @@ const props = defineProps({
6766
type: Boolean,
6867
default: false,
6968
},
69+
popupIdentifier: {
70+
type: String,
71+
default: "", // This ensures that popupIdentifier is still present
72+
},
7073
})
7174
7275
defineEmits(["click"])

assets/vue/components/userreluser/Layout.vue

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</BaseCard>
1919
</div>
2020
<div class="md:basis-3/4 lg:basis-5/6 2xl:basis-7/8">
21-
<router-view></router-view>
21+
<router-view @friend-request-sent="reloadRequestsList"></router-view>
2222
</div>
2323
</div>
2424
</template>
@@ -81,6 +81,12 @@ const reloadHandler = async () => {
8181
}
8282
}
8383
84+
const reloadRequestsList = () => {
85+
if (requestList.value) {
86+
requestList.value.loadRequests();
87+
}
88+
}
89+
8490
onMounted(async () => {
8591
await loadUser()
8692
await reloadHandler()

assets/vue/components/userreluser/UserRelUserRequestsList.vue

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,16 @@
3838
icon="user-add"
3939
only-icon
4040
type="black"
41-
@click="addFriend(request)"
41+
:tooltip="t('Accept invitation')"
42+
@click="acceptFriendRequest(request)"
43+
/>
44+
<BaseButton
45+
class="ml-2"
46+
icon="user-delete"
47+
only-icon
48+
type="danger"
49+
:tooltip="t('Reject invitation')"
50+
@click="rejectFriendRequest(request)"
4251
/>
4352
</div>
4453

@@ -100,15 +109,11 @@ const loadRequests = () => {
100109
waitingRequests.value = []
101110
102111
Promise.all([
103-
userRelUserService.findAll({
104-
params: friendRequestFilter,
105-
}),
106-
userRelUserService.findAll({
107-
params: waitingFilter,
108-
}),
112+
userRelUserService.findAll({ params: friendRequestFilter }),
113+
userRelUserService.findAll({ params: waitingFilter }),
109114
])
110115
.then(([sentRequestsResponse, waitingRequestsRespose]) =>
111-
Promise.all([sentRequestsResponse.json(), waitingRequestsRespose.json()]),
116+
Promise.all([sentRequestsResponse.json(), waitingRequestsRespose.json()])
112117
)
113118
.then(([sentRequestsJson, waitingRequestsJson]) => {
114119
friendRequests.value = sentRequestsJson["hydra:member"]
@@ -118,19 +123,26 @@ const loadRequests = () => {
118123
.finally(() => (loading.value = false))
119124
}
120125
121-
function addFriend(friend) {
122-
// Change from request to friend
126+
function acceptFriendRequest(request) {
123127
axios
124-
.put(friend["@id"], {
125-
relationType: 3,
128+
.put(request["@id"], { relationType: 3 })
129+
.then(() => {
130+
emit("accept-friend", request)
131+
notification.showSuccessNotification(t("Friend added successfully"))
132+
loadRequests()
126133
})
134+
.catch((e) => notification.showErrorNotification(e))
135+
}
136+
137+
function rejectFriendRequest(request) {
138+
axios
139+
.delete(request["@id"])
127140
.then(() => {
128-
emit("accept-friend", friend)
141+
notification.showSuccessNotification(t("Friend request rejected"))
142+
loadRequests()
129143
})
130144
.catch((e) => notification.showErrorNotification(e))
131145
}
132146
133-
defineExpose({
134-
loadRequests,
135-
})
147+
defineExpose({ loadRequests })
136148
</script>

assets/vue/views/userreluser/UserRelUserAdd.vue

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
</div>
3838
</template>
3939
<script setup>
40-
import { onMounted, ref } from "vue"
40+
import { onMounted, ref, defineEmits } from "vue"
4141
import { useRoute, useRouter } from "vue-router"
4242
import { useI18n } from "vue-i18n"
4343
import { useNotification } from "../../composables/notification"
@@ -48,6 +48,8 @@ import userService from "../../services/userService"
4848
import userRelUserService from "../../services/userRelUserService"
4949
import { useSecurityStore } from "../../store/securityStore"
5050
51+
const emit = defineEmits(["friend-request-sent"])
52+
5153
const securityStore = useSecurityStore()
5254
const router = useRouter()
5355
const route = useRoute()
@@ -72,13 +74,20 @@ const asyncFind = (query) => {
7274
})
7375
}
7476
77+
const extractIdFromPath = (path) => {
78+
const parts = path.split("/")
79+
return parts[parts.length - 1]
80+
}
81+
7582
const addFriend = (friend) => {
7683
isLoadingSelect.value = true
7784
7885
userRelUserService
7986
.sendFriendRequest(securityStore.user["@id"], friend["@id"])
8087
.then(() => {
8188
showSuccessNotification(t("Friend request sent successfully"))
89+
emit('friend-request-sent')
90+
sendNotificationMessage(friend)
8291
})
8392
.catch((error) => {
8493
showErrorNotification(t("Failed to send friend request"))
@@ -89,6 +98,38 @@ const addFriend = (friend) => {
8998
})
9099
}
91100
101+
const sendNotificationMessage = async (friend) => {
102+
const userId = extractIdFromPath(securityStore.user["@id"])
103+
const targetUserId = extractIdFromPath(friend["@id"])
104+
105+
const messageData = {
106+
userId: userId,
107+
targetUserId: targetUserId,
108+
action: 'send_message',
109+
subject: t('You have a new friend request'),
110+
content: t('You have received a new friend request. Visit the invitations page to accept or reject the request.') + ` <a href="/resources/friends">${t('here')}</a>`
111+
}
112+
113+
try {
114+
const response = await fetch('/social-network/user-action', {
115+
method: 'POST',
116+
headers: {
117+
'Content-Type': 'application/json',
118+
},
119+
body: JSON.stringify(messageData)
120+
})
121+
const result = await response.json()
122+
if (result.success) {
123+
showSuccessNotification(t('Notification message sent successfully'))
124+
} else {
125+
showErrorNotification(t('Failed to send notification message'))
126+
}
127+
} catch (error) {
128+
showErrorNotification(t('An error occurred while sending the notification message'))
129+
console.error('Error sending notification message:', error)
130+
}
131+
}
132+
92133
const goToBack = () => {
93134
router.push({ name: "UserRelUserList" })
94135
}

assets/vue/views/userreluser/UserRelUserList.vue

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
</template>
7474

7575
<script setup>
76-
import { inject, ref, onMounted } from "vue"
76+
import { ref, onMounted, watch } from "vue"
7777
import BaseToolbar from "../../components/basecomponents/BaseToolbar.vue"
7878
import BaseButton from "../../components/basecomponents/BaseButton.vue"
7979
import Skeleton from "primevue/skeleton"
@@ -84,10 +84,10 @@ import { useConfirm } from "primevue/useconfirm"
8484
import userRelUserService from "../../services/userreluser"
8585
import { useFormatDate } from "../../composables/formatDate"
8686
import { useNotification } from "../../composables/notification"
87+
import { useSocialInfo } from "../../composables/useSocialInfo"
8788
89+
const { user, isCurrentUser } = useSocialInfo()
8890
const { t } = useI18n()
89-
const user = inject('social-user')
90-
const isCurrentUser = inject('is-current-user')
9191
const items = ref([])
9292
const loadingFriends = ref(true)
9393
const notification = useNotification()
@@ -98,20 +98,38 @@ const confirm = useConfirm()
9898
const requestList = ref()
9999
100100
function reloadHandler() {
101+
if (!user.value) {
102+
console.log('User not defined yet');
103+
return;
104+
}
105+
101106
loadingFriends.value = true
102107
items.value = []
103108
104109
Promise.all([
105-
userRelUserService.findAll({ params: { user: user.id, relationType: 3 } }),
106-
userRelUserService.findAll({ params: { friend: user.id, relationType: 3 } }),
110+
userRelUserService.findAll({ params: { user: user.value.id, relationType: 3 } }),
111+
userRelUserService.findAll({ params: { friend: user.value.id, relationType: 3 } }),
107112
])
108-
.then(([friendshipResponse, friendshipBackResponse]) =>
109-
Promise.all([friendshipResponse.json(), friendshipBackResponse.json()])
110-
)
113+
.then(([friendshipResponse, friendshipBackResponse]) => {
114+
return Promise.all([friendshipResponse.json(), friendshipBackResponse.json()])
115+
})
111116
.then(([friendshipJson, friendshipBackJson]) => {
112-
items.value.push(...friendshipJson["hydra:member"], ...friendshipBackJson["hydra:member"])
117+
const friendsSet = new Set()
118+
items.value = [...friendshipJson["hydra:member"], ...friendshipBackJson["hydra:member"]]
119+
.filter(friend => {
120+
const friendId = friend.user['@id'] === user.value['@id'] ? friend.friend['@id'] : friend.user['@id']
121+
if (friendsSet.has(friendId)) {
122+
return false
123+
} else {
124+
friendsSet.add(friendId)
125+
return true
126+
}
127+
})
128+
})
129+
.catch((e) => {
130+
console.error('Error occurred', e);
131+
notification.showErrorNotification(e);
113132
})
114-
.catch((e) => notification.showErrorNotification(e))
115133
.finally(() => {
116134
loadingFriends.value = false
117135
if (requestList.value) {
@@ -120,8 +138,16 @@ function reloadHandler() {
120138
})
121139
}
122140
141+
watch(user, (newValue) => {
142+
if (newValue && newValue.id) {
143+
reloadHandler()
144+
}
145+
})
146+
123147
onMounted(() => {
124-
reloadHandler()
148+
if (user.value && user.value.id) {
149+
reloadHandler()
150+
}
125151
})
126152
127153
const goToAdd = () => {

0 commit comments

Comments
 (0)