From 182f3c4bc3008a95fd38a9ef0c1c870ba61ae2b0 Mon Sep 17 00:00:00 2001 From: Maxime Dor Date: Wed, 27 Sep 2017 04:37:45 +0200 Subject: [PATCH] Skeleton for HS User directory integration --- .../directory/UserDirectoryController.java | 59 +++++++++++++ .../io/UserDirectorySearchRequest.java | 35 ++++++++ .../io/UserDirectorySearchResult.java | 83 +++++++++++++++++++ .../mxisd/directory/DirectoryManager.java | 36 ++++++++ 4 files changed, 213 insertions(+) create mode 100644 src/main/java/io/kamax/mxisd/controller/directory/UserDirectoryController.java create mode 100644 src/main/java/io/kamax/mxisd/controller/directory/io/UserDirectorySearchRequest.java create mode 100644 src/main/java/io/kamax/mxisd/controller/directory/io/UserDirectorySearchResult.java create mode 100644 src/main/java/io/kamax/mxisd/directory/DirectoryManager.java diff --git a/src/main/java/io/kamax/mxisd/controller/directory/UserDirectoryController.java b/src/main/java/io/kamax/mxisd/controller/directory/UserDirectoryController.java new file mode 100644 index 0000000..2eaf4a2 --- /dev/null +++ b/src/main/java/io/kamax/mxisd/controller/directory/UserDirectoryController.java @@ -0,0 +1,59 @@ +/* + * mxisd - Matrix Identity Server Daemon + * Copyright (C) 2017 Maxime Dor + * + * https://max.kamax.io/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package io.kamax.mxisd.controller.directory; + +import com.google.gson.Gson; +import io.kamax.mxisd.controller.directory.io.UserDirectorySearchRequest; +import io.kamax.mxisd.controller.directory.io.UserDirectorySearchResult; +import io.kamax.mxisd.directory.DirectoryManager; +import io.kamax.mxisd.util.GsonParser; +import io.kamax.mxisd.util.GsonUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.CrossOrigin; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; +import java.net.URI; + +@RestController +@CrossOrigin +@RequestMapping(path = "/_matrix/client/r0/user_directory", produces = MediaType.APPLICATION_JSON_UTF8_VALUE) +public class UserDirectoryController { + + private Gson gson = GsonUtil.build(); + private GsonParser parser = new GsonParser(gson); + + @Autowired + private DirectoryManager mgr; + + @RequestMapping(path = "/search") + public String search(HttpServletRequest request, @RequestParam("access_token") String accessToken) throws IOException { + UserDirectorySearchRequest searchQuery = parser.parse(request, UserDirectorySearchRequest.class); + URI target = URI.create(request.getRequestURL().toString()); + UserDirectorySearchResult result = mgr.search(target, accessToken, searchQuery.getSearchTerm()); + return gson.toJson(result); + } + +} diff --git a/src/main/java/io/kamax/mxisd/controller/directory/io/UserDirectorySearchRequest.java b/src/main/java/io/kamax/mxisd/controller/directory/io/UserDirectorySearchRequest.java new file mode 100644 index 0000000..e269786 --- /dev/null +++ b/src/main/java/io/kamax/mxisd/controller/directory/io/UserDirectorySearchRequest.java @@ -0,0 +1,35 @@ +/* + * mxisd - Matrix Identity Server Daemon + * Copyright (C) 2017 Maxime Dor + * + * https://max.kamax.io/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package io.kamax.mxisd.controller.directory.io; + +public class UserDirectorySearchRequest { + + private String searchTerm; + + public String getSearchTerm() { + return searchTerm; + } + + public void setSearchTerm(String searchTerm) { + this.searchTerm = searchTerm; + } + +} diff --git a/src/main/java/io/kamax/mxisd/controller/directory/io/UserDirectorySearchResult.java b/src/main/java/io/kamax/mxisd/controller/directory/io/UserDirectorySearchResult.java new file mode 100644 index 0000000..128385c --- /dev/null +++ b/src/main/java/io/kamax/mxisd/controller/directory/io/UserDirectorySearchResult.java @@ -0,0 +1,83 @@ +/* + * mxisd - Matrix Identity Server Daemon + * Copyright (C) 2017 Maxime Dor + * + * https://max.kamax.io/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package io.kamax.mxisd.controller.directory.io; + +import java.util.ArrayList; +import java.util.List; + +public class UserDirectorySearchResult { + + public static class Result { + + private String displayName; + private String avatarUrl; + private String userId; + + public String getDisplayName() { + return displayName; + } + + public void setDisplayName(String displayName) { + this.displayName = displayName; + } + + public String getAvatarUrl() { + return avatarUrl; + } + + public void setAvatarUrl(String avatarUrl) { + this.avatarUrl = avatarUrl; + } + + public String getUserId() { + return userId; + } + + public void setUserId(String userId) { + this.userId = userId; + } + + } + + private boolean limited; + private List results = new ArrayList<>(); + + public boolean isLimited() { + return limited; + } + + public void setLimited(boolean limited) { + this.limited = limited; + } + + public List getResults() { + return results; + } + + public void setResults(List results) { + this.results = results; + } + + public void addResult(Result result) { + this.results.add(result); + } + +} diff --git a/src/main/java/io/kamax/mxisd/directory/DirectoryManager.java b/src/main/java/io/kamax/mxisd/directory/DirectoryManager.java new file mode 100644 index 0000000..fd609d7 --- /dev/null +++ b/src/main/java/io/kamax/mxisd/directory/DirectoryManager.java @@ -0,0 +1,36 @@ +/* + * mxisd - Matrix Identity Server Daemon + * Copyright (C) 2017 Maxime Dor + * + * https://max.kamax.io/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package io.kamax.mxisd.directory; + +import io.kamax.mxisd.controller.directory.io.UserDirectorySearchResult; +import io.kamax.mxisd.exception.NotImplementedException; +import org.springframework.stereotype.Component; + +import java.net.URI; + +@Component +public class DirectoryManager { + + public UserDirectorySearchResult search(URI target, String accessToken, String query) { + throw new NotImplementedException("user directory - search"); + } + +}