Ace & Markdown全新编辑器 & others

邀请注册
共享后图片可见问题 fileService::getFile()
笔记历史记录问题
共享后得到被共享者列表问题 shareService
themeService 博客主题新建问题, 模板循环引用问题
markdown编辑器双屏大小不能保存问题
This commit is contained in:
life
2015-01-08 21:15:56 +08:00
parent d24531dc78
commit c514d0bc1c
26 changed files with 719 additions and 254 deletions

View File

@ -5,6 +5,7 @@ import (
// "github.com/leanote/leanote/app/db"
"github.com/leanote/leanote/app/info"
// "github.com/revel/revel"
"strings"
. "github.com/leanote/leanote/app/lea"
"fmt"
"strconv"
@ -17,6 +18,8 @@ type AuthService struct {
// pwd已md5了
func (this *AuthService) Login(emailOrUsername, pwd string) info.User {
emailOrUsername = strings.Trim(emailOrUsername, " ")
// pwd = strings.Trim(pwd, " ")
userInfo := userService.LoginGetUserInfo(emailOrUsername, Md5(pwd))
return userInfo
}
@ -32,12 +35,16 @@ func (this *AuthService) Login(emailOrUsername, pwd string) info.User {
// 1. 添加用户
// 2. 将leanote共享给我
// [ok]
func (this *AuthService) Register(email, pwd string) (bool, string) {
func (this *AuthService) Register(email, pwd, fromUserId string) (bool, string) {
// 用户是否已存在
if userService.IsExistsUser(email) {
return false, "userHasBeenRegistered-" + email
}
user := info.User{UserId: bson.NewObjectId(), Email: email, Username: email, Pwd: Md5(pwd)}
if fromUserId != "" && IsObjectId(fromUserId) {
user.FromUserId = bson.ObjectIdHex(fromUserId)
}
LogJ(user)
return this.register(user)
}
@ -129,4 +136,4 @@ func (this *AuthService) ThirdRegister(thirdType, thirdUserId, thirdUsername str
}
_, _ = this.register(userInfo)
return
}
}

View File

@ -147,7 +147,16 @@ func (this *FileService) GetFile(userId, fileId string) string {
return path
}
// 2014/12/28 修复, 如果是分享给用户组, 那就不行, 这里可以实现
for _, noteId := range noteIds {
note := noteService.GetNoteById(noteId.Hex())
if shareService.HasReadPerm(note.UserId.Hex(), userId, noteId.Hex()) {
return path;
}
}
/*
// 若有共享给我的笔记?
// 对该笔记可读?
if db.Has(db.ShareNotes, bson.M{"ToUserId": bson.ObjectIdHex(userId), "NoteId": bson.M{"$in": noteIds}}) {
return path
}
@ -166,6 +175,7 @@ func (this *FileService) GetFile(userId, fileId string) string {
return path
}
}
*/
}
// 可能是刚复制到owner上, 但内容又没有保存, 所以没有note->imageId的映射, 此时看是否有fromFileId

View File

@ -33,7 +33,8 @@ func (this *NoteContentHistoryService) AddHistory(noteId, userId string, eachHis
// TODO
l := len(history.Histories)
if l >= maxSize {
history.Histories = history.Histories[l-maxSize:]
// history.Histories = history.Histories[l-maxSize:] // BUG, 致使都是以前的
history.Histories = history.Histories[:maxSize]
}
newHistory := []info.EachHistory{eachHistory}
newHistory = append(newHistory, history.Histories...) // 在开头加了, 最近的在最前
@ -61,4 +62,4 @@ func (this *NoteContentHistoryService) ListHistories(noteId, userId string) []in
histories := info.NoteContentHistory{}
db.GetByIdAndUserId(db.NoteContentHistories, noteId, userId, &histories)
return histories.Histories
}
}

View File

@ -49,6 +49,7 @@ func (this *ShareService) getOrQ(userId string) bson.M {
return q
}
// 得到共享给我的笔记本和用户(谁共享给了我)
func (this *ShareService) GetShareNotebooks(userId string) (info.ShareNotebooksByUser, []info.User) {
// 得到共享给我的用户s信息
// 得到我参与的组织
@ -60,7 +61,7 @@ func (this *ShareService) GetShareNotebooks(userId string) (info.ShareNotebooksB
db.Distinct(db.ShareNotes, q, "UserId", &userIds1)
userIds2 := []bson.ObjectId{}
db.Distinct(db.ShareNotebooks, q, "UserId", &userIds1)
db.Distinct(db.ShareNotebooks, q, "UserId", &userIds2) // BUG之前是userId1, 2014/12/29
userIds := append(userIds1, userIds2...)
userInfos := userService.GetUserInfosOrderBySeq(userIds);

View File

@ -13,6 +13,7 @@ import (
"fmt"
"html/template"
"regexp"
"io/ioutil"
"encoding/json"
)
@ -127,7 +128,8 @@ func (this *ThemeService) CopyDefaultTheme(userBlog info.UserBlog) (ok bool, the
// 设为active true
func (this *ThemeService) NewThemeForFirst(userBlog info.UserBlog) (ok bool, themeId string) {
ok, themeId = this.CopyDefaultTheme(userBlog)
db.UpdateByQField(db.Themes, bson.M{"_id": bson.ObjectIdHex(themeId)}, "IsActive", true)
this.ActiveTheme(userBlog.UserId.Hex(), themeId)
// db.UpdateByQField(db.Themes, bson.M{"_id": bson.ObjectIdHex(themeId)}, "IsActive", true)
return
}
@ -285,11 +287,15 @@ func (this *ThemeService) GetThemePath(userId, themeId string) string {
}
// 更新模板内容
func (this *ThemeService) UpdateTplContent(userId, themeId, filename, content string) (ok bool, msg string) {
path := this.GetThemeAbsolutePath(userId, themeId) + "/" + filename
basePath := this.GetThemeAbsolutePath(userId, themeId)
path := basePath + "/" + filename
if strings.Contains(filename, ".html") {
// 模板
if ok, msg = this.mustTpl(filename, content); ok {
ok = PutFileStrContent(path, content)
Log(">>")
if ok, msg = this.ValidateTheme(basePath, filename, content); ok {
// 模板
if ok, msg = this.mustTpl(filename, content); ok {
ok = PutFileStrContent(path, content)
}
}
return
} else if filename == "theme.json" {
@ -410,6 +416,11 @@ func (this *ThemeService) ImportTheme(userId, path string) (ok bool, msg string)
DeleteFile(targetPath)
return
}
// 主题验证
if ok, msg = this.ValidateTheme(targetPath, "", ""); !ok {
DeleteFile(targetPath)
return
}
// 解压成功, 那么新建之
// 保存到数据库中
theme, _ := this.getThemeConfig(targetPath)
@ -505,5 +516,119 @@ func (this *ThemeService) InstallTheme(userId, themeId string) (ok bool) {
ok = db.Insert(db.Themes, theme)
// 激活之
this.ActiveTheme(userId, themeId);
return ok
}
// 验证主题是否全法, 存在循环引用?
// filename, newContent 表示在修改模板时要判断模板修改时是否有错误
func (this *ThemeService) ValidateTheme(path string, filename, newContent string) (ok bool, msg string) {
Log("theme Path")
Log(path)
// 建立一个有向图
// 将该path下的所有文件提出, 得到文件的引用情况
files := ListDir(path)
LogJ(files);
size := len(files)
if(size > 100) {
ok = false;
msg = "tooManyFiles"
return
}
/*
111111111
111000000
*/
vector := make([][]int, size)
for i := 0; i < size; i++ {
vector[i] = make([]int, size)
}
fileIndexMap := map[string]int{} // fileName => index
fileContent := map[string]string{} // fileName => content
index := 0
// 得到文件内容, 和建立索引, 每个文件都有一个index, 对应数组位置
for _, t := range files {
if !strings.Contains(t, ".html") {
continue;
}
if t != filename {
fileBytes, err := ioutil.ReadFile(path + "/" + t)
if err != nil {
continue
}
fileIndexMap[t] = index;
// html内容
fileStr := string(fileBytes)
fileContent[t] = fileStr
} else {
fileIndexMap[t] = index
fileContent[t] = newContent
}
index++
}
// 分析文件内容, 建立有向图
reg, _ := regexp.Compile("{{ *template \"(.+?\\.html)\".*}}")
for filename, content := range fileContent {
thisIndex := fileIndexMap[filename]
finds := reg.FindAllStringSubmatch(content, -1) // 子匹配
LogJ(finds)
// Log(content)
if finds != nil && len(finds) > 0 {
for _, includes := range finds {
include := includes[1]
includeIndex, has := fileIndexMap[include]
Log(includeIndex)
Log("??")
Log(has)
if has {
vector[thisIndex][includeIndex] = 1
}
}
}
}
LogJ(vector)
LogJ(fileIndexMap)
// 建立图后, 判断是否有环
if this.hasRound(vector, index) {
ok = false
msg = "themeValidHasRoundInclude"
} else {
ok = true
}
return;
}
// 检测有向图是否有环, DFS
func (this *ThemeService) hasRound(vector [][]int, size int) (ok bool) {
for i := 0; i < size; i++ {
visited := make([]int, size)
if this.hasRoundEach(vector, i, size, visited) {
Log(">>")
Log(i);
return true;
}
}
return false
}
// 从每个节点出发, 判断是否有环
func (this *ThemeService) hasRoundEach(vector [][]int, index int, size int, visited []int) (ok bool) {
if visited[index] > 0 {
Log("<")
Log(index)
return true
}
visited[index] = 1;
// 遍历它的孩子
for i := 0; i < size; i++ {
if vector[index][i] > 0 {
return this.hasRoundEach(vector, i, size, visited);
}
}
visited[index] = 0;
return false;
}

View File

@ -369,7 +369,7 @@ func (this *UserService) ThirdAddUser(userId, email, pwd string) (ok bool, msg s
// 宽度
func (this *UserService)UpdateColumnWidth(userId string, notebookWidth, noteListWidth, mdEditorWidth int) bool {
return db.UpdateByQMap(db.Users, bson.M{"_id": bson.ObjectIdHex(userId)},
bson.M{"NotebookWidth": notebookWidth, "NoteListWidth": noteListWidth, "mdEditorWidth": mdEditorWidth})
bson.M{"NotebookWidth": notebookWidth, "NoteListWidth": noteListWidth, "MdEditorWidth": mdEditorWidth})
}
// 左侧是否隐藏
func (this *UserService)UpdateLeftIsMin(userId string, leftIsMin bool) bool {