2023-02-23 21:42:10 +00:00
|
|
|
import { SEND_AUTH_NOTIF, ENABLE_RIGHT_CLICK, DISABLE_RIGHT_CLICK } from '@/constants/messeges';
|
2023-02-13 19:20:50 +00:00
|
|
|
import { initStorage } from '@/utils/storage'
|
2023-09-28 04:51:36 +00:00
|
|
|
import { getStore, setProfile, setNotifStorageNotifs, setSettings,
|
|
|
|
setNotifStorageLastRewardNotif, getNotifStorageLastRewardNotif, getSetting, setSetting } from '@/utils/storage'
|
2023-02-13 19:20:50 +00:00
|
|
|
import type { Notification } from '@/utils/types';
|
|
|
|
import { API_BASE } from '@/constants/config';
|
|
|
|
import { getNotifications } from '@/utils/notifications';
|
2023-09-28 04:51:36 +00:00
|
|
|
import { setBadge, extrenalNavigate } from '@/utils/chrome-misc'
|
2023-02-22 12:31:37 +00:00
|
|
|
import { closeTo } from '@/utils/time';
|
2023-02-13 19:20:50 +00:00
|
|
|
import { getActionUsage } from '@/utils/user';
|
2023-02-23 21:42:10 +00:00
|
|
|
import { executeVote, getVotePayload } from '@/utils/votes';
|
2023-09-29 00:08:58 +00:00
|
|
|
import { YUP_APP_BASE } from '@/constants/config';
|
2023-02-23 21:42:10 +00:00
|
|
|
|
2023-02-13 19:20:50 +00:00
|
|
|
// Disable conflict with yup extension
|
|
|
|
const yupExtensionId = 'nhmeoaahigiljjdkoagafdccikgojjoi'
|
|
|
|
|
|
|
|
console.info('Service worker started')
|
|
|
|
|
2023-09-28 04:51:36 +00:00
|
|
|
let notificationUrl: string
|
|
|
|
|
|
|
|
const buttons = {
|
|
|
|
buttons: [{
|
|
|
|
title: 'Open in App',
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
|
|
|
|
const notificationActionListner = async (id: string) => {
|
|
|
|
try {
|
2023-09-29 00:08:58 +00:00
|
|
|
const url = new URL(notificationUrl ?? `${YUP_APP_BASE}/notifications`)
|
2023-09-28 04:51:36 +00:00
|
|
|
extrenalNavigate(url.href)
|
|
|
|
chrome.notifications.clear(id)
|
|
|
|
} catch {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!chrome.notifications.onButtonClicked.hasListener(notificationActionListner)){
|
|
|
|
chrome.notifications.onButtonClicked.addListener(notificationActionListner)
|
|
|
|
}
|
2023-02-23 21:42:10 +00:00
|
|
|
|
|
|
|
const onRightClickLike = async (store, info, tab) => {
|
|
|
|
const vote = await executeVote(getVotePayload({
|
|
|
|
store,
|
|
|
|
url: tab.url,
|
|
|
|
type: true
|
|
|
|
}))
|
|
|
|
if (store?.settings?.enableRightClickNotif) {
|
|
|
|
if (vote?._id) {
|
|
|
|
await chrome.notifications.create({
|
|
|
|
type: 'basic',
|
|
|
|
iconUrl: chrome.runtime.getURL('src/assets/icons/yup_ext_128.png'),
|
|
|
|
title: 'Yup Live Extension',
|
|
|
|
message: `Your rating has been submitted`,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
await chrome.notifications.create({
|
|
|
|
type: 'basic',
|
|
|
|
iconUrl: chrome.runtime.getURL('src/assets/icons/yup_ext_128.png'),
|
|
|
|
title: 'Yup Live Extension',
|
|
|
|
message: `There was an error submitting your rating`,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const enableRightClickVote = async () => {
|
|
|
|
try {
|
2023-02-28 13:25:00 +00:00
|
|
|
await chrome.contextMenus.removeAll();
|
2023-02-23 21:42:10 +00:00
|
|
|
await chrome.contextMenus.create({
|
|
|
|
id: "yup-like",
|
|
|
|
title: "Like this page",
|
|
|
|
contexts: ["page", "page_action"],
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const disableRightClickVote = async () => {
|
|
|
|
try {
|
|
|
|
await chrome.contextMenus.remove("yup-like");
|
|
|
|
} catch (error) {
|
|
|
|
// ignore
|
|
|
|
}
|
|
|
|
if(chrome.runtime.lastError) {
|
|
|
|
console.warn('Error creating context menu', chrome.runtime.lastError.message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-13 19:20:50 +00:00
|
|
|
const alarmHandler = async () => {
|
|
|
|
const store = await getStore()
|
|
|
|
const requests = {} as Record<string, Promise<Response | Notification[]>>
|
|
|
|
if (store?.user?.auth?.authToken) {
|
|
|
|
requests.profile = fetch(`${API_BASE}/web3-profiles/` + store.user.auth.address)
|
|
|
|
if (store?.settings.notificationsEnabled) {
|
|
|
|
requests.notifications = getNotifications({
|
2023-04-13 00:47:21 +00:00
|
|
|
type: null,
|
2023-02-13 19:20:50 +00:00
|
|
|
limit: '15',
|
|
|
|
skip: '0',
|
2023-09-28 04:51:36 +00:00
|
|
|
address: store.user.auth.address
|
2023-02-13 19:20:50 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const profile = await requests.profile as Response
|
|
|
|
const profileJson = await profile.json()
|
|
|
|
setProfile(profileJson).catch(console.error)
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error fetching profile', error)
|
|
|
|
}
|
2024-03-22 19:45:04 +00:00
|
|
|
const oneHour = 3600000
|
|
|
|
const isExpiredCoinGecko = !store?.settings?.coinGeckoPrice || (new Date().getTime() - store.settings.lastCoinGeckoPriceCheckTimestamp) > oneHour
|
|
|
|
if (isExpiredCoinGecko ) {
|
2023-02-13 19:20:50 +00:00
|
|
|
try {
|
2024-03-22 19:45:04 +00:00
|
|
|
requests.coinGecko = fetch('https://api.coingecko.com/api/v3/simple/price?ids=yup&vs_currencies=usd')
|
|
|
|
requests.coinGecko.catch(console.info)
|
2023-02-13 19:20:50 +00:00
|
|
|
const coinGecko = await requests.coinGecko as Response
|
|
|
|
const coinGeckoJson = await coinGecko.json()
|
|
|
|
const coinGeckoPrice = coinGeckoJson.yup.usd
|
|
|
|
if (store.settings.coinGeckoPrice !== coinGeckoPrice) {
|
2024-01-13 17:55:58 +00:00
|
|
|
await chrome.storage.local.set({ store: { ...store, settings: { ...store.settings, coinGeckoPrice, lastCoinGeckoPriceCheckTimestamp: new Date().getTime() } } })
|
2023-02-13 19:20:50 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2024-03-22 19:45:04 +00:00
|
|
|
console.info('Error fetching coinGecko', error)
|
2023-02-13 19:20:50 +00:00
|
|
|
}
|
2024-03-22 19:45:04 +00:00
|
|
|
}
|
2023-02-13 19:20:50 +00:00
|
|
|
|
|
|
|
if (store?.settings.notificationsEnabled) {
|
|
|
|
try {
|
|
|
|
const notifications = await requests.notifications as Notification[]
|
2023-09-29 00:08:58 +00:00
|
|
|
const notSeen = notifications.filter(notif => !notif.seen)
|
2023-02-13 19:20:50 +00:00
|
|
|
const updateSettings = {} as Record<string, boolean>
|
|
|
|
if (notSeen.length > 0) {
|
|
|
|
setBadge(String(notSeen.length))
|
|
|
|
setNotifStorageNotifs(notSeen).catch(console.error)
|
|
|
|
updateSettings.hasNewNotifications = true
|
|
|
|
} else {
|
|
|
|
setBadge('')
|
|
|
|
updateSettings.hasNewNotifications = false
|
|
|
|
}
|
2023-02-17 22:12:40 +00:00
|
|
|
setSettings(updateSettings).catch(console.error)
|
2023-02-13 19:20:50 +00:00
|
|
|
|
2023-09-28 04:51:36 +00:00
|
|
|
if (store.settings?.chromeNotifWhenReward && notSeen.some(notif => notif.eventType === 'reward')) {
|
|
|
|
const rewardNotif = notSeen.find(notif => notif.eventType === 'reward')
|
2023-02-13 19:20:50 +00:00
|
|
|
if (rewardNotif) {
|
2023-02-22 12:31:37 +00:00
|
|
|
const storeReward = (await getNotifStorageLastRewardNotif())
|
|
|
|
|
2023-02-23 21:42:10 +00:00
|
|
|
if (!storeReward || (storeReward.id !== rewardNotif._id
|
2023-02-22 12:31:37 +00:00
|
|
|
&& !closeTo(new Date(storeReward.createdAt), new Date(rewardNotif.createdAt), 2e4)
|
2023-02-23 21:42:10 +00:00
|
|
|
)) {
|
2023-02-13 19:20:50 +00:00
|
|
|
{
|
2023-02-23 21:42:10 +00:00
|
|
|
await setNotifStorageLastRewardNotif({ createdAt: rewardNotif.createdAt, id: rewardNotif._id });
|
|
|
|
await chrome.notifications.create({
|
|
|
|
type: 'basic',
|
|
|
|
iconUrl: chrome.runtime.getURL('src/assets/icons/yup_ext_128.png'),
|
|
|
|
title: 'Yup Live Extension',
|
2023-09-28 04:51:36 +00:00
|
|
|
message: `You have been alocated a future reward of ${rewardNotif?.meta.quantity ?? "unknown"} YUP`,
|
2023-02-23 21:42:10 +00:00
|
|
|
})
|
2023-02-13 19:20:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-28 04:51:36 +00:00
|
|
|
} else if (store.settings?.enableFollowNotif && notSeen.some(notif => notif.eventType === 'follow')) {
|
|
|
|
const followNotif = notSeen.filter(notif => notif.eventType === 'follow').sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime())[0]
|
|
|
|
const lastFollowNotif = await getSetting('lastfollowNotif') as number
|
|
|
|
const isNew = !lastFollowNotif || ( !closeTo(new Date(lastFollowNotif), new Date(followNotif.createdAt), 2e4))
|
|
|
|
if (followNotif && isNew) {
|
2023-10-01 03:46:14 +00:00
|
|
|
notificationUrl = followNotif?.senders?.[0]._id ? `${YUP_APP_BASE}/account/${followNotif?.senders?.[0]._id}`: undefined
|
2023-09-28 04:51:36 +00:00
|
|
|
await chrome.notifications.create({
|
|
|
|
type: 'basic',
|
|
|
|
iconUrl: chrome.runtime.getURL('src/assets/icons/yup_ext_128.png'),
|
|
|
|
title: 'Yup Live Extension',
|
|
|
|
message: `${followNotif.senders[0].handle} has followed you`,
|
|
|
|
...(buttons)
|
|
|
|
})
|
|
|
|
await setSetting('lastfollowNotif', new Date(followNotif.createdAt).getTime())
|
|
|
|
}
|
|
|
|
} else if (store.settings?.enableCommentNotif && notSeen.some(notif => notif.eventType === 'comment')) {
|
|
|
|
const commentNotif = notSeen.filter(notif => notif.eventType === 'comment').sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime())[0]
|
|
|
|
const lastCommentNotif = await getSetting('lastCommentNotif') as number
|
|
|
|
const isNew = !lastCommentNotif || ( !closeTo(new Date(lastCommentNotif), new Date(commentNotif.createdAt), 2e4))
|
|
|
|
if (commentNotif && isNew) {
|
2023-10-01 03:46:14 +00:00
|
|
|
notificationUrl = commentNotif?.meta?.postid ? `${YUP_APP_BASE}/post/${commentNotif?.meta?.postid}`: undefined
|
2023-09-28 04:51:36 +00:00
|
|
|
await chrome.notifications.create({
|
|
|
|
type: 'basic',
|
|
|
|
iconUrl: chrome.runtime.getURL('src/assets/icons/yup_ext_128.png'),
|
|
|
|
title: 'Yup Live Extension',
|
|
|
|
message: `${commentNotif.senders[0].handle} has commented on your post`,
|
|
|
|
...(buttons)
|
|
|
|
})
|
|
|
|
await setSetting('lastCommentNotif', new Date(commentNotif.createdAt).getTime())
|
|
|
|
}
|
|
|
|
} else if (store.settings?.enableMentionNotif && notSeen.some(notif => notif.eventType === 'mention')) {
|
|
|
|
const mentionNotif = notSeen.filter(notif => notif.eventType === 'mention').sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime())[0]
|
|
|
|
const lastMentionNotif = await getSetting('lastMentionNotif') as number
|
|
|
|
const isNew = !lastMentionNotif || ( !closeTo(new Date(lastMentionNotif), new Date(mentionNotif.createdAt), 2e4))
|
|
|
|
if (mentionNotif && isNew) {
|
2023-10-01 03:46:14 +00:00
|
|
|
notificationUrl = mentionNotif?.meta?.postid ? `${YUP_APP_BASE}/post/${mentionNotif?.meta?.postid}`: undefined
|
2023-09-28 04:51:36 +00:00
|
|
|
await chrome.notifications.create({
|
|
|
|
type: 'basic',
|
|
|
|
iconUrl: chrome.runtime.getURL('src/assets/icons/yup_ext_128.png'),
|
|
|
|
title: 'Yup Live Extension',
|
|
|
|
message: `${mentionNotif.senders[0].handle} has mentioned you`,
|
|
|
|
...(buttons)
|
|
|
|
})
|
|
|
|
await setSetting('lastMentionNotif', new Date(mentionNotif.createdAt).getTime())
|
|
|
|
}
|
2023-02-13 19:20:50 +00:00
|
|
|
}
|
|
|
|
if (store.settings?.chromeNotifWhenAbleToVote) {
|
2023-02-17 22:12:40 +00:00
|
|
|
await getActionUsage(store?.user?.auth?.userId)
|
2023-02-13 19:20:50 +00:00
|
|
|
}
|
2023-02-23 21:42:10 +00:00
|
|
|
|
2023-02-13 19:20:50 +00:00
|
|
|
} catch (error) {
|
2023-02-23 21:42:10 +00:00
|
|
|
// console.error('Error fetching notifications', error)
|
|
|
|
// ignore
|
2023-02-13 19:20:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
chrome.alarms.create(
|
|
|
|
'alarm',
|
|
|
|
{
|
2023-09-29 00:08:58 +00:00
|
|
|
periodInMinutes: 0.1,
|
2023-02-13 19:20:50 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
chrome.alarms.onAlarm.addListener(alarmHandler)
|
|
|
|
|
2023-02-23 21:42:10 +00:00
|
|
|
chrome.runtime.onInstalled.addListener(async () => {
|
2023-02-13 19:20:50 +00:00
|
|
|
initStorage()
|
|
|
|
chrome.management.setEnabled(yupExtensionId, false)
|
2023-02-23 21:42:10 +00:00
|
|
|
const store = await getStore()
|
|
|
|
if (store?.user?.auth?.authToken && store?.settings?.enableRightClick) {
|
|
|
|
enableRightClickVote()
|
|
|
|
}
|
2023-02-13 19:20:50 +00:00
|
|
|
});
|
|
|
|
|
2023-02-23 21:42:10 +00:00
|
|
|
chrome.runtime.onStartup.addListener(async () => {
|
2023-02-13 19:20:50 +00:00
|
|
|
initStorage()
|
|
|
|
chrome.management.setEnabled(yupExtensionId, false)
|
2023-02-23 21:42:10 +00:00
|
|
|
const store = await getStore()
|
|
|
|
if (store?.user?.auth?.authToken && store?.settings?.enableRightClick) {
|
|
|
|
enableRightClickVote()
|
|
|
|
}
|
2023-02-13 19:20:50 +00:00
|
|
|
})
|
|
|
|
|
2023-02-23 21:42:10 +00:00
|
|
|
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
|
|
|
|
if (info.menuItemId === 'yup-like') {
|
|
|
|
const store = await getStore()
|
|
|
|
if (store?.user?.auth?.authToken) {
|
|
|
|
onRightClickLike(store, info, tab)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2023-02-13 19:20:50 +00:00
|
|
|
chrome.runtime.onMessage.addListener(async (request, sender, sendResponse) => {
|
|
|
|
try {
|
2023-09-29 00:08:58 +00:00
|
|
|
// console.log('Message received', request)
|
2023-09-28 04:51:36 +00:00
|
|
|
const lastLoginNotifTime = Number(await getSetting('lastLoginNotif'))
|
|
|
|
const moreThanOneDay = (new Date().getTime() - lastLoginNotifTime) > 864e5
|
|
|
|
if (request.type === SEND_AUTH_NOTIF && moreThanOneDay) {
|
2023-02-13 19:20:50 +00:00
|
|
|
chrome.notifications.create({
|
|
|
|
type: 'basic',
|
|
|
|
iconUrl: chrome.runtime.getURL('src/assets/icons/yup_ext_128.png'),
|
|
|
|
title: 'Yup Live Extension',
|
|
|
|
message: 'You have been logged in.',
|
|
|
|
})
|
2023-09-28 04:51:36 +00:00
|
|
|
setSetting('lastLoginNotif', new Date().getTime())
|
2023-02-23 21:42:10 +00:00
|
|
|
sendResponse({ success: true })
|
|
|
|
} else if (request.type === ENABLE_RIGHT_CLICK) {
|
|
|
|
await enableRightClickVote()
|
|
|
|
sendResponse({ success: true })
|
|
|
|
} else if (request.type === DISABLE_RIGHT_CLICK) {
|
|
|
|
await disableRightClickVote()
|
|
|
|
sendResponse({ success: true })
|
2023-02-13 19:20:50 +00:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error in message listener', error)
|
|
|
|
sendResponse({ error })
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|