Skip to content

Commit a305245

Browse files
committed
docs: update java docs
1 parent 9f89e33 commit a305245

62 files changed

Lines changed: 1242 additions & 134 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,71 @@
11
package io.github.drednote.telegram.core.annotation;
22

3+
import io.github.drednote.telegram.response.ForbiddenTelegramResponse;
4+
import io.github.drednote.telegram.response.GenericTelegramResponse;
35
import java.lang.annotation.Documented;
46
import java.lang.annotation.ElementType;
57
import java.lang.annotation.Retention;
68
import java.lang.annotation.RetentionPolicy;
79
import java.lang.annotation.Target;
810

11+
/**
12+
* Annotation for specifying required roles for a Telegram handler method.
13+
* <p>
14+
* The method annotated with {@code @HasRole} will be checked during request processing, and access will be granted only
15+
* if the current user has the required roles according to the specified matching strategy.
16+
* </p>
17+
*
18+
* <p>Supported matching strategies:
19+
* <ul>
20+
* <li>{@link StrategyMatching#INTERSECTION} – at least one required role must be present.</li>
21+
* <li>{@link StrategyMatching#COMPLETE_MATCH} – all specified roles must be present.</li>
22+
* </ul>
23+
* </p>
24+
* <p>
25+
* If validation fails, the request is blocked and a response is added, either a {@link ForbiddenTelegramResponse} or a
26+
* {@link GenericTelegramResponse} based on the annotation's {@code description} field.
27+
*
28+
* @author Ivan Galushko
29+
*/
930
@Target({ElementType.METHOD})
1031
@Retention(RetentionPolicy.RUNTIME)
1132
@Documented
1233
public @interface HasRole {
1334

14-
String[] value();
35+
/**
36+
* One or more required roles that the current user must have.
37+
*
38+
* @return the array of role names
39+
*/
40+
String[] value();
1541

16-
String description() default "";
42+
/**
43+
* Optional description that will be sent to the user in case of access denial.
44+
*
45+
* @return a textual description or message
46+
*/
47+
String description() default "";
1748

18-
StrategyMatching strategyMatching() default StrategyMatching.INTERSECTION;
49+
/**
50+
* Strategy used to match the user's roles against the required ones. Defaults to
51+
* {@link StrategyMatching#INTERSECTION}.
52+
*
53+
* @return the matching strategy
54+
*/
55+
StrategyMatching strategyMatching() default StrategyMatching.INTERSECTION;
1956

20-
enum StrategyMatching {
21-
INTERSECTION, COMPLETE_MATCH
22-
}
57+
/**
58+
* Role matching strategy for evaluating the user's permissions.
59+
*/
60+
enum StrategyMatching {
61+
/**
62+
* Requires at least one of the specified roles to be present.
63+
*/
64+
INTERSECTION,
65+
66+
/**
67+
* Requires all specified roles to be present.
68+
*/
69+
COMPLETE_MATCH
70+
}
2371
}

src/main/java/io/github/drednote/telegram/core/request/DefaultTelegramRequest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import lombok.Data;
77
import lombok.NoArgsConstructor;
88

9+
/**
10+
* Default implementation.
11+
*/
912
@Data
1013
public class DefaultTelegramRequest implements TelegramRequest {
1114

src/main/java/io/github/drednote/telegram/datasource/kryo/KryoSerializationService.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,34 @@
22

33
import java.io.IOException;
44

5+
/**
6+
* Generic interface for serializing and deserializing objects using Kryo.
7+
* <p>
8+
* Implementations of this interface are expected to provide Kryo-based binary serialization for a specific type
9+
* {@code T}.
10+
* </p>
11+
*
12+
* <p>Serialization errors during encoding are propagated as {@link IOException}.</p>
13+
*
14+
* @param <T> the type of object to serialize and deserialize
15+
* @author Ivan Galushko
16+
*/
517
public interface KryoSerializationService<T> {
618

19+
/**
20+
* Serializes the given object into a byte array using Kryo.
21+
*
22+
* @param context the object to serialize
23+
* @return the serialized byte array
24+
* @throws IOException if serialization fails
25+
*/
726
byte[] serialize(T context) throws IOException;
827

28+
/**
29+
* Deserializes the given byte array into an object of type {@code T} using Kryo.
30+
*
31+
* @param bytes the byte array to deserialize
32+
* @return the deserialized object
33+
*/
934
T deserialize(byte[] bytes);
1035
}
Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,62 @@
11
package io.github.drednote.telegram.datasource.permission;
22

3+
import io.github.drednote.telegram.filter.pre.PreUpdateFilter;
34
import java.util.Set;
45
import org.springframework.lang.Nullable;
56

7+
/**
8+
* Represents a permission model that contains a unique identifier and a set of roles.
9+
* <p>
10+
* This abstraction is typically used to describe the roles associated with a specific user or context in the request
11+
* pipeline.
12+
* </p>
13+
*
14+
* <p>Implementations may vary, but the typical usage includes associating permissions
15+
* with access control filters such as {@link PreUpdateFilter}.</p>
16+
*
17+
* @author Ivan Galushko
18+
*/
619
public interface Permission {
720

8-
Long getId();
21+
/**
22+
* Returns the unique identifier of the permission object, typically associated with a user or session.
23+
*
24+
* @return the permission ID
25+
*/
26+
Long getId();
927

10-
@Nullable
11-
Set<String> getRoles();
28+
/**
29+
* Returns the set of roles associated with this permission.
30+
* <p>
31+
* Can return {@code null} if no roles are assigned.
32+
* </p>
33+
*
34+
* @return a set of role names or {@code null}
35+
*/
36+
@Nullable
37+
Set<String> getRoles();
1238

13-
record DefaultPermission(
14-
Long id, Set<String> roles
15-
) implements Permission {
39+
/**
40+
* Default implementation of {@link Permission} as an immutable Java record.
41+
* <p>
42+
* Provides a simple container for permission data with {@code id} and {@code roles}.
43+
* </p>
44+
*
45+
* @param id the permission identifier
46+
* @param roles the associated roles
47+
*/
48+
record DefaultPermission(
49+
Long id, Set<String> roles
50+
) implements Permission {
1651

17-
@Override
18-
public Long getId() {
19-
return id;
20-
}
52+
@Override
53+
public Long getId() {
54+
return id;
55+
}
2156

22-
@Override
23-
public Set<String> getRoles() {
24-
return roles;
57+
@Override
58+
public Set<String> getRoles() {
59+
return roles;
60+
}
2561
}
26-
}
2762
}

src/main/java/io/github/drednote/telegram/datasource/permission/PermissionRepositoryAdapter.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,23 @@
33
import io.github.drednote.telegram.datasource.DataSourceAdapter;
44
import org.springframework.lang.Nullable;
55

6+
/**
7+
* Adapter interface for retrieving permission information from an external data source.
8+
* <p>
9+
* Used to fetch {@link Permission} objects for a given {@code chatId}, typically during request processing to determine
10+
* user access rights.
11+
* </p>
12+
*
13+
* @author Ivan Galushko
14+
*/
615
public interface PermissionRepositoryAdapter extends DataSourceAdapter {
716

8-
@Nullable
9-
Permission findPermission(Long chatId);
17+
/**
18+
* Retrieves the {@link Permission} associated with the given chat ID.
19+
*
20+
* @param chatId the Telegram chat ID of the user
21+
* @return the corresponding {@link Permission}, or {@code null} if none found
22+
*/
23+
@Nullable
24+
Permission findPermission(Long chatId);
1025
}

src/main/java/io/github/drednote/telegram/datasource/scenarioid/ScenarioId.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,41 @@
11
package io.github.drednote.telegram.datasource.scenarioid;
22

3+
/**
4+
* Provides a link between the user and the currently active scenario.
5+
* <p>
6+
* Used to determine which {@code scenarioId} is currently assigned to the user (or other entity), identified by
7+
* {@code id}. At the same time, {@code scenarioId} can change during the interaction, reflecting a change in context,
8+
* stage or business flow.
9+
*
10+
* <p>For example, if the user goes through a sequence of steps (registration scenario, order placement, etc.),
11+
* then this entity allows you to understand which scenarios he is currently in.</p>
12+
*
13+
* <p>As a rule, they are used by storages and routing logic in Telegram bots.</p>
14+
*
15+
* @author Ivan Galushko
16+
*/
317
public interface ScenarioId {
418

19+
/**
20+
* Returns a unique identifier for the user or other entity that the scenario is attached to.
21+
*
22+
* @return a unique identifier (e.g. chatId)
23+
*/
524
String getId();
625

26+
/**
27+
* Returns the identifier of the scenario associated with the entity.
28+
*
29+
* @return the scenario ID
30+
*/
731
String getScenarioId();
832

9-
// void setScenarioId(String scenarioId);
10-
33+
/**
34+
* Default immutable implementation of {@link ScenarioId} using Java record.
35+
*
36+
* @param id the entity identifier (e.g. chatId)
37+
* @param scenarioId the scenario identifier
38+
*/
1139
record DefaultScenarioId(String id, String scenarioId) implements ScenarioId {
1240

1341
@Override

src/main/java/io/github/drednote/telegram/datasource/session/AbstractUpdateInboxRepositoryAdapter.java

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import io.github.drednote.telegram.core.request.ParsedUpdateRequest;
44
import io.github.drednote.telegram.session.SessionProperties;
55
import io.github.drednote.telegram.utils.Assert;
6-
import java.lang.reflect.InvocationTargetException;
76
import java.time.Instant;
87
import java.time.temporal.ChronoUnit;
98
import java.util.List;
@@ -13,6 +12,26 @@
1312
import org.springframework.dao.DataIntegrityViolationException;
1413
import org.telegram.telegrambots.meta.api.objects.Update;
1514

15+
/**
16+
* Abstract base class for implementing {@link UpdateInboxRepositoryAdapter} backed by a repository.
17+
* <p>
18+
* Handles core logic for persisting, retrieving, and updating {@link UpdateInbox} entities. The strategy for selecting
19+
* and managing inbox records is delegated to abstract methods.
20+
* </p>
21+
*
22+
* <p>This class expects subclasses to define how to:
23+
* <ul>
24+
* <li>Find idle entities for timeout processing</li>
25+
* <li>Retrieve the next update based on concurrency limits</li>
26+
* <li>Persist a single entity to the underlying storage</li>
27+
* </ul>
28+
* </p>
29+
*
30+
* <p>Entity creation is handled reflectively via {@link Class#getDeclaredConstructor(Class[])}}.</p>
31+
*
32+
* @param <T> the inbox entity type
33+
* @author Ivan Galushko
34+
*/
1635
public abstract class AbstractUpdateInboxRepositoryAdapter<T extends UpdateInbox>
1736
implements UpdateInboxRepositoryAdapter<T> {
1837

@@ -22,6 +41,13 @@ public abstract class AbstractUpdateInboxRepositoryAdapter<T extends UpdateInbox
2241
private final UpdateInboxRepository<T> repository;
2342
private final Class<T> clazz;
2443

44+
/**
45+
* Constructs the adapter with required dependencies.
46+
*
47+
* @param properties configuration for session control and threading
48+
* @param repository the inbox repository implementation
49+
* @param clazz the class type of the inbox entity
50+
*/
2551
protected AbstractUpdateInboxRepositoryAdapter(
2652
SessionProperties properties, UpdateInboxRepository<T> repository, Class<T> clazz
2753
) {
@@ -34,12 +60,35 @@ protected AbstractUpdateInboxRepositoryAdapter(
3460
this.properties = properties;
3561
}
3662

63+
/**
64+
* Must return a list of entities that have been in {@link UpdateInboxStatus#IN_PROGRESS} state longer than the
65+
* configured threshold.
66+
*
67+
* @param date cutoff instant for detecting idle entries
68+
* @return list of idle entities
69+
*/
3770
protected abstract List<T> getIdleEntities(Instant date);
3871

72+
/**
73+
* Retrieves the next update respecting the max threads per user limit.
74+
*
75+
* @param maxThreadsPerUser maximum number of simultaneous updates per user
76+
* @return optional inbox entity to process
77+
*/
3978
protected abstract Optional<T> findWithMaxThreadsPerUser(int maxThreadsPerUser);
4079

80+
/**
81+
* Retrieves the next available update without thread restrictions.
82+
*
83+
* @return optional inbox entity
84+
*/
4185
protected abstract Optional<T> findNextWithoutLimit();
4286

87+
/**
88+
* Persists the given inbox entity to the repository.
89+
*
90+
* @param entity the inbox entity to save
91+
*/
4392
protected abstract void persist(T entity);
4493

4594
@Override
@@ -57,8 +106,7 @@ public void persist(List<Update> updates) {
57106
log.warn("Cannot persist update inbox with id '{}'. Cause: {}",
58107
update.getUpdateId(), e.getMessage());
59108
log.debug(e.getMessage(), e);
60-
} catch (InvocationTargetException | InstantiationException | IllegalAccessException |
61-
NoSuchMethodException e) {
109+
} catch (Exception e) {
62110
log.error("Cannot persist update inbox with id '{}'. Cause: ", update.getUpdateId(), e);
63111
}
64112
}

0 commit comments

Comments
 (0)