Merge pull request #24 from masterElmar/16-implement-hash-for-feeds

16 implement hash for feeds
This commit is contained in:
masterElmar
2023-10-25 17:13:27 +02:00
committed by GitHub
13 changed files with 126 additions and 182 deletions

27
.vscode/launch.json vendored Normal file
View File

@@ -0,0 +1,27 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch file",
"type": "go",
"request": "launch",
"mode": "debug",
"program": "backend/main.go",
"args": ["serve"],
"showLog": true
},
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "backend",
"env": {},
"args": ["serve"],
"showLog": true
}
]
}

View File

@@ -1,4 +1,4 @@
FROM golang:1.20.5-alpine
FROM golang:1.21.3-alpine
# Set the Current Working Directory inside the container
WORKDIR /app

View File

@@ -1,8 +1,13 @@
package model
import "github.com/pocketbase/pocketbase/models"
type Feed struct {
Id string `db:"id" json:"id"`
Modules string `db:"modules" json:"modules"`
Created string `db:"created" json:"created"`
Updated string `db:"updated" json:"updated"`
models.BaseModel
}
// SetModules set modules field
func (f *Feed) SetModules(modules string) {
f.Modules = modules
}

View File

@@ -23,6 +23,7 @@ type Entry struct {
type Entries []*Entry
type FeedCollection struct {
UUID string `db:"uuid" json:"uuid"`
Name string `db:"Name" json:"name"`
Course string `db:"course" json:"course"`
UserDefinedName string `db:"userDefinedName" json:"userDefinedName"`

View File

@@ -1,6 +1,7 @@
package model
type Module struct {
UUID string `json:"uuid"`
Name string `json:"name"`
Prof string `json:"prof"`
Course string `json:"course"`

View File

@@ -1,172 +1,4 @@
[
{
"id": "_pb_users_auth_",
"name": "users",
"type": "auth",
"system": false,
"schema": [
{
"id": "users_name",
"name": "name",
"type": "text",
"system": false,
"required": false,
"options": {
"min": null,
"max": null,
"pattern": ""
}
},
{
"id": "users_avatar",
"name": "avatar",
"type": "file",
"system": false,
"required": false,
"options": {
"maxSelect": 1,
"maxSize": 5242880,
"mimeTypes": [
"image/jpeg",
"image/png",
"image/svg+xml",
"image/gif",
"image/webp"
],
"thumbs": null,
"protected": false
}
}
],
"indexes": [],
"listRule": "id = @request.auth.id",
"viewRule": "id = @request.auth.id",
"createRule": "",
"updateRule": "id = @request.auth.id",
"deleteRule": "id = @request.auth.id",
"options": {
"allowEmailAuth": true,
"allowOAuth2Auth": true,
"allowUsernameAuth": true,
"exceptEmailDomains": null,
"manageRule": null,
"minPasswordLength": 8,
"onlyEmailDomains": null,
"requireEmail": false
}
},
{
"id": "cfq9mqlmd97v8z5",
"name": "groups",
"type": "base",
"system": false,
"schema": [
{
"id": "85msl21p",
"name": "university",
"type": "text",
"system": false,
"required": false,
"options": {
"min": null,
"max": null,
"pattern": ""
}
},
{
"id": "2sii4dtp",
"name": "shortcut",
"type": "text",
"system": false,
"required": false,
"options": {
"min": null,
"max": null,
"pattern": ""
}
},
{
"id": "uiwgo28f",
"name": "groupId",
"type": "text",
"system": false,
"required": false,
"options": {
"min": null,
"max": null,
"pattern": ""
}
},
{
"id": "y0l1lrzs",
"name": "course",
"type": "text",
"system": false,
"required": false,
"options": {
"min": null,
"max": null,
"pattern": ""
}
},
{
"id": "kr62mhbz",
"name": "faculty",
"type": "text",
"system": false,
"required": false,
"options": {
"min": null,
"max": null,
"pattern": ""
}
},
{
"id": "ya6znpez",
"name": "facultyId",
"type": "text",
"system": false,
"required": false,
"options": {
"min": null,
"max": null,
"pattern": ""
}
}
],
"indexes": [
"CREATE UNIQUE INDEX `idx_rcaN2Oq` ON `groups` (`course`)"
],
"listRule": null,
"viewRule": null,
"createRule": null,
"updateRule": null,
"deleteRule": null,
"options": {}
},
{
"id": "d65h4wh7zk13gxp",
"name": "feeds",
"type": "base",
"system": false,
"schema": [
{
"id": "cowxjfmc",
"name": "modules",
"type": "json",
"system": false,
"required": true,
"options": {}
}
],
"indexes": [],
"listRule": null,
"viewRule": null,
"createRule": null,
"updateRule": null,
"deleteRule": null,
"options": {}
},
{
"id": "7her4515qsmrxe8",
"name": "events",

View File

@@ -12,16 +12,10 @@ import (
"io"
"net/http"
"net/url"
"os"
)
func AddRoutes(app *pocketbase.PocketBase) {
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
e.Router.GET("/*", apis.StaticDirectoryHandler(os.DirFS("./pb_public"), false))
return nil
})
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
_, err := e.Router.AddRoute(echo.Route{
Method: http.MethodGet,
@@ -243,4 +237,27 @@ func AddRoutes(app *pocketbase.PocketBase) {
}
return nil
})
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
_, err := e.Router.AddRoute(echo.Route{
Method: http.MethodGet,
Path: "/api/feed/migrate",
Handler: func(c echo.Context) error {
err := ical.MigrateFeedJson(app)
if err != nil {
return c.JSON(500, err)
} else {
return c.JSON(200, "Migrated")
}
},
Middlewares: []echo.MiddlewareFunc{
apis.ActivityLogger(app),
},
})
if err != nil {
return err
}
return nil
})
}

View File

@@ -53,6 +53,7 @@ func GetModuleByName(app *pocketbase.PocketBase, name string) (model.Module, err
return model.Module{}, err
} else {
return model.Module{
UUID: events[0].UUID,
Name: name,
Events: events,
Prof: events[0].Prof,

View File

@@ -16,8 +16,6 @@ import (
const expirationTime = 5 * time.Minute
func Feed(c echo.Context, app *pocketbase.PocketBase, token string) error {
layout := "2006-01-02 15:04:05 -0700 MST"
var result string
var responseWriter = c.Response().Writer
feed, err := db.FindFeedByToken(token, app)
@@ -25,11 +23,11 @@ func Feed(c echo.Context, app *pocketbase.PocketBase, token string) error {
return c.JSON(http.StatusNotFound, err)
}
created, _ := time.Parse(layout, feed.Created)
created := feed.Created
var modules []model.FeedCollection
_ = json.Unmarshal([]byte(feed.Modules), &modules)
if created.Add(time.Hour * 265).Before(time.Now()) {
if created.Time().Add(time.Hour * 265).Before(time.Now()) {
newFeed, err := createFeedForToken(app, modules)
if err != nil {
return c.JSON(http.StatusInternalServerError, err)

View File

@@ -0,0 +1,58 @@
package ical
import (
"encoding/json"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase"
"htwkalender/model"
)
//update ical feed json
//add uuid field
//remove module name field
func MigrateFeedJson(app *pocketbase.PocketBase) error {
records, err := app.Dao().FindRecordsByFilter("feeds", "1=1", "-created", 0, 0)
if err != nil {
return err
}
for _, feed := range records {
var modules []model.FeedCollection
err := json.Unmarshal([]byte(feed.GetString("modules")), &modules)
if err != nil {
return err
}
var uuidFeedCollections []model.FeedCollection
for _, module := range modules {
uuid := searchUUIDForModule(app, module.Name)
if uuid != "" {
uuidFeedCollections = append(uuidFeedCollections, model.FeedCollection{UUID: uuid, Name: module.Name, Course: module.Course, UserDefinedName: module.UserDefinedName})
}
}
jsonModules, _ := json.Marshal(uuidFeedCollections)
feed.Set("modules", string(jsonModules))
err = app.Dao().SaveRecord(feed)
if err != nil {
return err
}
}
return nil
}
func searchUUIDForModule(app *pocketbase.PocketBase, moduleName string) string {
var event model.Event
err := app.Dao().DB().Select("*").From("events").Where(dbx.NewExp("Name = {:name}", dbx.Params{"name": moduleName})).One(&event)
if err != nil {
return ""
}
return event.UUID
}

View File

@@ -27,6 +27,7 @@ export async function fetchModulesByCourseAndSemester(
modulesResponse.forEach((module: Module) =>
modules.push(
new Module(
module.uuid,
module.name,
course,
module.name,
@@ -50,6 +51,7 @@ export async function fetchAllModules(): Promise<Module[]> {
responseModules.forEach((module: Module) => {
modules.push(
new Module(
module.uuid,
module.name,
module.course,
module.name,

View File

@@ -16,6 +16,7 @@ export async function fetchModule(name: string): Promise<Module> {
.then(
(module: Module) =>
new Module(
module.uuid,
module.name,
module.course,
module.name,

View File

@@ -2,6 +2,7 @@ import { Event } from "./event";
export class Module {
constructor(
public uuid: string,
public name: string,
public course: string,
public userDefinedName: string,