Skip to content

Commit

Permalink
fix: linter issues, namings, logic bugs. (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy authored Mar 4, 2025
1 parent 8c1239c commit 0fb85a1
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 75 deletions.
24 changes: 14 additions & 10 deletions .github/workflows/linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@ on:
- main

jobs:
golangci:
name: lint
linting:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.22.5'
cache: false
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
- name: Install Go
uses: actions/setup-go@v5
with:
version: v1.63.4
go-version: "1.23.3"

- name: Checkout code
uses: actions/checkout@v4

- name: Installing golangci-lint
run: go install github.com/golangci/golangci-lint/cmd/[email protected]

- name: Formatting and linting the project
run: make check
2 changes: 1 addition & 1 deletion .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v5
with:
go-version: '1.22.5'
go-version: '1.23.3'

- name: Unit tests
run: make unit-test
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions cmd/relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ func New(cfg *config.Config) (*Relay, error) {
}
m := metrics.New()

meili := meilisearch.New(cfg.MeiliConf)
meili := meilisearch.New(cfg.Meili)

r, err := redis.New(cfg.RedisConf)
r, err := redis.New(cfg.Redis)
if err != nil {
return nil, err
}
Expand Down
13 changes: 6 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package config
import (
"os"

"github.com/joho/godotenv"
"gopkg.in/yaml.v3"

"github.com/dezh-tech/immortal/delivery/grpc"
"github.com/dezh-tech/immortal/delivery/websocket"
"github.com/dezh-tech/immortal/infrastructure/database"
Expand All @@ -14,6 +11,8 @@ import (
"github.com/dezh-tech/immortal/infrastructure/redis"
"github.com/dezh-tech/immortal/pkg/logger"
"github.com/dezh-tech/immortal/repository"
"github.com/joho/godotenv"
"gopkg.in/yaml.v3"
)

// Config represents the configs used by relay and other concepts on system.
Expand All @@ -22,8 +21,8 @@ type Config struct {
GRPCClient grpcclient.Config `yaml:"manager"`
WebsocketServer websocket.Config `yaml:"ws_server"`
Database database.Config `yaml:"database"`
RedisConf redis.Config `yaml:"redis"`
MeiliConf meilisearch.Config `yaml:"meili"`
Redis redis.Config `yaml:"redis"`
Meili meilisearch.Config `yaml:"meili"`
GRPCServer grpc.Config `yaml:"grpc_server"`
Logger logger.Config `yaml:"logger"`
Handler repository.Config
Expand Down Expand Up @@ -58,8 +57,8 @@ func Load(path string) (*Config, error) {
}

config.Database.URI = os.Getenv("IMMO_MONGO_URI")
config.RedisConf.URI = os.Getenv("IMMO_REDIS_URI")
config.MeiliConf.APIKey = os.Getenv("MEILI_API_KEY")
config.Redis.URI = os.Getenv("IMMO_REDIS_URI")
config.Meili.APIKey = os.Getenv("MEILI_API_KEY")

if err = config.basicCheck(); err != nil {
return nil, Error{
Expand Down
2 changes: 1 addition & 1 deletion delivery/websocket/event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (s *Server) handleEvent(conn *websocket.Conn, m message.Message) { //nolint
for conn, client := range s.conns {
client.Lock()
for id, filter := range client.subs {
if !filter.Match(msg.Event) {
if !filter.Match(msg.Event, *client.pubkey) {
continue
}
_ = conn.WriteMessage(1, message.MakeEvent(id, msg.Event))
Expand Down
3 changes: 1 addition & 2 deletions repository/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ import (
"fmt"
"strings"

"go.mongodb.org/mongo-driver/bson"

"github.com/dezh-tech/immortal/infrastructure/database"
grpcclient "github.com/dezh-tech/immortal/infrastructure/grpc_client"
"github.com/dezh-tech/immortal/infrastructure/meilisearch"
"github.com/dezh-tech/immortal/types"
"github.com/dezh-tech/immortal/types/filter"
"go.mongodb.org/mongo-driver/bson"
)

type Handler struct {
Expand Down
3 changes: 1 addition & 2 deletions repository/req.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"context"
"encoding/json"

"github.com/meilisearch/meilisearch-go"

"github.com/dezh-tech/immortal/pkg/logger"
"github.com/dezh-tech/immortal/types"
"github.com/dezh-tech/immortal/types/event"
"github.com/dezh-tech/immortal/types/filter"
"github.com/meilisearch/meilisearch-go"
)

func (h *Handler) HandleReq(f *filter.Filter, pubkey string) ([]event.Event, error) {
Expand Down
11 changes: 5 additions & 6 deletions repository/req_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,16 @@ import (
"testing"
"time"

meilisearchGo "github.com/meilisearch/meilisearch-go"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/meilisearch"

grpc_client "github.com/dezh-tech/immortal/infrastructure/grpc_client/gen"
infra "github.com/dezh-tech/immortal/infrastructure/meilisearch"
"github.com/dezh-tech/immortal/types"
"github.com/dezh-tech/immortal/types/event"
"github.com/dezh-tech/immortal/types/filter"
meilisearchGo "github.com/meilisearch/meilisearch-go"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/meilisearch"
)

type MockGRPC struct {
Expand Down
7 changes: 6 additions & 1 deletion types/filter/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ type Filter struct {
Search string `json:"search"` // see NIP-50.
}

// todo::: can/should we support nip-50 in match function?
// Match checks if the event is match with given filter.
// Note: this method intended to be used for already open subscriptions and recently received events.
// For new subscriptions and queries for stored data use the database query and don't use this to verify the result.
func (f *Filter) Match(e *event.Event) bool {
func (f *Filter) Match(e *event.Event, pubkey string) bool {
if e == nil {
return false
}
Expand Down Expand Up @@ -52,6 +53,10 @@ func (f *Filter) Match(e *event.Event) bool {
}
}

if e.Kind == types.KindGiftWrap && !e.Tags.ContainsTag("p", pubkey) {
return false
}

return true
}

Expand Down
4 changes: 2 additions & 2 deletions types/filter/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,13 @@ func TestFilter(t *testing.T) {
t.Run("Match", func(t *testing.T) {
for _, tc := range matchingTestCases {
if tc.IsMatch {
assert.True(t, tc.Filter.Match(tc.Event),
assert.True(t, tc.Filter.Match(tc.Event, ""),
"expected event %s to be match with filter %s, note: %s",
tc.Event.String(), tc.Filter.String(), tc.Note)

continue
}
assert.False(t, tc.Filter.Match(tc.Event),
assert.False(t, tc.Filter.Match(tc.Event, ""),
"expected event %s not to be match with filter %s, note: %s",
tc.Event.String(), tc.Filter.String(), tc.Note)
}
Expand Down
30 changes: 0 additions & 30 deletions types/filter/filters.go

This file was deleted.

28 changes: 17 additions & 11 deletions types/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,29 @@ type (
Tags []Tag
)

func (tags Tags) ContainsTag(tagKey, tagValue string) bool {
// ContainsTag checks if a tag with an specific name/value is exist.
func (tags Tags) ContainsTag(name, value string) bool {
for _, tag := range tags {
if tag[0] == tagKey && len(tag) > 1 {
if tag[1] == tagValue {
return true
}
if len(tag) < 2 {
continue
}

if tag[0] == name && tag[1] == value {
return true
}
}

return false
}

func (tags Tags) ContainsAny(tagName string, values []string) bool {
// ContainsAny checks if event have a tag with given name that its value is equal to one of given values.
func (tags Tags) ContainsAny(name string, values []string) bool {
for _, tag := range tags {
if len(tag) < 2 {
continue
}

if "#"+tag[0] != tagName {
if "#"+tag[0] != name {
continue
}

Expand All @@ -35,28 +39,30 @@ func (tags Tags) ContainsAny(tagName string, values []string) bool {
return false
}

func (tags Tags) GetValue(tagName string) string {
// GetValue returns the value of first tag with given name.
func (tags Tags) GetValue(name string) string {
for _, tag := range tags {
if len(tag) < 2 {
continue
}

if tag[0] == tagName {
if tag[0] == name {
return tag[1]
}
}

return ""
}

func (tags Tags) GetValues(tagName string) []string {
// GetValues returns all values of all tags with same name in an event.
func (tags Tags) GetValues(name string) []string {
values := []string{}
for _, tag := range tags {
if len(tag) < 2 {
continue
}

if tag[0] == tagName {
if tag[0] == name {
values = append(values, tag[1])
}
}
Expand Down

0 comments on commit 0fb85a1

Please sign in to comment.