From b053d1b99a6674da1bdc4ea18784dceea652391d Mon Sep 17 00:00:00 2001 From: Farhan Khan Date: Wed, 29 Sep 2021 02:47:10 +0000 Subject: [PATCH] Adding package --- shared/go.mod | 3 ++ shared/headers.go | 100 +++++++++++++++++++++++++++++++++++++++++++ shared/uniquefifo.go | 62 +++++++++++++++++++++++++++ 3 files changed, 165 insertions(+) create mode 100644 shared/go.mod create mode 100644 shared/headers.go create mode 100644 shared/uniquefifo.go diff --git a/shared/go.mod b/shared/go.mod new file mode 100644 index 0000000..90a6fe9 --- /dev/null +++ b/shared/go.mod @@ -0,0 +1,3 @@ +module gitlab.com/khanzf/fedilogue/shared + +go 1.16 diff --git a/shared/headers.go b/shared/headers.go new file mode 100644 index 0000000..6e8ab61 --- /dev/null +++ b/shared/headers.go @@ -0,0 +1,100 @@ +package shared + +import ( + "net/http" +) + +const ( + NEW_INSTANCE = 0 + RUNNING = 200 + UNAUTHORIZED = 401 + FORBIDDEN = 403 + NOT_FOUND = 404 + UNPROCESSABLE_ENTITY = 422 + TOOMANYREQUESTS = 429 + INTERNAL_ERROR = 500 + CLIENT_ISSUE = 600 + ONION_PROTOCOL = 601 + BAD_RESPONSE = 602 + BAD_NODEINFO = 604 + UNSUPPORTED_INSTANCE = 605 + STREAM_ENDED = 606 + KEEPALIVE = 607 +) + +type ObjectType struct { + Id string `json:"id"` +} + +// Parsing Unmarshal JSON type +type ReportActivity struct { + + // Retrieved values + Id string `json:"id"` + Uri string `json:"uri"` + Account AccountType + Content string `json:"content"` + Created_at string `json:"created_at"` + + // Derived values + normalized string +} + +type AccountType struct { + Acct string `json:"acct"` + Avatar string `json:"avatar"` + Bot bool `json:"bot"` + Created_at string `json:"created_at"` + Display_name string `json:"display_name"` + Url string `json:"url"` +} + +// Instance's new min_id value +type RunningInstance struct { + Software string `json:"software"` + Version string `json:"version"` + Status int `json:"status"` + LastRun string `json:"lastrun"` + CaptureType string `json:"capturetype"` + client http.Client + client_id string + client_secret string + recentactivities *UniqueFifo + recentactors *UniqueFifo +} + +type NodeInfoSoftware struct { + Name string `json:"name"` + Version string `json:"version"` +} + +type NodeInfo struct { + Software NodeInfoSoftware `json:"software"` +} + +type CommandMap struct { + Type string `json:"Type"` + Endpoint string `json:"Endpoint"` +} + +type ResponseBack struct { + Type string `json:"Type"` + Message string `json:"Message"` + RunningInstances map[string]RunningInstance `json:"RunningInstances"` +} + +type Userinfo struct { + Id string `"json:id"` + Type string `"json:type"` + Following string `"json:following"` + Followers string `"json:followers"` + Inbox string `"json:inbox"` + Outbox string `"json:outbox"` + Featured string `"json:featured"` + PreferredUsername string `"json:preferredUsername"` + Name string `"json:name"` + Summary string `"json:summary"` + Url string `"json:Url"` + ManuallyApprovesFollowers string `"json:manuallyApprovesFollowers"` + Discoverable string `"json:discoverable"` +} diff --git a/shared/uniquefifo.go b/shared/uniquefifo.go new file mode 100644 index 0000000..74a00e2 --- /dev/null +++ b/shared/uniquefifo.go @@ -0,0 +1,62 @@ +package shared + +import ( + "sync" +) + +type UniqueFifo struct { + slice []string + Mu sync.Mutex + size int +} + + +func NewUniqueFifo(size int) *UniqueFifo { + q := UniqueFifo{} + q.slice = make([]string, 0) + q.size = size + return &q +} + +func (q *UniqueFifo) Add(v string) bool { + ret := false + if len(q.slice) == 0 { + q.slice = append(q.slice, v) + ret = false + } else { + i := q.Contains(v) + if i != -1 { + q.Remove(i) + ret = true + } else { + ret = false + } + q.slice = append(q.slice, "") + copy(q.slice[1:], q.slice) + q.slice[0] = v + if len(q.slice) <= q.size { + q.slice = q.slice[:len(q.slice)] + } else { + q.slice = q.slice[:q.size] + } + } + + return ret +} + +func (q *UniqueFifo) Remove(r int) { + f := q.slice[:r] + e := q.slice[r+1:] + q.slice = f + q.slice = append(q.slice, e...) +} + + +func (q *UniqueFifo) Contains(v string) int { + for i, val := range q.slice { + if val == v { + return i + } + } + return -1 +}