1
0

List Backgrounds (#568)

Return the updated list when setting a list background

Add swagger docs for unsplash methods

Add unsplash info to search results

Fix misspell

Fix lint

Add rights check for setting and getting backgrounds

Show unsplash information when loading a single list

Make application id for pingbacks configurable

Remove old backgrounds when setting a new one

Return 404 if the list does not have a background

Implement getting list backgrounds

Implement actually setting a photo from unsplash as list background

go mod tidy

Add migration for background file id

Roughly implement setting a list background from unsplash

Implement saving a background

Add migration for unsplash photo table

Add unsplash search

Fix parsing page param

Fix parsing page param

Fix background config

Add unsplash wrapper library

Add enabled background providers to info endpoint

Add config options for backgrounds

Add unsplash background provider

Add routing handler for backgrounds

Add basic background provider interface

Co-authored-by: kolaente <k@knt.li>
Reviewed-on: https://kolaente.dev/vikunja/api/pulls/568
This commit is contained in:
konrad
2020-05-26 20:07:55 +00:00
parent c37b776f7a
commit e5e30d0915
18 changed files with 720 additions and 57 deletions

View File

@ -112,6 +112,11 @@ const (
AvatarProvider Key = `avatar.provider`
AvatarGravaterExpiration Key = `avatar.gravatarexpiration`
BackgroundsEnabled Key = `backgrounds.enabled`
BackgroundsUnsplashEnabled Key = `backgrounds.providers.unsplash.enabled`
BackgroundsUnsplashAccessToken Key = `backgrounds.providers.unsplash.accesstoken`
BackgroundsUnsplashApplicationID Key = `backgrounds.providers.unsplash.applicationid`
)
// GetString returns a string config value
@ -243,6 +248,9 @@ func InitDefaultConfig() {
// Avatar
AvatarProvider.setDefault("gravatar")
AvatarGravaterExpiration.setDefault(3600)
// List Backgrounds
BackgroundsEnabled.setDefault(false)
BackgroundsUnsplashEnabled.setDefault(false)
}
// InitConfig initializes the config, sets defaults etc.

View File

@ -50,3 +50,20 @@ func IsErrFileIsTooLarge(err error) bool {
_, ok := err.(ErrFileIsTooLarge)
return ok
}
// ErrFileIsNotUnsplashFile defines an error where a file is not downloaded from unsplash.
// Used in cases whenever unsplash information about a file is requested, but the file was not downloaded from unsplash.
type ErrFileIsNotUnsplashFile struct {
FileID int64
}
// Error is the error implementation of ErrFileIsNotUnsplashFile
func (err ErrFileIsNotUnsplashFile) Error() string {
return fmt.Sprintf("file was not downloaded from unsplash [FileID: %d]", err.FileID)
}
//IsErrFileIsNotUnsplashFile checks if an error is ErrFileIsNotUnsplashFile
func IsErrFileIsNotUnsplashFile(err error) bool {
_, ok := err.(ErrFileIsNotUnsplashFile)
return ok
}

View File

@ -0,0 +1,47 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package migration
import (
"src.techknowlogick.com/xormigrate"
"xorm.io/xorm"
)
type unsplashPhoto20200524221534 struct {
ID int64 `xorm:"autoincr not null unique pk"`
FileID int64 `xorm:"not null"`
UnsplashID string `xorm:"varchar(50)"`
Author string `xorm:"text"`
AuthorName string `xorm:"text"`
}
func (u unsplashPhoto20200524221534) TableName() string {
return "unsplash_photos"
}
func init() {
migrations = append(migrations, &xormigrate.Migration{
ID: "20200524221534",
Description: "Create unsplash photo table",
Migrate: func(tx *xorm.Engine) error {
return tx.Sync2(unsplashPhoto20200524221534{})
},
Rollback: func(tx *xorm.Engine) error {
return tx.DropTables(unsplashPhoto20200524221534{})
},
})
}

View File

@ -0,0 +1,43 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package migration
import (
"src.techknowlogick.com/xormigrate"
"xorm.io/xorm"
)
type list20200524224611 struct {
BackgroundFileID int64 `xorm:"null" json:"-"`
}
func (s list20200524224611) TableName() string {
return "list"
}
func init() {
migrations = append(migrations, &xormigrate.Migration{
ID: "20200524224611",
Description: "Add background file id property to list",
Migrate: func(tx *xorm.Engine) error {
return tx.Sync2(list20200524224611{})
},
Rollback: func(tx *xorm.Engine) error {
return tx.DropTables(list20200524224611{})
},
})
}

View File

@ -17,6 +17,7 @@
package models
import (
"code.vikunja.io/api/pkg/files"
"code.vikunja.io/api/pkg/metrics"
"code.vikunja.io/api/pkg/timeutil"
"code.vikunja.io/api/pkg/user"
@ -51,6 +52,11 @@ type List struct {
// Whether or not a list is archived.
IsArchived bool `xorm:"not null default false" json:"is_archived" query:"is_archived"`
// The id of the file this list has set as background
BackgroundFileID int64 `xorm:"null" json:"-"`
// Holds extra information about the background set since some background providers require attribution or similar. If not null, the background can be accessed at /lists/{listID}/background
BackgroundInformation interface{} `xorm:"-" json:"background_information"`
// A timestamp when this list was created. You cannot change this value.
Created timeutil.TimeStamp `xorm:"created not null" json:"created"`
// A timestamp when this list was last updated. You cannot change this value.
@ -166,6 +172,16 @@ func (l *List) ReadOne() (err error) {
l.IsArchived = true
}
}
// Get any background information if there is one set
if l.BackgroundFileID != 0 {
// Currently unsplash only
l.BackgroundInformation, err = GetUnsplashPhotoByFileID(l.BackgroundFileID)
if err != nil && !files.IsErrFileIsNotUnsplashFile(err) {
return
}
}
return nil
}
@ -560,3 +576,16 @@ func (l *List) Delete() (err error) {
_, err = x.Where("list_id = ?", l.ID).Delete(&Task{})
return
}
// SetListBackground sets a background file as list background in the db
func SetListBackground(listID int64, background *files.File) (err error) {
l := &List{
ID: listID,
BackgroundFileID: background.ID,
}
_, err = x.
Where("id = ?", l.ID).
Cols("background_file_id").
Update(l)
return
}

64
pkg/models/unsplash.go Normal file
View File

@ -0,0 +1,64 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package models
import "code.vikunja.io/api/pkg/files"
// Unsplash requires us to do pingbacks to their site and also name the image author.
// To do this properly, we need to save these information somewhere.
// UnsplashPhoto is an unsplash photo in the db
type UnsplashPhoto struct {
ID int64 `xorm:"autoincr not null unique pk" json:"id,omitempty"`
FileID int64 `xorm:"not null" json:"-"`
UnsplashID string `xorm:"varchar(50)" json:"unsplash_id"`
Author string `xorm:"text" json:"author"`
AuthorName string `xorm:"text" json:"author_name"`
}
// TableName contains the table name for an unsplash photo
func (u *UnsplashPhoto) TableName() string {
return "unsplash_photos"
}
// Save persists an unsplash photo to the db
func (u *UnsplashPhoto) Save() error {
_, err := x.Insert(u)
return err
}
// GetUnsplashPhotoByFileID returns an unsplash photo by its saved file id
func GetUnsplashPhotoByFileID(fileID int64) (u *UnsplashPhoto, err error) {
u = &UnsplashPhoto{}
exists, err := x.Where("file_id = ?", fileID).Get(u)
if err != nil {
return
}
if !exists {
return nil, files.ErrFileIsNotUnsplashFile{FileID: fileID}
}
return
}
// RemoveUnsplashPhoto removes an unsplash photo from the db
func RemoveUnsplashPhoto(fileID int64) (err error) {
// This is intentionally "fire and forget" which is why we don't check if we have an
// unsplash entry for that file at all. If there is one, it will be deleted.
// We do this to keep the function simple.
_, err = x.Where("file_id = ?", fileID).Delete(&UnsplashPhoto{})
return
}

View File

@ -0,0 +1,39 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package background
import (
"code.vikunja.io/api/pkg/models"
"code.vikunja.io/web"
)
// Image represents an image which can be used as a list background
type Image struct {
ID string `json:"id"`
URL string `json:"url"`
Thumb string `json:"thumb"`
// This can be used to supply extra information from an image provider to clients
Info interface{} `json:"info,omitempty"`
}
// Provider represents something that is able to get a list of images and set one of them as background
type Provider interface {
// Search is used to either return a pre-defined list of Image or let the user search for an image
Search(search string, page int64) (result []*Image, err error)
// Set sets an image which was most likely previously obtained by Search as list background
Set(image *Image, list *models.List, auth web.Auth) (err error)
}

View File

@ -0,0 +1,157 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package handler
import (
"code.vikunja.io/api/pkg/files"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/models"
"code.vikunja.io/api/pkg/modules/background"
"code.vikunja.io/api/pkg/modules/background/unsplash"
v1 "code.vikunja.io/api/pkg/routes/api/v1"
"code.vikunja.io/web/handler"
"github.com/labstack/echo/v4"
"net/http"
"strconv"
)
// BackgroundProvider represents a thing which holds a background provider
// Lets us get a new fresh provider every time we need one.
type BackgroundProvider struct {
Provider func() background.Provider
}
// SearchBackgrounds is the web handler to search for backgrounds
func (bp *BackgroundProvider) SearchBackgrounds(c echo.Context) error {
p := bp.Provider()
err := c.Bind(p)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "No or invalid model provided: "+err.Error())
}
search := c.QueryParam("s")
var page int64 = 1
pg := c.QueryParam("p")
if pg != "" {
page, err = strconv.ParseInt(pg, 10, 64)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid page number: "+err.Error())
}
}
result, err := p.Search(search, page)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "An error occurred: "+err.Error())
}
return c.JSON(http.StatusOK, result)
}
// SetBackground sets an Image as list background
func (bp *BackgroundProvider) SetBackground(c echo.Context) error {
auth, err := v1.GetAuthFromClaims(c)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid auth token: "+err.Error())
}
p := bp.Provider()
listID, err := strconv.ParseInt(c.Param("list"), 10, 64)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid list ID: "+err.Error())
}
// Check if the user has the right to change the list background
list := &models.List{ID: listID}
can, err := list.CanUpdate(auth)
if err != nil {
return handler.HandleHTTPError(err, c)
}
if !can {
log.Infof("Tried to update list background of list %d while not having the rights for it (User: %v)", listID, auth)
return echo.NewHTTPError(http.StatusForbidden)
}
image := &background.Image{}
err = c.Bind(image)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "No or invalid model provided: "+err.Error())
}
err = p.Set(image, list, auth)
if err != nil {
return handler.HandleHTTPError(err, c)
}
return c.JSON(http.StatusOK, list)
}
// GetListBackground serves a previously set background from a list
// It has no knowledge of the provider that was responsible for setting the background.
// @Summary Get the list background
// @Description Get the list background of a specific list. **Returns json on error.**
// @tags list
// @Produce octet-stream
// @Param id path int true "List ID"
// @Security JWTKeyAuth
// @Success 200 {} string "The list background file."
// @Failure 403 {object} models.Message "No access to this list."
// @Failure 404 {object} models.Message "The list does not exist."
// @Failure 500 {object} models.Message "Internal error"
// @Router /lists/{id}/background [get]
func GetListBackground(c echo.Context) error {
auth, err := v1.GetAuthFromClaims(c)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid auth token: "+err.Error())
}
listID, err := strconv.ParseInt(c.Param("list"), 10, 64)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid list ID: "+err.Error())
}
// Check if a background for this list exists + Rights
list := &models.List{ID: listID}
can, err := list.CanRead(auth)
if err != nil {
return handler.HandleHTTPError(err, c)
}
if !can {
log.Infof("Tried to get list background of list %d while not having the rights for it (User: %v)", listID, auth)
return echo.NewHTTPError(http.StatusForbidden)
}
if list.BackgroundFileID == 0 {
return echo.NotFoundHandler(c)
}
// Get the file
bgFile := &files.File{
ID: list.BackgroundFileID,
}
if err := bgFile.LoadFileByID(); err != nil {
return handler.HandleHTTPError(err, c)
}
// Unsplash requires pingbacks as per their api usage guidelines.
// To do this in a privacy-preserving manner, we do the ping from inside of Vikunja to not expose any user details.
// FIXME: This should use an event once we have events
unsplash.Pingback(bgFile)
// Serve the file
return c.Stream(http.StatusOK, "image/jpg", bgFile.File)
}

View File

@ -0,0 +1,241 @@
// Vikunja is a to-do list application to facilitate your life.
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package unsplash
import (
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/files"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/models"
"code.vikunja.io/api/pkg/modules/background"
"code.vikunja.io/web"
"encoding/json"
"net/http"
"strconv"
)
// Provider represents an unsplash image provider
type Provider struct {
}
// SearchResult is a search result from unsplash's api
type SearchResult struct {
Total int `json:"total"`
TotalPages int `json:"total_pages"`
Results []*Photo `json:"results"`
}
// Photo represents an unpslash photo as returned by their api
type Photo struct {
ID string `json:"id"`
CreatedAt string `json:"created_at"`
Width int `json:"width"`
Height int `json:"height"`
Color string `json:"color"`
Description string `json:"description"`
User struct {
Username string `json:"username"`
Name string `json:"name"`
} `json:"user"`
Urls struct {
Raw string `json:"raw"`
Full string `json:"full"`
Regular string `json:"regular"`
Small string `json:"small"`
Thumb string `json:"thumb"`
} `json:"urls"`
Links struct {
Self string `json:"self"`
HTML string `json:"html"`
Download string `json:"download"`
} `json:"links"`
}
// Very simple caching method - pretty much only used to retain information when saving an image
// FIXME: Should use a proper cache
var photos map[string]*Photo
func init() {
photos = make(map[string]*Photo)
}
func doGet(url string, result interface{}) (err error) {
req, err := http.NewRequest("GET", "https://api.unsplash.com/"+url, nil)
if err != nil {
return
}
req.Header.Add("Authorization", "Client-ID "+config.BackgroundsUnsplashAccessToken.GetString())
hc := http.Client{}
resp, err := hc.Do(req)
if err != nil {
return
}
err = json.NewDecoder(resp.Body).Decode(result)
return
}
// Search is the implementation to search on unsplash
// @Summary Search for a background from unsplash
// @Description Search for a list background from unsplash
// @tags list
// @Produce json
// @Security JWTKeyAuth
// @Param s query string false "Search backgrounds from unsplash with this search term."
// @Param p query int false "The page number. Used for pagination. If not provided, the first page of results is returned."
// @Success 200 {array} background.Image "An array with photos"
// @Failure 500 {object} models.Message "Internal error"
// @Router /backgrounds/unsplash/search [get]
func (p *Provider) Search(search string, page int64) (result []*background.Image, err error) {
// If we don't have a search query, return results from the unsplash featured collection
if search == "" {
collectionResult := []*Photo{}
err = doGet("collections/317099/photos?page="+strconv.FormatInt(page, 10)+"&per_page=25&order_by=latest", &collectionResult)
if err != nil {
return
}
result = []*background.Image{}
for _, p := range collectionResult {
result = append(result, &background.Image{
ID: p.ID,
URL: p.Urls.Raw,
Thumb: p.Urls.Thumb,
Info: &models.UnsplashPhoto{
UnsplashID: p.ID,
Author: p.User.Username,
AuthorName: p.User.Name,
},
})
photos[p.ID] = p
}
return
}
searchResult := &SearchResult{}
err = doGet("search/photos?query="+search+"&page="+strconv.FormatInt(page, 10)+"&per_page=25", &searchResult)
if err != nil {
return
}
result = []*background.Image{}
for _, p := range searchResult.Results {
result = append(result, &background.Image{
ID: p.ID,
URL: p.Urls.Raw,
Thumb: p.Urls.Thumb,
})
photos[p.ID] = p
}
return
}
// Set sets an unsplash photo as list background
// @Summary Set an unsplash photo as list background
// @Description Sets a photo from unsplash as list background.1
// @tags list
// @Accept json
// @Produce json
// @Security JWTKeyAuth
// @Param id path int true "List ID"
// @Param list body background.Image true "The image you want to set as background"
// @Success 200 {object} models.List "The background has been successfully set."
// @Failure 400 {object} code.vikunja.io/web.HTTPError "Invalid image object provided."
// @Failure 403 {object} code.vikunja.io/web.HTTPError "The user does not have access to the list"
// @Failure 500 {object} models.Message "Internal error"
// @Router /lists/{id}/backgrounds/unsplash [post]
func (p *Provider) Set(image *background.Image, list *models.List, auth web.Auth) (err error) {
// Find the photo
var photo *Photo
var exists bool
photo, exists = photos[image.ID]
if !exists {
log.Debugf("Image information for unsplash photo %s not cached, requesting from unsplash...", image.ID)
photo = &Photo{}
err = doGet("photos/"+image.ID, photo)
if err != nil {
return
}
}
// Download the photo from unsplash
// The parameters crop the image to a max width of 2560 and a max height of 2048 to save bandwidth and storage.
resp, err := http.Get(photo.Urls.Raw + "&w=2560&h=2048&q=90")
if err != nil {
return
}
defer resp.Body.Close()
log.Debugf("Downloaded Unsplash Photo %s", image.ID)
// Save it as a file in vikunja
file, err := files.Create(resp.Body, "", 0, auth)
if err != nil {
return
}
// Remove the old background if one exists
if list.BackgroundFileID != 0 {
file := files.File{ID: list.BackgroundFileID}
if err := file.Delete(); err != nil {
return err
}
if err := models.RemoveUnsplashPhoto(list.BackgroundFileID); err != nil {
return err
}
}
// Save the relation that we got it from unsplash
unsplashPhoto := &models.UnsplashPhoto{
FileID: file.ID,
UnsplashID: image.ID,
Author: photo.User.Username,
AuthorName: photo.User.Name,
}
err = unsplashPhoto.Save()
if err != nil {
return
}
log.Debugf("Saved unsplash photo %s as file %d with new entry %d", image.ID, file.ID, unsplashPhoto.ID)
// Set the file in the list
list.BackgroundFileID = file.ID
list.BackgroundInformation = unsplashPhoto
// Set it as the list background
return models.SetListBackground(list.ID, file)
}
// Pingback pings the unsplash api if an unsplash photo has been accessed.
func Pingback(f *files.File) {
// Check if the file is actually downloaded from unsplash
unsplashPhoto, err := models.GetUnsplashPhotoByFileID(f.ID)
if err != nil {
log.Errorf("Unsplash Pingback: %s", err.Error())
}
// Do the ping
if _, err := http.Get("https://views.unsplash.com/v?app_id=" + config.BackgroundsUnsplashApplicationID.GetString() + "&photo_id=" + unsplashPhoto.UnsplashID); err != nil {
log.Errorf("Unsplash Pingback Failed: %s", err.Error())
}
log.Debugf("Pinged unsplash for photo %s", unsplashPhoto.UnsplashID)
}

File diff suppressed because one or more lines are too long

View File

@ -26,14 +26,15 @@ import (
)
type vikunjaInfos struct {
Version string `json:"version"`
FrontendURL string `json:"frontend_url"`
Motd string `json:"motd"`
LinkSharingEnabled bool `json:"link_sharing_enabled"`
MaxFileSize string `json:"max_file_size"`
RegistrationEnabled bool `json:"registration_enabled"`
AvailableMigrators []string `json:"available_migrators"`
TaskAttachmentsEnabled bool `json:"task_attachments_enabled"`
Version string `json:"version"`
FrontendURL string `json:"frontend_url"`
Motd string `json:"motd"`
LinkSharingEnabled bool `json:"link_sharing_enabled"`
MaxFileSize string `json:"max_file_size"`
RegistrationEnabled bool `json:"registration_enabled"`
AvailableMigrators []string `json:"available_migrators"`
TaskAttachmentsEnabled bool `json:"task_attachments_enabled"`
EnabledBackgroundProviders []string `json:"enabled_background_providers"`
}
// Info is the handler to get infos about this vikunja instance
@ -44,7 +45,7 @@ type vikunjaInfos struct {
// @Success 200 {object} v1.vikunjaInfos
// @Router /info [get]
func Info(c echo.Context) error {
infos := vikunjaInfos{
info := vikunjaInfos{
Version: version.Version,
FrontendURL: config.ServiceFrontendurl.GetString(),
Motd: config.ServiceMotd.GetString(),
@ -57,12 +58,18 @@ func Info(c echo.Context) error {
// Migrators
if config.MigrationWunderlistEnable.GetBool() {
m := &wunderlist.Migration{}
infos.AvailableMigrators = append(infos.AvailableMigrators, m.Name())
info.AvailableMigrators = append(info.AvailableMigrators, m.Name())
}
if config.MigrationTodoistEnable.GetBool() {
m := &todoist.Migration{}
infos.AvailableMigrators = append(infos.AvailableMigrators, m.Name())
info.AvailableMigrators = append(info.AvailableMigrators, m.Name())
}
return c.JSON(http.StatusOK, infos)
if config.BackgroundsEnabled.GetBool() {
if config.BackgroundsUnsplashEnabled.GetBool() {
info.EnabledBackgroundProviders = append(info.EnabledBackgroundProviders, "unsplash")
}
}
return c.JSON(http.StatusOK, info)
}

View File

@ -47,6 +47,9 @@ import (
"code.vikunja.io/api/pkg/config"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/models"
"code.vikunja.io/api/pkg/modules/background"
backgroundHandler "code.vikunja.io/api/pkg/modules/background/handler"
"code.vikunja.io/api/pkg/modules/background/unsplash"
"code.vikunja.io/api/pkg/modules/migration"
migrationHandler "code.vikunja.io/api/pkg/modules/migration/handler"
"code.vikunja.io/api/pkg/modules/migration/todoist"
@ -444,6 +447,20 @@ func registerAPIRoutes(a *echo.Group) {
}
todoistMigrationHandler.RegisterRoutes(m)
}
// List Backgrounds
if config.BackgroundsEnabled.GetBool() {
a.GET("/lists/:list/background", backgroundHandler.GetListBackground)
if config.BackgroundsUnsplashEnabled.GetBool() {
unsplashBackgroundProvider := &backgroundHandler.BackgroundProvider{
Provider: func() background.Provider {
return &unsplash.Provider{}
},
}
a.GET("/backgrounds/unsplash/search", unsplashBackgroundProvider.SearchBackgrounds)
a.POST("/lists/:list/backgrounds/unsplash", unsplashBackgroundProvider.SetBackground)
}
}
}
func registerCalDavRoutes(c *echo.Group) {