LinkinStars commented on code in PR #194: URL: https://github.com/apache/incubator-answer-plugins/pull/194#discussion_r1714600350
########## notification-lark/utils.go: ########## @@ -0,0 +1,56 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package lark + +import ( + "crypto/rand" + "math/big" +) + +type GenerateRandomStringArgs struct { + Length uint64 + StringPool string +} + +// GenerateRandomString use crypto to generate a random string +func GenerateRandomString(args *GenerateRandomStringArgs) string { + // check args + if args.Length <= 0 || args.StringPool == "" { + return "" + } + + // generate random string + b := make([]byte, args.Length) + for i := uint64(0); i < args.Length; i++ { + idx := RandomInt(0, int64(len(args.StringPool))) + b[i] = args.StringPool[idx] + } + + return string(b) +} + +func RandomInt(min, max int64) int64 { + result, _ := rand.Int(rand.Reader, big.NewInt(int64(max-min))) Review Comment: The max and min are already `int64` ########## notification-lark/notification.go: ########## @@ -0,0 +1,329 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package lark + +import ( + "context" + "embed" + "encoding/json" + "fmt" + "strings" + + lark_i18n "github.com/apache/incubator-answer-plugins/notification-lark/i18n" + "github.com/apache/incubator-answer-plugins/util" + "github.com/apache/incubator-answer/plugin" + + "github.com/segmentfault/pacman/i18n" + "github.com/segmentfault/pacman/log" + + lark "github.com/larksuite/oapi-sdk-go/v3" + "github.com/larksuite/oapi-sdk-go/v3/event/dispatcher" + larkApplication "github.com/larksuite/oapi-sdk-go/v3/service/application/v6" + larkIM "github.com/larksuite/oapi-sdk-go/v3/service/im/v1" + larkWebSocket "github.com/larksuite/oapi-sdk-go/v3/ws" +) + +//go:embed info.yaml +var Info embed.FS + +type LarkClient struct { + ws *larkWebSocket.Client + http *lark.Client +} + +type Notification struct { + info *util.Info + config *NotificationConfig + client *LarkClient + userConfigCache *UserConfigCache +} + +const ( + LarkBindAccountMenuEventKey = "10001" + NotificationTypeInteractive = "interactive" + MsgTypeText = "text" + ReceiveIdTypeOpenId = "open_id" +) + +var ( + TagColor = []string{"neutral", "blue", "turquoise", "lime", "orange", "violet", "indigo", "wathet", "green", "yellow", "red", "purple", "carmine"} +) + +func init() { + plugin.Register(&Notification{ + userConfigCache: NewUserConfigCache(), + }) +} + +func (n Notification) Info() plugin.Info { + if n.info == nil { + info := &util.Info{} + info.GetInfo(Info) + n.info = info + } + + return plugin.Info{ + Name: plugin.MakeTranslator("NotificationLark"), + SlugName: n.info.SlugName, + Description: plugin.MakeTranslator(""), Review Comment: You can use ` plugin.MakeTranslator(i18n.InfoDescription),`. ########## notification-lark/notification.go: ########## @@ -0,0 +1,329 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package lark + +import ( + "context" + "embed" + "encoding/json" + "fmt" + "strings" + + lark_i18n "github.com/apache/incubator-answer-plugins/notification-lark/i18n" + "github.com/apache/incubator-answer-plugins/util" + "github.com/apache/incubator-answer/plugin" + + "github.com/segmentfault/pacman/i18n" + "github.com/segmentfault/pacman/log" + + lark "github.com/larksuite/oapi-sdk-go/v3" + "github.com/larksuite/oapi-sdk-go/v3/event/dispatcher" + larkApplication "github.com/larksuite/oapi-sdk-go/v3/service/application/v6" + larkIM "github.com/larksuite/oapi-sdk-go/v3/service/im/v1" + larkWebSocket "github.com/larksuite/oapi-sdk-go/v3/ws" +) + +//go:embed info.yaml +var Info embed.FS + +type LarkClient struct { + ws *larkWebSocket.Client + http *lark.Client +} + +type Notification struct { + info *util.Info + config *NotificationConfig + client *LarkClient + userConfigCache *UserConfigCache +} + +const ( + LarkBindAccountMenuEventKey = "10001" + NotificationTypeInteractive = "interactive" + MsgTypeText = "text" + ReceiveIdTypeOpenId = "open_id" +) + +var ( + TagColor = []string{"neutral", "blue", "turquoise", "lime", "orange", "violet", "indigo", "wathet", "green", "yellow", "red", "purple", "carmine"} +) + +func init() { + plugin.Register(&Notification{ + userConfigCache: NewUserConfigCache(), + }) +} + +func (n Notification) Info() plugin.Info { + if n.info == nil { + info := &util.Info{} + info.GetInfo(Info) + n.info = info + } + + return plugin.Info{ + Name: plugin.MakeTranslator("NotificationLark"), + SlugName: n.info.SlugName, + Description: plugin.MakeTranslator(""), + Author: n.info.Author, + Version: n.info.Version, + Link: n.info.Link, + } +} + +func renderTag(tags []string) string { + Review Comment: Remove the empty line. ########## notification-lark/notification.go: ########## @@ -0,0 +1,329 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package lark + +import ( + "context" + "embed" + "encoding/json" + "fmt" + "strings" + + lark_i18n "github.com/apache/incubator-answer-plugins/notification-lark/i18n" + "github.com/apache/incubator-answer-plugins/util" + "github.com/apache/incubator-answer/plugin" + + "github.com/segmentfault/pacman/i18n" + "github.com/segmentfault/pacman/log" + + lark "github.com/larksuite/oapi-sdk-go/v3" + "github.com/larksuite/oapi-sdk-go/v3/event/dispatcher" + larkApplication "github.com/larksuite/oapi-sdk-go/v3/service/application/v6" + larkIM "github.com/larksuite/oapi-sdk-go/v3/service/im/v1" + larkWebSocket "github.com/larksuite/oapi-sdk-go/v3/ws" +) + +//go:embed info.yaml +var Info embed.FS + +type LarkClient struct { + ws *larkWebSocket.Client + http *lark.Client +} + +type Notification struct { + info *util.Info + config *NotificationConfig + client *LarkClient + userConfigCache *UserConfigCache +} + +const ( + LarkBindAccountMenuEventKey = "10001" + NotificationTypeInteractive = "interactive" + MsgTypeText = "text" + ReceiveIdTypeOpenId = "open_id" +) + +var ( + TagColor = []string{"neutral", "blue", "turquoise", "lime", "orange", "violet", "indigo", "wathet", "green", "yellow", "red", "purple", "carmine"} +) + +func init() { + plugin.Register(&Notification{ + userConfigCache: NewUserConfigCache(), + }) +} + +func (n Notification) Info() plugin.Info { + if n.info == nil { + info := &util.Info{} + info.GetInfo(Info) + n.info = info + } + + return plugin.Info{ + Name: plugin.MakeTranslator("NotificationLark"), Review Comment: You can use `plugin.MakeTranslator(i18n.InfoName)`. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
