Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow filtering audit log by type #296

Merged
merged 2 commits into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions api/moderation.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ func (m *ModerationServiceHandler) ListAuditEvents(ctx context.Context, req *con
out, err := m.store.ListAuditEvents(ctx, store.ListAuditEventsOpts{
FilterSubjectDID: req.Msg.FilterSubjectDid,
FilterCreatedBefore: filterCreatedBefore,
FilterTypes: req.Msg.FilterTypes,
})
if err != nil {
return nil, fmt.Errorf("listing audit events: %w", err)
Expand Down
668 changes: 379 additions & 289 deletions proto/bff/v1/moderation_service.pb.go

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions proto/bff/v1/moderation_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,23 @@ message HoldBackPendingActorAuditPayload {
google.protobuf.Timestamp held_until = 1;
}

enum AuditEventType {
COMMENT = 0;
APPROVED = 1;
REJECTED = 2;
HELD_BACK = 3;
FORCE_APPROVED = 4;
UNAPPROVED = 5;
TRACKED = 6;
BANNED = 7;
ASSIGNED_ROLES = 8;
}

message ListAuditEventsRequest {
string filter_actor_did = 1;
string filter_subject_did = 2;
string filter_subject_record_uri = 3;
repeated AuditEventType filter_types = 6;

// limit specifies how many audit events to return. If unspecific, this
// defaults to 100.
Expand Down
45 changes: 44 additions & 1 deletion store/gen/audit_events.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions store/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ func auditEventToProto(in gen.AuditEvent) (*v1.AuditEvent, error) {
type ListAuditEventsOpts struct {
FilterSubjectDID string
FilterCreatedBefore *time.Time
FilterTypes []v1.AuditEventType

// Limit defaults to 100.
Limit int32
Expand All @@ -717,6 +718,9 @@ func (s *PGXStore) ListAuditEvents(ctx context.Context, opts ListAuditEventsOpts
Valid: true,
}
}
for _, typ := range opts.FilterTypes {
queryParams.Types = append(queryParams.Types, typ.String())
}

data, err := s.queries.ListAuditEvents(ctx, queryParams)
if err != nil {
Expand Down
41 changes: 41 additions & 0 deletions store/queries/audit_events.sql
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,47 @@ WHERE
OR ae.subject_record_uri = sqlc.arg(subject_record_uri)
)
AND (sqlc.arg(created_before)::timestamptz IS NULL OR ae.created_at < sqlc.arg(created_before))
AND (
coalesce(cardinality(sqlc.arg(types)::text []), 0) = 0
OR (
'COMMENT' = any(sqlc.arg(types))
AND payload ->> '@type' = 'type.googleapis.com/bff.v1.CommentAuditPayload'
)
OR (
'APPROVED' = any(sqlc.arg(types))
AND payload ->> '@type' = 'type.googleapis.com/bff.v1.ProcessApprovalQueueAuditPayload'
AND payload ->> 'action' = 'APPROVAL_QUEUE_ACTION_APPROVE'
)
OR (
'REJECTED' = any(sqlc.arg(types))
AND payload ->> '@type' = 'type.googleapis.com/bff.v1.ProcessApprovalQueueAuditPayload'
AND payload ->> 'action' = 'APPROVAL_QUEUE_ACTION_REJECT'
)
OR (
'HELD_BACK' = any(sqlc.arg(types))
AND payload ->> '@type' = 'type.googleapis.com/bff.v1.HoldBackPendingActorAuditPayload'
)
OR (
'FORCE_APPROVED' = any(sqlc.arg(types))
AND payload ->> '@type' = 'type.googleapis.com/bff.v1.ForceApproveActorAuditPayload'
)
OR (
'UNAPPROVED' = any(sqlc.arg(types))
AND payload ->> '@type' = 'type.googleapis.com/bff.v1.UnapproveActorAuditPayload'
)
OR (
'TRACKED' = any(sqlc.arg(types))
AND payload ->> '@type' = 'type.googleapis.com/bff.v1.CreateActorAuditPayload'
)
OR (
'BANNED' = any(sqlc.arg(types))
AND payload ->> '@type' = 'type.googleapis.com/bff.v1.BanActorAuditPayload'
)
OR (
'ASSIGNED_ROLES' = any(sqlc.arg(types))
AND payload ->> '@type' = 'type.googleapis.com/bff.v1.AssignRolesAuditPayload'
)
)
ORDER BY
ae.created_at DESC
LIMIT sqlc.arg(_limit);
Expand Down
3 changes: 2 additions & 1 deletion web/admin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@bufbuild/connect-web": "^0.11.0",
"@bufbuild/protobuf": "1.2.0",
"dompurify": "^3.0.5",
"marked": "^7.0.1"
"marked": "^7.0.1",
"vue-multiselect": "^3.1.0"
}
}
61 changes: 54 additions & 7 deletions web/admin/pages/audit-log.vue
Original file line number Diff line number Diff line change
@@ -1,23 +1,62 @@
<script setup lang="ts">
import Multiselect from "vue-multiselect";
import "vue-multiselect/dist/vue-multiselect.min.css";
import {
AuditEvent,
AuditEventType,
} from "../../proto/bff/v1/moderation_service_pb";

const api = await useAPI();

const types = ref<Array<{ key: AuditEventType; value: string }>>([]);

const error = ref<string>();

const { auditEvents } = await api.listAuditEvents({}).catch((err) => {
error.value = err.rawMessage;
const auditEvents: Ref<AuditEvent[]> = ref([]);

async function fetchAuditEvents() {
const resp = await api
.listAuditEvents({
filterTypes: types.value.map((a) => a.key),
})
.catch((err) => {
error.value = err.rawMessage;

return {
auditEvents: [],
};
});

return {
auditEvents: [],
};
});
auditEvents.value = resp.auditEvents;
}

await fetchAuditEvents();

watch(types, () => fetchAuditEvents());
</script>

<template>
<div>
<shared-card v-if="error" variant="error">{{ error }}</shared-card>
<div v-else>
<h1 class="text-xl font-bold">Audit log</h1>
<p class="text-muted">Showing the last 100 audit events.</p>
<p class="text-muted mb-1">Showing the last 100 audit events.</p>
<div class="mb-2">
<div class="flex flex-col gap-0.5 max-w-[400px]">
<label for="filter-types" class="font-bold">Types</label>
<Multiselect
v-model="types"
name="filter-types"
multiple
track-by="key"
:options="Object.entries(AuditEventType).filter(t => isNaN(t[0] as any)).map(([label, key]) => ({key, label}))"
:custom-label="
(option: any) => option.label.toLowerCase().replace(/^\w/, (a:string) => a.toUpperCase()).replace(/_/g, ' ')
"
placeholder="Select type"
/>
</div>
</div>
<action
v-for="event in auditEvents"
:key="event.id"
Expand All @@ -27,3 +66,11 @@ const { auditEvents } = await api.listAuditEvents({}).catch((err) => {
</div>
</div>
</template>

<style>
.multiselect__tag,
.multiselect__option--highlight,
.multiselect__option--highlight::after {
@apply bg-blue-500;
}
</style>
18 changes: 16 additions & 2 deletions web/admin/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,32 @@ const actorProfilesMap = computed(() => {
return map;
});

const avatarStockMap = ref(new Map<string, boolean>());

watch(actorProfiles, async () => {
for (const profile of actorProfiles.value) {
const { isStockAvatar } = await fetch(
`https://bsky-cdn.codingpa.ws/avatar/${profile.did}/info.json`
).then((r) => r.json());
avatarStockMap.value.set(profile.did, isStockAvatar);
}
});

const queues = computed(() => ({
All: actors.value,
"Probably furry": actors.value.filter((actor) => {
const profile = didToProfile(actor.did);

return isProbablyFurry(profile);
return isProbablyFurry(profile) && !avatarStockMap.value.get(actor.did);
}),
"Empty profiles": actors.value.filter((actor) => {
const profile = didToProfile(actor.did);
if (!profile) return false;

return !profile.displayName && !profile.description && !profile.postsCount;
return (
(!profile.displayName && !profile.description && !profile.postsCount) ||
avatarStockMap.value.get(actor.did)
);
}),
"Held back": heldBack.value,
}));
Expand Down
Loading
Loading