From a600be69495c49fbe97d568b13f8a5466b63f193 Mon Sep 17 00:00:00 2001
From: lanrenwo <lanrenwo@users.noreply.github.com>
Date: Fri, 25 Nov 2022 11:41:39 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8DPlatformVersion=E5=AD=97?=
 =?UTF-8?q?=E6=AE=B5=E6=BA=A2=E5=87=BA=E7=9A=84=E9=97=AE=E9=A2=98?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 server/dbdata/tables.go       |  2 +-
 server/dbdata/user_act_log.go | 23 ++++++++++++++++++++++-
 2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/server/dbdata/tables.go b/server/dbdata/tables.go
index 6f389b4..bdd446b 100644
--- a/server/dbdata/tables.go
+++ b/server/dbdata/tables.go
@@ -50,7 +50,7 @@ type UserActLog struct {
 	Client          uint8     `json:"client" xorm:"not null default 0 Int"`
 	Version         string    `json:"version" xorm:"varchar(15)"`
 	DeviceType      string    `json:"device_type" xorm:"varchar(128) not null default ''"`
-	PlatformVersion string    `json:"platform_version" xorm:"varchar(15) not null default ''"`
+	PlatformVersion string    `json:"platform_version" xorm:"varchar(128) not null default ''"`
 	Status          uint8     `json:"status" xorm:"not null default 0 Int"`
 	Info            string    `json:"info" xorm:"varchar(255) not null default ''"` // 详情
 	CreatedAt       time.Time `json:"created_at" xorm:"DateTime created"`
diff --git a/server/dbdata/user_act_log.go b/server/dbdata/user_act_log.go
index a4d36d7..b14e677 100644
--- a/server/dbdata/user_act_log.go
+++ b/server/dbdata/user_act_log.go
@@ -5,6 +5,7 @@ import (
 	"regexp"
 	"strings"
 
+	"github.com/bjdgyc/anylink/base"
 	"github.com/ivpusic/grpool"
 	"github.com/spf13/cast"
 	"xorm.io/xorm"
@@ -85,8 +86,17 @@ func (ua *UserActLogProcess) Add(u UserActLog, userAgent string) {
 			u.Info = u.Info[2:]
 		}
 	}
+	// limit the max length of char
+	u.Version = substr(u.Version, 0, 15)
+	u.DeviceType = substr(u.DeviceType, 0, 128)
+	u.PlatformVersion = substr(u.PlatformVersion, 0, 128)
+	u.Info = substr(u.Info, 0, 255)
+
 	UserActLogIns.Pool.JobQueue <- func() {
-		_ = Add(u)
+		err := Add(u)
+		if err != nil {
+			base.Error("Add UserActLog error: ", err)
+		}
 	}
 }
 
@@ -117,6 +127,7 @@ func (ua *UserActLogProcess) GetInfoOpsById(id uint8) string {
 	return ua.InfoOps[id]
 }
 
+// 解析user agent
 func (ua *UserActLogProcess) ParseUserAgent(userAgent string) (os_idx, client_idx uint8, ver string) {
 	// Unknown
 	if len(userAgent) == 0 {
@@ -187,3 +198,13 @@ func (ua *UserActLogProcess) GetSession(values url.Values) *xorm.Session {
 	}
 	return session
 }
+
+// 截取字符串
+func substr(s string, pos, length int) string {
+	runes := []rune(s)
+	l := pos + length
+	if l > len(runes) {
+		l = len(runes)
+	}
+	return string(runes[pos:l])
+}