Skip to content

Send and receive SMS messages using your Android phone programmatically via a simple HTTP API

Add ability to update the user's timezone

Acho Arnold09aa2139a0c2
diff --git a/api/pkg/requests/user_update_request.go b/api/pkg/requests/user_update_request.go
index ee30dd04..678c7824 100644
--- a/api/pkg/requests/user_update_request.go
+++ b/api/pkg/requests/user_update_request.go
@@ -2,6 +2,7 @@ package requests
import (
"strings"
+ "time"
"github.com/google/uuid"
@@ -11,18 +12,25 @@ import (
// UserUpdate is the payload for updating a phone
type UserUpdate struct {
request
+ Timezone string `json:"timezone" example:"Europe/Helsinki"`
ActivePhoneID string `json:"active_phone_id" example:"32343a19-da5e-4b1b-a767-3298a73703cb"`
}
// Sanitize sets defaults to MessageOutstanding
func (input *UserUpdate) Sanitize() UserUpdate {
input.ActivePhoneID = strings.TrimSpace(input.ActivePhoneID)
+ input.Timezone = strings.TrimSpace(input.Timezone)
return *input
}
// ToUpdateParams converts UserUpdate to services.UserUpdateParams
func (input *UserUpdate) ToUpdateParams() services.UserUpdateParams {
+ location, err := time.LoadLocation(input.Timezone)
+ if err != nil {
+ location = time.UTC
+ }
return services.UserUpdateParams{
ActivePhoneID: uuid.MustParse(input.ActivePhoneID),
+ Timezone: location,
}
}
diff --git a/api/pkg/services/user_service.go b/api/pkg/services/user_service.go
index bd707283..c9c6860c 100644
--- a/api/pkg/services/user_service.go
+++ b/api/pkg/services/user_service.go
@@ -71,6 +71,7 @@ func (service *UserService) Get(ctx context.Context, authUser entities.AuthUser)
// UserUpdateParams are parameters for updating an entities.User
type UserUpdateParams struct {
+ Timezone *time.Location
ActivePhoneID uuid.UUID
}
@@ -91,6 +92,7 @@ func (service *UserService) Update(ctx context.Context, authUser entities.AuthUs
service.marketingService.AddToList(ctx, user)
}
+ user.Timezone = params.Timezone.String()
user.ActivePhoneID = &params.ActivePhoneID
if err = service.repository.Update(ctx, user); err != nil {
diff --git a/web/components/MessageThreadHeader.vue b/web/components/MessageThreadHeader.vue
index 44b583a6..18fd3280 100644
--- a/web/components/MessageThreadHeader.vue
+++ b/web/components/MessageThreadHeader.vue
@@ -210,7 +210,7 @@ export default class MessageThreadHeader extends Vue {
}
async onOwnerChanged(owner: string) {
- await this.$store.dispatch('setOwner', owner)
+ await this.$store.dispatch('updateUser', { owner })
if (this.$route.name !== 'threads') {
await this.$store.dispatch('setThreadId', null)
await this.$router.push({ name: 'threads' })
diff --git a/web/pages/settings/index.vue b/web/pages/settings/index.vue
index 38a4c1e8..51655461 100644
--- a/web/pages/settings/index.vue
+++ b/web/pages/settings/index.vue
@@ -38,6 +38,17 @@
{{ mdiShieldCheck }}
</v-icon>
</h4>
+ <v-autocomplete
+ v-if="$store.getters.getUser"
+ dense
+ outlined
+ :value="$store.getters.getUser.timezone"
+ class="mx-auto mt-2"
+ style="max-width: 250px"
+ label="Timezone"
+ :items="timezones"
+ @change="updateUser"
+ ></v-autocomplete>
</div>
<h5 class="text-h4 mb-3 mt-3">API Key</h5>
<p class="text--secondary">
@@ -654,10 +665,14 @@ export default Vue.extend({
}
return this.$store.getters.getUser.api_key
},
+ timezones() {
+ return Intl.supportedValuesOf('timeZone')
+ },
},
mounted() {
this.$store.dispatch('clearAxiosError')
this.$store.dispatch('loadUser')
+ this.$store.dispatch('loadPhones')
this.loadWebhooks()
this.loadDiscordIntegrations()
},
@@ -821,6 +836,27 @@ export default Vue.extend({
})
},
+ updateUser(timezone) {
+ this.resetErrors()
+ this.$store
+ .dispatch('updateUser', {
+ owner: this.$store.getters.getOwner,
+ timezone,
+ })
+ .then(() => {
+ this.$store.dispatch('addNotification', {
+ message: 'Timezone updated successfully',
+ type: 'success',
+ })
+ })
+ .catch(() => {
+ this.$store.dispatch('addNotification', {
+ message: 'Failed to update timezone',
+ type: 'error',
+ })
+ })
+ },
+
updateWebhook() {
this.resetErrors()
this.updatingWebhook = true
diff --git a/web/store/index.ts b/web/store/index.ts
index 24c39458..6ed03402 100644
--- a/web/store/index.ts
+++ b/web/store/index.ts
@@ -510,7 +510,10 @@ export const actions = {
(x: Phone) => x.id === context.getters.getUser.active_phone_id
)
if (phone) {
- await context.dispatch('setOwner', phone.phone_number)
+ await context.dispatch('updateUser', {
+ owner: phone.phone_number,
+ timezone: context.getters.getUser.timezone,
+ })
}
}
},
@@ -550,8 +553,11 @@ export const actions = {
await context.commit('setAxiosError', null)
},
- async setOwner(context: ActionContext<State, State>, owner: string) {
- await context.commit('setOwner', owner)
+ async updateUser(
+ context: ActionContext<State, State>,
+ payload: { owner: string; timezone: string }
+ ) {
+ await context.commit('setOwner', payload.owner)
const phone = context.getters.getActivePhone as Phone | null
if (!phone) {
@@ -560,6 +566,7 @@ export const actions = {
const response = await axios.put('/v1/users/me', {
active_phone_id: phone.id,
+ timezone: payload.timezone ?? context.getters.getUser.timezone,
})
setApiKey(response.data.data.api_key)