bluesky 1.0.0-preview.8 bluesky: ^1.0.0-preview.8 copied to clipboard
The most famous and powerful Dart/Flutter library for Bluesky Social.
// Copyright 2023 Shinya Kato. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided the conditions.
import 'package:bluesky/atproto.dart';
import 'package:bluesky/bluesky.dart';
import 'package:bluesky/bluesky_chat.dart';
import 'package:bluesky/chat_bsky_convo_defs.dart';
import 'package:bluesky/com_atproto_server_create_session.dart';
import 'package:bluesky/core.dart';
import 'package:bluesky/moderation.dart';
/// https://atprotodart.com/docs/packages/bluesky
Future<void> main() async {
try {
//! First you need to establish session with ATP server.
final session = await _session;
final bsky = Bluesky.fromSession(
session,
//! The default is `bsky.social`, or resolve dynamically based on session
service: 'SERVICE_NAME',
//! The default is `bsky.network`
relayService: 'STREAM_SERVICE_NAME',
//! Automatic retry is available when server error or network error occurs
//! when communicating with the API.
retryConfig: RetryConfig(
maxAttempts: 5,
jitter: Jitter(
minInSeconds: 2,
maxInSeconds: 5,
),
onExecute: (event) => print(
'Retry after ${event.intervalInSeconds} seconds...'
'[${event.retryCount} times]',
),
),
//! The default timeout is 30 seconds.
timeout: Duration(seconds: 20),
);
//! Chat features
final chat = BlueskyChat.fromSession(session);
final convos = await chat.convo.listConvos();
for (final convo in convos.data.convos) {
await chat.convo.sendMessage(
convoId: convo.id,
message: MessageInput(text: 'Hello?'),
);
}
//! Moderation Stuffs
final preferences = await bsky.actor.getPreferences();
final moderationPrefs = preferences.data.getModerationPrefs();
final labelDefs = await bsky.labeler.getLabelDefinitions(moderationPrefs);
final moderationOpts = ModerationOpts(
userDid: bsky.session!.did,
prefs: moderationPrefs,
labelDefs: labelDefs,
);
//! Let's get home timeline!
final feeds = await bsky.feed.getTimeline(
$headers: getLabelerHeaders(moderationPrefs),
);
for (final feed in feeds.data.feed) {
final decision = moderatePost(
ModerationSubjectPost.postView(data: feed.post),
moderationOpts,
);
if (decision.getUI(ModerationBehaviorContext.contentView).alert) {
// Alert!
}
}
print(feeds);
//! Let's post cool stuff!
final record = await bsky.feed.post.create(text: 'Hello, Bluesky!');
//! And delete it.
await bsky.feed.post.delete(rkey: record.data.uri.rkey);
//! You can use Stream API easily.
final subscription = await bsky.atproto.sync.subscribeRepos();
subscription.data.stream.listen((event) {
event.when(
//! You can handle commit events very easily
//! with RepoCommitAdaptor.
commit: RepoCommitAdaptor(
//! Create events.
onCreatePost: (data) => data.record,
onCreateLike: print,
//! Update events.
onUpdateProfile: print,
//! Delete events.
onDeletePost: print,
).execute,
identity: print,
account: print,
info: print,
unknown: print,
);
});
} on UnauthorizedException catch (e) {
print(e);
} on XRPCException catch (e) {
print(e);
}
}
Future<Session> get _session async {
final session = await createSession(
$service: 'SERVICE_NAME', //! The default is `bsky.social`
identifier: 'YOUR_HANDLE_OR_EMAIL', //! Like `shinyakato.bsky.social`
password: 'YOUR_PASSWORD',
);
return session.data.toSession();
}