-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
49 changed files
with
1,277 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
**/.git* | ||
/README.md | ||
/Dockerfile | ||
/.dockerignore | ||
/migrations/add.sh | ||
/migrations/migrations.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
name: image | ||
|
||
on: | ||
workflow_run: | ||
workflows: [build] | ||
types: [completed] | ||
workflow_dispatch: | ||
|
||
jobs: | ||
image: | ||
runs-on: ubuntu-latest | ||
if: ${{ github.event.workflow_run.conclusion == 'success' }} | ||
steps: | ||
- uses: docker/login-action@v3 | ||
with: | ||
registry: ghcr.io | ||
username: ${{ github.actor }} | ||
password: ${{ secrets.GITHUB_TOKEN }} | ||
- uses: docker/build-push-action@v5 | ||
with: | ||
pull: true | ||
push: true | ||
tags: ghcr.io/${{ github.repository }}:latest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Copyright 2023 Dima Krasner | ||
# | ||
# Licensed 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. | ||
|
||
FROM golang:1.21-alpine AS build | ||
RUN apk add --no-cache gcc musl-dev openssl | ||
COPY go.mod /src/ | ||
COPY go.sum /src/ | ||
WORKDIR /src | ||
RUN go mod download | ||
COPY migrations /src/migrations | ||
RUN go generate ./migrations | ||
COPY . /src | ||
RUN go vet ./... | ||
RUN go test ./... -failfast -vet off | ||
RUN go build ./cmd/tootik | ||
|
||
FROM alpine | ||
RUN apk add --no-cache ca-certificates openssl | ||
COPY --from=build /src/tootik / | ||
COPY --from=build /src/LICENSE / | ||
RUN adduser -D tootik | ||
USER tootik | ||
WORKDIR /tmp | ||
ENTRYPOINT ["/tootik"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
Copyright 2023 Dima Krasner | ||
Licensed 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 fed | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/dimkr/tootik/ap" | ||
) | ||
|
||
func Delete(ctx context.Context, db *sql.DB, note *ap.Object) error { | ||
delete, err := json.Marshal(ap.Activity{ | ||
Context: "https://www.w3.org/ns/activitystreams", | ||
ID: note.ID + "#delete", | ||
Type: ap.DeleteActivity, | ||
Actor: note.AttributedTo, | ||
Object: ap.Object{ | ||
Type: note.Type, | ||
ID: note.ID, | ||
}, | ||
To: note.To, | ||
CC: note.CC, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("Failed to marshal delete: %w", err) | ||
} | ||
|
||
tx, err := db.BeginTx(ctx, nil) | ||
if err != nil { | ||
return fmt.Errorf("Failed to begin transaction: %w", err) | ||
} | ||
defer tx.Rollback() | ||
|
||
// mark this post as sent so recipients who haven't received it yet don't receive it | ||
if _, err := tx.ExecContext( | ||
ctx, | ||
`UPDATE outbox SET sent = 1 WHERE activity->>'object.id' = ? and activity->>'type' = 'Create'`, | ||
note.ID, | ||
); err != nil { | ||
return fmt.Errorf("Failed to insert delete activity: %w", err) | ||
} | ||
|
||
if _, err := tx.ExecContext( | ||
ctx, | ||
`DELETE FROM notes WHERE id = ?`, | ||
note.ID, | ||
); err != nil { | ||
return fmt.Errorf("Failed to delete note: %w", err) | ||
} | ||
|
||
if _, err := tx.ExecContext( | ||
ctx, | ||
`DELETE FROM outbox WHERE activity->>'object.id' = ?`, | ||
note.ID, | ||
); err != nil { | ||
return fmt.Errorf("Failed to delete activities: %w", err) | ||
} | ||
|
||
if _, err := tx.ExecContext( | ||
ctx, | ||
`INSERT INTO outbox (activity) VALUES (?)`, | ||
string(delete), | ||
); err != nil { | ||
return fmt.Errorf("Failed to insert delete activity: %w", err) | ||
} | ||
|
||
if err := tx.Commit(); err != nil { | ||
return fmt.Errorf("Failed to delete note: %w", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
Copyright 2023 Dima Krasner | ||
Licensed 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 fed | ||
|
||
import ( | ||
"context" | ||
"crypto/sha256" | ||
"database/sql" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/dimkr/tootik/ap" | ||
"github.com/dimkr/tootik/cfg" | ||
"time" | ||
) | ||
|
||
func Edit(ctx context.Context, db *sql.DB, note *ap.Object, newContent string) error { | ||
now := time.Now() | ||
|
||
note.Content = newContent | ||
note.Updated = &now | ||
|
||
body, err := json.Marshal(note) | ||
if err != nil { | ||
return fmt.Errorf("Failed to marshal note: %w", err) | ||
} | ||
|
||
updateID := fmt.Sprintf("https://%s/update/%x", cfg.Domain, sha256.Sum256([]byte(fmt.Sprintf("%s|%d", note.ID, now.Unix())))) | ||
|
||
update, err := json.Marshal(ap.Activity{ | ||
Context: "https://www.w3.org/ns/activitystreams", | ||
ID: updateID, | ||
Type: ap.UpdateActivity, | ||
Actor: note.AttributedTo, | ||
Object: note, | ||
To: note.To, | ||
CC: note.CC, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("Failed to marshal update: %w", err) | ||
} | ||
|
||
tx, err := db.BeginTx(ctx, nil) | ||
if err != nil { | ||
return fmt.Errorf("Failed to begin transaction: %w", err) | ||
} | ||
defer tx.Rollback() | ||
|
||
if _, err := tx.ExecContext( | ||
ctx, | ||
`UPDATE notes SET object = ? WHERE id = ?`, | ||
string(body), | ||
note.ID, | ||
); err != nil { | ||
return fmt.Errorf("Failed to update note: %w", err) | ||
} | ||
|
||
if _, err := tx.ExecContext( | ||
ctx, | ||
`INSERT INTO outbox (activity) VALUES(?)`, | ||
string(update), | ||
); err != nil { | ||
return fmt.Errorf("Failed to insert update activity: %w", err) | ||
} | ||
|
||
if err := tx.Commit(); err != nil { | ||
return fmt.Errorf("Failed to update note: %w", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.