Browse Source

fix: refactor endpoints

pull/9323/head
DarkPhoenix2704 2 months ago
parent
commit
fbc3502a7d
  1. 26
      packages/nc-gui/components/dashboard/Sidebar/Feed.vue
  2. 40
      packages/nc-gui/composables/useProductFeed.ts
  3. 6
      packages/nocodb/src/controllers/utils.controller.ts
  4. 27
      packages/nocodb/src/schema/swagger.json
  5. 41
      packages/nocodb/src/services/utils.service.ts

26
packages/nc-gui/components/dashboard/Sidebar/Feed.vue

@ -7,11 +7,14 @@ const { navigateToFeed } = workspaceStore
const { isFeedPageOpened } = storeToRefs(workspaceStore) const { isFeedPageOpened } = storeToRefs(workspaceStore)
const { appInfo } = useGlobal()
const { loadFeed, socialFeed } = useProductFeed() const { loadFeed, socialFeed } = useProductFeed()
const isNewFeedAvailable = ref(false) const isNewFeedAvailable = ref(false)
const checkNewFeed = async () => { const checkNewFeed = async () => {
try {
await loadFeed({ type: 'all', loadMore: false }) await loadFeed({ type: 'all', loadMore: false })
if (!socialFeed.value.length) return if (!socialFeed.value.length) return
@ -23,9 +26,30 @@ const checkNewFeed = async () => {
isNewFeedAvailable.value = true isNewFeedAvailable.value = true
localStorage.setItem('lastFeedPublishedTime', latestFeed['Published Time']) localStorage.setItem('lastFeedPublishedTime', latestFeed['Published Time'])
} }
} catch (error) {
console.error('Error while checking new feed', error)
}
}
const intervalId = ref()
const checkFeedWithInterval = async () => {
await checkNewFeed()
intervalId.value = setTimeout(checkFeedWithInterval, 3 * 60 * 60 * 1000)
}
onMounted(() => {
if (appInfo.value.feedEnabled) {
checkFeedWithInterval()
} }
})
onMounted(checkNewFeed) onUnmounted(() => {
if (intervalId.value) {
clearTimeout(intervalId.value)
intervalId.value = null
}
})
const gotoFeed = () => navigateToFeed() const gotoFeed = () => navigateToFeed()
</script> </script>

40
packages/nc-gui/composables/useProductFeed.ts

@ -5,8 +5,6 @@ export const useProductFeed = createSharedComposable(() => {
const { $api } = useNuxtApp() const { $api } = useNuxtApp()
const { appInfo } = useGlobal()
const youtubeFeed = ref<ProductFeedItem[]>([]) const youtubeFeed = ref<ProductFeedItem[]>([])
const githubFeed = ref<ProductFeedItem[]>([]) const githubFeed = ref<ProductFeedItem[]>([])
@ -19,8 +17,6 @@ export const useProductFeed = createSharedComposable(() => {
social: false, social: false,
}) })
const newFeedCount = ref(0)
const loadFeed = async ({ loadMore, type }: { loadMore: boolean; type: 'youtube' | 'github' | 'all' }) => { const loadFeed = async ({ loadMore, type }: { loadMore: boolean; type: 'youtube' | 'github' | 'all' }) => {
try { try {
let page = 1 let page = 1
@ -73,42 +69,6 @@ export const useProductFeed = createSharedComposable(() => {
} }
} }
const checkForNewFeed = async () => {
const lastPublishedAt = localStorage.getItem('last_published_at')
if (!lastPublishedAt) {
return
}
try {
const newFeeds = await $api.utils.feed({ last_published_at: lastPublishedAt })
if (typeof newFeeds === 'number') {
newFeedCount.value = newFeeds
} else if (typeof newFeeds === 'string') {
newFeedCount.value = 100
}
} catch (error) {
console.error(error)
}
}
const intervalId = ref()
const checkFeedWithInterval = async () => {
await checkForNewFeed()
intervalId.value = setTimeout(checkFeedWithInterval, 3 * 60 * 60 * 1000)
}
onMounted(() => {
if (appInfo.value.feedEnabled) {
checkFeedWithInterval()
}
})
onUnmounted(() => {
if (intervalId.value) clearTimeout(intervalId.value)
})
return { return {
isErrorOccurred, isErrorOccurred,
activeTab, activeTab,

6
packages/nocodb/src/controllers/utils.controller.ts

@ -173,10 +173,4 @@ export class UtilsController {
async feed(@Request() req: NcRequest) { async feed(@Request() req: NcRequest) {
return await this.utilsService.feed(req); return await this.utilsService.feed(req);
} }
@UseGuards(PublicApiLimiterGuard)
@Get('/api/v2/new-feed')
async newFeed(@Request() req: NcRequest) {
return await this.utilsService.getLatestFeed(req);
}
} }

27
packages/nocodb/src/schema/swagger.json

@ -16015,33 +16015,6 @@
] ]
} }
}, },
"/api/v2/new-feed": {
"get": {
"summary": "Get Feed",
"operationId": "utils-feed",
"parameters": [
{
"schema": {
"type": "string"
},
"name": "last_published_at",
"in": "query",
"required": false
}
],
"responses": {
"200": {
"description": "OK"
},
"400": {
"$ref": "#/components/responses/BadRequest"
}
},
"tags": [
"Utils"
]
}
},
"/api/v2/feed": { "/api/v2/feed": {
"get": { "get": {
"summary": "Get Feed", "summary": "Get Feed",

41
packages/nocodb/src/services/utils.service.ts

@ -544,45 +544,4 @@ export class UtilsService {
return response.data; return response.data;
} }
async getLatestFeed(req: NcRequest) {
const { last_published_at } = req.query as {
last_published_at: string;
};
if (!last_published_at) {
return 0;
}
const utils = {
found: false,
page: 1,
missedItems: 0,
};
const feed = await this.feed({
query: {
type: 'all',
page: utils.page.toString(),
per_page: '100',
},
} as unknown as NcRequest);
if (!feed || !feed?.length) {
return 0;
}
for (const item of feed) {
if (item['Published Time'] === last_published_at) {
utils.found = true;
break;
}
utils.missedItems++;
}
if (utils.found) {
return utils.missedItems;
}
return `${utils.missedItems}+`;
}
} }

Loading…
Cancel
Save