INSERT INTO `cp_filejs` (`id`,`app`,`path`,`modul`,`file_name`,`content`) VALUES ('303','3','u7/collector','auth','js/notifications.js--','import {\r\n getSessionUser\r\n} from \\\\\\\"./api/auth.js\\\\\\\"\r\nimport {\r\n maybeFollowUser\r\n} from \\\\\\\"./api/profile.js\\\\\\\"\r\nimport app from \\\\\\\"./app.js\\\\\\\"\r\nimport store from \\\\\\\"./store.js\\\\\\\"\r\n\r\nvar $ = Dom7\r\nvar notificationsStore = store.getters.getNotifications\r\nvar refreshed = false\r\n\r\n$(document).on(\\\\\\\\\\\\\\\'page:afterin\\\\\\\\\\\\\\\', \\\\\\\\\\\\\\\'.page[data-name=\\\\\\\"notifications\\\\\\\"]\\\\\\\\\\\\\\\', async function (e) {\r\n const data = notificationsStore.value\r\n\r\n if (!data || !data.success) {\r\n return\r\n }\r\n\r\n // a list if all unread notification ids\r\n const unreadNotificationIds = [\r\n ...data.data.recent,\r\n ...data.data.last_week,\r\n ...data.data.last_30_days,\r\n ].filter((item) => item.is_read === \\\\\\\"0\\\\\\\").map((item) => item._id);\r\n\r\n if (unreadNotificationIds.length > 0) {\r\n store.dispatch(\\\\\\\\\\\\\\\'markNotificationsAsRead\\\\\\\\\\\\\\\', unreadNotificationIds)\r\n }\r\n})\r\n\r\nnotificationsStore.onUpdated(async (data) => {\r\n if (!data || !data.success) {\r\n $(\\\\\\\\\\\\\\\'.notification-wrap\\\\\\\\\\\\\\\').html(\\\\\\\\\\\\\\\'
No notifications
\\\\\\\\\\\\\\\')\r\n return\r\n }\r\n\r\n const notifications = data.data\r\n\r\n const recentContainer = document.getElementById(\\\\\\\\\\\\\\\'recent\\\\\\\\\\\\\\\');\r\n const thisWeekContainer = document.getElementById(\\\\\\\\\\\\\\\'this-week\\\\\\\\\\\\\\\');\r\n\r\n if (refreshed) {\r\n recentContainer.innerHTML = \\\\\\\\\\\\\\\'\\\\\\\\\\\\\\\';\r\n thisWeekContainer.innerHTML = \\\\\\\\\\\\\\\'\\\\\\\\\\\\\\\';\r\n refreshed = false\r\n }\r\n\r\n document.querySelectorAll(\\\\\\\\\\\\\\\'.app-notification-title\\\\\\\\\\\\\\\').forEach(elem => {\r\n elem.innerHTML = elem.getAttribute(\\\\\\\\\\\\\\\'data-title\\\\\\\\\\\\\\\')\r\n })\r\n\r\n var user = await getSessionUser()\r\n\r\n if (!notifications.recent.length) {\r\n recentContainer.innerHTML = \\\\\\\\\\\\\\\'No recent notifications
\\\\\\\\\\\\\\\';\r\n }\r\n\r\n if (!notifications.last_week.length) {\r\n thisWeekContainer.innerHTML = \\\\\\\\\\\\\\\'No notifications from this week
\\\\\\\\\\\\\\\';\r\n }\r\n\r\n notifications.recent.forEach(notification => {\r\n const notificationItem = createNotificationItem(notification, user);\r\n recentContainer.appendChild(notificationItem);\r\n });\r\n\r\n notifications.last_week.forEach(notification => {\r\n const notificationItem = createNotificationItem(notification, user);\r\n thisWeekContainer.appendChild(notificationItem);\r\n });\r\n})\r\n\r\nfunction timeAgo(dateString) {\r\n const now = new Date();\r\n const past = new Date(dateString);\r\n const diffInHours = Math.floor((now - past) / (1000 * 60 * 60));\r\n return diffInHours > 24 ? `${Math.floor(diffInHours / 24)}d ago` : `${diffInHours}h ago`;\r\n}\r\n\r\nfunction createNotificationItem(notification, user) {\r\n const isFollow = notification.type === \\\\\\\\\\\\\\\'follow\\\\\\\\\\\\\\\' ? true : false;\r\n const container = document.createElement(isFollow ? \\\\\\\\\\\\\\\'div\\\\\\\\\\\\\\\' : \\\\\\\\\\\\\\\'a\\\\\\\\\\\\\\\');\r\n\r\n if (!isFollow) {\r\n container.href = `/post-view/${notification.entity.entity_id}`;\r\n }\r\n\r\n let isReadClass = notification.is_read == \\\\\\\"0\\\\\\\" ? \\\\\\\"unread-notif\\\\\\\" : \\\\\\\"\\\\\\\";\r\n\r\n container.className = `notification-item ${isReadClass}`;\r\n container.dataset.notificationId = notification._id;\r\n\r\n // Profile image and notification content container\r\n const leftContainer = document.createElement(\\\\\\\\\\\\\\\'div\\\\\\\\\\\\\\\');\r\n leftContainer.className = \\\\\\\\\\\\\\\'notification-left\\\\\\\\\\\\\\\';\r\n\r\n const imageDiv = document.createElement(\\\\\\\\\\\\\\\'a\\\\\\\\\\\\\\\');\r\n imageDiv.className = \\\\\\\\\\\\\\\'image-square image-rounded\\\\\\\\\\\\\\\';\r\n imageDiv.style.backgroundImage = `url(\\\\\\\\\\\\\\\'${notification.entity.initiator_data.profile_image || \\\\\\\\\\\\\\\'assets/img/profile-placeholder.jpg\\\\\\\\\\\\\\\'}\\\\\\\\\\\\\\\')`;\r\n imageDiv.href = `/profile-view/${notification.entity.user_id}`;\r\n\r\n const infoDiv = document.createElement(\\\\\\\\\\\\\\\'div\\\\\\\\\\\\\\\');\r\n infoDiv.className = \\\\\\\\\\\\\\\'notification-info\\\\\\\\\\\\\\\';\r\n\r\n let content = \\\\\\\\\\\\\\\'\\\\\\\\\\\\\\\';\r\n\r\n // Conditional content rendering based on the type\r\n if (notification.type === \\\\\\\\\\\\\\\'like\\\\\\\\\\\\\\\') {\r\n content = `\r\n\r\n ${notification.entity.initiator_data.display_name} liked your ${notification.entity.entity_type}\r\n ${notification.entity.entity_data.comment ? `: \\\\\\\"${notification.entity.entity_data.comment}\\\\\\\"` : \\\\\\\\\\\\\\\'\\\\\\\\\\\\\\\'}\r\n \r\n
\r\n `;\r\n } else if (notification.type === \\\\\\\\\\\\\\\'comment\\\\\\\\\\\\\\\') {\r\n const eclipseComment = notification.entity.entity_data.comment.length > 50 ? notification.entity.entity_data.comment.substring(0, 50) + \\\\\\\\\\\\\\\'...\\\\\\\\\\\\\\\' : notification.entity.entity_data.comment;\r\n\r\n container.href = `/post-view/${notification.entity.entity_data.post_id}?commentId=${notification.entity.entity_id}`;\r\n\r\n content = `\r\n \r\n ${notification.entity.initiator_data.display_name} commented on your post: \r\n \\\\\\\"${eclipseComment}\\\\\\\"\r\n \r\n
\r\n `;\r\n } else if (notification.type === \\\\\\\\\\\\\\\'follow\\\\\\\\\\\\\\\') {\r\n content = `\r\n \r\n ${notification.entity.initiator_data.display_name} followed you\r\n \r\n
\r\n `;\r\n } else if (notification.type === \\\\\\\\\\\\\\\'mention\\\\\\\\\\\\\\\') {\r\n content = `\r\n \r\n ${notification.entity.initiator_data.display_name} mentioned you in a ${notification.entity.entity_type}\r\n ${notification.entity.entity_data.comment ? `\\\\\\\"${notification.entity.entity_data.comment}\\\\\\\"` : \\\\\\\\\\\\\\\'\\\\\\\\\\\\\\\'}\r\n \r\n
\r\n `;\r\n } else if (notification.type === \\\\\\\\\\\\\\\'post\\\\\\\\\\\\\\\') {\r\n content = `\r\n \r\n ${notification.entity.initiator_data.display_name} has posted ${notification.entity.entity_type === \\\\\\\\\\\\\\\'car\\\\\\\\\\\\\\\' ? \\\\\\\"your car\\\\\\\" : \\\\\\\"a post\\\\\\\"} \r\n \r\n ${notification.entity.entity_data?.garage?.make || \\\\\\\\\\\\\\\'\\\\\\\\\\\\\\\'} ${notification.entity.entity_data?.garage?.model || \\\\\\\\\\\\\\\'\\\\\\\\\\\\\\\'}\r\n \r\n \r\n
\r\n `;\r\n } else if (notification.type === \\\\\\\\\\\\\\\'tag\\\\\\\\\\\\\\\') {\r\n content = `\r\n \r\n ${notification.entity.initiator_data.display_name} ${notification.entity.entity_type === \\\\\\\\\\\\\\\'car\\\\\\\\\\\\\\\' ? \\\\\\\"tagged your car in a post\\\\\\\" : \\\\\\\"tagged you in a post\\\\\\\"}\r\n \r\n
\r\n `;\r\n }\r\n\r\n // Add time ago\r\n const timeSpan = document.createElement(\\\\\\\\\\\\\\\'span\\\\\\\\\\\\\\\');\r\n timeSpan.className = \\\\\\\\\\\\\\\'notification-time\\\\\\\\\\\\\\\';\r\n timeSpan.textContent = timeAgo(notification.date);\r\n\r\n\r\n infoDiv.innerHTML = `${content} ${timeSpan.outerHTML}`;\r\n\r\n // Adding profile image and content to the left container\r\n leftContainer.appendChild(imageDiv);\r\n leftContainer.appendChild(infoDiv);\r\n\r\n // Append left container to the main container\r\n container.appendChild(leftContainer);\r\n\r\n if (notification.type === \\\\\\\\\\\\\\\'follow\\\\\\\\\\\\\\\') {\r\n const isFollowing = user.following.includes(notification.entity.user_id);\r\n let followBtn = `\r\n ${isFollowing ? \\\\\\\\\\\\\\\'Unfollow\\\\\\\\\\\\\\\' : \\\\\\\\\\\\\\\'Follow\\\\\\\\\\\\\\\'}\r\n
`;\r\n\r\n container.innerHTML += followBtn;\r\n } else {\r\n const rightContainer = document.createElement(\\\\\\\\\\\\\\\'a\\\\\\\\\\\\\\\');\r\n rightContainer.className = \\\\\\\\\\\\\\\'notification-left\\\\\\\\\\\\\\\';\r\n let path;\r\n\r\n if (notification.entity.entity_type === \\\\\\\\\\\\\\\'car\\\\\\\\\\\\\\\') {\r\n path = \\\\\\\\\\\\\\\'car\\\\\\\\\\\\\\\';\r\n rightContainer.href = `/${path}/${notification.entity.entity_id}`;\r\n\r\n } else if (notification.entity.entity_type === \\\\\\\\\\\\\\\'post\\\\\\\\\\\\\\\') {\r\n path = \\\\\\\\\\\\\\\'post-view\\\\\\\\\\\\\\\';\r\n rightContainer.href = `/${path}/${notification.entity.entity_id}`;\r\n\r\n } else if (notification.entity.entity_type === \\\\\\\\\\\\\\\'comment\\\\\\\\\\\\\\\') {\r\n path = \\\\\\\\\\\\\\\'post-view\\\\\\\\\\\\\\\';\r\n rightContainer.href = `/${path}/${notification.entity.entity_data.post_id}?commentId=${notification.entity.entity_id}`;\r\n } else {\r\n path = \\\\\\\\\\\\\\\'profile-view\\\\\\\\\\\\\\\';\r\n rightContainer.href = `/${path}/${notification.entity.user_id}`;\r\n }\r\n\r\n\r\n const imageDiv = document.createElement(\\\\\\\\\\\\\\\'div\\\\\\\\\\\\\\\');\r\n imageDiv.className = \\\\\\\\\\\\\\\'image-square image-rounded\\\\\\\\\\\\\\\';\r\n imageDiv.style.backgroundImage = `url(\\\\\\\\\\\\\\\'${notification.entity.entity_data.media}\\\\\\\\\\\\\\\')`;\r\n imageDiv.style.backgroundSize = \\\\\\\\\\\\\\\'cover\\\\\\\\\\\\\\\';\r\n imageDiv.style.backgroundPosition = \\\\\\\\\\\\\\\'center\\\\\\\\\\\\\\\';\r\n imageDiv.style.backgroundColor = \\\\\\\\\\\\\\\'#f1f1f1\\\\\\\\\\\\\\\';\r\n\r\n rightContainer.appendChild(imageDiv);\r\n container.appendChild(rightContainer)\r\n }\r\n\r\n return container;\r\n}\r\n\r\n$(document).on(\\\\\\\\\\\\\\\'click\\\\\\\\\\\\\\\', \\\\\\\\\\\\\\\'.toggle-follow\\\\\\\\\\\\\\\', async function (e) {\r\n const userId = e.target.dataset.userId;\r\n const isFollowing = e.target.dataset.isFollowing === \\\\\\\\\\\\\\\'true\\\\\\\\\\\\\\\';\r\n\r\n // update the button text\r\n e.target.textContent = isFollowing ? \\\\\\\\\\\\\\\'Follow\\\\\\\\\\\\\\\' : \\\\\\\\\\\\\\\'Unfollow\\\\\\\\\\\\\\\';\r\n e.target.dataset.isFollowing = !isFollowing;\r\n\r\n await maybeFollowUser(userId);\r\n store.dispatch(\\\\\\\\\\\\\\\'updateUserDetails\\\\\\\\\\\\\\\')\r\n});\r\n\r\n$(document).on(\\\\\\\\\\\\\\\'ptr:refresh\\\\\\\\\\\\\\\', \\\\\\\\\\\\\\\'.notification-page.ptr-content\\\\\\\\\\\\\\\', async function (e) {\r\n refreshed = true\r\n\r\n try {\r\n await store.dispatch(\\\\\\\\\\\\\\\'notificationCount\\\\\\\\\\\\\\\')\r\n await store.dispatch(\\\\\\\\\\\\\\\'fetchNotifications\\\\\\\\\\\\\\\')\r\n } catch (error) {\r\n console.log(error);\r\n }\r\n\r\n app.ptr.get(\\\\\\\\\\\\\\\'.notification-page.ptr-content\\\\\\\\\\\\\\\').done()\r\n})');