Browse Source

fix: refactor endpoints

pull/9323/head
DarkPhoenix2704 2 months ago
parent
commit
fbc3502a7d
  1. 42
      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

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

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

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

@ -5,8 +5,6 @@ export const useProductFeed = createSharedComposable(() => {
const { $api } = useNuxtApp()
const { appInfo } = useGlobal()
const youtubeFeed = ref<ProductFeedItem[]>([])
const githubFeed = ref<ProductFeedItem[]>([])
@ -19,8 +17,6 @@ export const useProductFeed = createSharedComposable(() => {
social: false,
})
const newFeedCount = ref(0)
const loadFeed = async ({ loadMore, type }: { loadMore: boolean; type: 'youtube' | 'github' | 'all' }) => {
try {
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 {
isErrorOccurred,
activeTab,

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

@ -173,10 +173,4 @@ export class UtilsController {
async feed(@Request() req: NcRequest) {
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": {
"get": {
"summary": "Get Feed",

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

@ -544,45 +544,4 @@ export class UtilsService {
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