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

Synchronize roster items: Apply changes to existing roster items and … #182

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@
import org.xmpp.packet.StreamError;

import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.*;

/**
* The Class UserServiceController.
Expand Down Expand Up @@ -369,6 +367,39 @@ public void updateRosterItem(String username, String rosterJid, RosterItemEntity
roster.updateRosterItem(rosterItem);
}

public Map<String, Integer> syncRosterEntities(String username, RosterEntities entities) throws UserNotFoundException,
ServiceException, UserAlreadyExistsException, SharedGroupException {
Map<String, Integer> ret = new HashMap<>();
ret.put("inserts", 0);
ret.put("updates", 0);

getAndCheckUser(username);
Roster roster = getUserRoster(username);
for(RosterItemEntity rosterItemEntity : entities.getRoster()) {
JID jid = new JID(rosterItemEntity.getJid());
RosterItem rosterItem = roster.getRosterItem(jid);
String key;
if(rosterItem != null) {
key = "updates";
if (rosterItemEntity.getNickname() != null) {
rosterItem.setNickname(rosterItemEntity.getNickname());
}
if (rosterItemEntity.getGroups() != null) {
rosterItem.setGroups(rosterItemEntity.getGroups());
}
}else{
key = "inserts";
rosterItem = roster.createRosterItem(jid, rosterItemEntity.getNickname(),
rosterItemEntity.getGroups(), false, true);
}
UserUtils.checkSubType(rosterItemEntity.getSubscriptionType());
rosterItem.setSubStatus(RosterItem.SubType.getTypeFromInt(rosterItemEntity.getSubscriptionType()));
roster.updateRosterItem(rosterItem);
int count = ret.get(key);
ret.put(key, count + 1);
}
return ret;
}
/**
* Delete roster item.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.HashMap;

@Path("restapi/v1/users/{username}/roster")
@Tag(name = "Users", description = "Managing Openfire users.")
Expand Down Expand Up @@ -148,4 +149,31 @@ public Response updateRoster(
}
return Response.status(Response.Status.OK).build();
}


@PATCH()
@Operation( summary = "Synchronize roster entries",
description = "Apply changes to the existing roster items, Insert non-existing entries")
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response syncRoster(
@Parameter(description = "The username of the user for which the update a roster entry.", required = true) @PathParam("username") String username,
@RequestBody(description = "The definition of the roster entries that are to be synched (added / updated).", required = true) RosterEntities entities)
throws ServiceException{

Object ret;
try {
ret = plugin.syncRosterEntities(username, entities);
} catch (UserNotFoundException e) {
throw new ServiceException(COULD_NOT_UPDATE_THE_ROSTER, "", ExceptionType.USER_NOT_FOUND_EXCEPTION,
Response.Status.NOT_FOUND, e);
} catch (SharedGroupException e) {
throw new ServiceException(COULD_NOT_UPDATE_THE_ROSTER, "", ExceptionType.SHARED_GROUP_EXCEPTION,
Response.Status.BAD_REQUEST, e);
} catch (UserAlreadyExistsException e) {
throw new ServiceException(COULD_NOT_UPDATE_THE_ROSTER, "",
ExceptionType.USER_ALREADY_EXISTS_EXCEPTION, Response.Status.CONFLICT, e);
}

return Response.ok(ret).build();
}
}