Skip to content

Commit

Permalink
feat: update dependencies and prepare using context
Browse files Browse the repository at this point in the history
  • Loading branch information
yukimochi committed Feb 4, 2024
1 parent 7f942e7 commit 2ed9bae
Show file tree
Hide file tree
Showing 21 changed files with 224 additions and 754 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.21.2-alpine AS build
FROM golang:1.21.6-alpine AS build

WORKDIR /Activity-Relay
COPY . /Activity-Relay
Expand All @@ -7,7 +7,7 @@ RUN mkdir -p /rootfs/usr/bin && \
apk add -U --no-cache git && \
go build -o /rootfs/usr/bin/relay -ldflags "-X main.version=$(git describe --tags HEAD | sed -r 's/v(.*)/\1/')" .

FROM alpine:3.18.4
FROM alpine:3.19.1

COPY --from=build /rootfs/usr/bin /usr/bin
RUN chmod +x /usr/bin/relay && \
Expand Down
3 changes: 2 additions & 1 deletion api/api_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"context"
"fmt"
"os"
"testing"
Expand Down Expand Up @@ -33,7 +34,7 @@ func TestMain(m *testing.M) {
os.Exit(1)
}
RelayState = models.NewState(RelayState.RedisClient, false)
RelayState.RedisClient.FlushAll().Result()
RelayState.RedisClient.FlushAll(context.TODO()).Result()
code := m.Run()
os.Exit(code)
}
9 changes: 5 additions & 4 deletions api/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package api

import (
"bytes"
"context"
"io"
"net/http"
"os"
Expand All @@ -12,7 +13,7 @@ import (
)

func TestDecodeActivity(t *testing.T) {
RelayState.RedisClient.FlushAll().Result()
RelayState.RedisClient.FlushAll(context.TODO()).Result()

RelayState.AddSubscriber(models.Subscriber{
Domain: "innocent.yukimochi.io",
Expand Down Expand Up @@ -41,7 +42,7 @@ func TestDecodeActivity(t *testing.T) {
}

func TestDecodeActivityWithNoSignature(t *testing.T) {
RelayState.RedisClient.FlushAll().Result()
RelayState.RedisClient.FlushAll(context.TODO()).Result()

RelayState.AddSubscriber(models.Subscriber{
Domain: "innocent.yukimochi.io",
Expand All @@ -65,7 +66,7 @@ func TestDecodeActivityWithNoSignature(t *testing.T) {
}

func TestDecodeActivityWithNotFoundKeyId(t *testing.T) {
RelayState.RedisClient.FlushAll().Result()
RelayState.RedisClient.FlushAll(context.TODO()).Result()

RelayState.AddSubscriber(models.Subscriber{
Domain: "innocent.yukimochi.io",
Expand All @@ -90,7 +91,7 @@ func TestDecodeActivityWithNotFoundKeyId(t *testing.T) {
}

func TestDecodeActivityWithInvalidDigest(t *testing.T) {
RelayState.RedisClient.FlushAll().Result()
RelayState.RedisClient.FlushAll(context.TODO()).Result()

RelayState.AddSubscriber(models.Subscriber{
Domain: "innocent.yukimochi.io",
Expand Down
33 changes: 17 additions & 16 deletions api/handle_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"context"
"encoding/json"
"io"
"log"
Expand Down Expand Up @@ -417,7 +418,7 @@ func TestHandleInboxValidFollow(t *testing.T) {
if r.StatusCode != 202 {
t.Fatalf("fail - StatusCode is not match")
}
res, _ := RelayState.RedisClient.Exists("relay:subscription:" + domain.Host).Result()
res, _ := RelayState.RedisClient.Exists(context.TODO(), "relay:subscription:"+domain.Host).Result()
if res != 1 {
t.Fatalf("fail - follow request not work")
}
Expand Down Expand Up @@ -445,11 +446,11 @@ func TestHandleInboxValidManuallyFollow(t *testing.T) {
if r.StatusCode != 202 {
t.Fatalf("fail - StatusCode is not match")
}
res, _ := RelayState.RedisClient.Exists("relay:pending:" + domain.Host).Result()
res, _ := RelayState.RedisClient.Exists(context.TODO(), "relay:pending:"+domain.Host).Result()
if res != 1 {
t.Fatalf("fail - manually follow not work")
}
res, _ = RelayState.RedisClient.Exists("relay:subscription:" + domain.Host).Result()
res, _ = RelayState.RedisClient.Exists(context.TODO(), "relay:subscription:"+domain.Host).Result()
if res != 0 {
t.Fatalf("fail - manually follow not work")
}
Expand Down Expand Up @@ -477,7 +478,7 @@ func TestHandleInboxValidFollowBlocked(t *testing.T) {
if r.StatusCode != 202 {
t.Fatalf("fail - StatusCode is not match")
}
res, _ := RelayState.RedisClient.Exists("relay:subscription:" + domain.Host).Result()
res, _ := RelayState.RedisClient.Exists(context.TODO(), "relay:subscription:"+domain.Host).Result()
if res != 0 {
t.Fatalf("fail - follow request not blocked")
}
Expand All @@ -503,7 +504,7 @@ func TestHandleInboxFollowLitePub(t *testing.T) {
if r.StatusCode != 202 {
t.Fatalf("fail - StatusCode is not match")
}
res, _ := RelayState.RedisClient.Exists("relay:subscription:" + domain.Host).Result()
res, _ := RelayState.RedisClient.Exists(context.TODO(), "relay:subscription:"+domain.Host).Result()
if res != 0 {
t.Fatalf("fail - follow request not blocked")
}
Expand All @@ -527,7 +528,7 @@ func TestHandleInboxInvalidFollow(t *testing.T) {
if r.StatusCode != 202 {
t.Fatalf("fail - StatusCode is not match")
}
res, _ := RelayState.RedisClient.Exists("relay:subscription:" + domain.Host).Result()
res, _ := RelayState.RedisClient.Exists(context.TODO(), "relay:subscription:"+domain.Host).Result()
if res != 0 {
t.Fatalf("fail - follow request not blocked")
}
Expand Down Expand Up @@ -556,7 +557,7 @@ func TestHandleInboxValidUnfollow(t *testing.T) {
if r.StatusCode != 202 {
t.Fatalf("fail - StatusCode is not match")
}
res, _ := RelayState.RedisClient.Exists("relay:subscription:" + domain.Host).Result()
res, _ := RelayState.RedisClient.Exists(context.TODO(), "relay:subscription:"+domain.Host).Result()
if res != 0 {
t.Fatalf("fail - unfollow request not works")
}
Expand All @@ -572,7 +573,7 @@ func TestHandleInboxValidManuallyUnFollow(t *testing.T) {
}))
defer s.Close()

RelayState.RedisClient.HMSet("relay:pending:"+domain.Host, map[string]interface{}{
RelayState.RedisClient.HMSet(context.TODO(), "relay:pending:"+domain.Host, map[string]interface{}{
"inbox_url": actor.Endpoints.SharedInbox,
"activity_id": activity.ID,
"type": "Follow",
Expand All @@ -592,11 +593,11 @@ func TestHandleInboxValidManuallyUnFollow(t *testing.T) {
if r.StatusCode != 202 {
t.Fatalf("fail - StatusCode is not match")
}
res, _ := RelayState.RedisClient.Exists("relay:pending:" + domain.Host).Result()
res, _ := RelayState.RedisClient.Exists(context.TODO(), "relay:pending:"+domain.Host).Result()
if res != 0 {
t.Fatalf("fail - pending follow request not deleted")
}
RelayState.RedisClient.Del("relay:pending:" + domain.Host)
RelayState.RedisClient.Del(context.TODO(), "relay:pending:"+domain.Host)
RelayState.SetConfig(ManuallyAccept, false)
}

Expand All @@ -623,7 +624,7 @@ func TestHandleInboxUnfollowAsActor(t *testing.T) {
if r.StatusCode != 202 {
t.Fatalf("fail - StatusCode is not match")
}
res, _ := RelayState.RedisClient.Exists("relay:subscription:" + domain.Host).Result()
res, _ := RelayState.RedisClient.Exists(context.TODO(), "relay:subscription:"+domain.Host).Result()
if res != 1 {
t.Fatalf("fail - invalid unfollow request should be blocked")
}
Expand Down Expand Up @@ -653,7 +654,7 @@ func TestHandleInboxUnfollowLitePub(t *testing.T) {
if r.StatusCode != 202 {
t.Fatalf("fail - StatusCode is not match")
}
res, _ := RelayState.RedisClient.Exists("relay:subscription:" + domain.Host).Result()
res, _ := RelayState.RedisClient.Exists(context.TODO(), "relay:subscription:"+domain.Host).Result()
if res != 1 {
t.Fatalf("fail - invalid unfollow request should be blocked")
}
Expand Down Expand Up @@ -689,8 +690,8 @@ func TestHandleInboxValidCreate(t *testing.T) {
}
RelayState.DelSubscriber(domain.Host)
RelayState.DelSubscriber("example.org")
RelayState.RedisClient.Del("relay:subscription:" + domain.Host).Result()
RelayState.RedisClient.Del("relay:subscription:example.org").Result()
RelayState.RedisClient.Del(context.TODO(), "relay:subscription:"+domain.Host).Result()
RelayState.RedisClient.Del(context.TODO(), "relay:subscription:example.org").Result()
}

func TestHandleInboxLimitedCreate(t *testing.T) {
Expand Down Expand Up @@ -769,6 +770,6 @@ func TestHandleInboxAnnounceLitePub(t *testing.T) {
}
RelayState.DelSubscriber(domain.Host)
RelayState.DelSubscriber("example.org")
RelayState.RedisClient.Del("relay:subscription:" + domain.Host).Result()
RelayState.RedisClient.Del("relay:subscription:example.org").Result()
RelayState.RedisClient.Del(context.TODO(), "relay:subscription:"+domain.Host).Result()
RelayState.RedisClient.Del(context.TODO(), "relay:subscription:example.org").Result()
}
19 changes: 10 additions & 9 deletions api/resolver.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package api

import (
"context"
"encoding/json"
"errors"
uuid "github.com/satori/go.uuid"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
"github.com/yukimochi/Activity-Relay/models"
"github.com/yukimochi/machinery-v1/v1/tasks"
Expand Down Expand Up @@ -87,15 +88,15 @@ func enqueueRelayActivity(inboxURL string, activityID string) {
}

func enqueueActivityForAll(sourceDomain string, body []byte) {
activityID := uuid.NewV4()
activityID := uuid.New()
remainCount := len(RelayState.SubscribersAndFollowers) - 1

if remainCount < 1 {
return
}

pushActivityScript := "redis.call('HSET',KEYS[1], 'body', ARGV[1], 'remain_count', ARGV[2]); redis.call('EXPIRE', KEYS[1], ARGV[3]);"
RelayState.RedisClient.Eval(pushActivityScript, []string{"relay:activity:" + activityID.String()}, body, remainCount, 2*60).Result()
RelayState.RedisClient.Eval(context.TODO(), pushActivityScript, []string{"relay:activity:" + activityID.String()}, body, remainCount, 2*60).Result()

for _, subscription := range RelayState.SubscribersAndFollowers {
if sourceDomain == subscription.Domain {
Expand All @@ -106,7 +107,7 @@ func enqueueActivityForAll(sourceDomain string, body []byte) {
}

func enqueueActivityForSubscriber(sourceDomain string, body []byte) {
activityID := uuid.NewV4()
activityID := uuid.New()
remainCount := len(RelayState.Subscribers)
if contains(RelayState.Subscribers, sourceDomain) {
remainCount = remainCount - 1
Expand All @@ -116,7 +117,7 @@ func enqueueActivityForSubscriber(sourceDomain string, body []byte) {
}

pushActivityScript := "redis.call('HSET',KEYS[1], 'body', ARGV[1], 'remain_count', ARGV[2]); redis.call('EXPIRE', KEYS[1], ARGV[3]);"
RelayState.RedisClient.Eval(pushActivityScript, []string{"relay:activity:" + activityID.String()}, body, remainCount, 2*60).Result()
RelayState.RedisClient.Eval(context.TODO(), pushActivityScript, []string{"relay:activity:" + activityID.String()}, body, remainCount, 2*60).Result()

for _, subscription := range RelayState.Subscribers {
if sourceDomain == subscription.Domain {
Expand All @@ -127,7 +128,7 @@ func enqueueActivityForSubscriber(sourceDomain string, body []byte) {
}

func enqueueActivityForFollower(sourceDomain string, body []byte) {
activityID := uuid.NewV4()
activityID := uuid.New()
remainCount := len(RelayState.Followers)
if contains(RelayState.Followers, sourceDomain) {
remainCount = remainCount - 1
Expand All @@ -137,7 +138,7 @@ func enqueueActivityForFollower(sourceDomain string, body []byte) {
}

pushActivityScript := "redis.call('HSET',KEYS[1], 'body', ARGV[1], 'remain_count', ARGV[2]); redis.call('EXPIRE', KEYS[1], ARGV[3]);"
RelayState.RedisClient.Eval(pushActivityScript, []string{"relay:activity:" + activityID.String()}, body, remainCount, 2*60).Result()
RelayState.RedisClient.Eval(context.TODO(), pushActivityScript, []string{"relay:activity:" + activityID.String()}, body, remainCount, 2*60).Result()

for _, subscription := range RelayState.Followers {
if sourceDomain == subscription.Domain {
Expand Down Expand Up @@ -220,7 +221,7 @@ func executeFollowing(activity *models.Activity, actor *models.Actor) error {
switch {
case contains(activity.Object, "https://www.w3.org/ns/activitystreams#Public"):
if RelayState.RelayConfig.ManuallyAccept {
RelayState.RedisClient.HMSet("relay:pending:"+actorID.Host, map[string]interface{}{
RelayState.RedisClient.HMSet(context.TODO(), "relay:pending:"+actorID.Host, map[string]interface{}{
"inbox_url": actor.Endpoints.SharedInbox,
"activity_id": activity.ID,
"type": "Follow",
Expand All @@ -243,7 +244,7 @@ func executeFollowing(activity *models.Activity, actor *models.Actor) error {
case contains(activity.Object, RelayActor.ID):
if isActorAbleToBeFollower(actorID) {
if RelayState.RelayConfig.ManuallyAccept {
RelayState.RedisClient.HMSet("relay:pending:"+actorID.Host, map[string]interface{}{
RelayState.RedisClient.HMSet(context.TODO(), "relay:pending:"+actorID.Host, map[string]interface{}{
"inbox_url": actor.Endpoints.SharedInbox,
"activity_id": activity.ID,
"type": "Follow",
Expand Down
13 changes: 7 additions & 6 deletions control/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package control

import (
"bytes"
"context"
"io"
"os"
"strings"
"testing"
)

func TestPersonOnly(t *testing.T) {
RelayState.RedisClient.FlushAll().Result()
RelayState.RedisClient.FlushAll(context.TODO()).Result()

app := configCmdInit()

Expand All @@ -29,7 +30,7 @@ func TestPersonOnly(t *testing.T) {
}

func TestManuallyAccept(t *testing.T) {
RelayState.RedisClient.FlushAll().Result()
RelayState.RedisClient.FlushAll(context.TODO()).Result()

app := configCmdInit()

Expand All @@ -49,7 +50,7 @@ func TestManuallyAccept(t *testing.T) {
}

func TestInvalidConfig(t *testing.T) {
RelayState.RedisClient.FlushAll().Result()
RelayState.RedisClient.FlushAll(context.TODO()).Result()

app := configCmdInit()
buffer := new(bytes.Buffer)
Expand All @@ -65,7 +66,7 @@ func TestInvalidConfig(t *testing.T) {
}

func TestListConfig(t *testing.T) {
RelayState.RedisClient.FlushAll().Result()
RelayState.RedisClient.FlushAll(context.TODO()).Result()

app := configCmdInit()
buffer := new(bytes.Buffer)
Expand Down Expand Up @@ -94,7 +95,7 @@ func TestListConfig(t *testing.T) {
}

func TestExportConfig(t *testing.T) {
RelayState.RedisClient.FlushAll().Result()
RelayState.RedisClient.FlushAll(context.TODO()).Result()

app := configCmdInit()
buffer := new(bytes.Buffer)
Expand All @@ -115,7 +116,7 @@ func TestExportConfig(t *testing.T) {
}

func TestImportConfig(t *testing.T) {
RelayState.RedisClient.FlushAll().Result()
RelayState.RedisClient.FlushAll(context.TODO()).Result()

app := configCmdInit()
file, err := os.Open("../misc/test/exampleConfig.json")
Expand Down
3 changes: 2 additions & 1 deletion control/control_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package control

import (
"context"
"fmt"
"os"
"testing"
Expand Down Expand Up @@ -34,7 +35,7 @@ func TestMain(m *testing.M) {
os.Exit(1)
}
RelayState = models.NewState(GlobalConfig.RedisClient(), false)
RelayState.RedisClient.FlushAll().Result()
RelayState.RedisClient.FlushAll(context.TODO()).Result()

InitProxy = emptyProxy
InitProxyE = emptyProxyE
Expand Down
Loading

0 comments on commit 2ed9bae

Please sign in to comment.