Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update open content, library, and video fields in tables/queries/frontend #588

Merged
merged 9 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions .air.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ root = "."
tmp_dir = "bin"

[build]
cmd = "go build -o ./bin/main backend/cmd/main.go"
bin = "bin/main"
include_dir = ["backend"]
cmd = "go build -o ./bin/main /app/backend/cmd/main.go"
bin = "./bin/main"
include_dir = ["./backend"]
include_file = []
exclude_file = []
exclude_regex = ["_test\\.go"]
exclude_ext = [".tsx", ".ts", ".json"]
exclude_regex = ["_test\\.go", "frontend/.*", "provider-middleware/.*"]
exclude_unchanged = true
# Follow symlink for directories
follow_symlink = true
Expand Down
10 changes: 5 additions & 5 deletions .middleware.air.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
root = "."
tmp_dir = "tmp"
tmp_dir = "bin"

[build]
cmd = "go build -o ./bin/provider-middleware ./provider-middleware/."
bin = "bin/provider-middleware"
include_dir = ["backend", "provider-middleware"]
cmd = "go build -o ./bin/provider-middleware /app/provider-middleware/."
bin = "./bin/provider-middleware"
include_dir = ["./backend", "./provider-middleware"]
include_file = []
exclude_dir = ["config", "backend/tasks"]
exclude_dir = ["./config", "./backend/tasks"]
exclude_regex = ["_test\\.go"]
exclude_unchanged = true

Expand Down
8 changes: 4 additions & 4 deletions .tasks.air.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
root = "."
tmp_dir = "tmp"
tmp_dir = "bin"

[build]
cmd = "go build -o ./bin/cron_tasks ./backend/tasks/."
cmd = "go build -o ./bin/cron_tasks /app/backend/tasks/."
bin = "bin/cron_tasks"
include_dir = ["backend"]
include_dir = ["backend", "backend/tasks"]
include_file = []
exclude_dir = ["frontend", "provider-middleware"]
exclude_regex = ["_test\\.go"]
exclude_regex = ["_test\\.go", "frontend/.*", "provider-middleware/.*"]
exclude_unchanged = true

log = "logs/tasks.air.log"
Expand Down
2 changes: 1 addition & 1 deletion backend/dev.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM golang:1.23-alpine

WORKDIR /app

RUN go install github.com/air-verse/air@latest
RUN go install github.com/air-verse/air@v1.61.0

COPY backend/go.mod backend/go.sum ./
RUN go mod download
Expand Down
31 changes: 31 additions & 0 deletions backend/migrations/00027_alter_open_content_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
-- +goose Up
-- +goose StatementBegin
ALTER TABLE public.open_content_providers
RENAME COLUMN name TO title;
ALTER TABLE public.open_content_providers
RENAME COLUMN base_url TO url;
ALTER TABLE public.open_content_providers
RENAME COLUMN thumbnail TO thumbnail_url;
ALTER TABLE public.libraries
RENAME COLUMN name TO title;
ALTER TABLE public.libraries
RENAME COLUMN path TO url;
ALTER TABLE public.videos
RENAME COLUMN youtube_id TO external_id;
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
ALTER TABLE public.open_content_providers
RENAME COLUMN title TO name;
ALTER TABLE public.open_content_providers
RENAME COLUMN url TO base_url;
ALTER TABLE public.open_content_providers
RENAME COLUMN thumbnail_url TO thumbnail;
ALTER TABLE public.libraries
RENAME COLUMN title TO name;
ALTER TABLE public.libraries
RENAME COLUMN url TO path;
ALTER TABLE public.videos
RENAME COLUMN external_id TO youtube_id;
-- +goose StatementEnd
22 changes: 11 additions & 11 deletions backend/seeder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,49 +91,49 @@ func seedTestData(db *gorm.DB) {
}
}
kiwix := models.OpenContentProvider{
BaseUrl: "https://library.kiwix.org",
Name: models.Kiwix,
Thumbnail: "https://images.fineartamerica.com/images/artworkimages/mediumlarge/3/llamas-wearing-party-hats-in-a-circle-looking-down-john-daniels.jpg",
Url: "https://library.kiwix.org",
Title: models.Kiwix,
ThumbnailUrl: "https://images.fineartamerica.com/images/artworkimages/mediumlarge/3/llamas-wearing-party-hats-in-a-circle-looking-down-john-daniels.jpg",
CurrentlyEnabled: true,
Description: "Kiwix open content provider",
}
log.Printf("Creating Open Content Provider %s", kiwix.BaseUrl)
log.Printf("Creating Open Content Provider %s", kiwix.Url)
if err := db.Create(&kiwix).Error; err != nil {
log.Printf("Failed to create open content provider: %v", err)
}
kiwixLibraries := []models.Library{
{
OpenContentProviderID: kiwix.ID,
ExternalID: models.StringPtr("urn:uuid:67440563-a62b-fabe-415c-4c3ee4546f78"),
Name: "TED ted connects",
Title: "TED ted connects",
Language: models.StringPtr("eng,spa,ara"),
Description: models.StringPtr("A collection of TED videos about ted connects"),
Path: "/content/ted_mul_ted-connects_2024-08",
Url: "/content/ted_mul_ted-connects_2024-08",
ThumbnailUrl: models.StringPtr("/catalog/v2/illustration/67440563-a62b-fabe-415c-4c3ee4546f78/?size=48"),
VisibilityStatus: true,
},
{
OpenContentProviderID: kiwix.ID,
ExternalID: models.StringPtr("urn:uuid:84812c13-fa65-feb7-c206-4f22cc2e0f9a"),
Name: "Python Documentation",
Title: "Python Documentation",
Language: models.StringPtr("eng"),
Description: models.StringPtr("All documentation for Python"),
Path: "/content/docs.python.org_en_2024-09",
Url: "/content/docs.python.org_en_2024-09",
ThumbnailUrl: models.StringPtr("/catalog/v2/illustration/84812c13-fa65-feb7-c206-4f22cc2e0f9a/?size=48"),
VisibilityStatus: true,
},
{
OpenContentProviderID: kiwix.ID,
ExternalID: models.StringPtr("urn:uuid:19e6fe12-09a9-0a38-5be4-71c0eba0a72d"),
Name: "Finiki",
Title: "Finiki",
Language: models.StringPtr("eng"),
Description: models.StringPtr("The Canadian financial wiki"),
Path: "/content/finiki_en_all_maxi_2024-06",
Url: "/content/finiki_en_all_maxi_2024-06",
ThumbnailUrl: models.StringPtr("/catalog/v2/illustration/19e6fe12-09a9-0a38-5be4-71c0eba0a72d/?size=48"),
VisibilityStatus: true,
}}
for idx := range kiwixLibraries {
log.Printf("Creating library %s", kiwixLibraries[idx].Name)
log.Printf("Creating library %s", kiwixLibraries[idx].Title)
if err := db.Create(&kiwixLibraries[idx]).Error; err != nil {
log.Printf("Failed to create library: %v", err)
}
Expand Down
12 changes: 6 additions & 6 deletions backend/src/database/DB.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ func (db *DB) SeedDefaultData(isTesting bool) {
if err := db.Create(&links).Error; err != nil {
log.Fatalf("Failed to create left menu links: %v", err)
}
openContent := []models.OpenContentProvider{{Name: models.Kiwix, BaseUrl: models.KiwixLibraryUrl, CurrentlyEnabled: true, Thumbnail: models.KiwixThumbnailURL, Description: models.Kiwix},
{Name: models.Youtube, BaseUrl: models.YoutubeApi, CurrentlyEnabled: true, Thumbnail: models.YoutubeThumbnail, Description: models.YoutubeDescription}}
openContent := []models.OpenContentProvider{{Title: models.Kiwix, Url: models.KiwixLibraryUrl, CurrentlyEnabled: true, ThumbnailUrl: models.KiwixThumbnailURL, Description: models.Kiwix},
calisio marked this conversation as resolved.
Show resolved Hide resolved
{Title: models.Youtube, Url: models.YoutubeApi, CurrentlyEnabled: true, ThumbnailUrl: models.YoutubeThumbnail, Description: models.YoutubeDescription}}
for idx := range openContent {
if err := db.Create(&openContent[idx]).Error; err != nil {
log.Fatalf("Failed to create kiwix open content provider: %v", err)
Expand Down Expand Up @@ -323,8 +323,8 @@ func (db *DB) SeedTestData() {
log.Fatalf("Failed to get users from db")
return
}
openContent := []models.OpenContentProvider{{Name: models.Kiwix, BaseUrl: models.KiwixLibraryUrl, CurrentlyEnabled: true, Thumbnail: models.KiwixThumbnailURL, Description: models.Kiwix},
{Name: models.Youtube, BaseUrl: models.YoutubeApi, CurrentlyEnabled: true, Thumbnail: models.YoutubeThumbnail, Description: models.YoutubeDescription}}
openContent := []models.OpenContentProvider{{Title: models.Kiwix, Url: models.KiwixLibraryUrl, CurrentlyEnabled: true, ThumbnailUrl: models.KiwixThumbnailURL, Description: models.Kiwix},
{Title: models.Youtube, Url: models.YoutubeApi, CurrentlyEnabled: true, ThumbnailUrl: models.YoutubeThumbnail, Description: models.YoutubeDescription}}
for idx := range openContent {
if err := db.Create(&openContent[idx]).Error; err != nil {
log.Fatalf("Failed to create kiwix open content provider: %v", err)
Expand All @@ -347,11 +347,11 @@ func (db *DB) SeedTestData() {
log.Fatalf("Failed to unmarshal test data: %v", err)
}
var kwixID uint //get id for kwix
if db.Model(&models.OpenContentProvider{}).Select("id").Where("name = ?", models.Kiwix).First(&kwixID).RowsAffected == 0 {
if db.Model(&models.OpenContentProvider{}).Select("id").Where("title = ?", models.Kiwix).First(&kwixID).RowsAffected == 0 {
log.Fatalf("Failed to get %s open_content_provider: %v", models.Kiwix, err)
}
var youtubeID uint
if db.Model(&models.OpenContentProvider{}).Select("id").Where("name = ?", models.Youtube).First(&youtubeID).RowsAffected == 0 {
if db.Model(&models.OpenContentProvider{}).Select("id").Where("title = ?", models.Youtube).First(&youtubeID).RowsAffected == 0 {
log.Fatalf("Failed to get %s open_content_provider: %v", models.Kiwix, err)
}
var url models.OpenContentUrl
Expand Down
2 changes: 1 addition & 1 deletion backend/src/database/helpful_links.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (db *DB) GetLinkFromId(id uint) (*models.HelpfulLink, error) {

func (db *DB) GetHelpfulLinkOpenContentProviderId() (uint, error) {
var provider models.OpenContentProvider
if err := db.Model(&models.OpenContentProvider{}).Where("name = ?", "HelpfulLinks").First(&provider).Error; err != nil {
if err := db.Model(&models.OpenContentProvider{}).Where("title = ?", "HelpfulLinks").First(&provider).Error; err != nil {
return 0, newGetRecordsDBError(err, "open_content_providers")
}
return provider.ID, nil
Expand Down
2 changes: 1 addition & 1 deletion backend/src/database/libraries.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (db *DB) GetAllLibraries(page, perPage int, userId, facilityId uint, visibi

if search != "" {
search = "%" + strings.ToLower(search) + "%"
tx = tx.Where("LOWER(name) LIKE ? OR LOWER(description) LIKE ?", search, search)
tx = tx.Where("LOWER(title) LIKE ? OR LOWER(description) LIKE ?", search, search)
}

if err := tx.Count(&total).Error; err != nil {
Expand Down
52 changes: 26 additions & 26 deletions backend/src/database/open_content.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (db *DB) ToggleLibraryFavorite(userId uint, facilityId *uint, libId int) er
return nil
}

func (db *DB) GetUserFavorites(userID uint, page, perPage int) (int64, []models.OpenContentFavorite, error) {
func (db *DB) GetUserFavorites(userID uint, page, perPage int) (int64, []models.OpenContentItem, error) {
var total int64
countQuery := `
SELECT COUNT(*) FROM (
Expand All @@ -88,13 +88,13 @@ func (db *DB) GetUserFavorites(userID uint, page, perPage int) (int64, []models.
return 0, nil, err
}

favorites := make([]models.OpenContentFavorite, 0, perPage)
favorites := make([]models.OpenContentItem, 0, perPage)
favoritesQuery := `
SELECT
id,
content_type,
content_id,
name,
title,
url,
thumbnail_url,
description,
visibility_status,
Expand All @@ -104,17 +104,17 @@ func (db *DB) GetUserFavorites(userID uint, page, perPage int) (int64, []models.
created_at
FROM (
SELECT
fav.id AS id,
'library' AS content_type,
fav.library_id AS content_id,
lib.name AS name,
lib.thumbnail_url AS thumbnail_url,
ocp.description AS description,
NOT lib.visibility_status AS visibility_status,
lib.open_content_provider_id AS open_content_provider_id,
ocp.name AS provider_name,
lib.title,
lib.url,
lib.thumbnail_url,
ocp.description,
lib.visibility_status,
lib.open_content_provider_id,
ocp.title AS provider_name,
NULL AS channel_title,
fav.created_at AS created_at
fav.created_at
FROM library_favorites fav
JOIN libraries lib ON lib.id = fav.library_id
JOIN open_content_providers ocp ON ocp.id = lib.open_content_provider_id
Expand All @@ -125,17 +125,17 @@ func (db *DB) GetUserFavorites(userID uint, page, perPage int) (int64, []models.
UNION ALL

SELECT
vf.id AS id,
'video' AS content_type,
vf.video_id AS content_id,
videos.title AS name,
videos.thumbnail_url AS thumbnail_url,
videos.description AS description,
NOT videos.visibility_status AS visibility_status,
videos.open_content_provider_id AS open_content_provider_id,
videos.title,
videos.url,
videos.thumbnail_url,
videos.description,
videos.visibility_status,
videos.open_content_provider_id,
NULL AS provider_name,
videos.channel_title AS channel_title,
vf.created_at AS created_at
videos.channel_title,
vf.created_at
FROM video_favorites vf
JOIN videos ON vf.video_id = videos.id
JOIN open_content_providers ocp ON ocp.id = videos.open_content_provider_id
Expand All @@ -156,17 +156,17 @@ func (db *DB) GetUserFavorites(userID uint, page, perPage int) (int64, []models.
func (db *DB) GetTopFacilityOpenContent(id int) ([]models.OpenContentItem, error) {
var content []models.OpenContentItem
if err := db.Raw("? UNION ? ORDER BY visits DESC LIMIT 5",
db.Select("v.title as name, v.url, v.thumbnail_url, v.open_content_provider_id, v.id as content_id, 'video' as type, count(v.id) as visits").
db.Select("v.title, v.url, v.thumbnail_url, v.open_content_provider_id, v.id as content_id, 'video' as content_type, count(v.id) as visits").
Table("open_content_activities oca").
Joins("LEFT JOIN videos v ON v.id = oca.content_id AND v.open_content_provider_id = oca.open_content_provider_id AND v.visibility_status = TRUE").
Where("oca.facility_id = ?", id).
Group("v.title, v.url, v.thumbnail_url, v.open_content_provider_id, v.id").
Having("count(v.id) > 0"),
db.Select("l.name, l.path as url, l.thumbnail_url as thumbnail_url, l.open_content_provider_id, l.id as content_id, 'library' as type, count(l.id) as visits").
db.Select("l.title, l.url, l.thumbnail_url, l.open_content_provider_id, l.id as content_id, 'library' as content_type, count(l.id) as visits").
Table("open_content_activities oca").
Joins("LEFT JOIN libraries l on l.id = oca.content_id AND l.open_content_provider_id = oca.open_content_provider_id AND l.visibility_status = TRUE").
Where("oca.facility_id = ?", id).
Group("l.name, l.path, l.thumbnail_url, l.open_content_provider_id, l.id").
Group("l.title, l.url, l.thumbnail_url, l.open_content_provider_id, l.id").
Having("count(l.id) > 0"),
).Find(&content).Error; err != nil {
return nil, newGetRecordsDBError(err, "open_content_items")
Expand All @@ -178,17 +178,17 @@ func (db *DB) GetTopUserOpenContent(id int) ([]models.OpenContentItem, error) {
var content []models.OpenContentItem
log.Println(id)
if err := db.Raw("? UNION ? ORDER BY visits DESC LIMIT 5",
db.Select("v.title as name, v.url, v.thumbnail_url, v.open_content_provider_id, v.id as content_id, 'video' as type, count(v.id) as visits").
db.Select("v.title, v.url, v.thumbnail_url, v.open_content_provider_id, v.id as content_id, 'video' as content_type, count(v.id) as visits").
Table("open_content_activities oca").
Joins("LEFT JOIN videos v ON v.id = oca.content_id AND v.open_content_provider_id = oca.open_content_provider_id AND v.visibility_status = TRUE").
Where("oca.user_id = ?", id).
Group("v.title, v.url, v.thumbnail_url, v.open_content_provider_id, v.id").
Having("count(v.id) > 0"),
db.Select("l.name, l.path as url, l.thumbnail_url as thumbnail_url, l.open_content_provider_id, l.id as content_id, 'library' as type, count(l.id) as visits").
db.Select("l.title, l.url, l.thumbnail_url, l.open_content_provider_id, l.id as content_id, 'library' as content_type, count(l.id) as visits").
Table("open_content_activities oca").
Joins("LEFT JOIN libraries l on l.id = oca.content_id AND l.open_content_provider_id = oca.open_content_provider_id AND l.visibility_status = TRUE").
Where("oca.user_id = ?", id).
Group("l.name, l.path, l.thumbnail_url, l.open_content_provider_id, l.id").
Group("l.title, l.url, l.thumbnail_url, l.open_content_provider_id, l.id").
Having("count(l.id) > 0"),
).Find(&content).Error; err != nil {
return nil, newGetRecordsDBError(err, "open_content_items")
Expand Down
2 changes: 1 addition & 1 deletion backend/src/database/videos.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func (db *DB) GetVideoByID(id int) (*models.Video, error) {

func (db *DB) GetVideoProvider() (*models.OpenContentProvider, error) {
var provider models.OpenContentProvider
if err := db.First(&provider, "name = ?", models.Youtube).Error; err != nil {
if err := db.First(&provider, "title = ?", models.Youtube).Error; err != nil {
return nil, newNotFoundDBError(err, "open_content_providers")
}
return &provider, nil
Expand Down
2 changes: 1 addition & 1 deletion backend/src/handlers/proxy_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (srv *Server) handleForwardKiwixProxy(w http.ResponseWriter, r *http.Reques
}

func (srv *Server) handleRedirectVideosLocal(w http.ResponseWriter, r *http.Request, video *models.Video) {
http.Redirect(w, r, fmt.Sprintf("/videos/%s.mp4", video.YoutubeID), http.StatusTemporaryRedirect)
http.Redirect(w, r, fmt.Sprintf("/videos/%s.mp4", video.ExternalID), http.StatusTemporaryRedirect)
}

func (srv *Server) handleRedirectVideosS3(w http.ResponseWriter, r *http.Request) {
Expand Down
Loading
Loading