A privacy-first, self-hosted, fully open source personal knowledge management software, written in typescript and golang. (PERSONAL FORK)
at upstream/main 173 lines 4.0 kB view raw
1// SiYuan - Refactor your thinking 2// Copyright (c) 2020-present, b3log.org 3// 4// This program is free software: you can redistribute it and/or modify 5// it under the terms of the GNU Affero General Public License as published by 6// the Free Software Foundation, either version 3 of the License, or 7// (at your option) any later version. 8// 9// This program is distributed in the hope that it will be useful, 10// but WITHOUT ANY WARRANTY; without even the implied warranty of 11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12// GNU Affero General Public License for more details. 13// 14// You should have received a copy of the GNU Affero General Public License 15// along with this program. If not, see <https://www.gnu.org/licenses/>. 16 17package api 18 19import ( 20 "net/http" 21 22 "github.com/88250/gulu" 23 "github.com/gin-gonic/gin" 24 "github.com/siyuan-note/siyuan/kernel/model" 25 "github.com/siyuan-note/siyuan/kernel/sql" 26 "github.com/siyuan-note/siyuan/kernel/treenode" 27 "github.com/siyuan-note/siyuan/kernel/util" 28) 29 30func getBookmarkLabels(c *gin.Context) { 31 ret := gulu.Ret.NewResult() 32 defer c.JSON(http.StatusOK, ret) 33 34 ret.Data = model.BookmarkLabels() 35} 36 37func batchGetBlockAttrs(c *gin.Context) { 38 ret := gulu.Ret.NewResult() 39 defer c.JSON(http.StatusOK, ret) 40 41 arg, ok := util.JsonArg(c, ret) 42 if !ok { 43 return 44 } 45 46 ids := arg["ids"].([]interface{}) 47 var idList []string 48 for _, id := range ids { 49 idList = append(idList, id.(string)) 50 } 51 52 ret.Data = sql.BatchGetBlockAttrs(idList) 53} 54 55func getBlockAttrs(c *gin.Context) { 56 ret := gulu.Ret.NewResult() 57 defer c.JSON(http.StatusOK, ret) 58 59 arg, ok := util.JsonArg(c, ret) 60 if !ok { 61 return 62 } 63 64 id := arg["id"].(string) 65 if util.InvalidIDPattern(id, ret) { 66 return 67 } 68 69 ret.Data = sql.GetBlockAttrs(id) 70} 71 72func setBlockAttrs(c *gin.Context) { 73 ret := gulu.Ret.NewResult() 74 defer c.JSON(http.StatusOK, ret) 75 76 arg, ok := util.JsonArg(c, ret) 77 if !ok { 78 return 79 } 80 81 id := arg["id"].(string) 82 if util.InvalidIDPattern(id, ret) { 83 return 84 } 85 86 attrs := arg["attrs"].(map[string]interface{}) 87 if 1 == len(attrs) && "" != attrs["scroll"] { 88 // 不记录用户指南滚动位置 89 if b := treenode.GetBlockTree(id); nil != b && (model.IsUserGuide(b.BoxID)) { 90 attrs["scroll"] = "" 91 } 92 } 93 94 nameValues := map[string]string{} 95 for name, value := range attrs { 96 if nil == value { // API `setBlockAttrs` 中如果存在属性值设置为 `null` 时移除该属性 https://github.com/siyuan-note/siyuan/issues/5577 97 nameValues[name] = "" 98 } else { 99 nameValues[name] = value.(string) 100 } 101 } 102 err := model.SetBlockAttrs(id, nameValues) 103 if err != nil { 104 ret.Code = -1 105 ret.Msg = err.Error() 106 return 107 } 108} 109 110func batchSetBlockAttrs(c *gin.Context) { 111 ret := gulu.Ret.NewResult() 112 defer c.JSON(http.StatusOK, ret) 113 114 arg, ok := util.JsonArg(c, ret) 115 if !ok { 116 return 117 } 118 119 blockAttrsArg := arg["blockAttrs"].([]interface{}) 120 var blockAttrs []map[string]interface{} 121 for _, blockAttrArg := range blockAttrsArg { 122 blockAttr := blockAttrArg.(map[string]interface{}) 123 id := blockAttr["id"].(string) 124 if util.InvalidIDPattern(id, ret) { 125 return 126 } 127 128 attrs := blockAttr["attrs"].(map[string]interface{}) 129 nameValues := map[string]string{} 130 for name, value := range attrs { 131 if nil == value { 132 nameValues[name] = "" 133 } else { 134 nameValues[name] = value.(string) 135 } 136 } 137 138 blockAttrs = append(blockAttrs, map[string]interface{}{ 139 "id": id, 140 "attrs": nameValues, 141 }) 142 } 143 144 err := model.BatchSetBlockAttrs(blockAttrs) 145 if err != nil { 146 ret.Code = -1 147 ret.Msg = err.Error() 148 return 149 } 150} 151 152func resetBlockAttrs(c *gin.Context) { 153 ret := gulu.Ret.NewResult() 154 defer c.JSON(http.StatusOK, ret) 155 156 arg, ok := util.JsonArg(c, ret) 157 if !ok { 158 return 159 } 160 161 id := arg["id"].(string) 162 attrs := arg["attrs"].(map[string]interface{}) 163 nameValues := map[string]string{} 164 for name, value := range attrs { 165 nameValues[name] = value.(string) 166 } 167 err := model.ResetBlockAttrs(id, nameValues) 168 if err != nil { 169 ret.Code = -1 170 ret.Msg = err.Error() 171 return 172 } 173}