github.com/status-im/status-go@v1.1.0/protocol/communities/community_events_factory.go (about) 1 package communities 2 3 import "github.com/status-im/status-go/protocol/protobuf" 4 5 func (o *Community) ToCreateChannelCommunityEvent(channelID string, channel *protobuf.CommunityChat) *CommunityEvent { 6 return &CommunityEvent{ 7 CommunityEventClock: o.nextEventClock(), 8 Type: protobuf.CommunityEvent_COMMUNITY_CHANNEL_CREATE, 9 ChannelData: &protobuf.ChannelData{ 10 ChannelId: channelID, 11 Channel: channel, 12 }, 13 } 14 } 15 16 func (o *Community) ToEditChannelCommunityEvent(channelID string, channel *protobuf.CommunityChat) *CommunityEvent { 17 return &CommunityEvent{ 18 CommunityEventClock: o.nextEventClock(), 19 Type: protobuf.CommunityEvent_COMMUNITY_CHANNEL_EDIT, 20 ChannelData: &protobuf.ChannelData{ 21 ChannelId: channelID, 22 Channel: channel, 23 }, 24 } 25 } 26 27 func (o *Community) ToDeleteChannelCommunityEvent(channelID string) *CommunityEvent { 28 return &CommunityEvent{ 29 CommunityEventClock: o.nextEventClock(), 30 Type: protobuf.CommunityEvent_COMMUNITY_CHANNEL_DELETE, 31 ChannelData: &protobuf.ChannelData{ 32 ChannelId: channelID, 33 }, 34 } 35 } 36 37 func (o *Community) ToReorderChannelCommunityEvent(categoryID string, channelID string, position int) *CommunityEvent { 38 return &CommunityEvent{ 39 CommunityEventClock: o.nextEventClock(), 40 Type: protobuf.CommunityEvent_COMMUNITY_CHANNEL_REORDER, 41 ChannelData: &protobuf.ChannelData{ 42 CategoryId: categoryID, 43 ChannelId: channelID, 44 Position: int32(position), 45 }, 46 } 47 } 48 49 func (o *Community) ToCreateCategoryCommunityEvent(categoryID string, categoryName string, channelsIds []string) *CommunityEvent { 50 return &CommunityEvent{ 51 CommunityEventClock: o.nextEventClock(), 52 Type: protobuf.CommunityEvent_COMMUNITY_CATEGORY_CREATE, 53 CategoryData: &protobuf.CategoryData{ 54 Name: categoryName, 55 CategoryId: categoryID, 56 ChannelsIds: channelsIds, 57 }, 58 } 59 } 60 61 func (o *Community) ToEditCategoryCommunityEvent(categoryID string, categoryName string, channelsIds []string) *CommunityEvent { 62 return &CommunityEvent{ 63 CommunityEventClock: o.nextEventClock(), 64 Type: protobuf.CommunityEvent_COMMUNITY_CATEGORY_EDIT, 65 CategoryData: &protobuf.CategoryData{ 66 Name: categoryName, 67 CategoryId: categoryID, 68 ChannelsIds: channelsIds, 69 }, 70 } 71 } 72 73 func (o *Community) ToDeleteCategoryCommunityEvent(categoryID string) *CommunityEvent { 74 return &CommunityEvent{ 75 CommunityEventClock: o.nextEventClock(), 76 Type: protobuf.CommunityEvent_COMMUNITY_CATEGORY_DELETE, 77 CategoryData: &protobuf.CategoryData{ 78 CategoryId: categoryID, 79 }, 80 } 81 } 82 83 func (o *Community) ToReorderCategoryCommunityEvent(categoryID string, position int) *CommunityEvent { 84 return &CommunityEvent{ 85 CommunityEventClock: o.nextEventClock(), 86 Type: protobuf.CommunityEvent_COMMUNITY_CATEGORY_REORDER, 87 CategoryData: &protobuf.CategoryData{ 88 CategoryId: categoryID, 89 Position: int32(position), 90 }, 91 } 92 } 93 94 func (o *Community) ToBanCommunityMemberCommunityEvent(pubkey string) *CommunityEvent { 95 return &CommunityEvent{ 96 CommunityEventClock: o.nextEventClock(), 97 Type: protobuf.CommunityEvent_COMMUNITY_MEMBER_BAN, 98 MemberToAction: pubkey, 99 } 100 } 101 102 func (o *Community) ToDeleteAllMemberMessagesEvent(pubkey string) *CommunityEvent { 103 return &CommunityEvent{ 104 CommunityEventClock: o.nextEventClock(), 105 Type: protobuf.CommunityEvent_COMMUNITY_DELETE_BANNED_MEMBER_MESSAGES, 106 MemberToAction: pubkey, 107 } 108 } 109 110 func (o *Community) ToUnbanCommunityMemberCommunityEvent(pubkey string) *CommunityEvent { 111 return &CommunityEvent{ 112 CommunityEventClock: o.nextEventClock(), 113 Type: protobuf.CommunityEvent_COMMUNITY_MEMBER_UNBAN, 114 MemberToAction: pubkey, 115 } 116 } 117 118 func (o *Community) ToKickCommunityMemberCommunityEvent(pubkey string) *CommunityEvent { 119 return &CommunityEvent{ 120 CommunityEventClock: o.nextEventClock(), 121 Type: protobuf.CommunityEvent_COMMUNITY_MEMBER_KICK, 122 MemberToAction: pubkey, 123 } 124 } 125 126 func (o *Community) ToCommunityEditCommunityEvent(description *protobuf.CommunityDescription) *CommunityEvent { 127 return &CommunityEvent{ 128 CommunityEventClock: o.nextEventClock(), 129 Type: protobuf.CommunityEvent_COMMUNITY_EDIT, 130 CommunityConfig: &protobuf.CommunityConfig{ 131 Identity: description.Identity, 132 Permissions: description.Permissions, 133 AdminSettings: description.AdminSettings, 134 IntroMessage: description.IntroMessage, 135 OutroMessage: description.OutroMessage, 136 Tags: description.Tags, 137 }, 138 } 139 } 140 141 func (o *Community) ToCommunityTokenPermissionChangeCommunityEvent(permission *protobuf.CommunityTokenPermission) *CommunityEvent { 142 return &CommunityEvent{ 143 CommunityEventClock: o.nextEventClock(), 144 Type: protobuf.CommunityEvent_COMMUNITY_MEMBER_TOKEN_PERMISSION_CHANGE, 145 TokenPermission: permission, 146 } 147 } 148 149 func (o *Community) ToCommunityTokenPermissionDeleteCommunityEvent(permission *protobuf.CommunityTokenPermission) *CommunityEvent { 150 return &CommunityEvent{ 151 CommunityEventClock: o.nextEventClock(), 152 Type: protobuf.CommunityEvent_COMMUNITY_MEMBER_TOKEN_PERMISSION_DELETE, 153 TokenPermission: permission, 154 } 155 } 156 157 func (o *Community) ToCommunityRequestToJoinAcceptCommunityEvent(member string, request *protobuf.CommunityRequestToJoin) *CommunityEvent { 158 return &CommunityEvent{ 159 CommunityEventClock: o.nextEventClock(), 160 Type: protobuf.CommunityEvent_COMMUNITY_REQUEST_TO_JOIN_ACCEPT, 161 MemberToAction: member, 162 RequestToJoin: request, 163 } 164 } 165 166 func (o *Community) ToCommunityRequestToJoinRejectCommunityEvent(member string, request *protobuf.CommunityRequestToJoin) *CommunityEvent { 167 return &CommunityEvent{ 168 CommunityEventClock: o.nextEventClock(), 169 Type: protobuf.CommunityEvent_COMMUNITY_REQUEST_TO_JOIN_REJECT, 170 MemberToAction: member, 171 RequestToJoin: request, 172 } 173 } 174 175 func (o *Community) ToAddTokenMetadataCommunityEvent(tokenMetadata *protobuf.CommunityTokenMetadata) *CommunityEvent { 176 return &CommunityEvent{ 177 CommunityEventClock: o.nextEventClock(), 178 Type: protobuf.CommunityEvent_COMMUNITY_TOKEN_ADD, 179 TokenMetadata: tokenMetadata, 180 } 181 } 182 183 func (o *Community) nextEventClock() uint64 { 184 latestEventClock := uint64(0) 185 if o.config.EventsData != nil { 186 for _, event := range o.config.EventsData.Events { 187 if event.CommunityEventClock > latestEventClock { 188 latestEventClock = event.CommunityEventClock 189 } 190 } 191 } 192 193 clock := o.config.CommunityDescription.Clock 194 if latestEventClock > clock { 195 clock = latestEventClock 196 } 197 198 // lamport timestamp 199 timestamp := o.timesource.GetCurrentTime() 200 if clock == 0 || clock < timestamp { 201 clock = timestamp 202 } else { 203 clock = clock + 1 204 } 205 206 return clock 207 }