-
Notifications
You must be signed in to change notification settings - Fork 885
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
XEP-0084: User Avatars #332
Open
vanitasvitae
wants to merge
2
commits into
igniterealtime:master
Choose a base branch
from
vanitasvitae:ramabit.avatar
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
smack-extensions/src/main/java/org/jivesoftware/smackx/avatar/AvatarMetadataStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/** | ||
* | ||
* Copyright 2019 Paul Schaub | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.jivesoftware.smackx.avatar; | ||
|
||
import org.jxmpp.jid.EntityBareJid; | ||
|
||
/** | ||
* The {@link AvatarMetadataStore} interface defines methods used by the {@link UserAvatarManager} to determine, | ||
* whether the client already has a local copy of a published avatar or if the user needs to be informed about the | ||
* update in order to download the image. | ||
*/ | ||
public interface AvatarMetadataStore { | ||
|
||
/** | ||
* Determine, if the client already has a copy of the avatar with {@code itemId} available or not. | ||
* | ||
* @param jid {@link EntityBareJid} of the entity that published the avatar. | ||
* @param itemId itemId of the avatar | ||
* | ||
* @return true if the client already has a local copy of the avatar, false otherwise | ||
*/ | ||
boolean hasAvatarAvailable(EntityBareJid jid, String itemId); | ||
|
||
/** | ||
* Mark the tuple (jid, itemId) as available. This means that the client already has a local copy of the avatar | ||
* available and wishes not to be notified about this particular avatar anymore. | ||
* | ||
* @param jid {@link EntityBareJid} of the entity that published the avatar. | ||
* @param itemId itemId of the avatar | ||
*/ | ||
void setAvatarAvailable(EntityBareJid jid, String itemId); | ||
} |
84 changes: 84 additions & 0 deletions
84
smack-extensions/src/main/java/org/jivesoftware/smackx/avatar/MemoryAvatarMetadataStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* | ||
* Copyright 2020 Paul Schaub | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.jivesoftware.smackx.avatar; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
import org.jivesoftware.smack.util.HashCode; | ||
import org.jivesoftware.smack.util.Objects; | ||
|
||
import org.jxmpp.jid.EntityBareJid; | ||
|
||
public class MemoryAvatarMetadataStore implements AvatarMetadataStore { | ||
|
||
private Map<Tuple<EntityBareJid, String>, Boolean> availabilityMap = new ConcurrentHashMap<>(); | ||
|
||
@Override | ||
public boolean hasAvatarAvailable(EntityBareJid jid, String itemId) { | ||
Boolean available = availabilityMap.get(new Tuple<>(jid, itemId)); | ||
return available != null && available; | ||
} | ||
|
||
@Override | ||
public void setAvatarAvailable(EntityBareJid jid, String itemId) { | ||
availabilityMap.put(new Tuple<>(jid, itemId), Boolean.TRUE); | ||
} | ||
|
||
private static class Tuple<A, B> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This class should be deleted and |
||
private final A first; | ||
private final B second; | ||
|
||
Tuple(A first, B second) { | ||
this.first = first; | ||
this.second = second; | ||
} | ||
|
||
public A getFirst() { | ||
return first; | ||
} | ||
|
||
public B getSecond() { | ||
return second; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return HashCode.builder() | ||
.append(first) | ||
.append(second) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj == null) { | ||
return false; | ||
} | ||
if (obj == this) { | ||
return true; | ||
} | ||
if (!(obj instanceof Tuple)) { | ||
return false; | ||
} | ||
|
||
@SuppressWarnings("unchecked") Tuple<A, B> other = (Tuple<A, B>) obj; | ||
return Objects.equals(getFirst(), other.getFirst()) | ||
&& Objects.equals(getSecond(), other.getSecond()); | ||
} | ||
} | ||
} |
127 changes: 127 additions & 0 deletions
127
smack-extensions/src/main/java/org/jivesoftware/smackx/avatar/MetadataInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/** | ||
* | ||
* Copyright 2017 Fernando Ramirez, 2019 Paul Schaub | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.jivesoftware.smackx.avatar; | ||
|
||
import java.net.URL; | ||
|
||
import org.jivesoftware.smack.datatypes.UInt16; | ||
import org.jivesoftware.smack.datatypes.UInt32; | ||
import org.jivesoftware.smack.util.StringUtils; | ||
|
||
/** | ||
* User Avatar metadata info model class. | ||
* | ||
* @author Fernando Ramirez | ||
* @author Paul Schaub | ||
* @see <a href="http://xmpp.org/extensions/xep-0084.html">XEP-0084: User | ||
* Avatar</a> | ||
*/ | ||
public class MetadataInfo { | ||
|
||
public static final int MAX_HEIGHT = 65536; | ||
public static final int MAX_WIDTH = 65536; | ||
|
||
private final String id; | ||
private final URL url; | ||
private final UInt32 bytes; | ||
private final String type; | ||
private final UInt16 height; | ||
private final UInt16 width; | ||
|
||
/** | ||
* MetadataInfo constructor. | ||
* | ||
* @param id SHA-1 hash of the image data | ||
* @param url http(s) url of the image | ||
* @param bytes size of the image in bytes | ||
* @param type content type of the image | ||
* @param pixelsHeight height of the image in pixels | ||
* @param pixelsWidth width of the image in pixels | ||
*/ | ||
public MetadataInfo(String id, URL url, long bytes, String type, int pixelsHeight, int pixelsWidth) { | ||
this.id = StringUtils.requireNotNullNorEmpty(id, "ID is required."); | ||
this.url = url; | ||
if (bytes <= 0) { | ||
throw new IllegalArgumentException("Number of bytes MUST be greater than 0."); | ||
} | ||
this.bytes = UInt32.from(bytes); | ||
this.type = StringUtils.requireNotNullNorEmpty(type, "Content Type is required."); | ||
if (pixelsHeight < 0 || pixelsHeight > MAX_HEIGHT) { | ||
throw new IllegalArgumentException("Image height value must be between 0 and 65536."); | ||
} | ||
if (pixelsWidth < 0 || pixelsWidth > MAX_WIDTH) { | ||
throw new IllegalArgumentException("Image width value must be between 0 and 65536."); | ||
} | ||
this.height = UInt16.from(pixelsHeight); | ||
this.width = UInt16.from(pixelsWidth); | ||
} | ||
|
||
/** | ||
* Get the id. | ||
* | ||
* @return the id | ||
*/ | ||
public String getId() { | ||
return id; | ||
} | ||
|
||
/** | ||
* Get the url of the avatar image. | ||
* | ||
* @return the url | ||
*/ | ||
public URL getUrl() { | ||
return url; | ||
} | ||
|
||
/** | ||
* Get the amount of bytes. | ||
* | ||
* @return the amount of bytes | ||
*/ | ||
public UInt32 getBytes() { | ||
return bytes; | ||
} | ||
|
||
/** | ||
* Get the type. | ||
* | ||
* @return the type | ||
*/ | ||
public String getType() { | ||
return type; | ||
} | ||
|
||
/** | ||
* Get the height in pixels. | ||
* | ||
* @return the height in pixels | ||
*/ | ||
public UInt16 getHeight() { | ||
return height; | ||
} | ||
|
||
/** | ||
* Get the width in pixels. | ||
* | ||
* @return the width in pixels | ||
*/ | ||
public UInt16 getWidth() { | ||
return width; | ||
} | ||
|
||
} |
86 changes: 86 additions & 0 deletions
86
smack-extensions/src/main/java/org/jivesoftware/smackx/avatar/MetadataPointer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/** | ||
* | ||
* Copyright 2017 Fernando Ramirez | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.jivesoftware.smackx.avatar; | ||
|
||
import java.util.Map; | ||
|
||
import org.jivesoftware.smack.util.StringUtils; | ||
|
||
/** | ||
* User Avatar metadata pointer model class. | ||
* A pointer element is used to point to an avatar which is not published via PubSub or HTTP, but provided by a | ||
* third-party service. | ||
* | ||
* @author Fernando Ramirez | ||
* @see <a href="https://xmpp.org/extensions/xep-0084.html">XEP-0084: User Avatar</a> | ||
*/ | ||
public class MetadataPointer { | ||
|
||
private final String namespace; | ||
private final Map<String, Object> fields; | ||
|
||
/** | ||
* Metadata Pointer constructor. | ||
* | ||
* The following example | ||
* <pre> | ||
* {@code | ||
* <pointer> | ||
* <x xmlns='http://example.com/virtualworlds'> | ||
* <game>Ancapistan</game> | ||
* <character>Kropotkin</character> | ||
* </x> | ||
* </pointer> | ||
* } | ||
* </pre> | ||
* can be created by constructing the object like this: | ||
* <pre> | ||
* {@code | ||
* Map fields = new HashMap<>(); | ||
* fields.add("game", "Ancapistan"); | ||
* fields.add("character", "Kropotkin"); | ||
* MetadataPointer pointer = new MetadataPointer("http://example.com/virtualworlds", fields); | ||
* } | ||
* </pre> | ||
* | ||
* @param namespace namespace of the child element of the metadata pointer. | ||
* @param fields fields of the child element as key, value pairs. | ||
*/ | ||
public MetadataPointer(String namespace, Map<String, Object> fields) { | ||
this.namespace = StringUtils.requireNotNullNorEmpty(namespace, "Namespace MUST NOT be null, nor empty."); | ||
this.fields = fields; | ||
} | ||
|
||
/** | ||
* Get the namespace of the pointers child element. | ||
* | ||
* @return the namespace | ||
*/ | ||
public String getNamespace() { | ||
return namespace; | ||
} | ||
|
||
/** | ||
* Get the fields of the pointers child element. | ||
* | ||
* @return the fields | ||
*/ | ||
public Map<String, Object> getFields() { | ||
return fields; | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am confused by this "store", which does not actually seem to store avatars, just their availability.
Why not make it a real store-interface, e.g.
where Avatar is a meta class that encapsulates the MIME type and the avatar bytes.