Skip to content

Commit

Permalink
Add support for Zulip 9
Browse files Browse the repository at this point in the history
Fixes #352
  • Loading branch information
jamesnetherton authored Aug 11, 2024
1 parent 1b0404c commit cddd633
Show file tree
Hide file tree
Showing 90 changed files with 5,101 additions and 3,064 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,13 @@ jobs:
done
./populatedb.sh
cd -
while [ "$(curl -k -L -s -o /dev/null -w "%{http_code}" http://localhost/login)" != "200" ]
do
sleep 5
done
echo "key=lDxDG5uoqwOhCdeA2d9iHvboTYAcOlVb" > zuliprc
echo "email=test@test.com" >> zuliprc
echo "site=https://localhost" >> zuliprc
echo "insecure=true" >> zuliprc
./createzuliprc.sh
- name: Build JDK ${{ matrix.java }}
run: |
./mvnw clean verify -ntp -B
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@
<releaseProfiles>release</releaseProfiles>
<autoVersionSubmodules>true</autoVersionSubmodules>
<preparationGoals>clean install</preparationGoals>
<arguments>-DskipTests -Dimpsort.skip</arguments>
<arguments>-DskipTests -Dformatter.skip -Dimpsort.skip</arguments>
</configuration>
</plugin>
<plugin>
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/github/jamesnetherton/zulip/client/Zulip.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.jamesnetherton.zulip.client.api.core.ZulipService;
import com.github.jamesnetherton.zulip.client.api.draft.DraftService;
import com.github.jamesnetherton.zulip.client.api.event.EventService;
import com.github.jamesnetherton.zulip.client.api.invitation.InvitationService;
import com.github.jamesnetherton.zulip.client.api.message.MessageService;
import com.github.jamesnetherton.zulip.client.api.server.ServerService;
import com.github.jamesnetherton.zulip.client.api.stream.StreamService;
Expand Down Expand Up @@ -72,6 +73,17 @@ public Zulip(ZulipConfiguration configuration) throws ZulipClientException {
this.client = factory.createZulipHttpClient(configuration);
}

/**
* Access the collection of channel Zulip APIs.
* <p>
* Since channels are analogous to streams. The {@link StreamService} is returned.
*
* @return The {@link StreamService} Zulip channel APIs
*/
public StreamService channels() {
return streams();
}

/**
* Access the collection of draft Zulip APIs.
*
Expand All @@ -90,6 +102,15 @@ public EventService events() {
return (EventService) services.computeIfAbsent(EventService.class, key -> new EventService(this.client));
}

/**
* Access the collection of invitation Zulip APIs.
*
* @return The {@link InvitationService} Zulip event APIs
*/
public InvitationService invitations() {
return (InvitationService) services.computeIfAbsent(InvitationService.class, key -> new InvitationService(this.client));
}

/**
* Access the collection of message Zulip APIs.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class EventPoller {
private final MessageEventListener listener;
private final ZulipHttpClient client;
private final Narrow[] narrows;
private volatile ExecutorService eventListenerExecutorService;
private volatile boolean userManagedEventListenerExecutorService = false;
private volatile EventQueue queue;
private volatile ExecutorService executor;
private volatile Status status = Status.STOPPED;
Expand All @@ -44,6 +46,25 @@ public EventPoller(ZulipHttpClient client, MessageEventListener listener, Narrow
this.narrows = narrows;
}

/**
* Constructs a {@link EventPoller}.
*
* @param client The Zulip HTTP client
* @param listener The {@link MessageEventListener} to be invoked on each message event
* @param narrows optional {@link Narrow} expressions to filter which message events are captured. E.g
* messages from a
* specific stream
* @param eventListenerExecutorService Custom {@link ExecutorService} to use for message event listener execution
*/
public EventPoller(ZulipHttpClient client, MessageEventListener listener, Narrow[] narrows,
ExecutorService eventListenerExecutorService) {
this.client = client;
this.listener = listener;
this.narrows = narrows;
this.eventListenerExecutorService = eventListenerExecutorService;
this.userManagedEventListenerExecutorService = true;
}

/**
* Starts event message polling.
*
Expand All @@ -60,6 +81,10 @@ public synchronized void start() throws ZulipClientException {
queue = createQueue.execute();
executor = Executors.newSingleThreadExecutor();

if (eventListenerExecutorService == null) {
eventListenerExecutorService = Executors.newCachedThreadPool();
}

executor.submit(new Runnable() {
private long lastEventId = queue.getLastEventId();

Expand All @@ -72,7 +97,7 @@ public void run() {

List<MessageEvent> messageEvents = getEvents.execute();
for (MessageEvent event : messageEvents) {
listener.onEvent(event.getMessage());
eventListenerExecutorService.submit(() -> listener.onEvent(event.getMessage()));
}

lastEventId = messageEvents.stream().max(Comparator.comparing(Event::getId))
Expand Down Expand Up @@ -111,6 +136,11 @@ public synchronized void stop() {
LOG.info("EventPoller stopping");
status = Status.STOPPING;
executor.shutdown();
if (userManagedEventListenerExecutorService) {
eventListenerExecutorService.shutdown();
eventListenerExecutorService = null;
}

DeleteEventQueueApiRequest deleteQueue = new DeleteEventQueueApiRequest(this.client, queue.getQueueId());
deleteQueue.execute();
} catch (ZulipClientException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.github.jamesnetherton.zulip.client.api.invitation;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.github.jamesnetherton.zulip.client.api.user.UserRole;
import java.time.Instant;

/**
* Defines a Zulip invitation.
*/
public class Invitation {
@JsonProperty
private long id;

@JsonProperty
private long invitedByUserId;

@JsonProperty
private Instant invited;

@JsonProperty
private Instant expiryDate;

@JsonProperty
private UserRole invitedAs;

@JsonProperty
private String email;

@JsonProperty
private boolean notifyReferrerOnJoin;

@JsonProperty
private String linkUrl;

@JsonProperty
private boolean isMultiuse;

public long getId() {
return id;
}

public long getInvitedByUserId() {
return invitedByUserId;
}

public Instant getInvited() {
return invited;
}

public Instant getExpiryDate() {
return expiryDate;
}

public UserRole getInvitedAs() {
return invitedAs;
}

public String getEmail() {
return email;
}

public boolean isNotifyReferrerOnJoin() {
return notifyReferrerOnJoin;
}

public String getLinkUrl() {
return linkUrl;
}

public boolean isMultiuse() {
return isMultiuse;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.github.jamesnetherton.zulip.client.api.invitation;

import com.github.jamesnetherton.zulip.client.api.core.ZulipService;
import com.github.jamesnetherton.zulip.client.api.invitation.request.CreateReusableInvitationLinkApiRequest;
import com.github.jamesnetherton.zulip.client.api.invitation.request.GetAllInvitationsApiRequest;
import com.github.jamesnetherton.zulip.client.api.invitation.request.ResendEmailInvitationApiRequest;
import com.github.jamesnetherton.zulip.client.api.invitation.request.RevokeEmailInvitationApiRequest;
import com.github.jamesnetherton.zulip.client.api.invitation.request.RevokeReusableInvitationApiRequest;
import com.github.jamesnetherton.zulip.client.api.invitation.request.SendInvitationsApiRequest;
import com.github.jamesnetherton.zulip.client.http.ZulipHttpClient;
import java.util.List;

/**
* Zulip invitation APIs.
*/
public class InvitationService implements ZulipService {

private final ZulipHttpClient client;

/**
* Constructs a {@link InvitationService}.
*
* @param client The Zulip HTTP client
*/
public InvitationService(ZulipHttpClient client) {
this.client = client;
}

/**
* Creates a new invitation link.
*
* @see <a href="https://zulip.com/api/create-invite-link">https://zulip.com/api/create-invite-link</a>
*
* @return The {@link CreateReusableInvitationLinkApiRequest} builder object
*/
public CreateReusableInvitationLinkApiRequest createReusableInvitationLink() {
return new CreateReusableInvitationLinkApiRequest(client);
}

/**
* Fetches all unexpired invitations.
*
* @see <a href="https://zulip.com/api/get-invites">https://zulip.com/api/get-invites</a>
*
* @return The {@link GetAllInvitationsApiRequest} builder object
*/
public GetAllInvitationsApiRequest getAllInvitations() {
return new GetAllInvitationsApiRequest(client);
}

/**
* Resends an email invitation.
*
* @see <a href="https://zulip.com/api/resend-email-invite">https://zulip.com/api/resend-email-invite</a>
*
* @param invitationId The id of the invitation to resend
* @return The {@link ResendEmailInvitationApiRequest} builder object
*/
public ResendEmailInvitationApiRequest resendEmailInvitation(long invitationId) {
return new ResendEmailInvitationApiRequest(client, invitationId);
}

/**
* Revokes an email invitation.
*
* @see <a href="https://zulip.com/api/revoke-email-invite">https://zulip.com/api/revoke-email-invite</a>
*
* @param invitationId The id of the invitation to revoke
* @return The {@link RevokeEmailInvitationApiRequest} builder object
*/
public RevokeEmailInvitationApiRequest revokeEmailInvitation(long invitationId) {
return new RevokeEmailInvitationApiRequest(client, invitationId);
}

/**
* Revokes a reusable invitation.
*
* @see <a href="https://zulip.com/api/revoke-invite-link">https://zulip.com/api/revoke-invite-link</a>
*
* @param invitationId The id of the invitation to revoke
* @return The {@link RevokeReusableInvitationApiRequest} builder object
*/
public RevokeReusableInvitationApiRequest revokeReusableInvitation(long invitationId) {
return new RevokeReusableInvitationApiRequest(client, invitationId);
}

/**
* Send invitations to specified email addresses.
*
* @see <a href="https://zulip.com/api/send-invites">https://zulip.com/api/send-invites</a>
*
* @param inviteeEmails The list of email addresses to invite
* @param streamIds The list of channel ids that the newly created user will be automatically subscribed to
* @return The {@link SendInvitationsApiRequest} builder object
*/
public SendInvitationsApiRequest sendInvitations(List<String> inviteeEmails, List<Long> streamIds) {
return new SendInvitationsApiRequest(client, inviteeEmails, streamIds);
}
}
Loading

0 comments on commit cddd633

Please sign in to comment.