Skip to content

Commit

Permalink
add tag colors support
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyGALLAND committed Aug 27, 2023
1 parent 5d0ca36 commit 9873916
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 4 deletions.
12 changes: 10 additions & 2 deletions app/controllers/adressBook.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"rustdesk-api-server/app/services"
"rustdesk-api-server/utils/beegoHelper"
"strings"
"strconv"
)

var Address = new(AddressBookController)
Expand All @@ -19,11 +20,18 @@ func (ctl *AddressBookController) List() {
ack := dto.AbGetAck{}
ack.Tags = []string{}
// 查询 tags
tag_colors := dto.AbTag_colors{}
tags := services.Tags.FindTags(ctl.loginUserInfo.Id)
for _, item := range tags {
ack.Tags = append(ack.Tags, item.Tag)

if (item.Color != ""){
tag_colors[item.Tag], _ = strconv.ParseInt(item.Color, 10, 64)
}
}

jdata_tag_colors, _ := json.Marshal(tag_colors)
ack.Tag_colors = string(jdata_tag_colors)

// 查询 peers
ack.Peers = []dto.AbGetPeer{}
peerDbs := services.Peers.FindPeers(ctl.loginUserInfo.Id)
Expand Down Expand Up @@ -95,7 +103,7 @@ func (ctl *AddressBookController) Update() {
services.Peers.DeleteAll(ctl.loginUserInfo.Id)

// 开始批量插入tags
if !services.Tags.BatchAdd(ctl.loginUserInfo.Id, reqSub.Tags) {
if !services.Tags.BatchAdd(ctl.loginUserInfo.Id, reqSub.Tags, reqSub.Tag_colors) {
ctl.JSON(beegoHelper.H{
"error": "导入标签失败",
})
Expand Down
4 changes: 4 additions & 0 deletions app/dto/ab.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package dto

type AbTag_colors map[string]int64

type AbGetAck struct {
Tags []string `json:"tags"`
Peers []AbGetPeer `json:"peers"`
Tag_colors string `json:"tag_colors,omitempty"`
}

type AbGetPeer struct {
Expand All @@ -21,4 +24,5 @@ type AbUpdateReq struct {
type AbUpdateSub struct {
Tags []string `json:"tags"`
Peers []AbGetPeer `json:"peers"`
Tag_colors string `json:"tag_colors,omitempty"`
}
1 change: 1 addition & 0 deletions app/models/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Tags struct {
Id int32 `json:"id"`
Uid int32 `json:"uid"`
Tag string `json:"tag"`
Color string `json:"color,omitempty"`
}

func (u *Tags) TableName() string {
Expand Down
17 changes: 15 additions & 2 deletions app/services/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package services
import (
"github.com/beego/beego/v2/client/orm"
"rustdesk-api-server/app/models"
"rustdesk-api-server/app/dto"
"encoding/json"
"strconv"
)

var Tags = new(TagsService)
Expand All @@ -11,16 +14,26 @@ type TagsService struct {
}

// 批量插入
func (this *TagsService) BatchAdd(uid int32, tags []string) bool {
func (this *TagsService) BatchAdd(uid int32, tags []string, tag_colors_str string) bool {
if len(tags) == 0 {
return false
}

tag_Colors := dto.AbTag_colors{}

_ = json.Unmarshal([]byte(tag_colors_str), &tag_Colors)

tagList := []models.Tags{}
for _, t := range tags {
tag_color := ""
if c, found := tag_Colors[t]; found {
tag_color = strconv.FormatInt(c, 10)
}

tagList = append(tagList, models.Tags{
Uid: uid,
Tag: t,
Color: tag_color,
})
}

Expand All @@ -42,7 +55,7 @@ func (this *TagsService) DeleteAll(uid int32) bool {
// 查询用户名下Tag
func (this *TagsService) FindTags(uid int32) []models.Tags {
ret := []models.Tags{}
_, err := orm.NewOrm().QueryTable(new(models.Tags)).Filter("uid", uid).All(&ret, "tag")
_, err := orm.NewOrm().QueryTable(new(models.Tags)).Filter("uid", uid).All(&ret, "tag", "color")
if err != nil {
return nil
}
Expand Down
1 change: 1 addition & 0 deletions db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ CREATE TABLE `rustdesk_tags` (
`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT COMMENT 'tagID',
`uid` int(10) unsigned NOT NULL COMMENT '用户ID',
`tag` char(20) NOT NULL DEFAULT '' COMMENT 'tag名称',
`color` char(10) NULL COMMENT 'Tag Color',
PRIMARY KEY (`id`),
UNIQUE KEY `tag` (`tag`,`uid`)
) ENGINE=MyISAM AUTO_INCREMENT=136 DEFAULT CHARSET=utf8 COMMENT='tags表';
Expand Down

0 comments on commit 9873916

Please # to comment.